本文整理匯總了Python中tarfile.TarError方法的典型用法代碼示例。如果您正苦於以下問題:Python tarfile.TarError方法的具體用法?Python tarfile.TarError怎麽用?Python tarfile.TarError使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類tarfile
的用法示例。
在下文中一共展示了tarfile.TarError方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: GET
# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarError [as 別名]
def GET(self):
try:
data = param_input()
response = get(str(data.file_location), cert=config_get('webui', 'usercert'), verify=False)
if not response.ok:
response.raise_for_status()
cont = response.content
file_like_object = BytesIO(cont)
tar = open(mode='r:gz', fileobj=file_like_object)
jsonResponse = {}
for member in tar.getmembers():
jsonResponse[member.name] = member.size
header('Content-Type', 'application/json')
return dumps(jsonResponse)
except ConnectionError as err:
raise generate_http_error(503, str(type(err)), str(err))
except TarError as err:
raise generate_http_error(415, str(type(err)), str(err))
except IOError as err:
raise generate_http_error(422, str(type(err)), str(err))
except Exception as err:
raise generate_http_error(500, str(type(err)), str(err))
示例2: _uploadPackage
# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarError [as 別名]
def _uploadPackage(self, buildId, suffix, audit, content):
# Set default signal handler so that KeyboardInterrupt is raised.
# Needed to gracefully handle ctrl+c.
signal.signal(signal.SIGINT, signal.default_int_handler)
try:
with self._openUploadFile(buildId, suffix) as (name, fileobj):
pax = { 'bob-archive-vsn' : "1" }
with gzip.open(name or fileobj, 'wb', 6) as gzf:
with tarfile.open(name, "w", fileobj=gzf,
format=tarfile.PAX_FORMAT, pax_headers=pax) as tar:
tar.add(audit, "meta/" + os.path.basename(audit))
tar.add(content, arcname="content")
except ArtifactExistsError:
return ("skipped ({} exists in archive)".format(content), SKIPPED)
except (ArtifactUploadError, tarfile.TarError, OSError) as e:
if self.__ignoreErrors:
return ("error ("+str(e)+")", ERROR)
else:
raise BuildError("Cannot upload artifact: " + str(e))
finally:
# Restore signals to default so that Ctrl+C kills process. Needed
# to prevent ugly backtraces when user presses ctrl+c.
signal.signal(signal.SIGINT, signal.SIG_DFL)
return ("ok", EXECUTED)
示例3: uncompress_content
# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarError [as 別名]
def uncompress_content(archive_content, to_directory):
if zipfile.is_zipfile(io.BytesIO(archive_content)):
with ZipFile(io.BytesIO(archive_content)) as zf:
# Check no path traversal
filenames = [os.path.join(to_directory, filename)
for filename in zf.namelist()]
raise_if_path_traversal(filenames, to_directory)
zf.extractall(to_directory)
else:
try:
with tarfile.open(fileobj=io.BytesIO(archive_content)) as tf:
# Check no path traversal
filenames = [os.path.join(to_directory, filename)
for filename in tf.getnames()]
raise_if_path_traversal(filenames, to_directory)
tf.extractall(to_directory)
except tarfile.TarError:
raise Exception('Archive must be zip or tar.*')
示例4: get
# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarError [as 別名]
def get(uri, cache_dir=None):
"""Entry method for download method
:param str uri: A formatted remote URL of a file
:param str: Absolute path to the downloaded content
:param str cache_dir: path to save downloaded files
"""
user_base_dir = cache_dir or CONF.remote.cache_dir
if user_base_dir:
try:
temp = tempfile.TemporaryFile(dir=os.path.abspath(user_base_dir))
temp.close()
except OSError:
LOG.error("Failed to write remote files to: %s",
os.path.abspath(user_base_dir))
exit(1)
abs_path = download(uri, os.path.abspath(user_base_dir))
else:
abs_path = download(uri)
try:
return extract_tar(abs_path)
except (tarfile.TarError, Exception):
msg = _("Not a gz file, returning abs_path")
LOG.debug(msg)
return abs_path
示例5: unpack
# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarError [as 別名]
def unpack(self, file_path, temp_dir):
try:
with tarfile.open(name=file_path, mode='r:*') as tf:
names = tf.getnames()
encoding = self.detect_list_encoding(names,
default=tf.encoding)
log.debug('Detected filename encoding: %s', encoding)
for name in names:
try:
fh = tf.extractfile(name)
self.extract_member(temp_dir, name, fh,
encoding=encoding)
except Exception as ex:
# TODO: should this be a fatal error?
log.debug("Failed to unpack [%r]: %s", name, ex)
except (tarfile.TarError, IOError, EOFError) as err:
raise ProcessingException('Invalid Tar file: %s' % err)
示例6: main_download
# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarError [as 別名]
def main_download(self):
"""HTTP download for main part of the software to self.archive_path.
"""
if self.offline:
raise IOError("%s not found, and offline "
"mode requested" % self.archive_path)
url = self.sources[main_software][1]
logger.info("Downloading %s ..." % url)
try:
msg = urlretrieve(url, self.archive_path)
if get_content_type(msg[1]) == 'text/html':
os.unlink(self.archive_path)
raise LookupError(
'Wanted version %r not found on server (tried %s)' % (
self.version_wanted, url))
except (tarfile.TarError, IOError):
# GR: ContentTooShortError subclasses IOError
os.unlink(self.archive_path)
raise IOError('The archive does not seem valid: ' +
repr(self.archive_path))
示例7: _extract_archive
# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarError [as 別名]
def _extract_archive(file_path, extract_path="."):
"""Extracts an archive.
"""
for archive_type in ["zip", "tar"]:
if archive_type == "zip":
open_fn = zipfile.ZipFile
is_match_fn = zipfile.is_zipfile
elif archive_type == "tar":
open_fn = tarfile.open
is_match_fn = tarfile.is_tarfile
if is_match_fn(file_path):
with open_fn(file_path) as archive:
try:
archive.extractall(extract_path)
except (tarfile.TarError, RuntimeError, KeyboardInterrupt):
if os.path.exists(extract_path):
if os.path.isfile(extract_path):
os.remove(extract_path)
else:
shutil.rmtree(extract_path)
raise
示例8: copy_revision
# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarError [as 別名]
def copy_revision(self, id_or_num: t.Union[int, str], sub_dir: str, dest_dirs: t.List[str]):
typecheck_locals(id_or_num=self.id_type, dest_dirs=List(Str())|Str())
if isinstance(dest_dirs, str):
dest_dirs = [dest_dirs]
if id_or_num == -1 or id_or_num == "HEAD":
self._copy_dir(sub_dir, dest_dirs)
sub_dir = os.path.join(self.base_path, sub_dir)
tar_file = os.path.abspath(os.path.join(Settings()["tmp_dir"], "tmp.tar"))
cmd = "git archive --format tar --output {} {}".format(tar_file, self._commit_number_to_id(id_or_num))
self._exec_command(cmd)
try:
with tarfile.open(tar_file) as tar:
for dest in dest_dirs:
if sub_dir == ".":
tar.extractall(os.path.abspath(dest))
else:
subdir_and_files = [
tarinfo for tarinfo in tar.getmembers() if tarinfo.name.startswith(sub_dir + "/") or tarinfo.name is sub_dir
]
tar.extractall(members=subdir_and_files, path=os.path.abspath(dest))
except tarfile.TarError as err:
os.remove(tar_file)
raise VCSError(str(err))
os.remove(tar_file)
示例9: _unpack_tarfile
# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarError [as 別名]
def _unpack_tarfile(filename, extract_dir):
"""Unpack tar/tar.gz/tar.bz2 `filename` to `extract_dir`
"""
try:
tarobj = tarfile.open(filename)
except tarfile.TarError:
raise ReadError(
"%s is not a compressed or uncompressed tar file" % filename)
try:
tarobj.extractall(extract_dir)
finally:
tarobj.close()
示例10: unpack_tarfile
# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarError [as 別名]
def unpack_tarfile(filename, extract_dir, progress_filter=default_filter):
"""Unpack tar/tar.gz/tar.bz2 `filename` to `extract_dir`
Raises ``UnrecognizedFormat`` if `filename` is not a tarfile (as determined
by ``tarfile.open()``). See ``unpack_archive()`` for an explanation
of the `progress_filter` argument.
"""
try:
tarobj = tarfile.open(filename)
except tarfile.TarError:
raise UnrecognizedFormat(
"%s is not a compressed or uncompressed tar file" % (filename,)
)
try:
tarobj.chown = lambda *args: None # don't do any chowning!
for member in tarobj:
name = member.name
# don't extract absolute paths or ones with .. in them
if not name.startswith('/') and '..' not in name.split('/'):
prelim_dst = os.path.join(extract_dir, *name.split('/'))
# resolve any links and to extract the link targets as normal files
while member is not None and (member.islnk() or member.issym()):
linkpath = member.linkname
if member.issym():
linkpath = posixpath.join(posixpath.dirname(member.name), linkpath)
linkpath = posixpath.normpath(linkpath)
member = tarobj._getmember(linkpath)
if member is not None and (member.isfile() or member.isdir()):
final_dst = progress_filter(name, prelim_dst)
if final_dst:
if final_dst.endswith(os.sep):
final_dst = final_dst[:-1]
try:
tarobj._extract_member(member, final_dst) # XXX Ugh
except tarfile.ExtractError:
pass # chown/chmod/mkfifo/mknode/makedev failed
return True
finally:
tarobj.close()
示例11: _downloadPackage
# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarError [as 別名]
def _downloadPackage(self, buildId, suffix, audit, content):
# Set default signal handler so that KeyboardInterrupt is raised.
# Needed to gracefully handle ctrl+c.
signal.signal(signal.SIGINT, signal.default_int_handler)
try:
with self._openDownloadFile(buildId, suffix) as (name, fileobj):
with tarfile.open(name, "r|*", fileobj=fileobj, errorlevel=1) as tar:
removePath(audit)
removePath(content)
os.makedirs(content)
self.__extractPackage(tar, audit, content)
return (True, None, None)
except ArtifactNotFoundError:
return (False, "not found", WARNING)
except ArtifactDownloadError as e:
return (False, e.reason, WARNING)
except BuildError as e:
raise
except OSError as e:
raise BuildError("Cannot download artifact: " + str(e))
except tarfile.TarError as e:
raise BuildError("Error extracting binary artifact: " + str(e))
finally:
# Restore signals to default so that Ctrl+C kills process. Needed
# to prevent ugly backtraces when user presses ctrl+c.
signal.signal(signal.SIGINT, signal.SIG_DFL)
示例12: _unpack_tarfile
# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarError [as 別名]
def _unpack_tarfile(filename, extract_dir):
"""Unpack tar/tar.gz/tar.bz2/tar.xz `filename` to `extract_dir`
"""
try:
tarobj = tarfile.open(filename)
except tarfile.TarError:
raise ReadError(
"%s is not a compressed or uncompressed tar file" % filename)
try:
tarobj.extractall(extract_dir)
finally:
tarobj.close()
示例13: _create_cbt_
# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarError [as 別名]
def _create_cbt_(dirpath, archivename):
"""
Create a Comic Book Archive in .cbt format (Tar Compression)
:param dirpath: Directory location to save the the book archive.
:param archivename: Name of the archive.
"""
try:
with tarfile.open(archivename, "w") as tar:
tar.add(dirpath, arcname=os.path.basename(dirpath))
except tarfile.TarError:
logging.error("Unable to create CBT file. Report to Developer.")
示例14: tar_do_data
# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarError [as 別名]
def tar_do_data(data, afile):
filelike = io.BytesIO(data)
try:
atar = tarfile.open(fileobj=filelike)
except tarfile.TarError as e:
log_error(str(e), afile)
return
tar_do_tar(atar, afile)
atar.close()
示例15: tar_do_file
# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarError [as 別名]
def tar_do_file(afile):
try:
atar = tarfile.open(afile)
except tarfile.TarError as e:
log_error(str(e), afile)
return
tar_do_tar(atar, afile)
atar.close()