本文整理匯總了Python中tarfile.is_tarfile方法的典型用法代碼示例。如果您正苦於以下問題:Python tarfile.is_tarfile方法的具體用法?Python tarfile.is_tarfile怎麽用?Python tarfile.is_tarfile使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類tarfile
的用法示例。
在下文中一共展示了tarfile.is_tarfile方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: load
# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import is_tarfile [as 別名]
def load(cls, file_):
name = os.path.basename(file_.name)
backup = Backup(name)
with open(backup.path, mode='wb') as f:
try:
for chunk in file_.chunks():
f.write(chunk)
except AttributeError:
# file_ as no chunks,
# read without them.
while True:
content = file_.read(4096)
if not content:
break
f.write(content)
if not ((name.endswith('.zip') and zipfile.is_zipfile(backup.path))
or tarfile.is_tarfile(backup.path)
):
os.unlink(backup.path)
raise ValueError(_("Not a {} file").format(
'zip' if name.endswith('.zip') else 'tar'))
return backup
示例2: test_build_sdist
# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import is_tarfile [as 別名]
def test_build_sdist():
hooks = get_hooks('pkg1')
with TemporaryDirectory() as sdistdir:
with modified_env({'PYTHONPATH': BUILDSYS_PKGS}):
sdist = hooks.build_sdist(sdistdir, {})
assert sdist.endswith('.tar.gz')
assert os.sep not in sdist
sdist = pjoin(sdistdir, sdist)
assert_isfile(sdist)
assert tarfile.is_tarfile(sdist)
with tarfile.open(sdist) as tf:
contents = tf.getnames()
assert 'pkg1-0.5/pyproject.toml' in contents
示例3: is_tarfile
# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import is_tarfile [as 別名]
def is_tarfile(path: str) -> bool:
"""
Returns if the path belongs to a tarfile or not.
.. versionadded:: 1.2.0
:param path: path to be checked
:returns: **True** if the path belongs to a tarball, **False** otherwise
"""
# Checking if it's a tar file may fail due to permissions so failing back
# to the mime type...
#
# IOError: [Errno 13] Permission denied: '/vmlinuz.old'
#
# With python 3 insuffient permissions raises an AttributeError instead...
#
# http://bugs.python.org/issue17059
try:
return tarfile.is_tarfile(path)
except (IOError, AttributeError):
return mimetypes.guess_type(path)[0] == 'application/x-tar'
示例4: _extract_file
# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import is_tarfile [as 別名]
def _extract_file(self, file_path, target_folder):
ark_type = self.ark_type
if self.ark_type == ArkType.AUTO:
if tarfile.is_tarfile(file_path):
ark_type = ArkType.TAR
elif zipfile.is_zipfile(file_path):
ark_type = ArkType.ZIP
if ark_type == ArkType.TAR:
download.extract_tar(file_path, target_folder)
elif ark_type == ArkType.ZIP:
download.extract_zip(file_path, target_folder)
else:
raise ValueError(
'Unrecognized archive type (Only zip/tar supported)!'
)
示例5: unpack_file
# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import is_tarfile [as 別名]
def unpack_file(filename, location, content_type, link):
filename = os.path.realpath(filename)
if (content_type == 'application/zip'
or filename.endswith('.zip')
or filename.endswith('.pybundle')
or filename.endswith('.whl')
or zipfile.is_zipfile(filename)):
unzip_file(filename, location, flatten=not filename.endswith(('.pybundle', '.whl')))
elif (content_type == 'application/x-gzip'
or tarfile.is_tarfile(filename)
or splitext(filename)[1].lower() in ('.tar', '.tar.gz', '.tar.bz2', '.tgz', '.tbz')):
untar_file(filename, location)
elif (content_type and content_type.startswith('text/html')
and is_svn_page(file_contents(filename))):
# We don't really care about this
from pip.vcs.subversion import Subversion
Subversion('svn+' + link.url).unpack(location)
else:
## FIXME: handle?
## FIXME: magic signatures?
logger.fatal('Cannot unpack file %s (downloaded from %s, content-type: %s); cannot detect archive format'
% (filename, location, content_type))
raise InstallationError('Cannot determine archive format of %s' % location)
示例6: _prepare_import_into_project
# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import is_tarfile [as 別名]
def _prepare_import_into_project(origin, project, schema=None):
"Prepare the data space at origin for import into project with the given schema function."
if os.path.isfile(origin):
if zipfile.is_zipfile(origin):
with zipfile.ZipFile(origin) as file:
yield _analyze_zipfile_for_import(file, project, schema)
elif tarfile.is_tarfile(origin):
with TemporaryDirectory() as tmpdir:
with tarfile.open(origin) as file:
yield _analyze_tarfile_for_import(file, project, schema, tmpdir)
else:
raise RuntimeError("Unknown file type: '{}'.".format(origin))
elif os.path.isdir(origin):
yield _analyze_directory_for_import(root=origin, project=project, schema=schema)
else:
raise ValueError("Unable to import from '{}'. Does the origin exist?".format(origin))
示例7: _prepare_serving_volumes
# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import is_tarfile [as 別名]
def _prepare_serving_volumes(self, model_location):
"""
Args:
model_location:
"""
volumes = []
host = self.hosts[0]
# Make the model available to the container. If this is a local file just mount it to
# the container as a volume. If it is an S3 location, the DataSource will download it, we
# just need to extract the tar file.
host_dir = os.path.join(self.container_root, host)
os.makedirs(host_dir)
model_data_source = sagemaker.local.data.get_data_source_instance(
model_location, self.sagemaker_session
)
for filename in model_data_source.get_file_list():
if tarfile.is_tarfile(filename):
with tarfile.open(filename) as tar:
tar.extractall(path=model_data_source.get_root_dir())
volumes.append(_Volume(model_data_source.get_root_dir(), "/opt/ml/model"))
return volumes
示例8: cached_path
# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import is_tarfile [as 別名]
def cached_path(path: Union[pathlib.Path, str], url: str, unzip=True) -> pathlib.Path:
if isinstance(path, str):
path = pathlib.Path(path)
msg_printer = Printer()
if path.is_file() or path.is_dir():
msg_printer.info(f"{path} exists.")
return path
download_file(url=url, dest_filename=str(path))
if unzip:
if zipfile.is_zipfile(str(path)):
extract_zip(filename=str(path), destination_dir=str(path.parent))
if tarfile.is_tarfile(str(path)):
if "tar" in path.suffix:
mode = "r"
elif "gz" in path.suffix:
mode = "r:gz"
else:
mode = "r"
extract_tar(filename=str(path), destination_dir=str(path.parent), mode=mode)
return path
示例9: uncompress_path
# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import is_tarfile [as 別名]
def uncompress_path(archive_path, to_directory):
if zipfile.is_zipfile(archive_path):
with ZipFile(archive_path, 'r') 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)
elif tarfile.is_tarfile(archive_path):
with tarfile.open(archive_path, 'r:*') 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)
else:
raise Exception('Archive must be zip or tar.gz')
示例10: prepare_fixtures
# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import is_tarfile [as 別名]
def prepare_fixtures(path):
path = os.path.abspath(path)
if not (os.path.exists(path) and os.path.isdir(path)):
raise ValueError("{} is not a directory!".format(path))
for root, dirs, files in os.walk(path):
package_name, _, _ = os.path.relpath(root, start=path).partition(os.path.sep)
if package_name not in ARTIFACTS:
ARTIFACTS[package_name] = Artifact(package_name)
for file in files:
file_path = os.path.join(root, file)
rel_path = os.path.relpath(file_path, start=path)
_, _, subpkg = rel_path.partition(os.path.sep)
subpkg, _, _ = subpkg.partition(os.path.sep)
pkg, ext = os.path.splitext(subpkg)
if not (is_tarfile(file_path) or is_zipfile(file_path) or ext == ".git"):
continue
if subpkg not in ARTIFACTS[package_name].files:
ARTIFACTS[package_name].add_file(os.path.join(root, file))
ARTIFACTS[package_name].add_file(os.path.join(root, file))
示例11: __init__
# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import is_tarfile [as 別名]
def __init__(self, tarfile_path, filename, **kwargs):
"""Create a reference to a file contained in a TAR archive.
Args:
tarfile_path (str): Path to TAR archive to read
filename (str): File name in the TAR archive.
**kwargs: Passed through to :meth:`FileReference.__init__`.
Raises:
IOError: if ``tarfile_path`` doesn't reference a TAR file,
or the TAR file does not contain ``filename``.
"""
if not os.path.isabs(tarfile_path):
logger.warning("Only absolute paths are accepted, but "
'got apparent relative path "%s".'
"\nAttempting to convert it to an absolute path.",
tarfile_path)
tarfile_path = os.path.abspath(tarfile_path)
if not tarfile.is_tarfile(tarfile_path):
raise IOError("{0} is not a valid TAR file.".format(tarfile_path))
self.tarf = None
super(FileInTAR, self).__init__(tarfile_path, filename, **kwargs)
示例12: get_archive_type
# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import is_tarfile [as 別名]
def get_archive_type(archive_path):
if zipfile.is_zipfile(archive_path):
return 'zip'
if tarfile.is_tarfile(archive_path):
max_len = max(len(x) for x in TAR_MAGIC_DICT)
with open(archive_path, 'rb') as f:
file_start = f.read(max_len)
for magic, ext in TAR_MAGIC_DICT.items():
if file_start.startswith(magic):
return ext
return 'tar'
raise RuntimeError("Can't recognize archive type; Archive path {0}"
.format(archive_path))
示例13: is_archive
# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import is_tarfile [as 別名]
def is_archive(file_path):
"""
Test if archive can be assumed by filename
@param file_path: Path to file
@type file_path: str | unicode
@return: True if file is archive
@rtype: str | None
"""
return tarfile.is_tarfile(file_path) or zipfile.is_zipfile(file_path)
示例14: unpack_file
# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import is_tarfile [as 別名]
def unpack_file(filename, location, content_type, link):
filename = os.path.realpath(filename)
if (content_type == 'application/zip' or
filename.lower().endswith(ZIP_EXTENSIONS) or
zipfile.is_zipfile(filename)):
unzip_file(
filename,
location,
flatten=not filename.endswith('.whl')
)
elif (content_type == 'application/x-gzip' or
tarfile.is_tarfile(filename) or
filename.lower().endswith(
TAR_EXTENSIONS + BZ2_EXTENSIONS + XZ_EXTENSIONS)):
untar_file(filename, location)
elif (content_type and content_type.startswith('text/html') and
is_svn_page(file_contents(filename))):
# We don't really care about this
from pip.vcs.subversion import Subversion
Subversion('svn+' + link.url).unpack(location)
else:
# FIXME: handle?
# FIXME: magic signatures?
logger.critical(
'Cannot unpack file %s (downloaded from %s, content-type: %s); '
'cannot detect archive format',
filename, location, content_type,
)
raise InstallationError(
'Cannot determine archive format of %s' % location
)
示例15: extract_tarballs
# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import is_tarfile [as 別名]
def extract_tarballs(self) -> None:
"""Extract tarballs to the given cache_dir, or verify that they've been extracted."""
for tarball in self.config.tarballs:
target_path = os.path.join(self.extracted_tarballs_dir, tarball.path)
tarball_path = os.path.join(self.get_setting(tarball.base_var), tarball.path)
if not os.path.isfile(tarball_path):
raise ValueError("Path {0} does not point to a valid tarball!".format(tarball_path))
if os.path.isdir(target_path):
# If the folder already seems to exist, continue
continue
else:
# Else, extract the tarballs.
os.makedirs(target_path, mode=0o700, exist_ok=True) # Make sure it exists or tar will not be happy.
self.logger.debug("Extracting/verifying tarball %s" % (tarball_path))
tarfile.open(tarball_path).extractall(target_path)
for root, dirs, files in os.walk(target_path):
for d in dirs:
os.chmod(os.path.join(root, d), mode=0o700)
for f in files:
file = os.path.join(root, f)
os.chmod(file, mode=0o700)
# extract tarball recursively
if tarfile.is_tarfile(file):
self.logger.debug("Extracting/verifying tarball %s" % (file))
tarfile.open(file).extractall(path=os.path.join(root, f + "_dir"))
os.remove(file)
os.renames(os.path.join(root, f + "_dir"), file)
self.post_install_script()