quixote ptl_compile.py,1.25,1.26
Roger Masse <rmasse-fVcApmY9cLvQ3/1i3zOLAti2O/[email protected]>
| Newsgroups | gmane.comp.web.quixote.cvs |
|---|---|
| Message-ID | <[email protected]> |
Update of /home/cvs/quixote
In directory hewson:/tmp/cvs-serv20269
Modified Files:
ptl_compile.py
Log Message:
Add new function 'compile_dir'. Inspired by 'compile_dir' from the
standard Python module 'compileall.py', compile_dir compiles all
PTL files in and below the 'dir' argument. There are two optional
args 'maxlevels' controls how many directory levels below dir are
traversed (default 10) and 'force' forces compilation even if the
timestamps are up-to-date (default False).
Index: ptl_compile.py
===================================================================
RCS file: /home/cvs/quixote/ptl_compile.py,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -d -r1.25 -r1.26
--- ptl_compile.py 14 Oct 2002 23:06:39 -0000 1.25
+++ ptl_compile.py 21 Oct 2002 17:31:44 -0000 1.26
@@ -16,6 +16,7 @@
import sys
import os
+import stat
import string
import symbol
import token
@@ -293,6 +294,58 @@
output.close()
os.unlink(outputname)
raise
+
+def compile_dir(dir, maxlevels=10, force=False):
+ """Byte-compile all PTL modules in the given directory tree.
+ (Adapted from compile_dir in Python module: compileall.py)
+
+ Arguments (only dir is required):
+
+ dir: the directory to byte-compile
+ maxlevels: maximum recursion level (default 10)
+ force: if True, force compilation, even if timestamps are up-to-date
+ """
+ print 'Listing', dir, '...'
+ try:
+ names = os.listdir(dir)
+ except os.error:
+ print "Can't list", dir
+ names = []
+ names.sort()
+ success = 1
+ for name in names:
+ fullname = os.path.join(dir, name)
+ if os.path.isfile(fullname):
+ head, tail = name[:-4], name[-4:]
+ if tail == '.ptl':
+ cfile = fullname + 'c'
+ ftime = os.stat(fullname)[stat.ST_MTIME]
+ try:
+ ctime = os.stat(cfile)[stat.ST_MTIME]
+ except os.error: ctime = 0
+ if (ctime > ftime) and not force:
+ continue
+ print 'Compiling', fullname, '...'
+ try:
+ ok = compile(fullname, cfile)
+ except KeyboardInterrupt:
+ raise KeyboardInterrupt
+ except:
+ # XXX compile catches SyntaxErrors
+ if type(sys.exc_type) == type(''):
+ exc_type_name = sys.exc_type
+ else: exc_type_name = sys.exc_type.__name__
+ print 'Sorry:', exc_type_name + ':',
+ print sys.exc_value
+ success = 0
+ else:
+ if ok == 0:
+ success = 0
+ elif (maxlevels > 0 and name != os.curdir and name != os.pardir and
+ os.path.isdir(fullname) and not os.path.islink(fullname)):
+ if not compile_dir(fullname, maxlevels - 1, force):
+ success = 0
+ return success
def main():
args = sys.argv[1:]