Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`compileall` --- Byte-compile Python libraries |
| 2 | =================================================== |
| 3 | |
| 4 | .. module:: compileall |
| 5 | :synopsis: Tools for byte-compiling all Python source files in a directory tree. |
| 6 | |
| 7 | |
| 8 | This module provides some utility functions to support installing Python |
R. David Murray | 43b2f45 | 2011-02-11 03:13:19 +0000 | [diff] [blame] | 9 | libraries. These functions compile Python source files in a directory tree. |
| 10 | This module can be used to create the cached byte-code files at library |
| 11 | installation time, which makes them available for use even by users who don't |
| 12 | have write permission to the library directories. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 13 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 14 | |
Éric Araujo | 3efdf06 | 2010-12-16 03:16:29 +0000 | [diff] [blame] | 15 | Command-line use |
| 16 | ---------------- |
| 17 | |
| 18 | This module can work as a script (using :program:`python -m compileall`) to |
| 19 | compile Python sources. |
| 20 | |
| 21 | .. program:: compileall |
| 22 | |
| 23 | .. cmdoption:: [directory|file]... |
| 24 | |
| 25 | Positional arguments are files to compile or directories that contain |
| 26 | source files, traversed recursively. If no argument is given, behave as if |
| 27 | the command line was ``-l <directories from sys.path>``. |
| 28 | |
| 29 | .. cmdoption:: -l |
| 30 | |
R. David Murray | 43b2f45 | 2011-02-11 03:13:19 +0000 | [diff] [blame] | 31 | Do not recurse into subdirectories, only compile source code files directly |
| 32 | contained in the named or implied directories. |
Éric Araujo | 3efdf06 | 2010-12-16 03:16:29 +0000 | [diff] [blame] | 33 | |
| 34 | .. cmdoption:: -f |
| 35 | |
| 36 | Force rebuild even if timestamps are up-to-date. |
| 37 | |
| 38 | .. cmdoption:: -q |
| 39 | |
R. David Murray | 43b2f45 | 2011-02-11 03:13:19 +0000 | [diff] [blame] | 40 | Do not print the list of files compiled, print only error messages. |
Éric Araujo | 3efdf06 | 2010-12-16 03:16:29 +0000 | [diff] [blame] | 41 | |
| 42 | .. cmdoption:: -d destdir |
| 43 | |
R. David Murray | 43b2f45 | 2011-02-11 03:13:19 +0000 | [diff] [blame] | 44 | Directory prepended to the path to each file being compiled. This will |
| 45 | appear in compilation time tracebacks, and is also compiled in to the |
| 46 | byte-code file, where it will be used in tracebacks and other messages in |
| 47 | cases where the source file does not exist at the time the byte-code file is |
| 48 | executed. |
Éric Araujo | 3efdf06 | 2010-12-16 03:16:29 +0000 | [diff] [blame] | 49 | |
| 50 | .. cmdoption:: -x regex |
| 51 | |
R. David Murray | 43b2f45 | 2011-02-11 03:13:19 +0000 | [diff] [blame] | 52 | regex is used to search the full path to each file considered for |
| 53 | compilation, and if the regex produces a match, the file is skipped. |
Éric Araujo | 3efdf06 | 2010-12-16 03:16:29 +0000 | [diff] [blame] | 54 | |
| 55 | |
| 56 | Public functions |
| 57 | ---------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 58 | |
Georg Brandl | c2a4f4f | 2009-04-10 09:03:43 +0000 | [diff] [blame] | 59 | .. function:: compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None, quiet=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 60 | |
| 61 | Recursively descend the directory tree named by *dir*, compiling all :file:`.py` |
R. David Murray | 43b2f45 | 2011-02-11 03:13:19 +0000 | [diff] [blame] | 62 | files along the way. |
| 63 | |
| 64 | The *maxlevels* parameter is used to limit the depth of the recursion; it |
| 65 | defaults to ``10``. |
| 66 | |
| 67 | If *ddir* is given, it is prepended to the path to each file being compiled |
| 68 | for use in compilation time tracebacks, and is also compiled in to the |
| 69 | byte-code file, where it will be used in tracebacks and other messages in |
| 70 | cases where the source file does not exist at the time the byte-code file is |
| 71 | executed. |
| 72 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 73 | If *force* is true, modules are re-compiled even if the timestamps are up to |
| 74 | date. |
| 75 | |
R. David Murray | 43b2f45 | 2011-02-11 03:13:19 +0000 | [diff] [blame] | 76 | If *rx* is given, its search method is called on the complete path to each |
| 77 | file considered for compilation, and if it returns a true value, the file |
| 78 | is skipped. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 79 | |
R. David Murray | 43b2f45 | 2011-02-11 03:13:19 +0000 | [diff] [blame] | 80 | If *quiet* is true, nothing is printed to the standard output unless errors |
| 81 | occur. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 82 | |
Georg Brandl | c2a4f4f | 2009-04-10 09:03:43 +0000 | [diff] [blame] | 83 | .. function:: compile_path(skip_curdir=True, maxlevels=0, force=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 84 | |
| 85 | Byte-compile all the :file:`.py` files found along ``sys.path``. If |
R. David Murray | 43b2f45 | 2011-02-11 03:13:19 +0000 | [diff] [blame] | 86 | *skip_curdir* is true (the default), the current directory is not included |
| 87 | in the search. All other parameters are passed to the :func:`compile_dir` |
| 88 | function. Note that unlike the other compile functions, ``maxlevels`` |
| 89 | defaults to ``0``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 90 | |
| 91 | To force a recompile of all the :file:`.py` files in the :file:`Lib/` |
| 92 | subdirectory and all its subdirectories:: |
| 93 | |
| 94 | import compileall |
| 95 | |
| 96 | compileall.compile_dir('Lib/', force=True) |
| 97 | |
| 98 | # Perform same compilation, excluding files in .svn directories. |
| 99 | import re |
| 100 | compileall.compile_dir('Lib/', rx=re.compile('/[.]svn'), force=True) |
| 101 | |
| 102 | |
| 103 | .. seealso:: |
| 104 | |
| 105 | Module :mod:`py_compile` |
| 106 | Byte-compile a single source file. |