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 |
| 9 | libraries. These functions compile Python source files in a directory tree, |
| 10 | allowing users without permission to write to the libraries to take advantage of |
| 11 | cached byte-code files. |
| 12 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 13 | |
Éric Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 14 | Command-line use |
| 15 | ---------------- |
| 16 | |
| 17 | This module can work as a script (using :program:`python -m compileall`) to |
| 18 | compile Python sources. |
| 19 | |
| 20 | .. program:: compileall |
| 21 | |
| 22 | .. cmdoption:: [directory|file]... |
| 23 | |
| 24 | Positional arguments are files to compile or directories that contain |
| 25 | source files, traversed recursively. If no argument is given, behave as if |
| 26 | the command line was ``-l <directories from sys.path>``. |
| 27 | |
| 28 | .. cmdoption:: -l |
| 29 | |
| 30 | Do not recurse. |
| 31 | |
| 32 | .. cmdoption:: -f |
| 33 | |
| 34 | Force rebuild even if timestamps are up-to-date. |
| 35 | |
| 36 | .. cmdoption:: -q |
| 37 | |
| 38 | Do not print the list of files compiled. |
| 39 | |
| 40 | .. cmdoption:: -d destdir |
| 41 | |
| 42 | Purported directory name for error messages. |
| 43 | |
| 44 | .. cmdoption:: -x regex |
| 45 | |
| 46 | Skip files with a full path that matches given regular expression. |
| 47 | |
| 48 | .. cmdoption:: -i list |
| 49 | |
| 50 | Expand list with its content (file and directory names). |
| 51 | |
| 52 | .. cmdoption:: -b |
| 53 | |
| 54 | Write legacy ``.pyc`` file path names. Default is to write :pep:`3147`-style |
| 55 | byte-compiled path names. |
| 56 | |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 57 | .. versionchanged:: 3.2 |
| 58 | Added the ``-i``, ``-b`` and ``-h`` options. |
Éric Araujo | f68fa05 | 2010-12-16 02:10:11 +0000 | [diff] [blame] | 59 | |
Éric Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 60 | |
| 61 | Public functions |
| 62 | ---------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 63 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 64 | .. function:: compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None, quiet=False, legacy=False, optimize=-1) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 65 | |
| 66 | Recursively descend the directory tree named by *dir*, compiling all :file:`.py` |
| 67 | files along the way. The *maxlevels* parameter is used to limit the depth of |
| 68 | the recursion; it defaults to ``10``. If *ddir* is given, it is used as the |
| 69 | base path from which the filenames used in error messages will be generated. |
| 70 | If *force* is true, modules are re-compiled even if the timestamps are up to |
| 71 | date. |
| 72 | |
| 73 | If *rx* is given, it specifies a regular expression of file names to exclude |
| 74 | from the search; that expression is searched for in the full path. |
| 75 | |
| 76 | If *quiet* is true, nothing is printed to the standard output in normal |
| 77 | operation. |
| 78 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 79 | If *legacy* is true, old-style ``.pyc`` file path names are written, |
Éric Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 80 | otherwise (the default), :pep:`3147`-style path names are written. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 81 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 82 | *optimize* specifies the optimization level for the compiler. It is passed to |
| 83 | the built-in :func:`compile` function. |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 84 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 85 | .. versionchanged:: 3.2 |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 86 | Added the *legacy* and *optimize* parameter. |
| 87 | |
| 88 | |
| 89 | .. function:: compile_file(fullname, ddir=None, force=False, rx=None, quiet=False, legacy=False, optimize=-1) |
| 90 | |
| 91 | Compile the file with path *fullname*. If *ddir* is given, it is used as the |
| 92 | base path from which the filename used in error messages will be generated. |
| 93 | If *force* is true, modules are re-compiled even if the timestamp is up to |
| 94 | date. |
| 95 | |
| 96 | If *rx* is given, it specifies a regular expression which, if matched, will |
| 97 | prevent compilation; that expression is searched for in the full path. |
| 98 | |
| 99 | If *quiet* is true, nothing is printed to the standard output in normal |
| 100 | operation. |
| 101 | |
| 102 | If *legacy* is true, old-style ``.pyc`` file path names are written, |
| 103 | otherwise (the default), :pep:`3147`-style path names are written. |
| 104 | |
| 105 | *optimize* specifies the optimization level for the compiler. It is passed to |
| 106 | the built-in :func:`compile` function. |
| 107 | |
| 108 | .. versionadded:: 3.2 |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 109 | |
| 110 | |
| 111 | .. function:: compile_path(skip_curdir=True, maxlevels=0, force=False, legacy=False, optimize=-1) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 112 | |
| 113 | Byte-compile all the :file:`.py` files found along ``sys.path``. If |
| 114 | *skip_curdir* is true (the default), the current directory is not included in |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 115 | the search. All other parameters are passed to the :func:`compile_dir` |
| 116 | function. |
| 117 | |
| 118 | .. versionchanged:: 3.2 |
Éric Araujo | 930df31 | 2010-12-16 06:28:48 +0000 | [diff] [blame] | 119 | Added the *legacy* and *optimize* parameter. |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 120 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 121 | |
| 122 | To force a recompile of all the :file:`.py` files in the :file:`Lib/` |
| 123 | subdirectory and all its subdirectories:: |
| 124 | |
| 125 | import compileall |
| 126 | |
| 127 | compileall.compile_dir('Lib/', force=True) |
| 128 | |
| 129 | # Perform same compilation, excluding files in .svn directories. |
| 130 | import re |
| 131 | compileall.compile_dir('Lib/', rx=re.compile('/[.]svn'), force=True) |
| 132 | |
| 133 | |
| 134 | .. seealso:: |
| 135 | |
| 136 | Module :mod:`py_compile` |
| 137 | Byte-compile a single source file. |