我们从Python开源项目中,提取了以下48个代码示例,用于说明如何使用zipfile.PyZipFile()。
def load_special_tools(self, var, ban=[]): global waf_dir if os.path.isdir(waf_dir): lst = self.root.find_node(waf_dir).find_node('waflib/extras').ant_glob(var) for x in lst: if not x.name in ban: load_tool(x.name.replace('.py', '')) else: from zipfile import PyZipFile waflibs = PyZipFile(waf_dir) lst = waflibs.namelist() for x in lst: if not re.match("waflib/extras/%s" % var.replace("*", ".*"), var): continue f = os.path.basename(x) doban = False for b in ban: r = b.replace("*", ".*") if re.match(r, f): doban = True if not doban: f = f.replace('.py', '') load_tool(f)
def load_special_tools(self, var, ban=[]): global waf_dir if os.path.isdir(waf_dir): lst = self.root.find_node(waf_dir).find_node('waflib/extras').ant_glob(var) for x in lst: if not x.name in ban: load_tool(x.name.replace('.py', '')) else: from zipfile import PyZipFile waflibs = PyZipFile(waf_dir) lst = waflibs.namelist() for x in lst: if not re.match("waflib/extras/%s" % var.replace("*", ".*"), var): continue f = os.path.basename(x) doban = False for b in ban: r = b.replace("*", ".*") if re.match(b, f): doban = True if not doban: f = f.replace('.py', '') load_tool(f)
def test_write_python_directory(self): os.mkdir(TESTFN2) try: with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp: fp.write("print(42)\n") with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp: fp.write("print(42 * 42)\n") with open(os.path.join(TESTFN2, "mod2.txt"), "w") as fp: fp.write("bla bla bla\n") with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: zipfp.writepy(TESTFN2) names = zipfp.namelist() self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names) self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names) self.assertNotIn('mod2.txt', names) finally: shutil.rmtree(TESTFN2)
def test_write_pyfile(self): self.requiresWriteAccess(os.path.dirname(__file__)) with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp: fn = __file__ if fn.endswith('.pyc') or fn.endswith('.pyo'): fn = fn[:-1] zipfp.writepy(fn) bn = os.path.basename(fn) self.assertNotIn(bn, zipfp.namelist()) self.assertTrue(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist()) with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp: fn = __file__ if fn.endswith(('.pyc', '.pyo')): fn = fn[:-1] zipfp.writepy(fn, "testpackage") bn = "%s/%s" % ("testpackage", os.path.basename(fn)) self.assertNotIn(bn, zipfp.namelist()) self.assertTrue(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist())
def test_write_python_directory(self): os.mkdir(TESTFN2) try: with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp: fp.write("print(42)\n") with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp: fp.write("print(42 * 42)\n") with open(os.path.join(TESTFN2, "mod2.txt"), "w") as fp: fp.write("bla bla bla\n") zipfp = zipfile.PyZipFile(TemporaryFile(), "w") zipfp.writepy(TESTFN2) names = zipfp.namelist() self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names) self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names) self.assertNotIn('mod2.txt', names) finally: rmtree(TESTFN2)
def test_write_python_directory(self): os.mkdir(TESTFN2) try: with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp: fp.write("print(42)\n") with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp: fp.write("print(42 * 42)\n") with open(os.path.join(TESTFN2, "mod2.txt"), "w") as fp: fp.write("bla bla bla\n") with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: zipfp.writepy(TESTFN2) names = zipfp.namelist() self.assertCompiledIn('mod1.py', names) self.assertCompiledIn('mod2.py', names) self.assertNotIn('mod2.txt', names) finally: shutil.rmtree(TESTFN2)
def test_write_pyfile(self): with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp: fn = __file__ if fn.endswith('.pyc') or fn.endswith('.pyo'): fn = fn[:-1] zipfp.writepy(fn) bn = os.path.basename(fn) self.assertNotIn(bn, zipfp.namelist()) self.assertTrue(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist()) with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp: fn = __file__ if fn.endswith(('.pyc', '.pyo')): fn = fn[:-1] zipfp.writepy(fn, "testpackage") bn = "%s/%s" % ("testpackage", os.path.basename(fn)) self.assertNotIn(bn, zipfp.namelist()) self.assertTrue(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist())
def test_write_with_optimization(self): import email packagedir = os.path.dirname(email.__file__) self.requiresWriteAccess(packagedir) # use .pyc if running test in optimization mode, # use .pyo if running test in debug mode optlevel = 1 if __debug__ else 0 ext = '.pyo' if optlevel == 1 else '.pyc' with TemporaryFile() as t, \ zipfile.PyZipFile(t, "w", optimize=optlevel) as zipfp: zipfp.writepy(packagedir) names = zipfp.namelist() self.assertIn('email/__init__' + ext, names) self.assertIn('email/mime/text' + ext, names)
def test_write_python_directory(self): os.mkdir(TESTFN2) try: with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp: fp.write("print(42)\n") with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp: fp.write("print(42 * 42)\n") with open(os.path.join(TESTFN2, "mod2.txt"), "w") as fp: fp.write("bla bla bla\n") with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: zipfp.writepy(TESTFN2) names = zipfp.namelist() self.assertCompiledIn('mod1.py', names) self.assertCompiledIn('mod2.py', names) self.assertNotIn('mod2.txt', names) finally: rmtree(TESTFN2)
def test_write_pyfile_bad_syntax(self): os.mkdir(TESTFN2) try: with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp: fp.write("Bad syntax in python file\n") with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: # syntax errors are printed to stdout with captured_stdout() as s: zipfp.writepy(os.path.join(TESTFN2, "mod1.py")) self.assertIn("SyntaxError", s.getvalue()) # as it will not have compiled the python file, it will # include the .py file not .pyc or .pyo names = zipfp.namelist() self.assertIn('mod1.py', names) self.assertNotIn('mod1.pyc', names) self.assertNotIn('mod1.pyo', names) finally: rmtree(TESTFN2)
def test_write_python_directory(self): os.mkdir(TESTFN2) try: with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp: fp.write("print(42)\n") with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp: fp.write("print(42 * 42)\n") with open(os.path.join(TESTFN2, "mod2.txt"), "w") as fp: fp.write("bla bla bla\n") zipfp = zipfile.PyZipFile(TemporaryFile(), "w") zipfp.writepy(TESTFN2) names = zipfp.namelist() self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names) self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names) self.assertNotIn('mod2.txt', names) finally: shutil.rmtree(TESTFN2)
def test_write_pyfile(self): with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: fn = __file__ if fn.endswith('.pyc') or fn.endswith('.pyo'): path_split = fn.split(os.sep) if os.altsep is not None: path_split.extend(fn.split(os.altsep)) if '__pycache__' in path_split: fn = imp.source_from_cache(fn) else: fn = fn[:-1] zipfp.writepy(fn) bn = os.path.basename(fn) self.assertNotIn(bn, zipfp.namelist()) self.assertTrue(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist()) with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: fn = __file__ if fn.endswith(('.pyc', '.pyo')): fn = fn[:-1] zipfp.writepy(fn, "testpackage") bn = "%s/%s" % ("testpackage", os.path.basename(fn)) self.assertNotIn(bn, zipfp.namelist()) self.assertTrue(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist())
def test_write_python_package(self): import email packagedir = os.path.dirname(email.__file__) with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: zipfp.writepy(packagedir) # Check for a couple of modules at different levels of the # hierarchy names = zipfp.namelist() self.assertTrue('email/__init__.pyo' in names or 'email/__init__.pyc' in names) self.assertTrue('email/mime/text.pyo' in names or 'email/mime/text.pyc' in names)
def test_write_with_optimization(self): import email packagedir = os.path.dirname(email.__file__) # use .pyc if running test in optimization mode, # use .pyo if running test in debug mode optlevel = 1 if __debug__ else 0 ext = '.pyo' if optlevel == 1 else '.pyc' with TemporaryFile() as t, \ zipfile.PyZipFile(t, "w", optimize=optlevel) as zipfp: zipfp.writepy(packagedir) names = zipfp.namelist() self.assertIn('email/__init__' + ext, names) self.assertIn('email/mime/text' + ext, names)
def test_write_non_pyfile(self): with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: with open(TESTFN, 'w') as f: f.write('most definitely not a python file') self.assertRaises(RuntimeError, zipfp.writepy, TESTFN) os.remove(TESTFN)
def test_write_python_package(self): import email packagedir = os.path.dirname(email.__file__) self.requiresWriteAccess(packagedir) with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp: zipfp.writepy(packagedir) # Check for a couple of modules at different levels of the # hierarchy names = zipfp.namelist() self.assertTrue('email/__init__.pyo' in names or 'email/__init__.pyc' in names) self.assertTrue('email/mime/text.pyo' in names or 'email/mime/text.pyc' in names)
def test_write_non_pyfile(self): with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp: with open(TESTFN, 'w') as fid: fid.write('most definitely not a python file') self.assertRaises(RuntimeError, zipfp.writepy, TESTFN) os.remove(TESTFN)
def release(): parser = optparse.OptionParser() parser.add_option('--cacert', dest='cacert', default=None) parser.add_option('--sslkey', dest='sslkey', default=None) parser.add_option('--sslcert', dest='sslcert', default=None) opts, argv = parser.parse_args() pyzfile, appuri = argv mainsrc = open(__file__, 'rb').read() mainlines = mainsrc.split('\n')[:-2] castr = 'None' keystr = 'None' certstr = 'None' if opts.cacert: castr = '"%s"' % open(opts.cacert, 'rb').read() if opts.sslkey: keystr = '"%s"' % open(opts.sslkey, 'rb').read() if opts.sslcert: certstr = '"%s"' % open(opts.sslcert, 'rb').read() mainlines.append(' appuri="%s"' % appuri) mainlines.append(' cacrt=%s' % castr) mainlines.append(' sslkey=%s' % keystr) mainlines.append(' sslcert=%s' % certstr) mainlines.append(' main(appuri, cacrt=cacrt, sslcert=sslcert, sslkey=sslkey)') mainsrc = '\n'.join(mainlines) pyz = zipfile.PyZipFile(pyzfile, 'w') pyz.writepy('cobra') pyz.writestr('__main__.py', mainsrc) pyz.close()
def main(): zipname = sys.argv[1] pz = zipfile.PyZipFile(zipname, 'w') dirnames = sys.argv[2:] if not len(dirnames): dirnames = [dname for dname in os.listdir('.') if os.path.isdir(dname) and dname != '.svn'] for dirname in dirnames: addSource(pz, dirname) pz.writepy(dirname) pz.close()
def test_write_pyfile(self): with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: fn = __file__ if fn.endswith('.pyc') or fn.endswith('.pyo'): path_split = fn.split(os.sep) if os.altsep is not None: path_split.extend(fn.split(os.altsep)) if '__pycache__' in path_split: fn = imp.source_from_cache(fn) else: fn = fn[:-1] zipfp.writepy(fn) bn = os.path.basename(fn) self.assertNotIn(bn, zipfp.namelist()) self.assertCompiledIn(bn, zipfp.namelist()) with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: fn = __file__ if fn.endswith(('.pyc', '.pyo')): fn = fn[:-1] zipfp.writepy(fn, "testpackage") bn = "%s/%s" % ("testpackage", os.path.basename(fn)) self.assertNotIn(bn, zipfp.namelist()) self.assertCompiledIn(bn, zipfp.namelist())
def test_write_python_package(self): import email packagedir = os.path.dirname(email.__file__) with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: zipfp.writepy(packagedir) # Check for a couple of modules at different levels of the # hierarchy names = zipfp.namelist() self.assertCompiledIn('email/__init__.py', names) self.assertCompiledIn('email/mime/text.py', names)
def test_write_python_package(self): import email packagedir = os.path.dirname(email.__file__) with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp: zipfp.writepy(packagedir) # Check for a couple of modules at different levels of the # hierarchy names = zipfp.namelist() self.assertTrue('email/__init__.pyo' in names or 'email/__init__.pyc' in names) self.assertTrue('email/mime/text.pyo' in names or 'email/mime/text.pyc' in names)
def test_write_pyfile(self): self.requiresWriteAccess(os.path.dirname(__file__)) with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: fn = __file__ if fn.endswith('.pyc') or fn.endswith('.pyo'): path_split = fn.split(os.sep) if os.altsep is not None: path_split.extend(fn.split(os.altsep)) if '__pycache__' in path_split: fn = importlib.util.source_from_cache(fn) else: fn = fn[:-1] zipfp.writepy(fn) bn = os.path.basename(fn) self.assertNotIn(bn, zipfp.namelist()) self.assertCompiledIn(bn, zipfp.namelist()) with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: fn = __file__ if fn.endswith(('.pyc', '.pyo')): fn = fn[:-1] zipfp.writepy(fn, "testpackage") bn = "%s/%s" % ("testpackage", os.path.basename(fn)) self.assertNotIn(bn, zipfp.namelist()) self.assertCompiledIn(bn, zipfp.namelist())
def test_write_python_package(self): import email packagedir = os.path.dirname(email.__file__) self.requiresWriteAccess(packagedir) with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: zipfp.writepy(packagedir) # Check for a couple of modules at different levels of the # hierarchy names = zipfp.namelist() self.assertCompiledIn('email/__init__.py', names) self.assertCompiledIn('email/mime/text.py', names)
def test_write_filtered_python_package(self): import test packagedir = os.path.dirname(test.__file__) self.requiresWriteAccess(packagedir) with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: # first make sure that the test folder gives error messages # (on the badsyntax_... files) with captured_stdout() as reportSIO: zipfp.writepy(packagedir) reportStr = reportSIO.getvalue() self.assertTrue('SyntaxError' in reportStr) # then check that the filter works on the whole package with captured_stdout() as reportSIO: zipfp.writepy(packagedir, filterfunc=lambda whatever: False) reportStr = reportSIO.getvalue() self.assertTrue('SyntaxError' not in reportStr) # then check that the filter works on individual files def filter(path): return not os.path.basename(path).startswith("bad") with captured_stdout() as reportSIO, self.assertWarns(UserWarning): zipfp.writepy(packagedir, filterfunc=filter) reportStr = reportSIO.getvalue() if reportStr: print(reportStr) self.assertTrue('SyntaxError' not in reportStr)
def test_write_non_pyfile(self): with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: with open(TESTFN, 'w') as f: f.write('most definitely not a python file') self.assertRaises(RuntimeError, zipfp.writepy, TESTFN) unlink(TESTFN)
def test_write_non_pyfile(self): with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp: open(TESTFN, 'w').write('most definitely not a python file') self.assertRaises(RuntimeError, zipfp.writepy, TESTFN) os.remove(TESTFN)
def test_write_pyfile(self): with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: fn = __file__ if fn.endswith('.pyc') or fn.endswith('.pyo'): path_split = fn.split(os.sep) if os.altsep is not None: path_split.extend(fn.split(os.altsep)) if '__pycache__' in path_split: fn = importlib.util.source_from_cache(fn) else: fn = fn[:-1] zipfp.writepy(fn) bn = os.path.basename(fn) self.assertNotIn(bn, zipfp.namelist()) self.assertCompiledIn(bn, zipfp.namelist()) with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: fn = __file__ if fn.endswith(('.pyc', '.pyo')): fn = fn[:-1] zipfp.writepy(fn, "testpackage") bn = "%s/%s" % ("testpackage", os.path.basename(fn)) self.assertNotIn(bn, zipfp.namelist()) self.assertCompiledIn(bn, zipfp.namelist())
def test_write_filtered_python_package(self): import test packagedir = os.path.dirname(test.__file__) with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: # first make sure that the test folder gives error messages # (on the badsyntax_... files) with captured_stdout() as reportSIO: zipfp.writepy(packagedir) reportStr = reportSIO.getvalue() self.assertTrue('SyntaxError' in reportStr) # then check that the filter works on the whole package with captured_stdout() as reportSIO: zipfp.writepy(packagedir, filterfunc=lambda whatever: False) reportStr = reportSIO.getvalue() self.assertTrue('SyntaxError' not in reportStr) # then check that the filter works on individual files with captured_stdout() as reportSIO, self.assertWarns(UserWarning): zipfp.writepy(packagedir, filterfunc=lambda fn: 'bad' not in fn) reportStr = reportSIO.getvalue() if reportStr: print(reportStr) self.assertTrue('SyntaxError' not in reportStr)
def prepare_zip_file(): clean_pyc_files() tmpd = tempfile.mkdtemp() egg = os.path.join(tmpd, 'testpkg1.egg') eggzip = zipfile.PyZipFile(egg, 'w', zipfile.ZIP_DEFLATED) eggzip.writepy(os.path.join(samples_dir, 'testmod1.py')) eggzip.writepy(os.path.join(samples_dir, 'testpkg1')) eggzip.close() return egg
def __init__(self, epub_file): if not epub_file: raise ValueError('Invalid epub file path') self.epub_file = epub_file self.epub_zip = PyZipFile(epub_file, 'r')
def create_py3_base_library(libzip_filename, graph): """ Package basic Python modules into .zip file. The .zip file with basic modules is necessary to have on PYTHONPATH for initializing libpython3 in order to run the frozen executable with Python 3. """ # TODO Replace this function with something better or something from standard Python library. # Helper functions. def _write_long(f, x): """ Write a 32-bit int to a file in little-endian order. """ f.write(bytes([x & 0xff, (x >> 8) & 0xff, (x >> 16) & 0xff, (x >> 24) & 0xff])) # Construct regular expression for matching modules that should be bundled # into base_library.zip. # Excluded are plain 'modules' or 'submodules.ANY_NAME'. # The match has to be exact - start and end of string not substring. regex_modules = '|'.join([r'(^%s$)' % x for x in PY3_BASE_MODULES]) regex_submod = '|'.join([r'(^%s\..*$)' % x for x in PY3_BASE_MODULES]) regex_str = regex_modules + '|' + regex_submod module_filter = re.compile(regex_str) try: # Remove .zip from previous run. if os.path.exists(libzip_filename): os.remove(libzip_filename) logger.debug('Adding python files to base_library.zip') # Class zipfile.PyZipFile is not suitable for PyInstaller needs. with zipfile.ZipFile(libzip_filename, mode='w') as zf: zf.debug = 3 for mod in graph.flatten(): if type(mod) in (modulegraph.SourceModule, modulegraph.Package): # Bundling just required modules. if module_filter.match(mod.identifier): st = os.stat(mod.filename) timestamp = int(st.st_mtime) size = st.st_size & 0xFFFFFFFF # Name inside a zip archive. # TODO use .pyo suffix if optimize flag is enabled. if type(mod) is modulegraph.Package: new_name = mod.identifier.replace('.', os.sep) + os.sep + '__init__' + '.pyc' else: new_name = mod.identifier.replace('.', os.sep) + '.pyc' # Write code to a file. # This code is similar to py_compile.compile(). with io.BytesIO() as fc: # Prepare all data in byte stream file-like object. fc.write(BYTECODE_MAGIC) _write_long(fc, timestamp) _write_long(fc, size) marshal.dump(mod.code, fc) zf.writestr(new_name, fc.getvalue()) except Exception as e: logger.error('base_library.zip could not be created!') raise