Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +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 | |
Georg Brandl | c066105 | 2014-10-02 08:38:39 +0200 | [diff] [blame] | 7 | **Source code:** :source:`Lib/compileall.py` |
| 8 | |
| 9 | -------------- |
| 10 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 11 | |
| 12 | This module provides some utility functions to support installing Python |
R. David Murray | 561b96f | 2011-02-11 17:25:54 +0000 | [diff] [blame] | 13 | libraries. These functions compile Python source files in a directory tree. |
| 14 | This module can be used to create the cached byte-code files at library |
| 15 | installation time, which makes them available for use even by users who don't |
| 16 | have write permission to the library directories. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 17 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 18 | |
Éric Araujo | a8132ec | 2010-12-16 03:53:53 +0000 | [diff] [blame] | 19 | Command-line use |
| 20 | ---------------- |
| 21 | |
| 22 | This module can work as a script (using :program:`python -m compileall`) to |
| 23 | compile Python sources. |
| 24 | |
| 25 | .. program:: compileall |
| 26 | |
Benjamin Peterson | beda110 | 2014-09-04 22:40:34 -0400 | [diff] [blame] | 27 | .. cmdoption:: directory ... |
| 28 | file ... |
Éric Araujo | a8132ec | 2010-12-16 03:53:53 +0000 | [diff] [blame] | 29 | |
| 30 | Positional arguments are files to compile or directories that contain |
| 31 | source files, traversed recursively. If no argument is given, behave as if |
| 32 | the command line was ``-l <directories from sys.path>``. |
| 33 | |
| 34 | .. cmdoption:: -l |
| 35 | |
R. David Murray | 561b96f | 2011-02-11 17:25:54 +0000 | [diff] [blame] | 36 | Do not recurse into subdirectories, only compile source code files directly |
| 37 | contained in the named or implied directories. |
Éric Araujo | a8132ec | 2010-12-16 03:53:53 +0000 | [diff] [blame] | 38 | |
| 39 | .. cmdoption:: -f |
| 40 | |
| 41 | Force rebuild even if timestamps are up-to-date. |
| 42 | |
| 43 | .. cmdoption:: -q |
| 44 | |
R. David Murray | 561b96f | 2011-02-11 17:25:54 +0000 | [diff] [blame] | 45 | Do not print the list of files compiled, print only error messages. |
Éric Araujo | a8132ec | 2010-12-16 03:53:53 +0000 | [diff] [blame] | 46 | |
| 47 | .. cmdoption:: -d destdir |
| 48 | |
R. David Murray | 561b96f | 2011-02-11 17:25:54 +0000 | [diff] [blame] | 49 | Directory prepended to the path to each file being compiled. This will |
| 50 | appear in compilation time tracebacks, and is also compiled in to the |
| 51 | byte-code file, where it will be used in tracebacks and other messages in |
| 52 | cases where the source file does not exist at the time the byte-code file is |
| 53 | executed. |
Éric Araujo | a8132ec | 2010-12-16 03:53:53 +0000 | [diff] [blame] | 54 | |
| 55 | .. cmdoption:: -x regex |
| 56 | |
R. David Murray | 561b96f | 2011-02-11 17:25:54 +0000 | [diff] [blame] | 57 | regex is used to search the full path to each file considered for |
| 58 | compilation, and if the regex produces a match, the file is skipped. |
Éric Araujo | a8132ec | 2010-12-16 03:53:53 +0000 | [diff] [blame] | 59 | |
| 60 | .. cmdoption:: -i list |
| 61 | |
R. David Murray | 561b96f | 2011-02-11 17:25:54 +0000 | [diff] [blame] | 62 | Read the file ``list`` and add each line that it contains to the list of |
| 63 | files and directories to compile. If ``list`` is ``-``, read lines from |
| 64 | ``stdin``. |
Éric Araujo | a8132ec | 2010-12-16 03:53:53 +0000 | [diff] [blame] | 65 | |
Éric Araujo | c11ba76 | 2010-12-16 06:15:02 +0000 | [diff] [blame] | 66 | .. versionchanged:: 2.7 |
| 67 | Added the ``-i`` option. |
Éric Araujo | a8132ec | 2010-12-16 03:53:53 +0000 | [diff] [blame] | 68 | |
| 69 | |
| 70 | Public functions |
| 71 | ---------------- |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 72 | |
Éric Araujo | c11ba76 | 2010-12-16 06:15:02 +0000 | [diff] [blame] | 73 | .. function:: compile_dir(dir[, maxlevels[, ddir[, force[, rx[, quiet]]]]]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 74 | |
| 75 | Recursively descend the directory tree named by *dir*, compiling all :file:`.py` |
R. David Murray | 561b96f | 2011-02-11 17:25:54 +0000 | [diff] [blame] | 76 | files along the way. |
| 77 | |
| 78 | The *maxlevels* parameter is used to limit the depth of the recursion; it |
| 79 | defaults to ``10``. |
| 80 | |
| 81 | If *ddir* is given, it is prepended to the path to each file being compiled |
| 82 | for use in compilation time tracebacks, and is also compiled in to the |
| 83 | byte-code file, where it will be used in tracebacks and other messages in |
| 84 | cases where the source file does not exist at the time the byte-code file is |
| 85 | executed. |
| 86 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 87 | If *force* is true, modules are re-compiled even if the timestamps are up to |
| 88 | date. |
| 89 | |
R. David Murray | 561b96f | 2011-02-11 17:25:54 +0000 | [diff] [blame] | 90 | If *rx* is given, its search method is called on the complete path to each |
| 91 | file considered for compilation, and if it returns a true value, the file |
| 92 | is skipped. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 93 | |
R. David Murray | 561b96f | 2011-02-11 17:25:54 +0000 | [diff] [blame] | 94 | If *quiet* is true, nothing is printed to the standard output unless errors |
| 95 | occur. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 96 | |
Éric Araujo | c11ba76 | 2010-12-16 06:15:02 +0000 | [diff] [blame] | 97 | |
| 98 | .. function:: compile_file(fullname[, ddir[, force[, rx[, quiet]]]]) |
| 99 | |
R. David Murray | 561b96f | 2011-02-11 17:25:54 +0000 | [diff] [blame] | 100 | Compile the file with path *fullname*. |
Éric Araujo | c11ba76 | 2010-12-16 06:15:02 +0000 | [diff] [blame] | 101 | |
R. David Murray | 561b96f | 2011-02-11 17:25:54 +0000 | [diff] [blame] | 102 | If *ddir* is given, it is prepended to the path to the file being compiled |
| 103 | for use in compilation time tracebacks, and is also compiled in to the |
| 104 | byte-code file, where it will be used in tracebacks and other messages in |
| 105 | cases where the source file does not exist at the time the byte-code file is |
| 106 | executed. |
Éric Araujo | c11ba76 | 2010-12-16 06:15:02 +0000 | [diff] [blame] | 107 | |
R. David Murray | 46c4e47 | 2011-02-11 22:54:34 +0000 | [diff] [blame] | 108 | If *rx* is given, its search method is passed the full path name to the |
R. David Murray | 561b96f | 2011-02-11 17:25:54 +0000 | [diff] [blame] | 109 | file being compiled, and if it returns a true value, the file is not |
| 110 | compiled and ``True`` is returned. |
| 111 | |
| 112 | If *quiet* is true, nothing is printed to the standard output unless errors |
| 113 | occur. |
Éric Araujo | c11ba76 | 2010-12-16 06:15:02 +0000 | [diff] [blame] | 114 | |
| 115 | .. versionadded:: 2.7 |
| 116 | |
| 117 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 118 | .. function:: compile_path([skip_curdir[, maxlevels[, force]]]) |
| 119 | |
| 120 | Byte-compile all the :file:`.py` files found along ``sys.path``. If |
R. David Murray | 561b96f | 2011-02-11 17:25:54 +0000 | [diff] [blame] | 121 | *skip_curdir* is true (the default), the current directory is not included |
| 122 | in the search. All other parameters are passed to the :func:`compile_dir` |
| 123 | function. Note that unlike the other compile functions, ``maxlevels`` |
| 124 | defaults to ``0``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 125 | |
| 126 | To force a recompile of all the :file:`.py` files in the :file:`Lib/` |
| 127 | subdirectory and all its subdirectories:: |
| 128 | |
| 129 | import compileall |
| 130 | |
| 131 | compileall.compile_dir('Lib/', force=True) |
| 132 | |
| 133 | # Perform same compilation, excluding files in .svn directories. |
| 134 | import re |
Georg Brandl | 95aa172 | 2013-04-14 12:02:43 +0200 | [diff] [blame] | 135 | compileall.compile_dir('Lib/', rx=re.compile(r'[/\\][.]svn'), force=True) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 136 | |
| 137 | |
| 138 | .. seealso:: |
| 139 | |
| 140 | Module :mod:`py_compile` |
| 141 | Byte-compile a single source file. |