6
6
7
7
8
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.
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.
12
13
13
14
14
15
Command-line use
@@ -27,32 +28,42 @@ compile Python sources.
27
28
28
29
.. cmdoption :: -l
29
30
30
- Do not recurse.
31
+ Do not recurse into subdirectories, only compile source code files directly
32
+ contained in the named or implied directories.
31
33
32
34
.. cmdoption :: -f
33
35
34
36
Force rebuild even if timestamps are up-to-date.
35
37
36
38
.. cmdoption :: -q
37
39
38
- Do not print the list of files compiled.
40
+ Do not print the list of files compiled, print only error messages .
39
41
40
42
.. cmdoption :: -d destdir
41
43
42
- Purported directory name for error messages.
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.
43
49
44
50
.. cmdoption :: -x regex
45
51
46
- Skip files with a full path that matches given regular expression.
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.
47
54
48
55
.. cmdoption :: -i list
49
56
50
- Expand list with its content (file and directory names).
57
+ Read the file ``list `` and add each line that it contains to the list of
58
+ files and directories to compile. If ``list `` is ``- ``, read lines from
59
+ ``stdin ``.
51
60
52
61
.. cmdoption :: -b
53
62
54
- Write legacy ``.pyc `` file path names. Default is to write :pep: `3147 `-style
55
- byte-compiled path names.
63
+ Write the byte-code files to their legacy locations and names, which may
64
+ overwrite byte-code files created by another version of Python. The default
65
+ is to write files to their :pep: `3147 ` locations and names, which allows
66
+ byte-code files from multiple versions of Python to coexist.
56
67
57
68
.. versionchanged :: 3.2
58
69
Added the ``-i ``, ``-b `` and ``-h `` options.
@@ -64,20 +75,32 @@ Public functions
64
75
.. function :: compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None, quiet=False, legacy=False, optimize=-1)
65
76
66
77
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.
78
+ files along the way.
79
+
80
+ The *maxlevels * parameter is used to limit the depth of the recursion; it
81
+ defaults to ``10 ``.
82
+
83
+ If *ddir * is given, it is prepended to the path to each file being compiled
84
+ for use in compilation time tracebacks, and is also compiled in to the
85
+ byte-code file, where it will be used in tracebacks and other messages in
86
+ cases where the source file does not exist at the time the byte-code file is
87
+ executed.
88
+
70
89
If *force * is true, modules are re-compiled even if the timestamps are up to
71
90
date.
72
91
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.
92
+ If *rx * is given, its search method is called on the complete path to each
93
+ file considered for compilation, and if it returns a true value, the file
94
+ is skipped.
75
95
76
- If *quiet * is true, nothing is printed to the standard output in normal
77
- operation .
96
+ If *quiet * is true, nothing is printed to the standard output unless errors
97
+ occur .
78
98
79
- If *legacy * is true, old-style ``.pyc `` file path names are written,
80
- otherwise (the default), :pep: `3147 `-style path names are written.
99
+ If *legacy * is true, byte-code files are written to their legacy locations
100
+ and names, which may overwrite byte-code files created by another version of
101
+ Python. The default is to write files to their :pep: `3147 ` locations and
102
+ names, which allows byte-code files from multiple versions of Python to
103
+ coexist.
81
104
82
105
*optimize * specifies the optimization level for the compiler. It is passed to
83
106
the built-in :func: `compile ` function.
@@ -88,19 +111,26 @@ Public functions
88
111
89
112
.. function :: compile_file(fullname, ddir=None, force=False, rx=None, quiet=False, legacy=False, optimize=-1)
90
113
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.
114
+ Compile the file with path *fullname *.
115
+
116
+ If *ddir * is given, it is prepended to the path to the file being compiled
117
+ for use in compilation time tracebacks, and is also compiled in to the
118
+ byte-code file, where it will be used in tracebacks and other messages in
119
+ cases where the source file does not exist at the time the byte-code file is
120
+ executed.
95
121
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.
122
+ If *ra * is given, its search method is passed the full path name to the
123
+ file being compiled, and if it returns a true value, the file is not
124
+ compiled and ``True `` is returned.
98
125
99
- If *quiet * is true, nothing is printed to the standard output in normal
100
- operation .
126
+ If *quiet * is true, nothing is printed to the standard output unless errors
127
+ occur .
101
128
102
- If *legacy * is true, old-style ``.pyc `` file path names are written,
103
- otherwise (the default), :pep: `3147 `-style path names are written.
129
+ If *legacy * is true, byte-code files are written to their legacy locations
130
+ and names, which may overwrite byte-code files created by another version of
131
+ Python. The default is to write files to their :pep: `3147 ` locations and
132
+ names, which allows byte-code files from multiple versions of Python to
133
+ coexist.
104
134
105
135
*optimize * specifies the optimization level for the compiler. It is passed to
106
136
the built-in :func: `compile ` function.
@@ -111,9 +141,10 @@ Public functions
111
141
.. function :: compile_path(skip_curdir=True, maxlevels=0, force=False, legacy=False, optimize=-1)
112
142
113
143
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
115
- the search. All other parameters are passed to the :func: `compile_dir `
116
- function.
144
+ *skip_curdir * is true (the default), the current directory is not included
145
+ in the search. All other parameters are passed to the :func: `compile_dir `
146
+ function. Note that unlike the other compile functions, ``maxlevels ``
147
+ defaults to ``0 ``.
117
148
118
149
.. versionchanged :: 3.2
119
150
Added the *legacy * and *optimize * parameter.
0 commit comments