mirror of
https://github.com/python/cpython.git
synced 2025-07-12 05:45:15 +00:00
New way of generating .pyc files, thanks to Sjoerd.
urllib.py: '+' is not always safe (even though the RFC says so :-( ) whrandom.py: throw away top bits of time to avoid overflow on Mac (where times can be negative)
This commit is contained in:
parent
7b1e974b4b
commit
3bb5448767
5 changed files with 96 additions and 2 deletions
67
Lib/compileall.py
Normal file
67
Lib/compileall.py
Normal file
|
@ -0,0 +1,67 @@
|
|||
# Routines to force "compilation" of all .py files in a directory
|
||||
# tree or on sys.path. By default recursion is pruned at a depth of
|
||||
# 10 and the current directory, if it occurs in sys.path, is skipped.
|
||||
# When called as a script, compiles argument directories, or sys.path
|
||||
# if no arguments.
|
||||
# After a similar module by Sjoerd Mullender.
|
||||
|
||||
import os
|
||||
import sys
|
||||
import py_compile
|
||||
|
||||
def compile_dir(dir, maxlevels = 10):
|
||||
print 'Listing', dir, '...'
|
||||
try:
|
||||
names = os.listdir(dir)
|
||||
except os.error:
|
||||
print "Can't list", dir
|
||||
names = []
|
||||
names.sort()
|
||||
for name in names:
|
||||
fullname = os.path.join(dir, name)
|
||||
if os.path.isfile(fullname):
|
||||
head, tail = name[:-3], name[-3:]
|
||||
if tail == '.py':
|
||||
print 'Compiling', fullname, '...'
|
||||
try:
|
||||
py_compile.compile(fullname)
|
||||
except KeyboardInterrupt:
|
||||
del names[:]
|
||||
print '\n[interrupt]'
|
||||
break
|
||||
except:
|
||||
print 'Sorry:', sys.exc_type + ':',
|
||||
print sys.exc_value
|
||||
elif maxlevels > 0 and \
|
||||
name != os.curdir and name != os.pardir and \
|
||||
os.path.isdir(fullname) and \
|
||||
not os.path.islink(fullname):
|
||||
compile_dir(fullname, maxlevels - 1)
|
||||
|
||||
def compile_path(skip_curdir = 1):
|
||||
for dir in sys.path:
|
||||
if dir == os.curdir and skip_curdir:
|
||||
print 'Skipping current directory'
|
||||
else:
|
||||
compile_dir(dir, 0)
|
||||
|
||||
def main():
|
||||
import getopt
|
||||
try:
|
||||
opts, args = getopt.getopt(sys.argv[1:], 'l')
|
||||
except getopt.error, msg:
|
||||
print msg
|
||||
print "usage: compileall [-l] [directory ...]"
|
||||
print "-l: don't recurse down"
|
||||
print "if no arguments, -l sys.path is assumed"
|
||||
maxlevels = 10
|
||||
for o, a in opts:
|
||||
if o == '-l': maxlevels = 0
|
||||
if args:
|
||||
for dir in sys.argv[1:]:
|
||||
compile_dir(dir, maxlevels)
|
||||
else:
|
||||
compile_path()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Add table
Add a link
Reference in a new issue