本文整理匯總了Python中zipfile.PyZipFile類的典型用法代碼示例。如果您正苦於以下問題:Python PyZipFile類的具體用法?Python PyZipFile怎麽用?Python PyZipFile使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了PyZipFile類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: load_special_tools
def load_special_tools(self, var, ban=[]):
"""
Loads third-party extensions modules for certain programming languages
by trying to list certain files in the extras/ directory. This method
is typically called once for a programming language group, see for
example :py:mod:`waflib.Tools.compiler_c`
:param var: glob expression, for example 'cxx\_\*.py'
:type var: string
:param ban: list of exact file names to exclude
:type ban: list of string
"""
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)
示例2: get_module_as_zip_from_module_directory
def get_module_as_zip_from_module_directory(module_directory, b64enc=True, src=True):
"""Compress a module directory
@param module_directory: The module directory
@param base64enc: if True the function will encode the zip file with base64
@param src: Integrate the source files
@return: a stream to store in a file-like object
"""
RE_exclude = re.compile('(?:^\..+\.swp$)|(?:\.py[oc]$)|(?:\.bak$)|(?:\.~.~$)', re.I)
def _zippy(archive, path, src=True):
path = os.path.abspath(path)
base = os.path.basename(path)
for f in tools.osutil.listdir(path, True):
bf = os.path.basename(f)
if not RE_exclude.search(bf) and (src or bf == '__terp__.py' or not bf.endswith('.py')):
archive.write(os.path.join(path, f), os.path.join(base, f))
archname = StringIO()
archive = PyZipFile(archname, "w", ZIP_DEFLATED)
archive.writepy(module_directory)
_zippy(archive, module_directory, src=src)
archive.close()
val = archname.getvalue()
archname.close()
if b64enc:
val = base64.encodestring(val)
return val
示例3: GenerateMission
def GenerateMission(filename):
# Create XML parser
parser = make_parser()
generator = MissionGenerator()
parser.setContentHandler(generator)
# Parse KMZ file
kmz = PyZipFile(filename, 'r')
kml = kmz.open('doc.kml', 'r')
parser.parse(kml)
kmz.close()
# Arrange the XML items into useful variables
items = {}
items['base_location'] = ParseCoordinates(generator.mapping, 'Base')
items['landing_site'] = ParseCoordinates(generator.mapping, 'Fake reported location')
items['path'] = ParseCoordinates(generator.mapping, 'Sample Nominal Path')
items['return_path']= items['path'].reverse()
items['base_geofence'] = GenerateGeofences(ParseCoordinates(generator.mapping, 'Base geofence'))
items['landing_site_geofence'] = GenerateGeofences(ParseCoordinates(generator.mapping, 'Sample remote landing site'))
items['mission_geofence'] = GenerateGeofences(ParseCoordinates(generator.mapping, 'Sample full geofence'))
return items
示例4: build_zip
def build_zip(dest):
print "Writing", dest
from zipfile import PyZipFile
f = PyZipFile(dest, "w")
f.writepy("src/singleshot")
f.writepy("lib")
f.writepy("lib/simpletal")
f.close()
示例5: build_zip
def build_zip(module_dir):
# This can fail at writepy if there is something wrong with the files
# in xframes. Go ahead anyway, but things will probably fail if this job is
# distributed
try:
tf = NamedTemporaryFile(suffix=".zip", delete=False)
z = PyZipFile(tf, "w")
z.writepy(module_dir)
z.close()
return tf.name
except:
logging.warn("Zip file distribution failed -- workers will not get xframes code.")
logging.warn("Check for unexpected files in xframes directory.")
return None
示例6: build_zip
def build_zip():
if 'XPATTERNS_HOME' not in os.environ:
return None
# This can fail at writepy if there is something wrong with the files
# in xpatterns. Go ahead anyway, but things will probably fail of this job is
# distributed
try:
tf = NamedTemporaryFile(suffix='.zip', delete=False)
z = PyZipFile(tf, 'w')
z.writepy(os.path.join(os.environ['XPATTERNS_HOME'], 'xpatterns'))
z.close()
return tf.name
except:
print 'Zip file distribution failed -- workers will not get xpatterns code.'
print 'Check for unexpected files in XPATTERNS_HOME/xpatterns.'
return None
示例7: get_module_meta_path
def get_module_meta_path(module_description):
"""Returns the finder to be appended to sys.meta_path
module_description is a tuple of 2 elements:
format: either 'zip', 'tar', 'tar:gz', 'tar:bz' or a string to be used as module name
content: a base64 encoded string of a zip archive, a tar(gz/bz2) archive or a plain python module
"""
raw_format = module_description[0].split(':')
if raw_format[0] in ('zip', 'tar'):
f = BytesIO()
f.write(decodestring(module_description[1]))
f.seek(0)
if raw_format[0] == 'zip':
zipf = PyZipFile(f)
module_dict = dict((splitext(z.filename)[0].replace('/', '.'), zipf.open(z.filename).read()) for z in zipf.infolist() if splitext(z.filename)[1] == ".py")
elif raw_format[0] == 'tar':
compression = raw_format[1] if len(raw_format) > 1 else ''
tarf = taropen(fileobj=f, mode="r:" + compression)
module_dict = dict((splitext(t.name)[0].replace('/', '.'), tarf.extractfile(t.name).read()) for t in tarf.getmembers() if splitext(t.name)[1] == ".py")
else:
module_dict = {module_description[0]: decodestring(module_description[1])}
return Finder(module_dict)
示例8: load_special_tools
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)
示例9: get_zip_from_directory
def get_zip_from_directory(directory, b64enc=True):
RE_exclude = re.compile('(?:^\..+\.swp$)|(?:\.py[oc]$)|(?:\.bak$)|(?:\.~.~$)', re.I)
def _zippy(archive, path):
path = os.path.abspath(path)
base = os.path.basename(path)
for f in tools.osutil.listdir(path, True):
bf = os.path.basename(f)
if not RE_exclude.search(bf):
archive.write(os.path.join(path, f), os.path.join(base, f))
archname = StringIO()
archive = PyZipFile(archname, "w", ZIP_DEFLATED)
archive.writepy(directory)
_zippy(archive, directory)
archive.close()
val = archname.getvalue()
archname.close()
if b64enc:
val = base64.encodestring(val)
return val
示例10: zip_directory
def zip_directory(directory, b64enc=True, src=True):
"""Compress a directory
@param directory: The directory to compress
@param base64enc: if True the function will encode the zip file with base64
@param src: Integrate the source files
@return: a string containing the zip file
"""
RE_exclude = re.compile('(?:^\..+\.swp$)|(?:\.py[oc]$)|(?:\.bak$)|(?:\.~.~$)', re.I)
def _zippy(archive, path, src=True):
path = os.path.abspath(path)
base = os.path.basename(path)
for f in tools.osutil.listdir(path, True):
bf = os.path.basename(f)
if not RE_exclude.search(bf) and (src or bf in ('__openerp__.py', '__terp__.py') or not bf.endswith('.py')):
archive.write(os.path.join(path, f), os.path.join(base, f))
archname = StringIO()
archive = PyZipFile(archname, "w", ZIP_DEFLATED)
# for Python 2.5, ZipFile.write() still expects 8-bit strings (2.6 converts to utf-8)
directory = tools.ustr(directory).encode('utf-8')
archive.writepy(directory)
_zippy(archive, directory, src=src)
archive.close()
archive_data = archname.getvalue()
archname.close()
if b64enc:
return base64.encodestring(archive_data)
return archive_data
示例11: setUp
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__))))
示例12: ArchiveTestCase
class ArchiveTestCase(TestCase):
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__))))
def tearDown(self):
os.remove(self.zipfile_path)
def test_create_zipfile(self):
archive = Archive('test.zip')
ok_(hasattr(archive.create_zipfile(), 'read'))
with PyZipFile(archive._zippath, 'r', compression=ZIP_DEFLATED) as zipfile:
ok_('lambda_function.pyc' in zipfile.namelist())
ok_('.lamvery_secret.json' in zipfile.namelist())
def test_create_zipfile_with_single_file(self):
archive = Archive('test.zip', function_filename='lambda_function.py', single_file=True)
archive.create_zipfile()
with PyZipFile(archive._zippath, 'r', compression=ZIP_DEFLATED) as zipfile:
ok_('lambda_function.py' in zipfile.namelist())
ok_(not ('.lamvery_secret.json' in zipfile.namelist()))
def test_archive_dir(self):
archive = Archive('test.zip')
archive._archive_dir(self.zipfile, self.pj_root)
ok_(isinstance(self.zipfile.getinfo('setup.pyc'), zipfile.ZipInfo))
def test_archive_file(self):
archive = Archive('test.zip')
archive._archive_file(
self.zipfile, os.path.join(self.pj_root, 'setup.py'))
ok_(isinstance(self.zipfile.getinfo('setup.pyc'), zipfile.ZipInfo))
archive._archive_file(
self.zipfile, os.path.join(self.pj_root, 'README.md'))
ok_(isinstance(self.zipfile.getinfo('README.md'), zipfile.ZipInfo))
@raises(KeyError)
def test_archive_single_file_key_error(self):
self._single_file = True
archive = Archive('test.zip', single_file=True)
archive._archive_file(
self.zipfile, os.path.join(self.pj_root, 'setup.py'))
ok_(isinstance(self.zipfile.getinfo('setup.pyc'), zipfile.ZipInfo))
def test_archive_single_file(self):
self._single_file = True
archive = Archive('test.zip', single_file=True)
archive._archive_file(
self.zipfile, os.path.join(self.pj_root, 'setup.py'))
ok_(isinstance(self.zipfile.getinfo('setup.py'), zipfile.ZipInfo))
def test_is_exclude(self):
archive = Archive('test.zip', exclude=['^\.lamvery\.yml$'])
eq_(archive.is_exclude('foo.txt'), False)
eq_(archive.is_exclude('.lamvery.yml'), True)
def test_is_exclude_file(self):
archive = Archive('test.zip')
eq_(archive.is_exclude_file('test.zip'), True)
eq_(archive.is_exclude_file('foo.txt'), False)
archive.is_exclude = Mock(return_value=True)
eq_(archive.is_exclude_file('foo.txt'), True)
def test_is_exclude_dir(self):
archive = Archive('test.zip')
eq_(archive.is_exclude_dir('.git'), True)
eq_(archive.is_exclude_dir('foo'), False)
archive.is_exclude = Mock(return_value=True)
eq_(archive.is_exclude_file('foo'), True)
def test_is_source_file(self):
archive = Archive('test.zip')
eq_(archive.is_source_file('foo.py'), True)
eq_(archive.is_source_file('foo.pyc'), True)
eq_(archive.is_source_file('foo.php'), False)
def test_get_paths(self):
archive = Archive('test.zip')
paths = archive._get_paths()
ok_(os.path.join(self.pj_root, 'lamvery') in paths)
archive = Archive('test.zip', no_libs=True)
paths = archive._get_paths()
ok_(os.path.join(self.pj_root, 'lamvery') in paths)
ok_(os.path.join(self.pj_root, 'lambda_function.py') in paths)
ok_(os.path.join(self.pj_root, 'lambda_function.pyc') in paths)
ok_(os.path.join(self.pj_root, '.lamvery.yml') in paths)
archive = Archive('test.zip', function_filename='test.py', single_file=True)
paths = archive._get_paths()
ok_(os.path.join(self.pj_root, 'test.py') in paths)
ok_(not os.path.join(self.pj_root, 'lambda_function.pyc') in paths)
ok_(not os.path.join(self.pj_root, '.lamvery.yml') in paths)
del os.environ['VIRTUAL_ENV']
archive = Archive('test.zip')
#.........這裏部分代碼省略.........
示例13: PyZipFile
#!/usr/bin/env python
#
# Packs Python standard library into zip file python$(ver).zip
#
import os, os.path, shutil, sys
from zipfile import PyZipFile
name = "python%i%i.zip" % (sys.version_info[0], sys.version_info[1])
print "creating %s..." % name
# delete tests, we don't need them:
for root, dirs, files in os.walk("Lib", topdown=False):
if "test" in dirs:
shutil.rmtree(os.path.join(root, "test"))
# pack Lib to a zipfile:
zip = PyZipFile(name, mode="w")
for f in os.listdir("Lib"):
fn = os.path.join("Lib", f)
if os.path.isdir(fn) or fn.endswith(".py"):
zip.writepy(fn)
else:
print "warning: ignoring file %s" % f
zip.close()
示例14: BuilderTestCase
class BuilderTestCase(TestCase):
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__))))
def tearDown(self):
os.remove(self.zipfile_path)
try:
os.remove('test.json')
except:
pass
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())
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()))
def test_generate_json(self):
builder = Builder('test.zip')
builder._generate_json(
JSON_FILE_NAME, {'foo': 2, 'bar': 3})
data = json.load(open(JSON_FILE_NAME, 'r'))
eq_(data.get('foo'), 2)
def test_archive_dir(self):
builder = Builder('test.zip')
builder._archive_dir(self.zipfile, self.pj_root)
ok_(isinstance(self.zipfile.getinfo('setup.pyc'), zipfile.ZipInfo))
def test_archive_file(self):
builder = Builder('test.zip')
builder._archive_file(
self.zipfile, os.path.join(self.pj_root, 'setup.py'))
ok_(isinstance(self.zipfile.getinfo('setup.pyc'), zipfile.ZipInfo))
builder._archive_file(
self.zipfile, os.path.join(self.pj_root, 'README.md'))
ok_(isinstance(self.zipfile.getinfo('README.md'), zipfile.ZipInfo))
def test_archive_dist(self):
builder = Builder('test.zip')
builder._archive_dist(self.zipfile, 'lamvery.js')
ok_(isinstance(self.zipfile.getinfo('lamvery.js'), zipfile.ZipInfo))
@raises(KeyError)
def test_archive_single_file_key_error(self):
self._single_file = True
builder = Builder('test.zip', single_file=True)
builder._archive_file(
self.zipfile, os.path.join(self.pj_root, 'setup.py'))
ok_(isinstance(self.zipfile.getinfo('setup.pyc'), zipfile.ZipInfo))
def test_archive_single_file(self):
self._single_file = True
builder = Builder('test.zip', single_file=True)
builder._archive_file(
self.zipfile, os.path.join(self.pj_root, 'setup.py'))
ok_(isinstance(self.zipfile.getinfo('setup.py'), zipfile.ZipInfo))
def test_is_exclude(self):
builder = Builder('test.zip', exclude=['^\.lamvery\.yml$'])
eq_(builder.is_exclude('foo.txt'), False)
eq_(builder.is_exclude('.lamvery.yml'), True)
def test_is_exclude_file(self):
builder = Builder('test.zip')
eq_(builder.is_exclude_file('test.zip'), True)
eq_(builder.is_exclude_file('foo.txt'), False)
builder.is_exclude = Mock(return_value=True)
eq_(builder.is_exclude_file('foo.txt'), True)
def test_is_exclude_dir(self):
builder = Builder('test.zip')
eq_(builder.is_exclude_dir('.git'), True)
eq_(builder.is_exclude_dir('foo'), False)
builder.is_exclude = Mock(return_value=True)
eq_(builder.is_exclude_file('foo'), True)
def test_is_source_file(self):
builder = Builder('test.zip')
eq_(builder.is_source_file('foo.py'), True)
eq_(builder.is_source_file('foo.pyc'), True)
eq_(builder.is_source_file('foo.php'), False)
def test_get_paths(self):
builder = Builder('test.zip')
paths = builder._get_paths()
#.........這裏部分代碼省略.........
示例15: EPub
class EPub(object):
epub_file = None
epub_zip = None
title = None
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 table_of_contents(self):
basedir = None
# find opf file
soup = BeautifulSoup(self.epub_zip.read('META-INF/container.xml'))
opf = dict(soup.find('rootfile').attrs)['full-path']
basedir = os.path.dirname(opf)
if basedir:
basedir = '{0}/'.format(basedir)
soup = BeautifulSoup(self.epub_zip.read(opf), "html.parser")
# title
self.title = soup.find('dc:title').text
# all files, not in order
x, ncx = {}, None
for item in soup.find('manifest').findAll('item'):
d = dict(item.attrs)
x[d['id']] = '{0}{1}'.format(basedir, d['href'])
if d['media-type'] == 'application/x-dtbncx+xml':
ncx = '{0}{1}'.format(basedir, d['href'])
book_sections = []
for item in soup.find('spine').findAll('itemref'):
book_sections.append(x[dict(item.attrs)['idref']])
nav_labels = {}
if ncx:
soup = BeautifulSoup(self.epub_zip.read(ncx), "html.parser")
for navpoint in soup('navpoint'):
k = navpoint.content.get('src', None)
# strip off any anchor text
k = k.split('#')[0]
if k:
nav_labels[k] = navpoint.navlabel.text
toc = []
for section in book_sections:
if section in nav_labels:
toc.append(nav_labels[section])
else:
toc.append(section)
return toc
def chapter_to_text(self, chapter):
soup = BeautifulSoup(self.epub_zip.read(chapter), "html.parser")
return soup.find('body').getText()
def book_parts(self):
toc = self.table_of_contents()
book = []
for chapter in toc:
book.append((chapter, self.chapter_to_text(chapter)))
return book