Extract a local or GCS archive file to a folder.

  Args:
    archive_path: local or gcs path to a *.tar.gz or *.tar file
    dest: local folder the archive will be extracted to
439421
def extract_archive(archive_path, dest):
  
  # Make the dest folder if it does not exist
  if not os.path.isdir(dest):
    os.makedirs(dest)

  try:
    tmpfolder = None

    if (not tf.gfile.Exists(archive_path)) or tf.gfile.IsDirectory(archive_path):
      raise ValueError('archive path %s is not a file' % archive_path)

    if archive_path.startswith('gs://'):
      # Copy the file to a local temp folder
      tmpfolder = tempfile.mkdtemp()
      cmd_args = ['gsutil', 'cp', archive_path, tmpfolder]
      _shell_process.run_and_monitor(cmd_args, os.getpid())
      archive_path = os.path.join(tmpfolder, os.path.name(archive_path))

    if archive_path.lower().endswith('.tar.gz'):
      flags = '-xzf'
    elif archive_path.lower().endswith('.tar'):
      flags = '-xf'
    else:
      raise ValueError('Only tar.gz or tar.Z files are supported.')

    cmd_args = ['tar', flags, archive_path, '-C', dest]
    _shell_process.run_and_monitor(cmd_args, os.getpid())
  finally:
    if tmpfolder:
      shutil.rmtree(tmpfolder)
379689
def tarfile_extract(fileobj, dest_path):
        
        # Though this method doesn't fit cleanly into the TarPartition object,
        # tarballs are only ever extracted for partitions so the logic jives
        # for the most part.
        tar = tarfile.open(mode='r|', fileobj=fileobj,
                           bufsize=pipebuf.PIPE_BUF_BYTES)

        # canonicalize dest_path so the prefix check below works
        dest_path = os.path.realpath(dest_path)

        # list of files that need fsyncing
        extracted_files = []

        # Iterate through each member of the tarfile individually. We must
        # approach it this way because we are dealing with a pipe and the
        # getmembers() method will consume it before we extract any data.
        for member in tar:
            assert not member.name.startswith('/')
            relpath = os.path.join(dest_path, member.name)

            # Workaround issue with tar handling of symlink, see:
            # https://bugs.python.org/issue12800
            if member.issym():
                target_path = os.path.join(dest_path, member.name)
                try:
                    os.symlink(member.linkname, target_path)
                except OSError as e:
                    if e.errno == errno.EEXIST:
                        os.remove(target_path)
                        os.symlink(member.linkname, target_path)
                    else:
                        raise
                continue

            if member.isreg() and member.size >= pipebuf.PIPE_BUF_BYTES:
                cat_extract(tar, member, relpath)
            else:
                tar.extract(member, path=dest_path)

            filename = os.path.realpath(relpath)
            extracted_files.append(filename)

            # avoid accumulating an unbounded list of strings which
            # could be quite large for a large database
            if len(extracted_files) > 1000:
                _fsync_files(extracted_files)
                del extracted_files[:]
        tar.close()
        _fsync_files(extracted_files)
215098
def archive_path(self, real_path):
        
        if (not self.path_prefix):
            return(real_path)
        else:
            return(os.path.relpath(real_path, self.path_prefix))
3930
def extract_tarfile(archive_name, destpath):
    ""
    archive = tarfile.open(archive_name)
    archive.extractall(destpath)
1226919
def archive_pathname(file_pathname)
      path = Pathname.new(archive_directory)
      path = file_pathname.dirname.join(archive_directory) if path.relative?

      begin
        path.mkpath unless path.exist?
      rescue Errno::ENOENT => exc
        raise(Errno::ENOENT, "DirmonJob failed to create archive directory: #{path}, #{exc.message}")
      end
      path.realpath
    end
279794
def extract_archive(client, archive_path, extract_path=None):
    
    command = 'tar -xf {path}'.format(path=archive_path)

    if extract_path:
        command += ' -C {extract_path}'.format(extract_path=extract_path)

    out = execute_ssh_command(client, command)
    return out
236836
def makedirs(self, path, mode=0o700):
        
        os.makedirs(os.path.join(self._archive_root, path), mode=mode)
        self.log_debug("created directory at '%s' in FileCacheArchive '%s'"
                       % (path, self._archive_root))
1264280
def extract_files(file_paths, target_path):
        
        os.makedirs(target_path, exist_ok=True)
        extracted = []

        for file_path in file_paths:
            with tarfile.open(file_path, 'r') as archive:
                archive.extractall(target_path)

            file_name = os.path.splitext(os.path.basename(file_path))[0]
            extracted.append(os.path.join(target_path, file_name))

        return extracted
