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

Side by Side Diff: Lib/compileall.py

Issue 16104: Use multiprocessing in compileall script
Patch Set: Created 5 years, 6 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
21 from functools import partial
22
20 __all__ = ["compile_dir","compile_file","compile_path"] 23 __all__ = ["compile_dir","compile_file","compile_path"]
21 24
25 def _walk_dir(dir, ddir=None, maxlevels=10, quiet=False):
26 if not quiet:
27 print('Listing {!r}...'.format(dir))
28 try:
29 names = os.listdir(dir)
30 except OSError:
31 print("Can't list {!r}".format(dir))
32 names = []
33 names.sort()
34 for name in names:
35 if name == '__pycache__':
36 continue
37 fullname = os.path.join(dir, name)
38 if ddir is not None:
39 dfile = os.path.join(ddir, name)
40 else:
41 dfile = None
42 if not os.path.isdir(fullname):
43 yield fullname
44 elif (maxlevels > 0 and name != os.curdir and name != os.pardir and
45 os.path.isdir(fullname) and not os.path.islink(fullname)):
46 yield from _walk_dir(fullname, ddir=dfile,
47 maxlevels=maxlevels - 1, quiet=quiet)
48
22 def compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None, 49 def compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None,
23 quiet=False, legacy=False, optimize=-1): 50 quiet=False, legacy=False, optimize=-1, processes=None):
24 """Byte-compile all modules in the given directory tree. 51 """Byte-compile all modules in the given directory tree.
25 52
26 Arguments (only dir is required): 53 Arguments (only dir is required):
27 54
28 dir: the directory to byte-compile 55 dir: the directory to byte-compile
29 maxlevels: maximum recursion level (default 10) 56 maxlevels: maximum recursion level (default 10)
30 ddir: the directory that will be prepended to the path to the 57 ddir: the directory that will be prepended to the path to the
31 file as it is compiled into each byte-code file. 58 file as it is compiled into each byte-code file.
32 force: if True, force compilation, even if timestamps are up-to-date 59 force: if True, force compilation, even if timestamps are up-to-date
33 quiet: if True, be quiet during compilation 60 quiet: if True, be quiet during compilation
34 legacy: if True, produce legacy pyc paths instead of PEP 3147 paths 61 legacy: if True, produce legacy pyc paths instead of PEP 3147 paths
35 optimize: optimization level or -1 for level of the interpreter 62 optimize: optimization level or -1 for level of the interpreter
63 processes: if given, it will be the number of workers which will
64 process the given directory.
36 """ 65 """
37 if not quiet: 66 files = _walk_dir(dir, quiet=quiet, maxlevels=maxlevels,
38 print('Listing {!r}...'.format(dir)) 67 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 68 success = 1
46 for name in names: 69 if processes:
47 if name == '__pycache__': 70 with ProcessPoolExecutor(
48 continue 71 max_workers=processes) as executor:
49 fullname = os.path.join(dir, name) 72 results = executor.map(partial(compile_file,
eric.araujo 2014/03/12 22:14:03 Ah, I see that my suggestion of executor.map may h
Claudiu.Popa 2014/03/12 22:15:25 Actually, I'm liking this version more, because th
50 if ddir is not None: 73 ddir=ddir, force=force,
51 dfile = os.path.join(ddir, name) 74 rx=rx, quiet=quiet,
52 else: 75 legacy=legacy,
53 dfile = None 76 optimize=optimize),
54 if not os.path.isdir(fullname): 77 files)
55 if not compile_file(fullname, ddir, force, rx, quiet, 78 for result in results:
79 success = 0 if not result else 1
80 else:
81 for file in files:
82 if not compile_file(file, ddir, force, rx, quiet,
56 legacy, optimize): 83 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 84 success = 0
63 return success 85 return success
64 86
65 def compile_file(fullname, ddir=None, force=False, rx=None, quiet=False, 87 def compile_file(fullname, ddir=None, force=False, rx=None, quiet=False,
66 legacy=False, optimize=-1): 88 legacy=False, optimize=-1):
67 """Byte-compile one file. 89 """Byte-compile one file.
68 90
69 Arguments (only fullname is required): 91 Arguments (only fullname is required):
70 92
71 fullname: the file to byte-compile 93 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 ' 208 'the regexp is searched for in the full path '
187 'of each file considered for compilation')) 209 'of each file considered for compilation'))
188 parser.add_argument('-i', metavar='FILE', dest='flist', 210 parser.add_argument('-i', metavar='FILE', dest='flist',
189 help=('add all the files and directories listed in ' 211 help=('add all the files and directories listed in '
190 'FILE to the list considered for compilation; ' 212 'FILE to the list considered for compilation; '
191 'if "-", names are read from stdin')) 213 'if "-", names are read from stdin'))
192 parser.add_argument('compile_dest', metavar='FILE|DIR', nargs='*', 214 parser.add_argument('compile_dest', metavar='FILE|DIR', nargs='*',
193 help=('zero or more file and directory names ' 215 help=('zero or more file and directory names '
194 'to compile; if no arguments given, defaults ' 216 'to compile; if no arguments given, defaults '
195 'to the equivalent of -l sys.path')) 217 'to the equivalent of -l sys.path'))
218 parser.add_argument('-j', '--processes', action='store', default=None,
219 type=int, help='Run compileall concurrently')
220
196 args = parser.parse_args() 221 args = parser.parse_args()
197
198 compile_dests = args.compile_dest 222 compile_dests = args.compile_dest
199 223
200 if (args.ddir and (len(compile_dests) != 1 224 if (args.ddir and (len(compile_dests) != 1
201 or not os.path.isdir(compile_dests[0]))): 225 or not os.path.isdir(compile_dests[0]))):
202 parser.exit('-d destdir requires exactly one directory argument') 226 parser.exit('-d destdir requires exactly one directory argument')
203 if args.rx: 227 if args.rx:
204 import re 228 import re
205 args.rx = re.compile(args.rx) 229 args.rx = re.compile(args.rx)
206 230
207 # if flist is provided then load it 231 # if flist is provided then load it
(...skipping 10 matching lines...) Expand all
218 try: 242 try:
219 if compile_dests: 243 if compile_dests:
220 for dest in compile_dests: 244 for dest in compile_dests:
221 if os.path.isfile(dest): 245 if os.path.isfile(dest):
222 if not compile_file(dest, args.ddir, args.force, args.rx, 246 if not compile_file(dest, args.ddir, args.force, args.rx,
223 args.quiet, args.legacy): 247 args.quiet, args.legacy):
224 success = False 248 success = False
225 else: 249 else:
226 if not compile_dir(dest, args.maxlevels, args.ddir, 250 if not compile_dir(dest, args.maxlevels, args.ddir,
227 args.force, args.rx, args.quiet, 251 args.force, args.rx, args.quiet,
228 args.legacy): 252 args.legacy, processes=args.processes):
229 success = False 253 success = False
230 return success 254 return success
231 else: 255 else:
232 return compile_path(legacy=args.legacy, force=args.force, 256 return compile_path(legacy=args.legacy, force=args.force,
233 quiet=args.quiet) 257 quiet=args.quiet)
234 except KeyboardInterrupt: 258 except KeyboardInterrupt:
235 print("\n[interrupted]") 259 print("\n[interrupted]")
236 return False 260 return False
237 return True 261 return True
238 262
239 263
240 if __name__ == '__main__': 264 if __name__ == '__main__':
241 exit_status = int(not main()) 265 exit_status = int(not main())
242 sys.exit(exit_status) 266 sys.exit(exit_status)
OLDNEW

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