Byte-compile a collection of Python source files to .pyc files in a __pycache__ subdirectory. 'py_files' is a list of files to compile; any files that don't end in ".py" are silently skipped. 'optimize' must be one of the following: 0 - don't optimize 1 - normal optimi
(py_files,
optimize=0, force=0,
prefix=None, base_dir=None,
verbose=1, dry_run=0,
direct=None)
| 322 | |
| 323 | |
| 324 | def byte_compile (py_files, |
| 325 | optimize=0, force=0, |
| 326 | prefix=None, base_dir=None, |
| 327 | verbose=1, dry_run=0, |
| 328 | direct=None): |
| 329 | """Byte-compile a collection of Python source files to .pyc |
| 330 | files in a __pycache__ subdirectory. 'py_files' is a list |
| 331 | of files to compile; any files that don't end in ".py" are silently |
| 332 | skipped. 'optimize' must be one of the following: |
| 333 | 0 - don't optimize |
| 334 | 1 - normal optimization (like "python -O") |
| 335 | 2 - extra optimization (like "python -OO") |
| 336 | If 'force' is true, all files are recompiled regardless of |
| 337 | timestamps. |
| 338 | |
| 339 | The source filename encoded in each bytecode file defaults to the |
| 340 | filenames listed in 'py_files'; you can modify these with 'prefix' and |
| 341 | 'basedir'. 'prefix' is a string that will be stripped off of each |
| 342 | source filename, and 'base_dir' is a directory name that will be |
| 343 | prepended (after 'prefix' is stripped). You can supply either or both |
| 344 | (or neither) of 'prefix' and 'base_dir', as you wish. |
| 345 | |
| 346 | If 'dry_run' is true, doesn't actually do anything that would |
| 347 | affect the filesystem. |
| 348 | |
| 349 | Byte-compilation is either done directly in this interpreter process |
| 350 | with the standard py_compile module, or indirectly by writing a |
| 351 | temporary script and executing it. Normally, you should let |
| 352 | 'byte_compile()' figure out to use direct compilation or not (see |
| 353 | the source for details). The 'direct' flag is used by the script |
| 354 | generated in indirect mode; unless you know what you're doing, leave |
| 355 | it set to None. |
| 356 | """ |
| 357 | |
| 358 | # Late import to fix a bootstrap issue: _posixsubprocess is built by |
| 359 | # setup.py, but setup.py uses distutils. |
| 360 | import subprocess |
| 361 | |
| 362 | # nothing is done if sys.dont_write_bytecode is True |
| 363 | if sys.dont_write_bytecode: |
| 364 | raise DistutilsByteCompileError('byte-compiling is disabled.') |
| 365 | |
| 366 | # First, if the caller didn't force us into direct or indirect mode, |
| 367 | # figure out which mode we should be in. We take a conservative |
| 368 | # approach: choose direct mode *only* if the current interpreter is |
| 369 | # in debug mode and optimize is 0. If we're not in debug mode (-O |
| 370 | # or -OO), we don't know which level of optimization this |
| 371 | # interpreter is running with, so we can't do direct |
| 372 | # byte-compilation and be certain that it's the right thing. Thus, |
| 373 | # always compile indirectly if the current interpreter is in either |
| 374 | # optimize mode, or if either optimization level was requested by |
| 375 | # the caller. |
| 376 | if direct is None: |
| 377 | direct = (__debug__ and optimize == 0) |
| 378 | |
| 379 | # "Indirect" byte-compilation: write a temporary script and then |
| 380 | # run it with the appropriate flags. |
| 381 | if not direct: |
no test coverage detected