當前位置: 首頁>>代碼示例>>Python>>正文


Python zipfile.PyZipFile方法代碼示例

本文整理匯總了Python中zipfile.PyZipFile方法的典型用法代碼示例。如果您正苦於以下問題:Python zipfile.PyZipFile方法的具體用法?Python zipfile.PyZipFile怎麽用?Python zipfile.PyZipFile使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在zipfile的用法示例。


在下文中一共展示了zipfile.PyZipFile方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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


注:本文中的zipfile.PyZipFile方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。