本文整理汇总了Python中tarfile.TarFile.open方法的典型用法代码示例。如果您正苦于以下问题:Python TarFile.open方法的具体用法?Python TarFile.open怎么用?Python TarFile.open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tarfile.TarFile
的用法示例。
在下文中一共展示了TarFile.open方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: rebuild
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import open [as 别名]
def rebuild(filename, tag=None, format="gz", zonegroups=[], metadata=None):
"""Rebuild the internal timezone info in dateutil/zoneinfo/zoneinfo*tar*
filename is the timezone tarball from ``ftp.iana.org/tz``.
"""
tmpdir = tempfile.mkdtemp()
zonedir = os.path.join(tmpdir, "zoneinfo")
moduledir = os.path.dirname(__file__)
try:
with TarFile.open(filename) as tf:
for name in zonegroups:
tf.extract(name, tmpdir)
filepaths = [os.path.join(tmpdir, n) for n in zonegroups]
try:
check_call(["zic", "-d", zonedir] + filepaths)
except OSError as e:
_print_on_nosuchfile(e)
raise
# write metadata file
with open(os.path.join(zonedir, METADATA_FN), 'w') as f:
json.dump(metadata, f, indent=4, sort_keys=True)
target = os.path.join(moduledir, ZONEFILENAME)
with TarFile.open(target, "w:%s" % format) as tf:
for entry in os.listdir(zonedir):
entrypath = os.path.join(zonedir, entry)
tf.add(entrypath, entry)
finally:
shutil.rmtree(tmpdir)
示例2: install_packages
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import open [as 别名]
def install_packages(self):
self._update_package_with_install_path()
installed_packages = []
for p in self._packages:
if 'install_from' not in p:
print("[dem] Could not find package: {}, version: {}".format(p['name'], p['version']))
else:
if not self._cache.is_package_installed(p['name'], p['version']):
print('[dem] installing {}-{}'.format(p['name'], p['version']))
if p['install_from_ext'] == 'zip':
with ZipFile(p['install_from'], 'r') as archive:
locations = self._extract(archive, p)
elif p['install_from_ext'] == 'tar.gz':
with TarFile.open(p['install_from'], 'r:gz') as archive:
locations = self._extract(archive, p)
elif p['install_from_ext'] == 'tar.bz2':
with TarFile.open(p['install_from'], 'r:bz2') as archive:
locations = self._extract(archive, p)
elif p['install_from_ext'] == 'gz':
with gzip.open(p['install_from'], 'r') as archive:
locations = self._extract(archive, p)
if 'pkg-config' in p:
PkgConfigProcessor.replace_prefix(locations, p['pkg-config'])
else:
print('[dem] {}-{} already installed'.format(p['name'], p['version']))
locations = self._cache.install_locations(p['name'])
package = dict()
package[p['name']] = {'version': p['version'], 'type': 'local', 'install_locations': locations}
installed_packages.append(package)
return installed_packages
示例3: rebuild
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import open [as 别名]
def rebuild(filename, tag=None, format="gz"):
import tempfile, shutil
tmpdir = tempfile.mkdtemp()
zonedir = os.path.join(tmpdir, "zoneinfo")
moduledir = os.path.dirname(__file__)
if tag: tag = "-"+tag
targetname = "zoneinfo%s.tar.%s" % (tag, format)
try:
tf = TarFile.open(filename)
for name in tf.getnames():
if not (name.endswith(".sh") or
name.endswith(".tab") or
name == "leapseconds"):
tf.extract(name, tmpdir)
filepath = os.path.join(tmpdir, name)
os.system("zic -d %s %s" % (zonedir, filepath))
tf.close()
target = os.path.join(moduledir, targetname)
for entry in os.listdir(moduledir):
if entry.startswith("zoneinfo") and ".tar." in entry:
os.unlink(os.path.join(moduledir, entry))
tf = TarFile.open(target, "w:%s" % format)
for entry in os.listdir(zonedir):
entrypath = os.path.join(zonedir, entry)
tf.add(entrypath, entry)
tf.close()
finally:
shutil.rmtree(tmpdir)
示例4: rebuild
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import open [as 别名]
def rebuild(filename, tag=None, format="gz"):
import tempfile, shutil
tmpdir = tempfile.mkdtemp()
zonedir = os.path.join(tmpdir, "zoneinfo")
moduledir = os.path.dirname(__file__)
if tag: tag = "-"+tag
targetname = "zoneinfo%s.tar.%s" % (tag, format)
try:
tf = TarFile.open(filename)
# The "backwards" zone file contains links to other files, so must be
# processed as last
for name in sorted(tf.getnames(),
key=lambda k: k != "backward" and k or "z"):
if not (name.endswith(".sh") or
name.endswith(".tab") or
name == "leapseconds"):
tf.extract(name, tmpdir)
filepath = os.path.join(tmpdir, name)
os.system("zic -d %s %s" % (zonedir, filepath))
tf.close()
target = os.path.join(moduledir, targetname)
for entry in os.listdir(moduledir):
if entry.startswith("zoneinfo") and ".tar." in entry:
os.unlink(os.path.join(moduledir, entry))
tf = TarFile.open(target, "w:%s" % format)
for entry in os.listdir(zonedir):
entrypath = os.path.join(zonedir, entry)
tf.add(entrypath, entry)
tf.close()
finally:
shutil.rmtree(tmpdir)
示例5: __enter__
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import open [as 别名]
def __enter__(self):
if self.tf is not None:
raise ValueError('Cannot re-enter')
if '://' in self.web_archive:
info('Downloading from {0}'.format(self.web_archive))
dl = requests.get(self.web_archive)
self.tf = TarFile.open(path(self.web_archive).basename(),
'r:*', fileobj=io.BytesIO(dl.content))
else:
self.tf = TarFile.open(self.web_archive)
return self.tf
示例6: extract_package
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import open [as 别名]
def extract_package(package_name, path, logger):
try:
if ".tar" in package_name:
TarFile.open(package_name).extractall(path)
elif ".zip" in package_name:
ZipFile(package_name).extractall(path)
else:
raise FileTypeError("It's not a TAR or ZIP archive.")
except Exception as err:
register_exception(alert_admin=True,
prefix="Elsevier error extracting package.")
logger.error("Error extraction package file: %s %s"
% (path, err))
示例7: test_windows_with_mocks
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import open [as 别名]
def test_windows_with_mocks(self):
from infi.logs_collector.items import windows
result, archive_path = logs_collector.run("test", windows(), None, None)
self.assertTrue(path.exists(archive_path))
self.assertTrue(archive_path.endswith(".tar.gz"))
archive = TarFile.open(archive_path, "r:gz")
示例8: open_archive
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import open [as 别名]
def open_archive(path):
from tarfile import TarFile
archive = TarFile.open(name=path, mode="w:gz", bufsize=16*1024)
try:
yield archive
finally:
archive.close()
示例9: download
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import open [as 别名]
def download(archive_url, archive_name, extract_to_path='.', require_basic_auth=False):
# download the file to extract_to_path
if not path.exists(extract_to_path):
makedirs(extract_to_path)
archive_path = path.join(extract_to_path, archive_name)
stdout.write("Downloading '%s' to '%s'...\n" % (archive_url, archive_path))
source = urlopen(archive_url) if not require_basic_auth else teamcityurlopen(archive_url)
with open(archive_path, "wb") as destination:
more = True
while more:
data = source.read(8192)
if data:
destination.write(data)
else:
more = False
if archive_name.endswith('.zip'):
stdout.write("Unzipping '%s' to '%s'...\n" % (archive_path, extract_to_path))
zip_ref = ZipFile(archive_path, 'r')
zip_ref.extractall(extract_to_path)
unzip_folder = zip_ref.namelist()[0]
zip_ref.close()
return unzip_folder
elif archive_name.endswith('.tar.gz'):
stdout.write("Unarchiving '%s' to '%s'...\n" % (archive_path, extract_to_path))
tar_ref = TarFile.open(archive_path)
tar_ref.extractall(extract_to_path)
untar_folder=tar_ref.getnames()[0]
tar_ref.close()
return untar_folder
示例10: _verifyTarballEntry
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import open [as 别名]
def _verifyTarballEntry(self, fileish, entry_name, data):
fileish.seek(0)
tarfile = TarFile.open('foo.tar.gz', fileobj=fileish, mode='r:gz')
extract = tarfile.extractfile(entry_name)
found = extract.read()
self.assertEqual(found, data)
示例11: fetch
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import open [as 别名]
def fetch(self):
"""
Download the specififc workflow bundle.
"""
self.path = self.path or ""
if not self.path or (os.path.exists(self.path) and
os.path.samefile(self.path, os.getcwd())):
self.path = os.path.join(self.path, "%[email protected]%s" % (self.name,
self.version))
if os.path.exists(self.path):
if self.force:
shutil.rmtree(self.path)
else:
raise ValueError("Directory %s already exists and -f/--force "
"option was not specified" % self.path)
os.mkdir(self.path)
url = "%s%s?%s" % (
self.base, root.common.forge.fetch_name,
urlencode((("name", self.name), ("version", self.version))))
self.debug("Requesting %s", url)
fn = wget.download(url, self.path)
print("")
self.debug("Downloaded %s", fn)
with TarFile.open(fn) as tar:
tar.extractall(self.path)
os.remove(fn)
self.info("Put the following files into %s:\n %s", self.path,
"\n ".join(sorted(os.listdir(self.path))))
metadata = self._parse_metadata(self.path)
self._check_deps(metadata)
return self.path, metadata
示例12: handle
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import open [as 别名]
def handle():
try:
if request.method == 'GET': return _as_text( app.config[ 'TAR_DIGEST' ] )
try:
signature = request.form[ 'signature' ]
except KeyError:
uid = None
else:
uid = extract_uid( signature )
if not uid:
EVENTS_LOG.info( 'Unauthorized: {0}@{1}'.format( uid, request.remote_addr ) )
return _as_text( '# {0}\n'.format( _( 'Invalid or absent signature!' ) ), 401 )
if 'tar' in request.form: # this is an upload
data = decodestring( request.form[ 'tar' ] )
dest = join( app.config[ 'UPLOAD_DIR' ], uid, str( int( time() * 1000 ) ) + '.tar' )
with open( dest, 'w' ) as f: f.write( data )
tf = TarFile.open( dest, mode = 'r' )
names = tf.getnames()
tf.close()
EVENTS_LOG.info( 'Upload: {0}@{1}'.format( uid, request.remote_addr ) )
return _as_text( '\n'.join( sorted( names ) ) )
else: # this is a download
EVENTS_LOG.info( 'Download: {0}@{1}'.format( uid, request.remote_addr ) )
return _as_text( app.config[ 'TAR_DATA' ] )
except:
if app.debug:
raise
else:
app.logger.exception( '' )
return _as_text( '# {0}\n'.format( _( 'An unexpected server error occurred!' ) ), 500 )
示例13: retrieveCoreInfo
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import open [as 别名]
def retrieveCoreInfo():
DIR_PREFIX = 'libretro-super-master/dist/info/'
res = urllib2.urlopen('https://github.com/libretro/libretro-super/archive/master.tar.gz')
archive = StringIO(res.read())
tf = TarFile.open(mode='r:gz', fileobj=archive)
# filter files and sort them
files = [f for f in tf.getmembers() if f.name.startswith(DIR_PREFIX)]
files = sorted(files, key=lambda m: m.name.split(DIR_PREFIX)[1])
def infoToDict( files=[]):
output = {'cores': OrderedDict()}
for f in files:
info = OrderedDict()
for l in tf.extractfile(f).readlines():
s = shlex.shlex(l)
s.quotes = '"'
op = []
try:
while True:
t = s.get_token()
if t == s.eof:
break
op.append(t)
# 'var' '=' 'value'
if len(op) == 3:
assert op[1] in ('=', ':')
# For: 'some:var = value' syntax
# Merge 'some:var' into 'somevar' (?)
if op[1] == ':':
op = [''.join(op)]
continue
# Strip quotes
if op[2][0] == '"' and op[2][-1] == '"':
op[2] = op[2][1:-1]
# Convert string to boolean
if op[2] in ('true', 'True'):
op[2] = True
elif op[2] in ('false', 'False'):
op[2] = False
# Decode utf-8 into unicode
if isinstance(op[2], str):
op[2] = op[2].decode('utf-8')
info[op[0]] = op[2]
break
except (ValueError, AssertionError) as e:
print("Could not process file %s (%s) " % (f.name, e), file=sys.stderr)
continue
output['cores'][f.name.split(DIR_PREFIX)[1].split('.info')[0]] = info
return output
return infoToDict(files)
示例14: push
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import open [as 别名]
def push(self, local, remote):
if os.path.isdir(local):
if not os.path.exists(self.cache): os.mkdir(self.cache)
cached_local = local.replace('/', '_')[1:] + '.tar.gz'
cached_file = os.path.join(self.cache, cached_local)
if not os.path.exists(cached_file):
log.debug('Creating cache file %s', cached_file)
tar = TarFile.open(cached_file, 'w:gz')
tar.add(os.path.realpath(local), arcname='.')
log.debug('Done...')
tar.close()
with open(cached_file,'r') as f:
self.run('mkdir -p {0}'.format(remote))
return self.run(
'tar zxf - -C {0}'.format(remote),
in_=ProcessFile(f)
)
else:
with open(local) as f:
return self.run(
'cat > {0}'.format(remote),
in_=ProcessFile(f)
)
示例15: _get_bin_for_release
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import open [as 别名]
def _get_bin_for_release(self, release_file_path: Path):
if not release_file_path.exists():
raise ValueError(f'Release file {release_file_path} not found')
if release_file_path.suffix == '.gz':
opener = TarFile.open(release_file_path, 'r:*')
else:
opener = ZipFile(release_file_path, 'r')
with opener as archive:
if release_file_path.suffix == '.gz':
contents = archive.getnames()
else:
contents = archive.namelist()
if len(contents) != 1:
raise ValueError(
f'Release archive has unexpected content. '
f'Expected 1 file, found {len(contents)}: {", ".join(contents)}',
)
bin_file_path = self._bin_path.joinpath(contents[0])
if not bin_file_path.exists():
log.debug(
'Extracting Raiden binary',
release_file_name=release_file_path.name,
bin_file_name=bin_file_path.name,
)
archive.extract(contents[0], self._bin_path)
bin_file_path.chmod(0o770)
return bin_file_path