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 | |
Benjamin Peterson | 5478b47 | 2008-09-17 22:25:09 +0000 | [diff] [blame] | 13 | This module may also be used as a script (using the :option:`-m` Python flag) to |
| 14 | compile Python sources. Directories to recursively traverse (passing |
| 15 | :option:`-l` stops the recursive behavior) for sources are listed on the command |
| 16 | line. If no arguments are given, the invocation is equivalent to ``-l |
| 17 | sys.path``. Printing lists of the files compiled can be disabled with the |
| 18 | :option:`-q` flag. In addition, the :option:`-x` option takes a regular |
| 19 | expression argument. All files that match the expression will be skipped. |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 20 | The :option:`-b` flag may be given to write legacy ``.pyc`` file path names, |
| 21 | otherwise :pep:`3147` style byte-compiled path names are written. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 22 | |
| 23 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 24 | .. function:: compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None, quiet=False, legacy=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 25 | |
| 26 | Recursively descend the directory tree named by *dir*, compiling all :file:`.py` |
| 27 | files along the way. The *maxlevels* parameter is used to limit the depth of |
| 28 | the recursion; it defaults to ``10``. If *ddir* is given, it is used as the |
| 29 | base path from which the filenames used in error messages will be generated. |
| 30 | If *force* is true, modules are re-compiled even if the timestamps are up to |
| 31 | date. |
| 32 | |
| 33 | If *rx* is given, it specifies a regular expression of file names to exclude |
| 34 | from the search; that expression is searched for in the full path. |
| 35 | |
| 36 | If *quiet* is true, nothing is printed to the standard output in normal |
| 37 | operation. |
| 38 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 39 | If *legacy* is true, old-style ``.pyc`` file path names are written, |
| 40 | otherwise (the default), :pep:`3147` style path names are written. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 41 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 42 | |
| 43 | .. function:: compile_path(skip_curdir=True, maxlevels=0, force=False, legacy=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 44 | |
| 45 | Byte-compile all the :file:`.py` files found along ``sys.path``. If |
| 46 | *skip_curdir* is true (the default), the current directory is not included in |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 47 | the search. The *maxlevels* parameter defaults to ``0``, and the *force* |
| 48 | and *legacy* parameters default to ``False``. All are |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 49 | passed to the :func:`compile_dir` function. |
| 50 | |
| 51 | To force a recompile of all the :file:`.py` files in the :file:`Lib/` |
| 52 | subdirectory and all its subdirectories:: |
| 53 | |
| 54 | import compileall |
| 55 | |
| 56 | compileall.compile_dir('Lib/', force=True) |
| 57 | |
| 58 | # Perform same compilation, excluding files in .svn directories. |
| 59 | import re |
| 60 | compileall.compile_dir('Lib/', rx=re.compile('/[.]svn'), force=True) |
| 61 | |
| 62 | |
| 63 | .. seealso:: |
| 64 | |
| 65 | Module :mod:`py_compile` |
| 66 | Byte-compile a single source file. |
| 67 | |