compileall.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. """Module/script to byte-compile all .py files to .pyc files.
  2. When called as a script with arguments, this compiles the directories
  3. given as arguments recursively; the -l option prevents it from
  4. recursing into directories.
  5. Without arguments, if compiles all modules on sys.path, without
  6. recursing into subdirectories. (Even though it should do so for
  7. packages -- for now, you'll have to deal with packages separately.)
  8. See module py_compile for details of the actual byte-compilation.
  9. """
  10. import os
  11. import sys
  12. import importlib.util
  13. import py_compile
  14. import struct
  15. import filecmp
  16. from functools import partial
  17. from pathlib import Path
  18. __all__ = ["compile_dir","compile_file","compile_path"]
  19. def _walk_dir(dir, maxlevels, quiet=0):
  20. if quiet < 2 and isinstance(dir, os.PathLike):
  21. dir = os.fspath(dir)
  22. if not quiet:
  23. print('Listing {!r}...'.format(dir))
  24. try:
  25. names = os.listdir(dir)
  26. except OSError:
  27. if quiet < 2:
  28. print("Can't list {!r}".format(dir))
  29. names = []
  30. names.sort()
  31. for name in names:
  32. if name == '__pycache__':
  33. continue
  34. fullname = os.path.join(dir, name)
  35. if not os.path.isdir(fullname):
  36. yield fullname
  37. elif (maxlevels > 0 and name != os.curdir and name != os.pardir and
  38. os.path.isdir(fullname) and not os.path.islink(fullname)):
  39. yield from _walk_dir(fullname, maxlevels=maxlevels - 1,
  40. quiet=quiet)
  41. def compile_dir(dir, maxlevels=None, ddir=None, force=False,
  42. rx=None, quiet=0, legacy=False, optimize=-1, workers=1,
  43. invalidation_mode=None, *, stripdir=None,
  44. prependdir=None, limit_sl_dest=None, hardlink_dupes=False):
  45. """Byte-compile all modules in the given directory tree.
  46. Arguments (only dir is required):
  47. dir: the directory to byte-compile
  48. maxlevels: maximum recursion level (default `sys.getrecursionlimit()`)
  49. ddir: the directory that will be prepended to the path to the
  50. file as it is compiled into each byte-code file.
  51. force: if True, force compilation, even if timestamps are up-to-date
  52. quiet: full output with False or 0, errors only with 1,
  53. no output with 2
  54. legacy: if True, produce legacy pyc paths instead of PEP 3147 paths
  55. optimize: int or list of optimization levels or -1 for level of
  56. the interpreter. Multiple levels leads to multiple compiled
  57. files each with one optimization level.
  58. workers: maximum number of parallel workers
  59. invalidation_mode: how the up-to-dateness of the pyc will be checked
  60. stripdir: part of path to left-strip from source file path
  61. prependdir: path to prepend to beginning of original file path, applied
  62. after stripdir
  63. limit_sl_dest: ignore symlinks if they are pointing outside of
  64. the defined path
  65. hardlink_dupes: hardlink duplicated pyc files
  66. """
  67. ProcessPoolExecutor = None
  68. if ddir is not None and (stripdir is not None or prependdir is not None):
  69. raise ValueError(("Destination dir (ddir) cannot be used "
  70. "in combination with stripdir or prependdir"))
  71. if ddir is not None:
  72. stripdir = dir
  73. prependdir = ddir
  74. ddir = None
  75. if workers < 0:
  76. raise ValueError('workers must be greater or equal to 0')
  77. if workers != 1:
  78. try:
  79. # Only import when needed, as low resource platforms may
  80. # fail to import it
  81. from concurrent.futures import ProcessPoolExecutor
  82. except ImportError:
  83. workers = 1
  84. if maxlevels is None:
  85. maxlevels = sys.getrecursionlimit()
  86. files = _walk_dir(dir, quiet=quiet, maxlevels=maxlevels)
  87. success = True
  88. if workers != 1 and ProcessPoolExecutor is not None:
  89. # If workers == 0, let ProcessPoolExecutor choose
  90. workers = workers or None
  91. with ProcessPoolExecutor(max_workers=workers) as executor:
  92. results = executor.map(partial(compile_file,
  93. ddir=ddir, force=force,
  94. rx=rx, quiet=quiet,
  95. legacy=legacy,
  96. optimize=optimize,
  97. invalidation_mode=invalidation_mode,
  98. stripdir=stripdir,
  99. prependdir=prependdir,
  100. limit_sl_dest=limit_sl_dest,
  101. hardlink_dupes=hardlink_dupes),
  102. files)
  103. success = min(results, default=True)
  104. else:
  105. for file in files:
  106. if not compile_file(file, ddir, force, rx, quiet,
  107. legacy, optimize, invalidation_mode,
  108. stripdir=stripdir, prependdir=prependdir,
  109. limit_sl_dest=limit_sl_dest,
  110. hardlink_dupes=hardlink_dupes):
  111. success = False
  112. return success
  113. def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0,
  114. legacy=False, optimize=-1,
  115. invalidation_mode=None, *, stripdir=None, prependdir=None,
  116. limit_sl_dest=None, hardlink_dupes=False):
  117. """Byte-compile one file.
  118. Arguments (only fullname is required):
  119. fullname: the file to byte-compile
  120. ddir: if given, the directory name compiled in to the
  121. byte-code file.
  122. force: if True, force compilation, even if timestamps are up-to-date
  123. quiet: full output with False or 0, errors only with 1,
  124. no output with 2
  125. legacy: if True, produce legacy pyc paths instead of PEP 3147 paths
  126. optimize: int or list of optimization levels or -1 for level of
  127. the interpreter. Multiple levels leads to multiple compiled
  128. files each with one optimization level.
  129. invalidation_mode: how the up-to-dateness of the pyc will be checked
  130. stripdir: part of path to left-strip from source file path
  131. prependdir: path to prepend to beginning of original file path, applied
  132. after stripdir
  133. limit_sl_dest: ignore symlinks if they are pointing outside of
  134. the defined path.
  135. hardlink_dupes: hardlink duplicated pyc files
  136. """
  137. if ddir is not None and (stripdir is not None or prependdir is not None):
  138. raise ValueError(("Destination dir (ddir) cannot be used "
  139. "in combination with stripdir or prependdir"))
  140. success = True
  141. if quiet < 2 and isinstance(fullname, os.PathLike):
  142. fullname = os.fspath(fullname)
  143. name = os.path.basename(fullname)
  144. dfile = None
  145. if ddir is not None:
  146. dfile = os.path.join(ddir, name)
  147. if stripdir is not None:
  148. fullname_parts = fullname.split(os.path.sep)
  149. stripdir_parts = stripdir.split(os.path.sep)
  150. ddir_parts = list(fullname_parts)
  151. for spart, opart in zip(stripdir_parts, fullname_parts):
  152. if spart == opart:
  153. ddir_parts.remove(spart)
  154. dfile = os.path.join(*ddir_parts)
  155. if prependdir is not None:
  156. if dfile is None:
  157. dfile = os.path.join(prependdir, fullname)
  158. else:
  159. dfile = os.path.join(prependdir, dfile)
  160. if isinstance(optimize, int):
  161. optimize = [optimize]
  162. # Use set() to remove duplicates.
  163. # Use sorted() to create pyc files in a deterministic order.
  164. optimize = sorted(set(optimize))
  165. if hardlink_dupes and len(optimize) < 2:
  166. raise ValueError("Hardlinking of duplicated bytecode makes sense "
  167. "only for more than one optimization level")
  168. if rx is not None:
  169. mo = rx.search(fullname)
  170. if mo:
  171. return success
  172. if limit_sl_dest is not None and os.path.islink(fullname):
  173. if Path(limit_sl_dest).resolve() not in Path(fullname).resolve().parents:
  174. return success
  175. opt_cfiles = {}
  176. if os.path.isfile(fullname):
  177. for opt_level in optimize:
  178. if legacy:
  179. opt_cfiles[opt_level] = fullname + 'c'
  180. else:
  181. if opt_level >= 0:
  182. opt = opt_level if opt_level >= 1 else ''
  183. cfile = (importlib.util.cache_from_source(
  184. fullname, optimization=opt))
  185. opt_cfiles[opt_level] = cfile
  186. else:
  187. cfile = importlib.util.cache_from_source(fullname)
  188. opt_cfiles[opt_level] = cfile
  189. head, tail = name[:-3], name[-3:]
  190. if tail == '.py':
  191. if not force:
  192. try:
  193. mtime = int(os.stat(fullname).st_mtime)
  194. expect = struct.pack('<4sLL', importlib.util.MAGIC_NUMBER,
  195. 0, mtime & 0xFFFF_FFFF)
  196. for cfile in opt_cfiles.values():
  197. with open(cfile, 'rb') as chandle:
  198. actual = chandle.read(12)
  199. if expect != actual:
  200. break
  201. else:
  202. return success
  203. except OSError:
  204. pass
  205. if not quiet:
  206. print('Compiling {!r}...'.format(fullname))
  207. try:
  208. for index, opt_level in enumerate(optimize):
  209. cfile = opt_cfiles[opt_level]
  210. ok = py_compile.compile(fullname, cfile, dfile, True,
  211. optimize=opt_level,
  212. invalidation_mode=invalidation_mode)
  213. if index > 0 and hardlink_dupes:
  214. previous_cfile = opt_cfiles[optimize[index - 1]]
  215. if filecmp.cmp(cfile, previous_cfile, shallow=False):
  216. os.unlink(cfile)
  217. os.link(previous_cfile, cfile)
  218. except py_compile.PyCompileError as err:
  219. success = False
  220. if quiet >= 2:
  221. return success
  222. elif quiet:
  223. print('*** Error compiling {!r}...'.format(fullname))
  224. else:
  225. print('*** ', end='')
  226. # escape non-printable characters in msg
  227. encoding = sys.stdout.encoding or sys.getdefaultencoding()
  228. msg = err.msg.encode(encoding, errors='backslashreplace').decode(encoding)
  229. print(msg)
  230. except (SyntaxError, UnicodeError, OSError) as e:
  231. success = False
  232. if quiet >= 2:
  233. return success
  234. elif quiet:
  235. print('*** Error compiling {!r}...'.format(fullname))
  236. else:
  237. print('*** ', end='')
  238. print(e.__class__.__name__ + ':', e)
  239. else:
  240. if ok == 0:
  241. success = False
  242. return success
  243. def compile_path(skip_curdir=1, maxlevels=0, force=False, quiet=0,
  244. legacy=False, optimize=-1,
  245. invalidation_mode=None):
  246. """Byte-compile all module on sys.path.
  247. Arguments (all optional):
  248. skip_curdir: if true, skip current directory (default True)
  249. maxlevels: max recursion level (default 0)
  250. force: as for compile_dir() (default False)
  251. quiet: as for compile_dir() (default 0)
  252. legacy: as for compile_dir() (default False)
  253. optimize: as for compile_dir() (default -1)
  254. invalidation_mode: as for compiler_dir()
  255. """
  256. success = True
  257. for dir in sys.path:
  258. if (not dir or dir == os.curdir) and skip_curdir:
  259. if quiet < 2:
  260. print('Skipping current directory')
  261. else:
  262. success = success and compile_dir(
  263. dir,
  264. maxlevels,
  265. None,
  266. force,
  267. quiet=quiet,
  268. legacy=legacy,
  269. optimize=optimize,
  270. invalidation_mode=invalidation_mode,
  271. )
  272. return success
  273. def main():
  274. """Script main program."""
  275. import argparse
  276. parser = argparse.ArgumentParser(
  277. description='Utilities to support installing Python libraries.')
  278. parser.add_argument('-l', action='store_const', const=0,
  279. default=None, dest='maxlevels',
  280. help="don't recurse into subdirectories")
  281. parser.add_argument('-r', type=int, dest='recursion',
  282. help=('control the maximum recursion level. '
  283. 'if `-l` and `-r` options are specified, '
  284. 'then `-r` takes precedence.'))
  285. parser.add_argument('-f', action='store_true', dest='force',
  286. help='force rebuild even if timestamps are up to date')
  287. parser.add_argument('-q', action='count', dest='quiet', default=0,
  288. help='output only error messages; -qq will suppress '
  289. 'the error messages as well.')
  290. parser.add_argument('-b', action='store_true', dest='legacy',
  291. help='use legacy (pre-PEP3147) compiled file locations')
  292. parser.add_argument('-d', metavar='DESTDIR', dest='ddir', default=None,
  293. help=('directory to prepend to file paths for use in '
  294. 'compile-time tracebacks and in runtime '
  295. 'tracebacks in cases where the source file is '
  296. 'unavailable'))
  297. parser.add_argument('-s', metavar='STRIPDIR', dest='stripdir',
  298. default=None,
  299. help=('part of path to left-strip from path '
  300. 'to source file - for example buildroot. '
  301. '`-d` and `-s` options cannot be '
  302. 'specified together.'))
  303. parser.add_argument('-p', metavar='PREPENDDIR', dest='prependdir',
  304. default=None,
  305. help=('path to add as prefix to path '
  306. 'to source file - for example / to make '
  307. 'it absolute when some part is removed '
  308. 'by `-s` option. '
  309. '`-d` and `-p` options cannot be '
  310. 'specified together.'))
  311. parser.add_argument('-x', metavar='REGEXP', dest='rx', default=None,
  312. help=('skip files matching the regular expression; '
  313. 'the regexp is searched for in the full path '
  314. 'of each file considered for compilation'))
  315. parser.add_argument('-i', metavar='FILE', dest='flist',
  316. help=('add all the files and directories listed in '
  317. 'FILE to the list considered for compilation; '
  318. 'if "-", names are read from stdin'))
  319. parser.add_argument('compile_dest', metavar='FILE|DIR', nargs='*',
  320. help=('zero or more file and directory names '
  321. 'to compile; if no arguments given, defaults '
  322. 'to the equivalent of -l sys.path'))
  323. parser.add_argument('-j', '--workers', default=1,
  324. type=int, help='Run compileall concurrently')
  325. invalidation_modes = [mode.name.lower().replace('_', '-')
  326. for mode in py_compile.PycInvalidationMode]
  327. parser.add_argument('--invalidation-mode',
  328. choices=sorted(invalidation_modes),
  329. help=('set .pyc invalidation mode; defaults to '
  330. '"checked-hash" if the SOURCE_DATE_EPOCH '
  331. 'environment variable is set, and '
  332. '"timestamp" otherwise.'))
  333. parser.add_argument('-o', action='append', type=int, dest='opt_levels',
  334. help=('Optimization levels to run compilation with. '
  335. 'Default is -1 which uses the optimization level '
  336. 'of the Python interpreter itself (see -O).'))
  337. parser.add_argument('-e', metavar='DIR', dest='limit_sl_dest',
  338. help='Ignore symlinks pointing outsite of the DIR')
  339. parser.add_argument('--hardlink-dupes', action='store_true',
  340. dest='hardlink_dupes',
  341. help='Hardlink duplicated pyc files')
  342. args = parser.parse_args()
  343. compile_dests = args.compile_dest
  344. if args.rx:
  345. import re
  346. args.rx = re.compile(args.rx)
  347. if args.limit_sl_dest == "":
  348. args.limit_sl_dest = None
  349. if args.recursion is not None:
  350. maxlevels = args.recursion
  351. else:
  352. maxlevels = args.maxlevels
  353. if args.opt_levels is None:
  354. args.opt_levels = [-1]
  355. if len(args.opt_levels) == 1 and args.hardlink_dupes:
  356. parser.error(("Hardlinking of duplicated bytecode makes sense "
  357. "only for more than one optimization level."))
  358. if args.ddir is not None and (
  359. args.stripdir is not None or args.prependdir is not None
  360. ):
  361. parser.error("-d cannot be used in combination with -s or -p")
  362. # if flist is provided then load it
  363. if args.flist:
  364. try:
  365. with (sys.stdin if args.flist=='-' else open(args.flist)) as f:
  366. for line in f:
  367. compile_dests.append(line.strip())
  368. except OSError:
  369. if args.quiet < 2:
  370. print("Error reading file list {}".format(args.flist))
  371. return False
  372. if args.invalidation_mode:
  373. ivl_mode = args.invalidation_mode.replace('-', '_').upper()
  374. invalidation_mode = py_compile.PycInvalidationMode[ivl_mode]
  375. else:
  376. invalidation_mode = None
  377. success = True
  378. try:
  379. if compile_dests:
  380. for dest in compile_dests:
  381. if os.path.isfile(dest):
  382. if not compile_file(dest, args.ddir, args.force, args.rx,
  383. args.quiet, args.legacy,
  384. invalidation_mode=invalidation_mode,
  385. stripdir=args.stripdir,
  386. prependdir=args.prependdir,
  387. optimize=args.opt_levels,
  388. limit_sl_dest=args.limit_sl_dest,
  389. hardlink_dupes=args.hardlink_dupes):
  390. success = False
  391. else:
  392. if not compile_dir(dest, maxlevels, args.ddir,
  393. args.force, args.rx, args.quiet,
  394. args.legacy, workers=args.workers,
  395. invalidation_mode=invalidation_mode,
  396. stripdir=args.stripdir,
  397. prependdir=args.prependdir,
  398. optimize=args.opt_levels,
  399. limit_sl_dest=args.limit_sl_dest,
  400. hardlink_dupes=args.hardlink_dupes):
  401. success = False
  402. return success
  403. else:
  404. return compile_path(legacy=args.legacy, force=args.force,
  405. quiet=args.quiet,
  406. invalidation_mode=invalidation_mode)
  407. except KeyboardInterrupt:
  408. if args.quiet < 2:
  409. print("\n[interrupted]")
  410. return False
  411. return True
  412. if __name__ == '__main__':
  413. exit_status = int(not main())
  414. sys.exit(exit_status)