Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(38680)

Side by Side Diff: Lib/compileall.py

Issue 16104: Use multiprocessing in compileall script
Patch Set: Created 5 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 """Module/script to byte-compile all .py files to .pyc (or .pyo) files. 1 """Module/script to byte-compile all .py files to .pyc (or .pyo) files.
2 2
3 When called as a script with arguments, this compiles the directories 3 When called as a script with arguments, this compiles the directories
4 given as arguments recursively; the -l option prevents it from 4 given as arguments recursively; the -l option prevents it from
5 recursing into directories. 5 recursing into directories.
6 6
7 Without arguments, if compiles all modules on sys.path, without 7 Without arguments, if compiles all modules on sys.path, without
8 recursing into subdirectories. (Even though it should do so for 8 recursing into subdirectories. (Even though it should do so for
9 packages -- for now, you'll have to deal with packages separately.) 9 packages -- for now, you'll have to deal with packages separately.)
10 10
11 See module py_compile for details of the actual byte-compilation. 11 See module py_compile for details of the actual byte-compilation.
12 """ 12 """
13 import os 13 import os
14 import sys 14 import sys
15 import errno 15 import errno
16 import importlib.util 16 import importlib.util
17 import py_compile 17 import py_compile
18 import struct 18 import struct
19 19
20 from concurrent.futures import ProcessPoolExecutor, as_completed
21
20 __all__ = ["compile_dir","compile_file","compile_path"] 22 __all__ = ["compile_dir","compile_file","compile_path"]
21 23
24 def _walk_dir(dir, ddir=None, maxlevels=10, quiet=False):
25 if not quiet:
26 print('Listing {!r}...'.format(dir))
27 try:
28 names = os.listdir(dir)
29 except OSError:
30 print("Can't list {!r}".format(dir))
31 names = []
32 names.sort()
33 for name in names:
34 if name == '__pycache__':
35 continue
36 fullname = os.path.join(dir, name)
37 if ddir is not None:
38 dfile = os.path.join(ddir, name)
39 else:
40 dfile = None
41 if not os.path.isdir(fullname):
42 yield fullname
43 elif (maxlevels > 0 and name != os.curdir and name != os.pardir and
44 os.path.isdir(fullname) and not os.path.islink(fullname)):
45 yield from _walk_dir(fullname, ddir=dfile,
46 maxlevels=maxlevels - 1, quiet=quiet)
47
22 def compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None, 48 def compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None,
23 quiet=False, legacy=False, optimize=-1): 49 quiet=False, legacy=False, optimize=-1, processes=None):
24 """Byte-compile all modules in the given directory tree. 50 """Byte-compile all modules in the given directory tree.
25 51
26 Arguments (only dir is required): 52 Arguments (only dir is required):
27 53
28 dir: the directory to byte-compile 54 dir: the directory to byte-compile
29 maxlevels: maximum recursion level (default 10) 55 maxlevels: maximum recursion level (default 10)
30 ddir: the directory that will be prepended to the path to the 56 ddir: the directory that will be prepended to the path to the
31 file as it is compiled into each byte-code file. 57 file as it is compiled into each byte-code file.
32 force: if True, force compilation, even if timestamps are up-to-date 58 force: if True, force compilation, even if timestamps are up-to-date
33 quiet: if True, be quiet during compilation 59 quiet: if True, be quiet during compilation
34 legacy: if True, produce legacy pyc paths instead of PEP 3147 paths 60 legacy: if True, produce legacy pyc paths instead of PEP 3147 paths
35 optimize: optimization level or -1 for level of the interpreter 61 optimize: optimization level or -1 for level of the interpreter
62 processes: if given, it will be the number of workers which will
63 process the given directory.
36 """ 64 """
37 if not quiet: 65 files = _walk_dir(dir, quiet=quiet, maxlevels=maxlevels,
38 print('Listing {!r}...'.format(dir)) 66 ddir=ddir)
39 try:
40 names = os.listdir(dir)
41 except OSError:
42 print("Can't list {!r}".format(dir))
43 names = []
44 names.sort()
45 success = 1 67 success = 1
46 for name in names: 68 if processes:
47 if name == '__pycache__': 69 with ProcessPoolExecutor(
48 continue 70 max_workers=processes) as executor:
49 fullname = os.path.join(dir, name) 71 futures = [
50 if ddir is not None: 72 executor.submit(compile_file, file,
eric.araujo 2014/03/12 06:48:47 Isn’t executor.map nicer to use than submit in a l
51 dfile = os.path.join(ddir, name) 73 ddir, force, rx, quiet,
52 else: 74 legacy, optimize)
53 dfile = None 75 for file in files
54 if not os.path.isdir(fullname): 76 ]
55 if not compile_file(fullname, ddir, force, rx, quiet, 77 for future in as_completed(futures):
78 success = 0 if not future.result() else 1
79 else:
80 for file in files:
81 if not compile_file(file, ddir, force, rx, quiet,
56 legacy, optimize): 82 legacy, optimize):
57 success = 0
58 elif (maxlevels > 0 and name != os.curdir and name != os.pardir and
59 os.path.isdir(fullname) and not os.path.islink(fullname)):
60 if not compile_dir(fullname, maxlevels - 1, dfile, force, rx,
61 quiet, legacy, optimize):
62 success = 0 83 success = 0
63 return success 84 return success
64 85
65 def compile_file(fullname, ddir=None, force=False, rx=None, quiet=False, 86 def compile_file(fullname, ddir=None, force=False, rx=None, quiet=False,
66 legacy=False, optimize=-1): 87 legacy=False, optimize=-1):
67 """Byte-compile one file. 88 """Byte-compile one file.
68 89
69 Arguments (only fullname is required): 90 Arguments (only fullname is required):
70 91
71 fullname: the file to byte-compile 92 fullname: the file to byte-compile
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 'the regexp is searched for in the full path ' 207 'the regexp is searched for in the full path '
187 'of each file considered for compilation')) 208 'of each file considered for compilation'))
188 parser.add_argument('-i', metavar='FILE', dest='flist', 209 parser.add_argument('-i', metavar='FILE', dest='flist',
189 help=('add all the files and directories listed in ' 210 help=('add all the files and directories listed in '
190 'FILE to the list considered for compilation; ' 211 'FILE to the list considered for compilation; '
191 'if "-", names are read from stdin')) 212 'if "-", names are read from stdin'))
192 parser.add_argument('compile_dest', metavar='FILE|DIR', nargs='*', 213 parser.add_argument('compile_dest', metavar='FILE|DIR', nargs='*',
193 help=('zero or more file and directory names ' 214 help=('zero or more file and directory names '
194 'to compile; if no arguments given, defaults ' 215 'to compile; if no arguments given, defaults '
195 'to the equivalent of -l sys.path')) 216 'to the equivalent of -l sys.path'))
217 parser.add_argument('-j', '--processes', action='store', default=None,
218 type=int, help='Run compileall concurrently')
219
196 args = parser.parse_args() 220 args = parser.parse_args()
197
198 compile_dests = args.compile_dest 221 compile_dests = args.compile_dest
199 222
200 if (args.ddir and (len(compile_dests) != 1 223 if (args.ddir and (len(compile_dests) != 1
201 or not os.path.isdir(compile_dests[0]))): 224 or not os.path.isdir(compile_dests[0]))):
202 parser.exit('-d destdir requires exactly one directory argument') 225 parser.exit('-d destdir requires exactly one directory argument')
203 if args.rx: 226 if args.rx:
204 import re 227 import re
205 args.rx = re.compile(args.rx) 228 args.rx = re.compile(args.rx)
206 229
207 # if flist is provided then load it 230 # if flist is provided then load it
(...skipping 10 matching lines...) Expand all
218 try: 241 try:
219 if compile_dests: 242 if compile_dests:
220 for dest in compile_dests: 243 for dest in compile_dests:
221 if os.path.isfile(dest): 244 if os.path.isfile(dest):
222 if not compile_file(dest, args.ddir, args.force, args.rx, 245 if not compile_file(dest, args.ddir, args.force, args.rx,
223 args.quiet, args.legacy): 246 args.quiet, args.legacy):
224 success = False 247 success = False
225 else: 248 else:
226 if not compile_dir(dest, args.maxlevels, args.ddir, 249 if not compile_dir(dest, args.maxlevels, args.ddir,
227 args.force, args.rx, args.quiet, 250 args.force, args.rx, args.quiet,
228 args.legacy): 251 args.legacy, processes=args.processes):
229 success = False 252 success = False
230 return success 253 return success
231 else: 254 else:
232 return compile_path(legacy=args.legacy, force=args.force, 255 return compile_path(legacy=args.legacy, force=args.force,
233 quiet=args.quiet) 256 quiet=args.quiet)
234 except KeyboardInterrupt: 257 except KeyboardInterrupt:
235 print("\n[interrupted]") 258 print("\n[interrupted]")
236 return False 259 return False
237 return True 260 return True
238 261
239 262
240 if __name__ == '__main__': 263 if __name__ == '__main__':
241 exit_status = int(not main()) 264 exit_status = int(not main())
242 sys.exit(exit_status) 265 sys.exit(exit_status)
OLDNEW

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+