]>
Commit | Line | Data |
---|---|---|
3257aa99 DM |
1 | """Module/script to byte-compile all .py files to .pyc (or .pyo) files.\r |
2 | \r | |
3 | When called as a script with arguments, this compiles the directories\r | |
4 | given as arguments recursively; the -l option prevents it from\r | |
5 | recursing into directories.\r | |
6 | \r | |
7 | Without arguments, if compiles all modules on sys.path, without\r | |
8 | recursing into subdirectories. (Even though it should do so for\r | |
9 | packages -- for now, you'll have to deal with packages separately.)\r | |
10 | \r | |
11 | See module py_compile for details of the actual byte-compilation.\r | |
12 | """\r | |
13 | import os\r | |
14 | import sys\r | |
15 | import py_compile\r | |
16 | import struct\r | |
17 | import imp\r | |
18 | \r | |
19 | __all__ = ["compile_dir","compile_file","compile_path"]\r | |
20 | \r | |
21 | def compile_dir(dir, maxlevels=10, ddir=None,\r | |
22 | force=0, rx=None, quiet=0):\r | |
23 | """Byte-compile all modules in the given directory tree.\r | |
24 | \r | |
25 | Arguments (only dir is required):\r | |
26 | \r | |
27 | dir: the directory to byte-compile\r | |
28 | maxlevels: maximum recursion level (default 10)\r | |
29 | ddir: the directory that will be prepended to the path to the\r | |
30 | file as it is compiled into each byte-code file.\r | |
31 | force: if 1, force compilation, even if timestamps are up-to-date\r | |
32 | quiet: if 1, be quiet during compilation\r | |
33 | """\r | |
34 | if not quiet:\r | |
35 | print 'Listing', dir, '...'\r | |
36 | try:\r | |
37 | names = os.listdir(dir)\r | |
38 | except os.error:\r | |
39 | print "Can't list", dir\r | |
40 | names = []\r | |
41 | names.sort()\r | |
42 | success = 1\r | |
43 | for name in names:\r | |
44 | fullname = os.path.join(dir, name)\r | |
45 | if ddir is not None:\r | |
46 | dfile = os.path.join(ddir, name)\r | |
47 | else:\r | |
48 | dfile = None\r | |
49 | if not os.path.isdir(fullname):\r | |
50 | if not compile_file(fullname, ddir, force, rx, quiet):\r | |
51 | success = 0\r | |
52 | elif maxlevels > 0 and \\r | |
53 | name != os.curdir and name != os.pardir and \\r | |
54 | os.path.isdir(fullname) and \\r | |
55 | not os.path.islink(fullname):\r | |
56 | if not compile_dir(fullname, maxlevels - 1, dfile, force, rx,\r | |
57 | quiet):\r | |
58 | success = 0\r | |
59 | return success\r | |
60 | \r | |
61 | def compile_file(fullname, ddir=None, force=0, rx=None, quiet=0):\r | |
62 | """Byte-compile one file.\r | |
63 | \r | |
64 | Arguments (only fullname is required):\r | |
65 | \r | |
66 | fullname: the file to byte-compile\r | |
67 | ddir: if given, the directory name compiled in to the\r | |
68 | byte-code file.\r | |
69 | force: if 1, force compilation, even if timestamps are up-to-date\r | |
70 | quiet: if 1, be quiet during compilation\r | |
71 | """\r | |
72 | success = 1\r | |
73 | name = os.path.basename(fullname)\r | |
74 | if ddir is not None:\r | |
75 | dfile = os.path.join(ddir, name)\r | |
76 | else:\r | |
77 | dfile = None\r | |
78 | if rx is not None:\r | |
79 | mo = rx.search(fullname)\r | |
80 | if mo:\r | |
81 | return success\r | |
82 | if os.path.isfile(fullname):\r | |
83 | head, tail = name[:-3], name[-3:]\r | |
84 | if tail == '.py':\r | |
85 | if not force:\r | |
86 | try:\r | |
87 | mtime = int(os.stat(fullname).st_mtime)\r | |
88 | expect = struct.pack('<4sl', imp.get_magic(), mtime)\r | |
89 | cfile = fullname + (__debug__ and 'c' or 'o')\r | |
90 | with open(cfile, 'rb') as chandle:\r | |
91 | actual = chandle.read(8)\r | |
92 | if expect == actual:\r | |
93 | return success\r | |
94 | except IOError:\r | |
95 | pass\r | |
96 | if not quiet:\r | |
97 | print 'Compiling', fullname, '...'\r | |
98 | try:\r | |
99 | ok = py_compile.compile(fullname, None, dfile, True)\r | |
100 | except py_compile.PyCompileError,err:\r | |
101 | if quiet:\r | |
102 | print 'Compiling', fullname, '...'\r | |
103 | print err.msg\r | |
104 | success = 0\r | |
105 | except IOError, e:\r | |
106 | print "Sorry", e\r | |
107 | success = 0\r | |
108 | else:\r | |
109 | if ok == 0:\r | |
110 | success = 0\r | |
111 | return success\r | |
112 | \r | |
113 | def compile_path(skip_curdir=1, maxlevels=0, force=0, quiet=0):\r | |
114 | """Byte-compile all module on sys.path.\r | |
115 | \r | |
116 | Arguments (all optional):\r | |
117 | \r | |
118 | skip_curdir: if true, skip current directory (default true)\r | |
119 | maxlevels: max recursion level (default 0)\r | |
120 | force: as for compile_dir() (default 0)\r | |
121 | quiet: as for compile_dir() (default 0)\r | |
122 | """\r | |
123 | success = 1\r | |
124 | for dir in sys.path:\r | |
125 | if (not dir or dir == os.curdir) and skip_curdir:\r | |
126 | print 'Skipping current directory'\r | |
127 | else:\r | |
128 | success = success and compile_dir(dir, maxlevels, None,\r | |
129 | force, quiet=quiet)\r | |
130 | return success\r | |
131 | \r | |
132 | def expand_args(args, flist):\r | |
133 | """read names in flist and append to args"""\r | |
134 | expanded = args[:]\r | |
135 | if flist:\r | |
136 | try:\r | |
137 | if flist == '-':\r | |
138 | fd = sys.stdin\r | |
139 | else:\r | |
140 | fd = open(flist)\r | |
141 | while 1:\r | |
142 | line = fd.readline()\r | |
143 | if not line:\r | |
144 | break\r | |
145 | expanded.append(line[:-1])\r | |
146 | except IOError:\r | |
147 | print "Error reading file list %s" % flist\r | |
148 | raise\r | |
149 | return expanded\r | |
150 | \r | |
151 | def main():\r | |
152 | """Script main program."""\r | |
153 | import getopt\r | |
154 | try:\r | |
155 | opts, args = getopt.getopt(sys.argv[1:], 'lfqd:x:i:')\r | |
156 | except getopt.error, msg:\r | |
157 | print msg\r | |
158 | print "usage: python compileall.py [-l] [-f] [-q] [-d destdir] " \\r | |
159 | "[-x regexp] [-i list] [directory|file ...]"\r | |
160 | print\r | |
161 | print "arguments: zero or more file and directory names to compile; " \\r | |
162 | "if no arguments given, "\r | |
163 | print " defaults to the equivalent of -l sys.path"\r | |
164 | print\r | |
165 | print "options:"\r | |
166 | print "-l: don't recurse into subdirectories"\r | |
167 | print "-f: force rebuild even if timestamps are up-to-date"\r | |
168 | print "-q: output only error messages"\r | |
169 | print "-d destdir: directory to prepend to file paths for use in " \\r | |
170 | "compile-time tracebacks and in"\r | |
171 | print " runtime tracebacks in cases where the source " \\r | |
172 | "file is unavailable"\r | |
173 | print "-x regexp: skip files matching the regular expression regexp; " \\r | |
174 | "the regexp is searched for"\r | |
175 | print " in the full path of each file considered for " \\r | |
176 | "compilation"\r | |
177 | print "-i file: add all the files and directories listed in file to " \\r | |
178 | "the list considered for"\r | |
179 | print ' compilation; if "-", names are read from stdin'\r | |
180 | \r | |
181 | sys.exit(2)\r | |
182 | maxlevels = 10\r | |
183 | ddir = None\r | |
184 | force = 0\r | |
185 | quiet = 0\r | |
186 | rx = None\r | |
187 | flist = None\r | |
188 | for o, a in opts:\r | |
189 | if o == '-l': maxlevels = 0\r | |
190 | if o == '-d': ddir = a\r | |
191 | if o == '-f': force = 1\r | |
192 | if o == '-q': quiet = 1\r | |
193 | if o == '-x':\r | |
194 | import re\r | |
195 | rx = re.compile(a)\r | |
196 | if o == '-i': flist = a\r | |
197 | if ddir:\r | |
198 | if len(args) != 1 and not os.path.isdir(args[0]):\r | |
199 | print "-d destdir require exactly one directory argument"\r | |
200 | sys.exit(2)\r | |
201 | success = 1\r | |
202 | try:\r | |
203 | if args or flist:\r | |
204 | try:\r | |
205 | if flist:\r | |
206 | args = expand_args(args, flist)\r | |
207 | except IOError:\r | |
208 | success = 0\r | |
209 | if success:\r | |
210 | for arg in args:\r | |
211 | if os.path.isdir(arg):\r | |
212 | if not compile_dir(arg, maxlevels, ddir,\r | |
213 | force, rx, quiet):\r | |
214 | success = 0\r | |
215 | else:\r | |
216 | if not compile_file(arg, ddir, force, rx, quiet):\r | |
217 | success = 0\r | |
218 | else:\r | |
219 | success = compile_path()\r | |
220 | except KeyboardInterrupt:\r | |
221 | print "\n[interrupted]"\r | |
222 | success = 0\r | |
223 | return success\r | |
224 | \r | |
225 | if __name__ == '__main__':\r | |
226 | exit_status = int(not main())\r | |
227 | sys.exit(exit_status)\r |