当前位置: 首页>>代码示例>>Python>>正文


Python zipfile.PyZipFile方法代码示例

本文整理汇总了Python中zipfile.PyZipFile方法的典型用法代码示例。如果您正苦于以下问题:Python zipfile.PyZipFile方法的具体用法?Python zipfile.PyZipFile怎么用?Python zipfile.PyZipFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在模块zipfile的用法示例。

在下文中一共展示了zipfile.PyZipFile方法的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: test_write_pyfile

# 需要导入模块: import zipfile [as 别名]
# 或者: from zipfile import PyZipFile [as 别名]
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()) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:27,代码来源:test_zipfile.py


示例2: test_write_python_directory

# 需要导入模块: import zipfile [as 别名]
# 或者: from zipfile import PyZipFile [as 别名]
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) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:24,代码来源:test_zipfile.py


示例3: test_write_pyfile

# 需要导入模块: import zipfile [as 别名]
# 或者: from zipfile import PyZipFile [as 别名]
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()) 
开发者ID:dxwu,项目名称:BinderFilter,代码行数:26,代码来源:test_zipfile.py


示例4: test_write_python_directory

# 需要导入模块: import zipfile [as 别名]
# 或者: from zipfile import PyZipFile [as 别名]
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) 
开发者ID:dxwu,项目名称:BinderFilter,代码行数:24,代码来源:test_zipfile.py


示例5: load_special_tools

# 需要导入模块: import zipfile [as 别名]
# 或者: from zipfile import PyZipFile [as 别名]
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) 
开发者ID:MOSAIC-UA,项目名称:802.11ah-ns3,代码行数:25,代码来源:Context.py


示例6: load_special_tools

# 需要导入模块: import zipfile [as 别名]
# 或者: from zipfile import PyZipFile [as 别名]
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) 
开发者ID:KTH,项目名称:royal-chaos,代码行数:25,代码来源:Context.py


示例7: test_write_python_directory

# 需要导入模块: import zipfile [as 别名]
# 或者: from zipfile import PyZipFile [as 别名]
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) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:24,代码来源:test_zipfile.py


示例8: test_write_pyfile_bad_syntax

# 需要导入模块: import zipfile [as 别名]
# 或者: from zipfile import PyZipFile [as 别名]
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
                names = zipfp.namelist()
                self.assertIn('mod1.py', names)
                self.assertNotIn('mod1.pyc', names)

        finally:
            rmtree(TESTFN2) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:23,代码来源:test_zipfile.py


示例9: test_write_with_optimization

# 需要导入模块: import zipfile [as 别名]
# 或者: from zipfile import PyZipFile [as 别名]
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) 
开发者ID:IronLanguages,项目名称:ironpython3,代码行数:18,代码来源:test_zipfile.py


示例10: test_write_pyfile_bad_syntax

# 需要导入模块: import zipfile [as 别名]
# 或者: from zipfile import PyZipFile [as 别名]
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) 
开发者ID:IronLanguages,项目名称:ironpython3,代码行数:24,代码来源:test_zipfile.py


示例11: check_zip

# 需要导入模块: import zipfile [as 别名]
# 或者: from zipfile import PyZipFile [as 别名]
def check_zip(test, temp_dir, org_name, artifact_name, version, files, dependencies):
    """
    Checks if the zip exists and the contents of the pom and jar are valid.
    :param temp_dir: Directory where the zip should exist
    :param org_name: organization (group) id of the package
    :param artifact_name: artifact id of package
    :param version: version of release
    :param files: List of entries expected in the jar
    :param dependencies: List of dependencies expected in the pom
    """
    artifact_format = "%s-%s" % (artifact_name, version)
    zip = join(temp_dir, artifact_format + ".zip" )
    test.assertTrue(isfile(zip))
    with zipfile.PyZipFile(zip, 'r') as myzip:
        entries = myzip.namelist()
        test.assertTrue(artifact_format + ".pom" in entries)
        test.assertTrue(artifact_format + ".jar" in entries)
        check_jar(test, myzip.extract(artifact_format + ".jar", temp_dir), files)
        check_pom(test, myzip.open(artifact_format + ".pom"),
                  org_name, artifact_name, version, dependencies) 
开发者ID:databricks,项目名称:spark-package-cmd-tool,代码行数:22,代码来源:tests.py


