Flutter Engine
The Flutter Engine
cacheimages.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
3# for details. All rights reserved. Use of this source code is governed by a
4# BSD-style license that can be found in the LICENSE file.
5'''
6This script finds all HTML pages in a folder and downloads all images, replacing
7the urls with local ones.
8'''
9import os, sys, optparse, subprocess, multiprocessing
10from os.path import abspath, basename, dirname, join
11
12SWARM_PATH = dirname(abspath(__file__))
13CLIENT_PATH = dirname(dirname(SWARM_PATH))
14CLIENT_TOOLS_PATH = join(CLIENT_PATH, 'tools')
15
16# Add the client tools directory so we can find htmlconverter.py.
17sys.path.append(CLIENT_TOOLS_PATH)
18import htmlconverter
19converter = CLIENT_TOOLS_PATH + '/htmlconverter.py'
20
21
22# This has to be a top level function to use with multiprocessing
23def convertImgs(infile):
24 global options
25 try:
26 htmlconverter.convertForOffline(infile,
27 infile,
28 verbose=options.verbose,
29 encode_images=options.inline_images)
30 print('Converted ' + infile)
31 except BaseException as e:
32 print('Caught error: %s' % e)
33
34
35def Flags():
36 """ Constructs a parser for extracting flags from the command line. """
37 parser = optparse.OptionParser()
38 parser.add_option(
39 "--inline_images",
40 help=("Encode img payloads as data:// URLs rather than local files."),
41 default=False,
42 action='store_true')
43 parser.add_option("--verbose",
44 help="Print verbose output",
45 default=False,
46 action="store_true")
47 return parser
48
49
50def main():
51 global options
52 parser = Flags()
53 options, args = parser.parse_args()
54 print("args: %s" % args)
55 if len(args) < 1 or 'help' in args[0]:
56 print('Usage: %s DIRECTORY' % basename(sys.argv[0]))
57 return 1
58
59 dirname = args[0]
60 print('Searching directory ' + dirname)
61
62 files = []
63 for root, dirs, fnames in os.walk(dirname):
64 for fname in fnames:
65 if fname.endswith('.html'):
66 files.append(join(root, fname))
67
68 count = 4 * multiprocessing.cpu_count()
69 pool = multiprocessing.Pool(processes=count)
70 # Note: need a timeout to get keyboard interrupt due to a Python bug
71 pool.map_async(convertImgs, files).get(3600) # one hour
72
73
74if __name__ == '__main__':
75 main()
def main()
Definition: cacheimages.py:50
def Flags()
Definition: cacheimages.py:35
def convertImgs(infile)
Definition: cacheimages.py:23
Definition: main.py:1
basename
Definition: malisc.py:23
const myers::Point & get(const myers::Segment &)
def print(*args, **kwargs)
Definition: run_tests.py:49
static SkString join(const CommandLineFlags::StringArray &)
Definition: skpbench.cpp:741