def compile_pyc(files): newfiles = list(files) for fname, dest in files: if fname.endswith('.py'): compileall.compile_file(fname) newfiles.append((fname + 'c', dest)) return tuple(newfiles)
def compileAllPySources(self, source, build, variant, optimize=-1): # zip_handle is zipfile handle for root, dirs, filenames in os.walk(source): for filename in filenames: fullname = os.path.join(root, filename) relative_filename = os.path.relpath(fullname, source) if self.checkForIgnorableFiles(source, relative_filename): continue _, extension = os.path.splitext(filename) if extension in python_sources: destdir = os.path.join(build, relative_filename) destdir = os.path.split(destdir)[0] os.makedirs(destdir, exist_ok=True) if variant in ("binary+source", "source", "binary"): filename_copied = "{}/{}".format(destdir, filename) shutil.copyfile(fullname, filename_copied) os.chmod(filename_copied, 0o600) print("d Copying: {}".format(relative_filename)) if variant in ("binary+source", "binary"): compileall.compile_file( filename_copied, ddir=relative_filename, quiet=2, optimize=optimize, ) print("d Compiled: {}".format(relative_filename)) if variant is "binary": if relative_filename != "__init__.py": print( "d Removing: {}".format(relative_filename)) os.remove(filename_copied) else: print("e Invalid variant!") print("i Python files compiled and optionally copied source(s)!")
def pyc_project_path(tmpdir): path = tmpdir.strpath dummy_package_path = os.path.join(path, "dummy_package") os.mkdir(dummy_package_path) with open(os.path.join(dummy_package_path, "__init__.py"), 'w'): pass dummy_path = os.path.join(dummy_package_path, 'dummy.py') with open(dummy_path, 'w') as f: f.write(SRC) import compileall compileall.compile_file(dummy_path) os.remove(dummy_path) if sys.version_info.major == 3: # Python3 specific: # To import pyc modules, we must move them out of the __pycache__ # directory and rename them to remove ".cpython-%s%d" # see: http://stackoverflow.com/questions/11648440/python-does-not-detect-pyc-files pycache = os.path.join(dummy_package_path, "__pycache__") for f in os.listdir(pycache): dst = f.replace('.cpython-%s%s' % sys.version_info[:2], "") dst = os.path.join(dummy_package_path, dst) shutil.copy(os.path.join(pycache, f), dst) try: yield path finally: shutil.rmtree(path)
def test_compile_files(self): # Test compiling a single file, and complete directory for fn in (self.bc_path, self.bc_path2): try: os.unlink(fn) except: pass self.assertTrue( compileall.compile_file(self.source_path, force=False, quiet=True)) self.assertTrue( os.path.isfile(self.bc_path) and not os.path.isfile(self.bc_path2)) os.unlink(self.bc_path) self.assertTrue( compileall.compile_dir(self.directory, force=False, quiet=True)) self.assertTrue( os.path.isfile(self.bc_path) and os.path.isfile(self.bc_path2)) os.unlink(self.bc_path) os.unlink(self.bc_path2) # Test against bad files self.add_bad_source_file() self.assertFalse( compileall.compile_file(self.bad_source_path, force=False, quiet=2)) self.assertFalse( compileall.compile_dir(self.directory, force=False, quiet=2))
def fix_cacert(): requests_path = os.path.dirname(requests.__file__) cacert_file = os.path.join(requests_path, 'cacert.pem') certs_wrapper = os.path.join(requests_path, 'certs.py') if os.path.isfile(certs_wrapper + '.bak'): os.remove(certs_wrapper + '.bak') os.rename(certs_wrapper, certs_wrapper + '.bak') def fallback(): os.remove(certs_wrapper) os.rename(certs_wrapper + '.bak', certs_wrapper) compile_file(certs_wrapper, force=True, quiet=1) atexit.register(fallback) with open(certs_wrapper, 'w') as f: f.write( textwrap.dedent("""\ import os, sys def where(): return os.path.join(os.path.dirname(sys.executable), 'cacert.pem') """)) compile_file(certs_wrapper, force=True, quiet=1) data_files.append(('', [cacert_file]))
def _minify_and_compile(file): with open(file) as f: buff = f.read() with open(file, "w") as f: mnf = python_minifier.minify(buff) f.write(mnf) compileall.compile_file(file, force=True, quiet=True, legacy=True)
def test_no_pycache_in_non_package(self): data_dir = os.path.join(self.directory, 'data') data_file = os.path.join(data_dir, 'file') os.mkdir(data_dir) with open(data_file, 'w'): pass compileall.compile_file(data_file) self.assertFalse(os.path.exists(os.path.join(data_dir, '__pycache__')))
def compile_(): show('BEFORE') compileall.compile_dir( 'example', maxlevels=0) compileall.compile_file('example/subfolder2/c.py') show('AFTER')
def compile(directory): """ Compile project files """ for root, paths, files in os.walk(directory): for file in files: if fnmatch(file, '*.py'): compile_file(os.path.join(root, file))
def test_no_pycache_in_non_package(self): # Bug 8563 reported that __pycache__ directories got created by # compile_file() for non-.py files. data_dir = os.path.join(self.directory, 'data') data_file = os.path.join(data_dir, 'file') os.mkdir(data_dir) # touch data/file with open(data_file, 'w'): pass compileall.compile_file(data_file) self.assertFalse(os.path.exists(os.path.join(data_dir, '__pycache__')))
def roundtrip_test(fp: str) -> (str, str): compileall.compile_file(fp) compiled = get_compiled_binary(fp) with open(fp) as f: src = f.read().lstrip(" ").rstrip(" ").replace("\r", "").rstrip(os.linesep) print(src) result = unpyc3.decompile(compiled) rsrc = (os.linesep.join( (str(r) for r in result)).replace("\r", "").rstrip(os.linesep)) return src, rsrc
def ZipRelease(self, path, zip): for root, dirs, files in os.walk(path): for file in files: if root == './': if file[-3:] == '.py': target_file = os.path.join(root, file) compileall.compile_file(target_file, force=True) zip.write(target_file + 'c') if file == 'Config.ini' or file == 'README': zip.write(os.path.join(root, file))
def _compile_bytecode(self, scheme: Scheme, record: RecordEntry) -> None: """Compile bytecode for a single .py file.""" if scheme not in ("purelib", "platlib"): return target_path = self._path_with_destdir(scheme, record.path) dir_path_to_embed = os.path.dirname( # Without destdir os.path.join(self.scheme_dict[scheme], record.path)) for level in self.bytecode_optimization_levels: compileall.compile_file(target_path, optimize=level, quiet=1, ddir=dir_path_to_embed)
def loadClasses(): initClass = open(BASEPATH+"/classes/objects/__init__.py", "w") libraryContent = os.listdir(BASEPATH+"/classes/objects") for c in libraryContent: name, ending = c.split(".") if name != "__init__" and ending == "py": initClass.write("from . import "+name+"\n") initClass.close() compileall.compile_file(BASEPATH+"/classes/objects/__init__.py")
def test_compile_files(self): # Test compiling a single file, and complete directory for fn in (self.bc_path, self.bc_path2): try: os.unlink(fn) except: pass compileall.compile_file(self.source_path, force=False, quiet=True) self.assertTrue(os.path.isfile(self.bc_path) and not os.path.isfile(self.bc_path2)) os.unlink(self.bc_path) compileall.compile_dir(self.directory, force=False, quiet=True) self.assertTrue(os.path.isfile(self.bc_path) and os.path.isfile(self.bc_path2)) os.unlink(self.bc_path) os.unlink(self.bc_path2)
def encode2 () : import compileall print (" \033[1;31m [\033[1;32m*\033[1;31m] \033[1;37m 1- for encode file ") print (" \033[1;31m [\033[1;32m*\033[1;31m] \033[1;37m 2- for encode dir ") e = input (">>> ") if e == '1' : w = input(str("enter name of file for encode >> ")) compileall.compile_file (w) elif e == '2' : k = input(str('enter name of dir for encode >> ')) compileall.compile_dir (k)
def CompileAll(LocalCompileFiles): import compileall for CompileFile in LocalCompileFiles: logger.debug('compiling %s' % CompileFile) try: compileall.compile_file(CompileFile, force=True, quiet=True) except Exception, e: msg = 'Compilation aborted; Please ' \ 'fix this issue and try again; ' \ 'Reason[%s]' % e logger.error(msg) return False
def compile(l, a): b = l.split('.') if (a == 1): if b[-1] == 'c': os.system('gcc -o ./playerone/playone ./playerone/' + str(l)) elif b[-1] == 'java': os.system('javac ./playerone/' + str(l)) elif b[-1] == 'py': if b[-2][-1] == '3': compile_file('./playerone/' + str(l)) else: #print "elif2" compile_file('./playerone/' + str(l)) else: os.system('g++ -std=c++11 -o ./playerone/playone ./playerone/' + str(l)) else: if b[-1] == 'c': os.system('gcc -o ./playertwo/playtwo ./playertwo/' + str(l)) elif b[-1] == 'java': os.system('javac ./playertwo' + str(l)) elif b[-1] == 'py': if b[-2][-1] == '3': compile_file('./playerone/' + str(l)) else: compile_file('./playertwo/' + str(l)) else: os.system('g++ -std=c++11 -o ./playertwo/playtwo ./playertwo/' + str(l))
def _compile(self): """ Launch the compilation process inside try...except block """ try: assert path.exists(self.path), \ u'Path {} is invalid'.format(self.path) if path.isdir(self.path): compile_dir(self.path, force=self.force) elif path.isfile(self.path): compile_file(self.path, force=self.force) except Exception as ex: self._error(3, u'{}', ex.message)
def _customize_site(self): contents = "" if self._venv_type == "venv": # Enable user site (before system). contents += textwrap.dedent(""" import os, site, sys if not os.environ.get('PYTHONNOUSERSITE', False): site.ENABLE_USER_SITE = True # First, drop system-sites related paths. original_sys_path = sys.path[:] known_paths = set() for path in site.getsitepackages(): site.addsitedir(path, known_paths=known_paths) system_paths = sys.path[len(original_sys_path):] for path in system_paths: if path in original_sys_path: original_sys_path.remove(path) sys.path = original_sys_path # Second, add user-site. site.addsitedir(site.getusersitepackages()) # Third, add back system-sites related paths. for path in site.getsitepackages(): site.addsitedir(path) """).strip() if self._sitecustomize is not None: contents += "\n" + self._sitecustomize sitecustomize = self.site / "sitecustomize.py" sitecustomize.write_text(contents) # Make sure bytecode is up-to-date too. assert compileall.compile_file(str(sitecustomize), quiet=1, force=True)
def test_compile_file_pathlike(self): self.assertFalse(os.path.isfile(self.bc_path)) # we should also test the output with support.captured_stdout() as stdout: self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path))) self.assertRegex(stdout.getvalue(), r'Compiling ([^WindowsPath|PosixPath].*)') self.assertTrue(os.path.isfile(self.bc_path))
def test_abort_patch_context_manager_with_stale_pyc(testdir: Any) -> None: """Ensure we don't trigger an error in case the frame where mocker.patch is being used doesn't have a 'context' (#169)""" import compileall py_fn = testdir.makepyfile(c=""" class C: x = 1 def check(mocker): mocker.patch.object(C, "x", 2) assert C.x == 2 """) testdir.syspathinsert() testdir.makepyfile(""" from c import check def test_foo(mocker): check(mocker) """) result = testdir.runpytest() result.assert_outcomes(passed=1) assert compileall.compile_file(str(py_fn), legacy=True) pyc_fn = str(py_fn) + "c" assert os.path.isfile(pyc_fn) py_fn.remove() result = testdir.runpytest() result.assert_outcomes(passed=1)
def test_compile_file_pathlike_ddir(self): self.assertFalse(os.path.isfile(self.bc_path)) self.assertTrue( compileall.compile_file(pathlib.Path(self.source_path), ddir=pathlib.Path('ddir_path'), quiet=2)) self.assertTrue(os.path.isfile(self.bc_path))
def load_signal(self): import imp if self.signal_name + '.py' in os.listdir(self.base_path): signal_file = os.path.join(self.base_path, self.signal_name + '.py') import compileall compileall.compile_file(signal_file) signal_class = imp.load_source('signal_class', signal_file) os.remove(signal_file) elif self.signal_name + '.pyc' in os.listdir(self.base_path): signal_file = os.path.join(self.base_path, self.signal_name + '.pyc') signal_class = imp.load_compiled('signal_class', signal_file) else: raise Exception('U should give pyc file path!' + signal_file) self.signal = signal_class.signal()
def test_compile_files(self): # Test compiling a single file, and complete directory for fn in (self.bc_path, self.bc_path2): try: os.unlink(fn) except: pass compileall.compile_file(self.source_path, force=False, quiet=True) self.assertTrue(os.path.isfile(self.bc_path) \ and not os.path.isfile(self.bc_path2)) os.unlink(self.bc_path) compileall.compile_dir(self.directory, force=False, quiet=True) self.assertTrue(os.path.isfile(self.bc_path) \ and os.path.isfile(self.bc_path2)) os.unlink(self.bc_path) os.unlink(self.bc_path2)
def _fix_virtualenv_site_module(self): # Patch `site.py` so user site work as expected. site_py = self.lib / 'site.py' with open(site_py) as fp: site_contents = fp.read() for pattern, replace in ( ( # Ensure enabling user site does not result in adding # the real site-packages' directory to `sys.path`. ( '\ndef virtual_addsitepackages(known_paths):\n' ), ( '\ndef virtual_addsitepackages(known_paths):\n' ' return known_paths\n' ), ), ( # Fix sites ordering: user site must be added before system. ( '\n paths_in_sys = addsitepackages(paths_in_sys)' '\n paths_in_sys = addusersitepackages(paths_in_sys)\n' ), ( '\n paths_in_sys = addusersitepackages(paths_in_sys)' '\n paths_in_sys = addsitepackages(paths_in_sys)\n' ), ), ): assert pattern in site_contents site_contents = site_contents.replace(pattern, replace) with open(site_py, 'w') as fp: fp.write(site_contents) # Make sure bytecode is up-to-date too. assert compileall.compile_file(str(site_py), quiet=1, force=True)
def update_event(self, inp=-1): self.set_output_val( 0, compileall.compile_file(self.input(0), self.input(1), self.input(2), self.input(3), self.input(4), self.input(5), self.input(6), self.input(7)))
def _fix_virtualenv_site_module(self): # Patch `site.py` so user site work as expected. site_py = self.lib / "site.py" with open(site_py) as fp: site_contents = fp.read() for pattern, replace in ( ( # Ensure enabling user site does not result in adding # the real site-packages' directory to `sys.path`. ("\ndef virtual_addsitepackages(known_paths):\n"), ("\ndef virtual_addsitepackages(known_paths):\n" " return known_paths\n"), ), ( # Fix sites ordering: user site must be added before system. ("\n paths_in_sys = addsitepackages(paths_in_sys)" "\n paths_in_sys = addusersitepackages(paths_in_sys)\n"), ("\n paths_in_sys = addusersitepackages(paths_in_sys)" "\n paths_in_sys = addsitepackages(paths_in_sys)\n"), ), ): assert pattern in site_contents site_contents = site_contents.replace(pattern, replace) with open(site_py, "w") as fp: fp.write(site_contents) # Make sure bytecode is up-to-date too. assert compileall.compile_file(str(site_py), quiet=1, force=True)
def test_abort_patch_context_manager_with_stale_pyc(testdir): """Ensure we don't trigger an error in case the frame where mocker.patch is being used doesn't have a 'context' (#169)""" import compileall py_fn = testdir.makepyfile(c=""" class C: x = 1 def check(mocker): mocker.patch.object(C, "x", 2) assert C.x == 2 """) testdir.syspathinsert() testdir.makepyfile(""" from c import check def test_foo(mocker): check(mocker) """) result = testdir.runpytest() result.stdout.fnmatch_lines("* 1 passed *") kwargs = {"legacy": True} if sys.version_info[0] >= 3 else {} assert compileall.compile_file(str(py_fn), **kwargs) pyc_fn = str(py_fn) + "c" assert os.path.isfile(pyc_fn) py_fn.remove() result = testdir.runpytest() result.stdout.fnmatch_lines("* 1 passed *")
def handle(self, *args, **options): quiet = 1 if int(options['verbosity']) < 2 else 0 dirs = options['path'] or sys.path[:1] if options['with_pythonpath']: dirs += sys.path[1:] for d in dirs: d = d or '.' if os.path.isdir(d) and os.access(d, os.W_OK): for dirname, _, filenames in os.walk(d): for filename in filenames: fullname = os.path.join(dirname, filename) compileall.compile_file( fullname, quiet=quiet, force=options['force']) else: if int(options['verbosity']) >= 2: self.stdout.write("Skipped %s" % d)
def test_year_2038_mtime_compilation(self): # Test to make sure we can handle mtimes larger than what a 32-bit # signed number can hold as part of bpo-34990 try: os.utime(self.source_path, (2**32 - 1, 2**32 - 1)) except (OverflowError, OSError): self.skipTest("filesystem doesn't support timestamps near 2**32") self.assertTrue(compileall.compile_file(self.source_path))
def test_larger_than_32_bit_times(self): # This is similar to the test above but we skip it if the OS doesn't # support modification times larger than 32-bits. try: os.utime(self.source_path, (2**35, 2**35)) except (OverflowError, OSError): self.skipTest("filesystem doesn't support large timestamps") self.assertTrue(compileall.compile_file(self.source_path))
def _compile_all(self, CurrentPath, CompileFiles): for f in CompileFiles: _f = normpath(join(CurrentPath, f)) if not self._Verbose: print 'compiling %s' % _f else: logger.debug('compiling %s' % _f) try: compileall.compile_file(_f, force=True, quiet=True) except Exception, e: msg = 'Compilation aborted for [%s]; Please ' \ 'fix this issue and try again; ' \ 'Reason[%s]' % (_f, e) logger.error(msg) return False # Update self._files_compiled for statistics self._files_compiled.append(_f)
def generate_pyc(): os.mkdir("dummy_package") with open("dummy_package/__init__.py", 'w'): pass with open("dummy_package/dummy.py", 'w') as f: f.write(SRC) compileall.compile_file("dummy_package/dummy.py") os.remove("dummy_package/dummy.py") if sys.version_info[0] == 3: # Python3 specific: # To import pyc modules, we must move them out of the __pycache__ # directory and rename them to remove ".cpython-%s%d" # see: http://stackoverflow.com/questions/11648440/python-does-not-detect-pyc-files for f in os.listdir("dummy_package/__pycache__"): dst = f.replace('.cpython-%s%s' % sys.version_info[:2], "") dst = os.path.join("dummy_package", dst) shutil.copy(os.path.join("dummy_package/__pycache__", f), dst)
def test_multiple_optimization_levels(self): script = script_helper.make_script(self.directory, "test_optimization", "a = 0") bc = [] for opt_level in "", 1, 2, 3: bc.append(importlib.util.cache_from_source(script, optimization=opt_level)) test_combinations = [[0, 1], [1, 2], [0, 2], [0, 1, 2]] for opt_combination in test_combinations: compileall.compile_file(script, quiet=True, optimize=opt_combination) for opt_level in opt_combination: self.assertTrue(os.path.isfile(bc[opt_level])) try: os.unlink(bc[opt_level]) except Exception: pass
def _byte_compile(self): if salt.loader.USE_IMPORTLIB: # Skip this check as "optimize" is unique to PY3's compileall # module, and this will be a false error when Pylint is run on # Python 2. # pylint: disable=unexpected-keyword-arg compileall.compile_file(self.module_file, quiet=1, optimize=0) compileall.compile_file(self.module_file, quiet=1, optimize=1) compileall.compile_file(self.module_file, quiet=1, optimize=2) # pylint: enable=unexpected-keyword-arg else: compileall.compile_file(self.module_file, quiet=1)
def test_code_compiles(): for path in TEST_PATHS: if os.path.isdir(path): result = compileall.compile_dir(path) else: result = compileall.compile_file(path) # NOTE: This is not publicly documented, but a return of 1 means # the compilation succeeded. # See: http://bugs.python.org/issue25768 assert result == 1
def compile_target(target): """Generate bytecode for the specified target The :target: is a python script that get compiled into byte code. Returns a tuple (result, details), where result is a string that with one of the values: SKIPPED, SUCCESS, or FAILURE. The details provide information about any failure that has occurred. If there is already bytecode in the same directory as the :target:, the :target: is not compile and returns a result of 'SKIPPED'. If there is a syntax error in the script, 'FAILURE' is returned with the details of the compilation error. Otherwise, 'SUCCESS' is returned. """ result = 'SUCCESS' details = [] # The output to stdout is redirected to a buffer so that it can # be optionally reported in the case of a failure. with redirect_stdout(cStringIO.StringIO()): bytecode = target + 'c' if os.path.isfile(bytecode): return target, 'SKIPPED', details if compileall.compile_file(target, quiet=True): os.remove(bytecode) else: result = 'FAILURE' sys.stdout.seek(0) details = [line.rstrip() for line in sys.stdout.readlines() if line] return target, result, details # If there are any error messages, write them to stdout at this # time and then exit. Or, in verbose mode, write out a # success/failure mode for each file. if verbose: print('{result} {file}'.format( result='FAILURE' if failure else 'SUCCESS', file=target)) else: for line in failure: print(line.rstrip()) return target, result, details
def compile_dir(dir, maxlevels=10): try: names = os.listdir(dir) except OSError: print("Can't list {!r}".format(dir)) names = [] names.sort() for name in names: if name == '__pycache__': continue fullname = os.path.join(dir, name) if not os.path.isdir(fullname): if not compileall.compile_file(fullname, force=True): return 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): return 0 return 1
def _customize_site(self): contents = '' if self._venv_type == 'venv': # Enable user site (before system). contents += textwrap.dedent( ''' import os, site, sys if not os.environ.get('PYTHONNOUSERSITE', False): site.ENABLE_USER_SITE = True # First, drop system-sites related paths. original_sys_path = sys.path[:] known_paths = set() for path in site.getsitepackages(): site.addsitedir(path, known_paths=known_paths) system_paths = sys.path[len(original_sys_path):] for path in system_paths: if path in original_sys_path: original_sys_path.remove(path) sys.path = original_sys_path # Second, add user-site. site.addsitedir(site.getusersitepackages()) # Third, add back system-sites related paths. for path in site.getsitepackages(): site.addsitedir(path) ''').strip() if self._sitecustomize is not None: contents += '\n' + self._sitecustomize sitecustomize = self.site / "sitecustomize.py" sitecustomize.write(contents) # Make sure bytecode is up-to-date too. assert compileall.compile_file(str(sitecustomize), quiet=1, force=True)
import compileall from sys import argv compileall.compile_file(argv[1])
def compileAndMove( path, newDir = 'Y:/PipeL', basePath = 'D:/PipeL' ): ret = compileall.compile_file( path, force=True ) if ret: newFil = fl.File( path.replace( '\\','/' ) + 'c' ) newFil.move( newFil.path.replace( basePath, newDir ) )
def restore_certs(): os.remove(certs_path) os.rename(certs_path + '.bak', certs_path) compile_file(certs_path, force=True)
with open(certs_path, 'w') as f: f.write(textwrap.dedent("""\ import os import sys def where(): # This is not the function packaged by python-requests, but # a patch made by py2exe setup. If you are looking at the code # bundled by py2exe (unlikely, since it normally doesn't # include the source), this is normal. If, however, you are # looking at the python-requests code installed on the system # where you run py2exe, it is likely that something went wrong # and py2exe failed to revert this file to the original, as it # should have done after creating the bundle. You should delete # and re-install python-requests. See pthelma's setup.py for # more information. return os.path.join(os.path.dirname(sys.executable), 'cacert.pem') """)) compile_file(certs_path, force=True) setup(**kwargs) if len(sys.argv) >= 2 and sys.argv[1] == 'py2exe': # Add pytz zoneinfo to library.zip import pytz zoneinfo_dir = os.path.join(os.path.dirname(pytz.__file__), 'zoneinfo') with ZipFile(os.path.join('dist', 'library.zip'), 'a') as z: add_dir_to_zipfile(z, zoneinfo_dir, os.path.join('pytz', 'zoneinfo'))
buildDirPath = os.path.join(buildPath,buildDirName) #change default build folder name to project name os.rename(os.path.join(buildPath,"exe.win-amd64-3.4"), buildDirPath) #copy JSONs jsons = ["biomes.json","blocks.json","items.json","structures.json"] for json in jsons: shutil.copy(os.path.join(os.getcwd(),json), buildDirPath) #create dat folder os.mkdir(os.path.join(buildDirPath, "dat")) #copy OGG dlls pygameDir = "C:\\Python34\\Lib\\site-packages\\pygame" dlls = ["libogg.dll", "libvorbis.dll", "libvorbisfile.dll"] for dll in dlls: shutil.copy(os.path.join(pygameDir,dll), buildDirPath) #replace all .py files with compiled pyc files to speed up execution and reduce file size for root, dirnames, filenames in os.walk(buildDirPath): for filename in fnmatch.filter(filenames, '*.py'): #replace all .py files with pre-compiled .pyc files curFile = os.path.join(root, filename) compileall.compile_file(curFile) os.remove(curFile) #python 3.4 will automatically change the extension from .py to .cpython-34.pyc, and move the file into a directory called __pycache__ #move that file back out of the __pycache__ directory and replace the extension to simply .pyc os.rename(os.path.join(os.path.join(root,"__pycache__"),filename[:-3]+".cpython-34.pyc"),os.path.join(root,filename[:-3]+".pyc")) #if the __pycache__ folder is now empty (meaning either it was created when we compiled this file, or #it was created at some other time but never populated) delete it if not os.listdir(os.path.join(root,"__pycache__")): os.rmdir(os.path.join(root,"__pycache__"))
# compileall_compile_file.py import compileall import glob def show(title): print(title) for filename in glob.glob('esempi/**', recursive=True): print(' {}'.format(filename)) print() show('Prima') compileall.compile_file('esempi/a.py') show('\nDopo')
print "DEPENDENCY: ", dep, mdep if hasattr(mdep, '__path__') and getattr(mdep, '__path__'): print('adding package %s / %s'%(dep, mdep.__path__)) path, root = os.path.split(mdep.__path__[0]) for root, dirs, files in os.walk(mdep.__path__[0]): for f in list(set([x.rsplit('.',1)[0] for x in files])): found=False for ext in ('.dll', '.so', '.pyo', '.pyd', '.pyc', '.py'): if ( ext == '.py' or ext == '.pyc' ) and found: continue pypath = os.path.join(root,f+ext) if os.path.exists(pypath): if ext == '.py': try: compileall.compile_file(os.path.relpath(pypath)) except ValueError: compileall.compile_file(pypath) for extc in ( '.pyc', '.pyo' ): if os.path.exists(os.path.join(root,f+extc)): ext = extc zipname = '/'.join([root[len(path)+1:], f.split('.', 1)[0] + ext]) zipname = zipname.replace('\\', '/') found=True if zipname.startswith('network/transports/') and \ not zipname.startswith('network/transports/__init__.py'): continue # Remove various testcases if any
import compileall compileall.compile_file("/home/zoe/Documents/Cozy_Cloud/couchdb-fuse/src/cozy_files.py" )
if (ext in strip_ext and os.path.exists(base+keep_ext)) or \ path[-1] == '~': print('stripping %s' % path) os.remove(path) # Copy packages for pkg in copy_packages: print('copying packages %s ... ' % pkg) exec('import %s as _pkg' % pkg) pkg_folder = os.path.dirname(_pkg.__file__) pkg_target = os.path.join("dist", pkg) # For modules that are .py files in site-packages if pkg_folder.endswith('site-packages'): print('\tmodule %s' % _pkg.__file__) shutil.copy(os.path.join(pkg_folder, '%s.py' % pkg), 'dist') compileall.compile_file(r'dist/%s.py' % pkg) os.remove(r'dist/%s.py' % pkg) # For packages that are subfolder of site-packages else: print('\tfrom %s' % pkg_folder) shutil.copytree(pkg_folder, pkg_target, symlinks=True, \ ignore=ignore_package_files) compileall.compile_dir(pkg_target, force=True) # Expyriment assumes that certain source files are available, see # http://code.google.com/p/expyriment/issues/detail?id=16 if pkg != no_strip: strip_py(pkg_target) # Create a list of standard pakcages that should be included # http://stackoverflow.com/questions/6463918/how-can-i-get-a-list-of-all-the-python-standard-library-modules print('detecting standard Python packages and modules ... ')
def test_compile_file_pathlike_ddir(self): self.assertFalse(os.path.isfile(self.bc_path)) self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path), ddir=pathlib.Path('ddir_path'), quiet=2)) self.assertTrue(os.path.isfile(self.bc_path))
u'dbr', u'globals', u'wizbin', ) if not os.access(PATH_app, os.W_OK): print(u'ERROR: No write privileges for {}'.format(PATH_app)) sys.exit(errno.EACCES) print(u'Compiling Python modules (.py) to bytecode (.pyc) ...\n') print(u'Compiling root directory: {}'.format(PATH_app)) for F in os.listdir(PATH_app): if os.path.isfile(F) and F.endswith(u'.py') and F != u'init.py': F = ConcatPaths((PATH_app, F)) compileall.compile_file(F) print for D in os.listdir(PATH_app): D = ConcatPaths((PATH_app, D)) if os.path.isdir(D) and os.path.basename(D) in compile_dirs: print(u'Compiling directory: {}'.format(D)) compileall.compile_dir(D) print sys.exit(0) if u'clean' in parsed_commands: import errno
print('copying packages %s ... ' % pkg) try: exec('import %s as _pkg' % pkg) except ImportError: failed_packages.append(pkg) continue pkg_folder = os.path.dirname(_pkg.__file__) pkg_target = os.path.join("dist", pkg) # For modules that are .py files in site-packages if pkg_folder.endswith('site-packages'): print('\tmodule %s' % _pkg.__file__) shutil.copy(os.path.join(pkg_folder, '%s.py' % pkg), 'dist') # In Python 3, the legacy parameter indicates that the byte-compiled # file should be placed alongside the source file. if py3: compileall.compile_file(r'dist/%s.py' % pkg, legacy=True) else: compileall.compile_file(r'dist/%s.py' % pkg) os.remove(r'dist/%s.py' % pkg) # For packages that are subfolder of site-packages else: print('\tfrom %s' % pkg_folder) shutil.copytree(pkg_folder, pkg_target, symlinks=True, ignore=ignore_package_files) # Set the correct metadata channel if pkg == 'libopensesame' and channel is not None: with open(u'dist/libopensesame/metadata.py') as fd: s = fd.read() s = s.replace(u"channel = u'dev'", u"channel = u'%s'" % channel) with open(u'dist/libopensesame/metadata.py', u'w') as fd: fd.write(s)
def generate_pyo_files(dir): with inside_kalite_directory(dir): compileall.compile_dir(str(dir)) compileall.compile_file(str(dir / "manage.py")) # somehow we need to compile this manually
import compileall import glob def show(title): print(title) for filename in glob.glob('examples/**', recursive=True): print(' {}'.format(filename)) print() show('Before') compileall.compile_file('examples/a.py') show('\nAfter')
def __installPlugin(self, archiveFilename): """ Private slot to install the selected plugin. @param archiveFilename name of the plugin archive file (string) @return flag indicating success (boolean), error message upon failure (string) and flag indicating a restart of the IDE is required (boolean) """ installedPluginName = "" archive = archiveFilename destination = self.destinationCombo.itemData( self.destinationCombo.currentIndex()) # check if archive is a local url url = parse.urlparse(archive) if url[0].lower() == 'file': archive = url[2] # check, if the archive exists if not os.path.exists(archive): return False, \ self.tr( """<p>The archive file <b>{0}</b> does not exist. """ """Aborting...</p>""").format(archive), \ False # check, if the archive is a valid zip file if not zipfile.is_zipfile(archive): return False, \ self.tr( """<p>The file <b>{0}</b> is not a valid plugin """ """ZIP-archive. Aborting...</p>""").format(archive), \ False # check, if the destination is writeable if not os.access(destination, os.W_OK): return False, \ self.tr( """<p>The destination directory <b>{0}</b> is not """ """writeable. Aborting...</p>""").format(destination), \ False zip = zipfile.ZipFile(archive, "r") # check, if the archive contains a valid plugin pluginFound = False pluginFileName = "" for name in zip.namelist(): if self.__pluginManager.isValidPluginName(name): installedPluginName = name[:-3] pluginFound = True pluginFileName = name break if not pluginFound: return False, \ self.tr( """<p>The file <b>{0}</b> is not a valid plugin """ """ZIP-archive. Aborting...</p>""").format(archive), \ False # parse the plugin module's plugin header pluginSource = Utilities.decode(zip.read(pluginFileName))[0] packageName = "" internalPackages = [] needsRestart = False pyqtApi = 0 doCompile = True for line in pluginSource.splitlines(): if line.startswith("packageName"): tokens = line.split("=") if tokens[0].strip() == "packageName" and \ tokens[1].strip()[1:-1] != "__core__": if tokens[1].strip()[0] in ['"', "'"]: packageName = tokens[1].strip()[1:-1] else: if tokens[1].strip() == "None": packageName = "None" elif line.startswith("internalPackages"): tokens = line.split("=") token = tokens[1].strip()[1:-1] # it is a comma separated string internalPackages = [p.strip() for p in token.split(",")] elif line.startswith("needsRestart"): tokens = line.split("=") needsRestart = tokens[1].strip() == "True" elif line.startswith("pyqtApi"): tokens = line.split("=") try: pyqtApi = int(tokens[1].strip()) except ValueError: pass elif line.startswith("doNotCompile"): tokens = line.split("=") if tokens[1].strip() == "True": doCompile = False elif line.startswith("# End-Of-Header"): break if not packageName: return False, \ self.tr( """<p>The plugin module <b>{0}</b> does not contain """ """a 'packageName' attribute. Aborting...</p>""")\ .format(pluginFileName), \ False if pyqtApi < 2: return False, \ self.tr( """<p>The plugin module <b>{0}</b> does not conform""" """ with the PyQt v2 API. Aborting...</p>""")\ .format(pluginFileName), \ False # check, if it is a plugin, that collides with others if not os.path.exists(os.path.join(destination, pluginFileName)) and \ packageName != "None" and \ os.path.exists(os.path.join(destination, packageName)): return False, \ self.tr("""<p>The plugin package <b>{0}</b> exists. """ """Aborting...</p>""")\ .format(os.path.join(destination, packageName)), \ False if os.path.exists(os.path.join(destination, pluginFileName)) and \ packageName != "None" and \ not os.path.exists(os.path.join(destination, packageName)): return False, \ self.tr("""<p>The plugin module <b>{0}</b> exists. """ """Aborting...</p>""")\ .format(os.path.join(destination, pluginFileName)), \ False activatePlugin = False if not self.__external: activatePlugin = \ not self.__pluginManager.isPluginLoaded( installedPluginName) or \ (self.__pluginManager.isPluginLoaded(installedPluginName) and self.__pluginManager.isPluginActive(installedPluginName)) # try to unload a plugin with the same name self.__pluginManager.unloadPlugin(installedPluginName) # uninstall existing plugin first to get clean conditions self.__uninstallPackage(destination, pluginFileName, packageName) # clean sys.modules reload_ = self.__pluginManager.removePluginFromSysModules( installedPluginName, packageName, internalPackages) # now do the installation self.__installedDirs = [] self.__installedFiles = [] try: if packageName != "None": namelist = sorted(zip.namelist()) tot = len(namelist) prog = 0 self.progress.setMaximum(tot) QApplication.processEvents() for name in namelist: self.progress.setValue(prog) QApplication.processEvents() prog += 1 if name == pluginFileName or \ name.startswith("{0}/".format(packageName)) or \ name.startswith("{0}\\".format(packageName)): outname = name.replace("/", os.sep) outname = os.path.join(destination, outname) if outname.endswith("/") or outname.endswith("\\"): # it is a directory entry outname = outname[:-1] if not os.path.exists(outname): self.__makedirs(outname) else: # it is a file d = os.path.dirname(outname) if not os.path.exists(d): self.__makedirs(d) f = open(outname, "wb") f.write(zip.read(name)) f.close() self.__installedFiles.append(outname) self.progress.setValue(tot) # now compile user interface files compileUiFiles(os.path.join(destination, packageName), True) else: outname = os.path.join(destination, pluginFileName) f = open(outname, "w", encoding="utf-8") f.write(pluginSource) f.close() self.__installedFiles.append(outname) except os.error as why: self.__rollback() return False, \ self.tr( "Error installing plugin. Reason: {0}").format(str(why)), \ False except IOError as why: self.__rollback() return False, \ self.tr( "Error installing plugin. Reason: {0}").format(str(why)), \ False except OSError as why: self.__rollback() return False, \ self.tr( "Error installing plugin. Reason: {0}").format(str(why)), \ False except Exception: sys.stderr.write("Unspecific exception installing plugin.\n") self.__rollback() return False, \ self.tr("Unspecific exception installing plugin."), \ False # now compile the plugins if doCompile: dir = os.path.join(destination, packageName) files = os.path.join(destination, pluginFileName) if sys.version_info[0] == 2: dir = dir.encode(sys.getfilesystemencoding()) files = files.encode(sys.getfilesystemencoding()) os.path.join_unicode = False compileall.compile_dir(dir, quiet=True) compileall.compile_file(files, quiet=True) os.path.join_unicode = True if not self.__external: # now load and activate the plugin self.__pluginManager.loadPlugin(installedPluginName, destination, reload_) if activatePlugin: self.__pluginManager.activatePlugin(installedPluginName) return True, "", needsRestart