当前位置: 首页>>代码示例>>Python>>正文


Python tarfile.is_tarfile方法代码示例

本文整理汇总了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 
开发者ID:ideascube,项目名称:ideascube,代码行数:24,代码来源:backup.py

示例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 
开发者ID:pypa,项目名称:pep517,代码行数:18,代码来源:test_call_hooks.py

示例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' 
开发者ID:torproject,项目名称:stem,代码行数:26,代码来源:system.py

示例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)!'
            ) 
开发者ID:ynop,项目名称:audiomate,代码行数:19,代码来源:downloader.py

示例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) 
开发者ID:aliyun,项目名称:oss-ftp,代码行数:25,代码来源:util.py

示例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)) 
开发者ID:glotzerlab,项目名称:signac,代码行数:18,代码来源:import_export.py

示例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 
开发者ID:aws,项目名称:sagemaker-python-sdk,代码行数:27,代码来源:image.py

示例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 
开发者ID:abhinavkashyap,项目名称:sciwing,代码行数:27,代码来源:common.py

示例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') 
开发者ID:SubstraFoundation,项目名称:substra-backend,代码行数:26,代码来源:utils.py

示例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)) 
开发者ID:pypa,项目名称:pipenv,代码行数:21,代码来源:app.py

示例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) 
开发者ID:glennmatthews,项目名称:cot,代码行数:24,代码来源:file_reference.py

示例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)) 
开发者ID:cloudify-cosmo,项目名称:cloudify-manager,代码行数:18,代码来源:archiving.py

示例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) 
开发者ID:CAMI-challenge,项目名称:CAMISIM,代码行数:13,代码来源:archive.py

示例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
        ) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:33,代码来源:__init__.py

示例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() 
开发者ID:ucb-bar,项目名称:hammer,代码行数:30,代码来源:hammer_tech.py


注:本文中的tarfile.is_tarfile方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。