1031834
def _extract_archive(archive, verbosity=0, interactive=True, outdir=None,
                     program=None, format=None, compression=None):
    
    if format is None:
        format, compression = get_archive_format(archive)
    check_archive_format(format, compression)
    program = find_archive_program(format, 'extract', program=program)
    check_program_compression(archive, 'extract', program, compression)
    get_archive_cmdlist = get_archive_cmdlist_func(program, 'extract', format)
    if outdir is None:
        outdir = util.tmpdir(dir=".")
        do_cleanup_outdir = True
    else:
        do_cleanup_outdir = False
    try:
        cmdlist = get_archive_cmdlist(archive, compression, program, verbosity, interactive, outdir)
        if cmdlist:
            # an empty command list means the get_archive_cmdlist() function
            # already handled the command (eg. when it's a builtin Python
            # function)
            run_archive_cmdlist(cmdlist, verbosity=verbosity)
        if do_cleanup_outdir:
            target, msg = cleanup_outdir(outdir, archive)
        else:
            target, msg = outdir, "`%s'" % outdir
        if verbosity >= 0:
            util.log_info("... %s extracted to %s." % (archive, msg))
        return target
    finally:
        # try to remove an empty temporary output directory
        if do_cleanup_outdir:
            try:
                os.rmdir(outdir)
            except OSError:
                pass
906701
def check_files(self, to_path=None):
        
        if to_path:
            target_path = os.path.normpath(os.path.realpath(to_path))
        else:
            target_path = os.getcwd()
        for filename in self.filenames():
            extract_path = os.path.join(target_path, filename)
            extract_path = os.path.normpath(os.path.realpath(extract_path))
            if not extract_path.startswith(target_path):
                raise UnsafeArchive(
                    "Archive member destination is outside the target"
                    " directory.  member: %s" % filename)
827545
def extract_zipfile(archive_name, destpath):
    ""
    archive = zipfile.ZipFile(archive_name)
    archive.extractall(destpath)
99124
def is_archive(self):
        '''
            
        '''
        try:
            if zipfile.is_zipfile(self.attachment.path) or tarfile.is_tarfile(self.attachment.path):
                return True
        except Exception:
            pass
        return False
1290188
def extract_archive(archive, verbosity=0, outdir=None, program=None, interactive=True):
    """"""
    util.check_existing_filename(archive)
    if verbosity >= 0:
        util.log_info("Extracting %s ..." % archive)
    return _extract_archive(archive, verbosity=verbosity, interactive=interactive, outdir=outdir, program=program)
430142
def src_archive
      from_directory do
        Find.find(File.join("packages", name)) do |file|
          if File.file?(file) && file.match(/\.(spec)|(sh)|(patch)$/).nil?
            return File.basename(file)
          end
        end
        nil
      end
    end
757090
def copy_dir(self, path):
        
        for directory in path:
            if os.path.isdir(path):
                full_path = os.path.join(self.archive_dir, directory.lstrip('/'))
                logger.debug("Copying %s to %s", directory, full_path)
                shutil.copytree(directory, full_path)
            else:
                logger.debug("Not a directory: %s", directory)
        return path
1061314
def extract_archive(self, client, archive_path, extract_path=None):
        """"""
        try:
            out = ipa_utils.extract_archive(client, archive_path, extract_path)
        except Exception as error:
            raise IpaCloudException(
                'Failed to extract archive, "{0}": {1}.'.format(
                    archive_path, error
                )
            )

        else:
            self._write_to_log(out)
1235053
def extract_zipped_paths(path):
    
    if os.path.exists(path):
        # this is already a valid path, no need to do anything further
        return path

    # find the first valid part of the provided path and treat that as a zip archive
    # assume the rest of the path is the name of a member in the archive
    archive, member = os.path.split(path)
    while archive and not os.path.exists(archive):
        archive, prefix = os.path.split(archive)
        member = '/'.join([prefix, member])

    if not zipfile.is_zipfile(archive):
        return path

    zip_file = zipfile.ZipFile(archive)
    if member not in zip_file.namelist():
        return path

    # we have a valid zip archive and a valid member of that archive
    tmp = tempfile.gettempdir()
    extracted_path = os.path.join(tmp, *member.split('/'))
    if not os.path.exists(extracted_path):
        extracted_path = zip_file.extract(member, path=tmp)

    return extracted_path