def compile_file(fullname, ddir=None, force=0, rx=None, quiet=0):
"""Byte-compile one file.
Arguments (only fullname is required):
fullname: the file to byte-compile
ddir: if given, the directory name compiled in to the
byte-code file.
force: if 1, force compilation, even if timestamps are up-to-date
quiet: if 1, be quiet during compilation
"""
success = 1
name = os.path.basename(fullname)
if ddir is not None:
dfile = os.path.join(ddir, name)
else:
dfile = None
if rx is not None:
mo = rx.search(fullname)
if mo:
return success
if os.path.isfile(fullname):
head, tail = name[:-3], name[-3:]
if tail == '.py':
if not force:
try:
mtime = int(os.stat(fullname).st_mtime)
expect = struct.pack('<4sl', imp.get_magic(), mtime)
cfile = fullname + (__debug__ and 'c' or 'o')
with open(cfile, 'rb') as chandle:
actual = chandle.read(8)
if expect == actual:
return success
except IOError:
pass
if not quiet:
print 'Compiling', fullname, '...'
try:
ok = py_compile.compile(fullname, None, dfile, True)
except py_compile.PyCompileError,err:
if quiet:
print 'Compiling', fullname, '...'
print err.msg
success = 0
except IOError, e:
print "Sorry", e
success = 0
else:
if ok == 0:
success = 0
return success
python类PyCompileError()的实例源码
def compile_file(fullname, ddir=None, force=0, rx=None, quiet=0):
"""Byte-compile one file.
Arguments (only fullname is required):
fullname: the file to byte-compile
ddir: if given, the directory name compiled in to the
byte-code file.
force: if 1, force compilation, even if timestamps are up-to-date
quiet: if 1, be quiet during compilation
"""
success = 1
name = os.path.basename(fullname)
if ddir is not None:
dfile = os.path.join(ddir, name).replace(os.sep, '/')
else:
dfile = None
if rx is not None:
mo = rx.search(fullname)
if mo:
return success
if os.path.isfile(fullname):
head, tail = name[:-3], name[-3:]
if tail == '.py':
timeStr = subprocess.check_output(
['git', '--no-pager', 'log', '-n', '1', '--format="%ct"', '--', fullname])[1:-2]
if not force:
try:
if not timeStr:
mtime = int(os.stat(fullname).st_mtime)
else:
mtime = int(timeStr)
expect = struct.pack('<4sl', imp.get_magic(), mtime)
cfile = fullname + (__debug__ and 'c' or 'o')
with open(cfile, 'rb') as chandle:
actual = chandle.read(8)
if expect == actual:
return success
except IOError:
pass
if not quiet:
print 'Compiling', fullname, '...'
try:
ok = do_compile(fullname, None, dfile, True, timeStr)
except py_compile.PyCompileError, err:
if quiet:
print 'Compiling', fullname, '...'
print err.msg
success = 0
except IOError, e:
print "Sorry", e
success = 0
else:
if ok == 0:
success = 0
return success
def putData(self, mibname, data, comments=[], dryRun=False):
if dryRun:
debug.logger & debug.flagWriter and debug.logger('dry run mode')
return
if not os.path.exists(self._path):
try:
os.makedirs(self._path)
except OSError:
raise error.PySmiWriterError('failure creating destination directory %s: %s' % (self._path, sys.exc_info()[1]), writer=self)
if comments:
data = '#\n' + ''.join(['# %s\n' % x for x in comments]) + '#\n' + data
pyfile = os.path.join(self._path, decode(mibname)) + self.suffixes[imp.PY_SOURCE][0][0]
try:
fd, tfile = tempfile.mkstemp(dir=self._path)
os.write(fd, encode(data))
os.close(fd)
os.rename(tfile, pyfile)
except (OSError, IOError, UnicodeEncodeError):
exc = sys.exc_info()
try:
os.unlink(tfile)
except OSError:
pass
raise error.PySmiWriterError('failure writing file %s: %s' % (pyfile, exc[1]), file=pyfile, writer=self)
debug.logger & debug.flagWriter and debug.logger('created file %s' % pyfile)
if self.pyCompile:
try:
if sys.version_info[0:2] > (3, 1):
py_compile.compile(pyfile, doraise=True, optimize=self.pyOptimizationLevel)
else:
py_compile.compile(pyfile, doraise=True)
except (SyntaxError, py_compile.PyCompileError):
pass # XXX
except:
try:
os.unlink(pyfile)
except Exception:
pass
raise error.PySmiWriterError('failure compiling %s: %s' % (pyfile, sys.exc_info()[1]), file=mibname, writer=self)
debug.logger & debug.flagWriter and debug.logger('%s stored' % mibname)