我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用tarfile.is_tarfile()。
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)
def is_tarfile(path): """ Returns if the path belongs to a tarfile or not. .. versionadded:: 1.2.0 :param str 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'
def extract_file(self,compressed_file, target_path): """ extract_file(compressed_file, target_path) extracts the compressed_file to target_path """ if os.path.exists(compressed_file): print "Extracting %s to %s\n" % (compressed_file, target_path) if zipfile.is_zipfile(compressed_file): with zipfile.ZipFile(compressed_file, "r") as z: z.extractall(path=target_path) if tarfile.is_tarfile(compressed_file): tar = tarfile.open(compressed_file) tar.extractall(path=target_path) tar.close() print "Cleaning up\n" os.remove(compressed_file) print "Done!\n" else: print "%s does not exist, cannot extract" % compressed_file
def __init__(self, compressed_file, target_path): """ __init__(compressed_file, target_path) extracts the compressed_file to target_path """ if os.path.exists(compressed_file): print "Extracting %s to %s\n" % (compressed_file, target_path) if zipfile.is_zipfile(compressed_file): with zipfile.ZipFile(compressed_file, "r") as z: z.extractall(path=target_path) if tarfile.is_tarfile(compressed_file): tar = tarfile.open(compressed_file) tar.extractall(path=target_path) tar.close() print "Cleaning up\n" os.remove(compressed_file) print "Done!\n" else: print "%s does not exist, cannot extract" % compressed_file
def extract_ar(archive, dst, *kwargs): if sys.version_info[0] < 3 and archive.endswith('.xz'): with contextlib.closing(lzma.LZMAFile(archive)) as xz: with tarfile.open(fileobj=xz, *kwargs) as f: f.extractall(dst) elif archive.endswith('.zip'): with zipfile.ZipFile(archive,'r') as f: f.extractall(dst) elif tarfile.is_tarfile(archive): if USE_CMAKE_TAR: cmd([which('cmake'), '-E', 'tar', 'xzf', os.path.abspath(archive)], cwd=dst) else: tarfile.open(archive, *kwargs).extractall(dst) else: # Treat as a single source file d = os.path.join(dst, 'header') mkdir(d) copy_to(archive, d)
def untar_dir(tar_file,dest_dir=None): '''untar_dir will extract a tarfile to a directory. If an extraction destination is not defined, a temporary directory will be created and used. :param tar_file: the .tar.gz file to untar/decompress :param dest_dir: the destination directory ''' if dest_dir == None: dest_dir = tempfile.mkdtemp() contents = [] if tarfile.is_tarfile(tar_file): with tarfile.open(tar_file) as tf: tf.extractall(dest_dir) return dest_dir
def unpack_file(filename, location, content_type, link): if (content_type == 'application/zip' or filename.endswith('.zip') or filename.endswith('.pybundle') or zipfile.is_zipfile(filename)): unzip_file(filename, location, flatten=not filename.endswith('.pybundle')) 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)
def test_build_sdist(): hooks = Pep517HookCaller(pjoin(SAMPLES_DIR, '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
def _scantar(self, filename, ftype=None): """Scans the passed TAR archive and indexes all the files contained by it. """ if not tarfile.is_tarfile(filename): raise TypeError("file '%s' is not a valid TAR archive" % filename) mode = 'r' if ftype: if ftype not in ('gz', 'bz2'): raise TypeError("invalid TAR compression type") mode = "r:%s" % ftype archname = os.path.abspath(filename) archtype = 'tar' if ftype: archtype = 'tar%s' % ftype tar = tarfile.open(filename, mode) for path in tar.getnames(): fname = os.path.split(path)[1] self.files[fname] = (archname, archtype, path) tar.close()
def uploadLanguagePack(): input_name = "file" input_upload_path = input_name + "." + settings().get(["server", "uploads", "pathSuffix"]) input_upload_name = input_name + "." + settings().get(["server", "uploads", "nameSuffix"]) if not input_upload_path in request.values or not input_upload_name in request.values: return make_response("No file included", 400) upload_name = request.values[input_upload_name] upload_path = request.values[input_upload_path] exts = filter(lambda x: upload_name.lower().endswith(x), (".zip", ".tar.gz", ".tgz", ".tar")) if not len(exts): return make_response("File doesn't have a valid extension for a language pack archive", 400) target_path = settings().getBaseFolder("translations") if tarfile.is_tarfile(upload_path): _unpack_uploaded_tarball(upload_path, target_path) elif zipfile.is_zipfile(upload_path): _unpack_uploaded_zipfile(upload_path, target_path) else: return make_response("Neither zip file nor tarball included", 400) return getInstalledLanguagePacks()
def __init__(self, compressed_file_location=None): self.compressed_file_location = compressed_file_location self.tmp_dir = tempfile.mkdtemp(prefix='/var/tmp/') self.is_file = os.path.isfile(self.compressed_file_location) self.is_tarfile = (tarfile.is_tarfile(self.compressed_file_location) if self.is_file else False) if self.is_file and self.is_tarfile: try: tar = tarfile.open(self.compressed_file_location) tar.extractall(path=self.tmp_dir) tar.close() logger.debug("Compressed filesystem %s extracted to %s", self.compressed_file_location, self.tmp_dir) except: logger.debug("Invalid compressed tar filesystem provided. " "Could not extract contents.") else: logger.debug("Invalid compressed tar filesystem provided.")
def get_archive_handler(archive_name): if os.path.isfile(archive_name): if tarfile.is_tarfile(archive_name): return extract_tarfile elif zipfile.is_zipfile(archive_name): return extract_zipfile else: # look at the file name for ext in ('.tar', '.tar.gz', '.tgz', 'tar.bz2', '.tbz2', '.tbz'): if archive_name.endswith(ext): return extract_tarfile for ext in ('.zip', '.jar'): if archive_name.endswith(ext): return extract_zipfile
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 )
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._internal.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 )
def open_dict(self, fname, membername=None): if tarfile.is_tarfile(fname): if not membername: raise ValueError('archive membername not set!') tf = tarfile.open(fname) f = tf.extractfile(membername) yield f f.close() tf.close() else: with open(fname) as f: yield f
def maybe_extract(compressed_filepath, train_dir, test_dir): def is_image(filepath): extensions = ('.jpg', '.jpeg', '.png', '.gif') return any(filepath.endswith(ext) for ext in extensions) os.makedirs(train_dir, exist_ok=True) os.makedirs(test_dir, exist_ok=True) print('Extracting: "{}"'.format(compressed_filepath)) if zipfile.is_zipfile(compressed_filepath): with zipfile.ZipFile(compressed_filepath) as zf: files = [member for member in zf.infolist() if is_image(member.filename)] count = len(files) train_test_boundary = int(count * 0.99) for i in range(count): if i < train_test_boundary: extract_dir = train_dir else: extract_dir = test_dir if not os.path.exists(os.path.join(extract_dir, files[i].filename)): zf.extract(files[i], extract_dir) elif tarfile.is_tarfile(compressed_filepath): with tarfile.open(compressed_filepath) as tar: files = [member for member in tar if is_image(member.name)] count = len(files) train_test_boundary = int(count * 0.99) for i in range(count): if i < train_test_boundary: extract_dir = train_dir else: extract_dir = test_dir if not os.path.exists(os.path.join(extract_dir, files[i].name)): tar.extract(files[i], extract_dir) else: raise NotImplemented print('Extraction complete')
def extract_configs(configs, parent_dir): """Extracts configs to a new dir under parent_dir and returns it""" config_dir = os.path.join(parent_dir, "configs") if os.path.isdir(configs): shutil.copytree(configs, config_dir, symlinks=True) elif tarfile.is_tarfile(configs): with tarfile.open(configs, "r") as tar: tar.extractall(config_dir) else: raise errors.Error("Unknown configurations file type") return config_dir