本文整理汇总了Python中py_compile.compile方法的典型用法代码示例。如果您正苦于以下问题:Python py_compile.compile方法的具体用法?Python py_compile.compile怎么用?Python py_compile.compile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类py_compile
的用法示例。
在下文中一共展示了py_compile.compile方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getFileDisassembled
# 需要导入模块: import py_compile [as 别名]
# 或者: from py_compile import compile [as 别名]
def getFileDisassembled(path, optimization):
"""Dsassembles a file"""
if not os.path.exists(path):
raise Exception("Cannot find " + path + " to disassemble")
if not os.access(path, os.R_OK):
raise Exception("No read permissions for " + path)
tempPycFile = makeTempFile(suffix='.pyc')
try:
py_compile.compile(path, tempPycFile,
doraise=True, optimize=optimization)
except Exception as exc:
safeUnlink(tempPycFile)
raise Exception("Cannot disassemble file " + path + ': ' + str(exc))
try:
result = getCompiledfileDisassembled(tempPycFile, path, optimization)
except:
safeUnlink(tempPycFile)
raise
safeUnlink(tempPycFile)
return result
示例2: compile_path
# 需要导入模块: import py_compile [as 别名]
# 或者: from py_compile import compile [as 别名]
def compile_path(skip_curdir=1, maxlevels=0, force=0, quiet=0):
"""Byte-compile all module on sys.path.
Arguments (all optional):
skip_curdir: if true, skip current directory (default true)
maxlevels: max recursion level (default 0)
force: as for compile_dir() (default 0)
quiet: as for compile_dir() (default 0)
"""
success = 1
for dir in sys.path:
if (not dir or dir == os.curdir) and skip_curdir:
print 'Skipping current directory'
else:
success = success and compile_dir(dir, maxlevels, None,
force, quiet=quiet)
return success
示例3: _get_codename
# 需要导入模块: import py_compile [as 别名]
# 或者: from py_compile import compile [as 别名]
def _get_codename(self, pathname, basename):
"""Return (filename, archivename) for the path.
Given a module name path, return the correct file path and
archive name, compiling if necessary. For example, given
/python/lib/string, return (/python/lib/string.pyc, string).
"""
file_py = pathname + ".py"
file_pyc = pathname + ".pyc"
file_pyo = pathname + ".pyo"
if os.path.isfile(file_pyo) and \
os.stat(file_pyo).st_mtime >= os.stat(file_py).st_mtime:
fname = file_pyo # Use .pyo file
elif not os.path.isfile(file_pyc) or \
os.stat(file_pyc).st_mtime < os.stat(file_py).st_mtime:
import py_compile
if self.debug:
print "Compiling", file_py
try:
py_compile.compile(file_py, file_pyc, None, True)
except py_compile.PyCompileError,err:
print err.msg
fname = file_pyc
示例4: generate_bytecode_files
# 需要导入模块: import py_compile [as 别名]
# 或者: from py_compile import compile [as 别名]
def generate_bytecode_files(package_name, installed_files):
"""
Generate Python byte code files for the ``*.py`` files installed by a package.
Uses :mod:`py_compile.compile()` to generate bytecode files.
:param package_name: The name of the system package (a string).
:param installed_files: A list of strings with the absolute pathnames of
installed files.
"""
num_generated = 0
for filename in installed_files:
if filename.endswith('.py'):
py_compile.compile(filename)
num_generated += 1
if num_generated > 0:
logger.info("Generated %i Python bytecode files(s) for %s package.", num_generated, package_name)
示例5: test_foreign_code
# 需要导入模块: import py_compile [as 别名]
# 或者: from py_compile import compile [as 别名]
def test_foreign_code(self):
py_compile.compile(self.file_name)
with open(self.compiled_name, "rb") as f:
header = f.read(8)
code = marshal.load(f)
constants = list(code.co_consts)
foreign_code = test_main.func_code
pos = constants.index(1)
constants[pos] = foreign_code
code = type(code)(code.co_argcount, code.co_nlocals, code.co_stacksize,
code.co_flags, code.co_code, tuple(constants),
code.co_names, code.co_varnames, code.co_filename,
code.co_name, code.co_firstlineno, code.co_lnotab,
code.co_freevars, code.co_cellvars)
with open(self.compiled_name, "wb") as f:
f.write(header)
marshal.dump(code, f)
mod = self.import_module()
self.assertEqual(mod.constant.co_filename, foreign_code.co_filename)
示例6: compile_pyc
# 需要导入模块: import py_compile [as 别名]
# 或者: from py_compile import compile [as 别名]
def compile_pyc(path = './python/'):
if os.path.exists(path+'__pycache__'):
shutil.rmtree(path+'__pycache__')
items = os.listdir(path)
try:
items.remove('old_files')
except:
print('No deprecated old files')
print(items)
for item in items:
py_compile.compile(path+item, doraise=True, optimize=2)
pycs = os.listdir(path+'__pycache__')
for pyc in pycs:
true_name = pyc.split('.')[0]+'.pyc'
print(true_name)
os.rename(path+'__pycache__/'+pyc,path+'__pycache__/'+true_name)
copyfile(path+'__pycache__/'+true_name,'./pyc/'+true_name)
示例7: configure
# 需要导入模块: import py_compile [as 别名]
# 或者: from py_compile import compile [as 别名]
def configure(conf):
"""
Detect the python interpreter
"""
try:
conf.find_program('python', var='PYTHON')
except conf.errors.ConfigurationError:
warn("could not find a python executable, setting to sys.executable '%s'" % sys.executable)
conf.env.PYTHON = sys.executable
if conf.env.PYTHON != sys.executable:
warn("python executable '%s' different from sys.executable '%s'" % (conf.env.PYTHON, sys.executable))
conf.env.PYTHON = conf.cmd_to_list(conf.env.PYTHON)
v = conf.env
v['PYCMD'] = '"import sys, py_compile;py_compile.compile(sys.argv[1], sys.argv[2])"'
v['PYFLAGS'] = ''
v['PYFLAGS_OPT'] = '-O'
v['PYC'] = getattr(Options.options, 'pyc', 1)
v['PYO'] = getattr(Options.options, 'pyo', 1)
示例8: compile_path
# 需要导入模块: import py_compile [as 别名]
# 或者: from py_compile import compile [as 别名]
def compile_path(skip_curdir=1, maxlevels=0, force=0, quiet=0):
"""Byte-compile all module on sys.path.
Arguments (all optional):
skip_curdir: if true, skip current directory (default true)
maxlevels: max recursion level (default 0)
force: as for compile_dir() (default 0)
quiet: as for compile_dir() (default 0)
"""
success = 1
for dir in sys.path:
if (not dir or dir == os.curdir) and skip_curdir:
print 'Skipping current directory'
else:
success = success and compile_dir(dir, maxlevels, None,
force, quiet=quiet)
return success
示例9: byte_compile
# 需要导入模块: import py_compile [as 别名]
# 或者: from py_compile import compile [as 别名]
def byte_compile(self, path, optimize=False, force=False, prefix=None, hashed_invalidation=False):
dpath = cache_from_source(path, not optimize)
logger.info('Byte-compiling %s to %s', path, dpath)
if not self.dry_run:
if force or self.newer(path, dpath):
if not prefix:
diagpath = None
else:
assert path.startswith(prefix)
diagpath = path[len(prefix):]
compile_kwargs = {}
if hashed_invalidation and hasattr(py_compile, 'PycInvalidationMode'):
compile_kwargs['invalidation_mode'] = py_compile.PycInvalidationMode.CHECKED_HASH
py_compile.compile(path, dpath, diagpath, True, **compile_kwargs) # raise error
self.record_as_written(dpath)
return dpath
示例10: compile
# 需要导入模块: import py_compile [as 别名]
# 或者: from py_compile import compile [as 别名]
def compile(self, root, relpaths):
"""Compiles the given python source files using this compiler's interpreter.
:param string root: The root path all the source files are found under.
:param list relpaths: The relative paths from the `root` of the source files to compile.
:returns: A list of relative paths of the compiled bytecode files.
:raises: A :class:`Compiler.Error` if there was a problem bytecode compiling any of the files.
"""
with named_temporary_file() as fp:
fp.write(to_bytes(_COMPILER_MAIN % {'root': root, 'relpaths': relpaths}, encoding='utf-8'))
fp.flush()
try:
_, out, _ = self._interpreter.execute(args=[fp.name])
except Executor.NonZeroExit as e:
raise self.CompilationFailure(
'encountered %r during bytecode compilation.\nstderr was:\n%s\n' % (e, e.stderr)
)
return out.splitlines()
示例11: _compile_module
# 需要导入模块: import py_compile [as 别名]
# 或者: from py_compile import compile [as 别名]
def _compile_module(smimodule):
if not smimodule.name:
return # unnamed from where?
fname = os.path.join(USERMIBPATH, convert_name(smimodule.name)+".py")
oidfname = os.path.join(USERMIBPATH, convert_name(smimodule.name)+"_OID.py")
if not os.path.exists(fname):
print ("Compiling module", smimodule.name)
fd = open(fname, "w")
oidfd = open(oidfname, "w")
generator = ObjectSourceGenerator(fd, oidfd, smimodule)
generator.genAll()
fd.close()
try:
py_compile.compile(fname)
except Exception as err:
print ("***", err)
else:
print (" +++ file %r exists, skipping." % (fname, ))
示例12: make_sourceless
# 需要导入模块: import py_compile [as 别名]
# 或者: from py_compile import compile [as 别名]
def make_sourceless(path, style):
import py_compile
py_compile.compile(path)
if style == "simple" and has_pep3147():
pyc_path = util.pyc_file_from_path(path)
suffix = get_current_bytecode_suffixes()[0]
filepath, ext = os.path.splitext(path)
simple_pyc_path = filepath + suffix
shutil.move(pyc_path, simple_pyc_path)
pyc_path = simple_pyc_path
elif style == "pep3147" and not has_pep3147():
raise NotImplementedError()
else:
assert style in ("pep3147", "simple")
pyc_path = util.pyc_file_from_path(path)
assert os.access(pyc_path, os.F_OK)
os.unlink(path)
示例13: test_tracing_pyc_file
# 需要导入模块: import py_compile [as 别名]
# 或者: from py_compile import compile [as 别名]
def test_tracing_pyc_file(self):
# Create two Python files.
self.make_file("mod.py", "a = 1\n")
self.make_file("main.py", "import mod\n")
# Make one into a .pyc.
py_compile.compile("mod.py")
# Run the program.
cov = coverage.Coverage()
cov.start()
import main # pragma: nested # pylint: disable=import-error, unused-variable
cov.stop() # pragma: nested
report = self.get_report(cov).splitlines()
self.assertIn("mod.py 1 0 100%", report)
示例14: byte_compile
# 需要导入模块: import py_compile [as 别名]
# 或者: from py_compile import compile [as 别名]
def byte_compile(self, path, optimize=False, force=False, prefix=None):
dpath = cache_from_source(path, not optimize)
logger.info('Byte-compiling %s to %s', path, dpath)
if not self.dry_run:
if force or self.newer(path, dpath):
if not prefix:
diagpath = None
else:
assert path.startswith(prefix)
diagpath = path[len(prefix):]
py_compile.compile(path, dpath, diagpath, True) # raise error
self.record_as_written(dpath)
return dpath
示例15: getBufferDisassembled
# 需要导入模块: import py_compile [as 别名]
# 或者: from py_compile import compile [as 别名]
def getBufferDisassembled(content, encoding, path, optimization):
"""Disassembles a memory buffer"""
tempSrcFile = makeTempFile(suffix='.py')
tempPycFile = makeTempFile(suffix='.pyc')
try:
saveToFile(tempSrcFile, content, allowException=True, enc=encoding)
py_compile.compile(tempSrcFile, tempPycFile, path,
doraise=True, optimize=optimization)
except Exception:
safeUnlink(tempSrcFile)
safeUnlink(tempPycFile)
raise
try:
result = getCompiledfileDisassembled(tempPycFile, path,
optimization, True)
except:
safeUnlink(tempSrcFile)
safeUnlink(tempPycFile)
raise
safeUnlink(tempSrcFile)
safeUnlink(tempPycFile)
if path:
return result.replace('file "' + path + '",',
'unsaved buffer "' + path + '",')
return result