1 """Module/script to "compile" all .py files to .pyc (or .pyo) file.
3 When called as a script with arguments, this compiles the directories
4 given as arguments recursively; the -l option prevents it from
5 recursing into directories.
7 Without arguments, if compiles all modules on sys.path, without
8 recursing into subdirectories. (Even though it should do so for
9 packages -- for now, you'll have to deal with packages separately.)
11 See module py_compile for details of the actual byte-compilation.
20 __all__ = [
"compile_dir",
"compile_path"]
22 def compile_dir(dir, maxlevels=10, ddir=None, force=0, rx=None):
23 """Byte-compile all modules in the given directory tree.
25 Arguments (only dir is required):
27 dir: the directory to byte-compile
28 maxlevels: maximum recursion level (default 10)
29 ddir: if given, purported directory name (this is the
30 directory name that will show up in error messages)
31 force: if 1, force compilation, even if timestamps are up-to-date
34 print 'Listing', dir,
'...'
36 names = os.listdir(dir)
38 print "Can't list", dir
43 fullname = os.path.join(dir, name)
45 dfile = os.path.join(ddir, name)
49 mo = rx.search(fullname)
52 if os.path.isfile(fullname):
53 head, tail = name[:-3], name[-3:]
55 cfile = fullname + (__debug__
and 'c' or 'o')
56 ftime = os.stat(fullname)[stat.ST_MTIME]
57 try: ctime = os.stat(cfile)[stat.ST_MTIME]
58 except os.error: ctime = 0
59 if (ctime > ftime)
and not force:
continue
60 print 'Compiling', fullname,
'...'
63 except KeyboardInterrupt:
64 raise KeyboardInterrupt
67 if type(sys.exc_type) == type(
''):
68 exc_type_name = sys.exc_type
69 else: exc_type_name = sys.exc_type.__name__
70 print 'Sorry:', exc_type_name +
':',
76 elif maxlevels > 0
and \
77 name != os.curdir
and name != os.pardir
and \
78 os.path.isdir(fullname)
and \
79 not os.path.islink(fullname):
80 if not compile_dir(fullname, maxlevels - 1, dfile, force, rx):
85 """Byte-compile all module on sys.path.
87 Arguments (all optional):
89 skip_curdir: if true, skip current directory (default true)
90 maxlevels: max recursion level (default 0)
91 force: as for compile_dir() (default 0)
96 if (
not dir
or dir == os.curdir)
and skip_curdir:
97 print 'Skipping current directory'
99 success = success
and compile_dir(dir, maxlevels,
None, force)
103 """Script main program."""
107 except getopt.error, msg:
109 print "usage: python compileall.py [-l] [-f] [-d destdir] " \
110 "[-s regexp] [directory ...]"
111 print "-l: don't recurse down"
112 print "-f: force rebuild even if timestamps are up-to-date"
113 print "-d destdir: purported directory name for error messages"
114 print " if no directory arguments, -l sys.path is assumed"
115 print "-x regexp: skip files matching the regular expression regexp"
116 print " the regexp is search for in the full path of the file"
123 if o ==
'-l': maxlevels = 0
124 if o ==
'-d': ddir = a
125 if o ==
'-f': force = 1
131 print "-d destdir require exactly one directory argument"
137 if not compile_dir(dir, maxlevels, ddir, force, rx):
141 except KeyboardInterrupt:
142 print "\n[interrupt]"
146 if __name__ ==
'__main__':
148 sys.exit(exit_status)