示例12: setUp

# 需要导入模块: import zipfile [as 别名]
# 或者: from zipfile import PyZipFile [as 别名]
def setUp(self):
        tmp = tempfile.mkstemp(prefix=__name__)
        self.zipfile_path = tmp[1]
        self.zipfile = PyZipFile(self.zipfile_path, 'w')
        self.pj_root = os.path.abspath(
            os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) 
开发者ID:marcy-terui,项目名称:lamvery,代码行数:8,代码来源:build_test.py


示例13: test_build

# 需要导入模块: import zipfile [as 别名]
# 或者: from zipfile import PyZipFile [as 别名]
def test_build(self):
        builder = Builder('test.zip')
        builder._runtime = RUNTIME_NODE_JS
        ok_(hasattr(builder.build(), 'read'))
        with PyZipFile(builder._zippath, 'r', compression=ZIP_DEFLATED) as zipfile:
            ok_('lambda_function.pyc' in zipfile.namelist())
            ok_('.lamvery_secret.json' in zipfile.namelist()) 
开发者ID:marcy-terui,项目名称:lamvery,代码行数:9,代码来源:build_test.py


示例14: test_build_with_single_file

# 需要导入模块: import zipfile [as 别名]
# 或者: from zipfile import PyZipFile [as 别名]
def test_build_with_single_file(self):
        builder = Builder('test.zip', function_filename='lambda_function.py', single_file=True)
        builder.build()
        with PyZipFile(builder._zippath, 'r', compression=ZIP_DEFLATED) as zipfile:
            ok_('lambda_function.py' in zipfile.namelist())
            ok_(not ('.lamvery_secret.json' in zipfile.namelist())) 
开发者ID:marcy-terui,项目名称:lamvery,代码行数:8,代码来源:build_test.py


示例15: build

# 需要导入模块: import zipfile [as 别名]
# 或者: from zipfile import PyZipFile [as 别名]
def build(self):
        if self._clean_build:
            self._prepare_clean_build()

        self._run_hooks(self._hooks.get('pre', []))

        with PyZipFile(self._zippath, 'w', compression=ZIP_DEFLATED) as zipfile:
            for p in self._get_paths():
                if os.path.isdir(p):
                    self._archive_dir(zipfile, p)
                else:
                    self._archive_file(zipfile, p)

            if not self._single_file:
                secret_path = os.path.join(self._tmpdir, lamvery.secret.SECRET_FILE_NAME)
                env_path = os.path.join(self._tmpdir, lamvery.env.ENV_FILE_NAME)
                self._generate_json(secret_path, self._secret)
                self._generate_json(env_path, self._env)
                self._archive_file(zipfile, secret_path)
                self._archive_file(zipfile, env_path)

                if self._runtime == lamvery.config.RUNTIME_NODE_JS:
                    self._archive_dist(zipfile, 'lamvery.js')

        self._run_hooks(self._hooks.get('post', []))

        return open(self._zippath, 'rb') 
开发者ID:marcy-terui,项目名称:lamvery,代码行数:29,代码来源:build.py


示例16: test_write_python_package

# 需要导入模块: import zipfile [as 别名]
# 或者: from zipfile import PyZipFile [as 别名]
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) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:17,代码来源:test_zipfile.py


示例17: test_write_non_pyfile

# 需要导入模块: import zipfile [as 别名]
# 或者: from zipfile import PyZipFile [as 别名]
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) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:8,代码来源:test_zipfile.py


示例18: main

# 需要导入模块: import zipfile [as 别名]
# 或者: from zipfile import PyZipFile [as 别名]
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() 
开发者ID:joxeankoret,项目名称:nightmare,代码行数:16,代码来源:pyzip.py


示例19: test_write_python_package

# 需要导入模块: import zipfile [as 别名]
# 或者: from zipfile import PyZipFile [as 别名]
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) 
开发者ID:dxwu,项目名称:BinderFilter,代码行数:16,代码来源:test_zipfile.py


示例20: test_write_non_pyfile

# 需要导入模块: import zipfile [as 别名]
# 或者: from zipfile import PyZipFile [as 别名]
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) 
开发者ID:dxwu,项目名称:BinderFilter,代码行数:7,代码来源:test_zipfile.py



注:本文中的zipfile.PyZipFile方法示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。