예제 #1
0
def unpack(ext: str, source: IO[bytes], dest_path: str):
    """Unpack the archive |source| to |dest_path|.

    Args:
        ext (str): Extension of the archive.
        source (IO[bytes]): File handle to the source.
        dest_path ([type]): Destination path to unpack to.

    """
    try:

        if ext == '.tar.gz' or ext == '.tgz':
            un_tar_directory(source, dest_path, 'gz')
        elif ext == '.tar.bz2':
            un_tar_directory(source, dest_path, 'bz2')
        elif ext == '.bz2':
            un_bz2_file(source, dest_path)
        elif ext == '.gz':
            with open(dest_path, 'wb') as f:
                shutil.copyfileobj(un_gzip_stream(source), f)
        elif ext == '.zip':
            unzip_directory(source, dest_path)
        else:
            raise UsageError('Not an archive.')
    except (tarfile.TarError, IOError) as e:
        logging.error("Invalid archive upload: failed to unpack archive: %s",
                      e)
        raise UsageError('Invalid archive upload: failed to unpack archive.')
예제 #2
0
def unpack(ext, source, dest_path):
    """
    Unpack the archive |source| to |dest_path|.
    Note: |source| can be a file handle or a path.
    |ext| contains the extension of the archive.
    """
    close_source = False
    try:
        if isinstance(source, str):
            source = open(source, 'rb')
            close_source = True

        if ext == '.tar.gz' or ext == '.tgz':
            un_tar_directory(source, dest_path, 'gz')
        elif ext == '.tar.bz2':
            un_tar_directory(source, dest_path, 'bz2')
        elif ext == '.bz2':
            un_bz2_file(source, dest_path)
        elif ext == '.gz':
            with open(dest_path, 'wb') as f:
                shutil.copyfileobj(un_gzip_stream(source), f)
        elif ext == '.zip':
            unzip_directory(source, dest_path)
        else:
            raise UsageError('Not an archive.')
    except (tarfile.TarError, IOError):
        raise UsageError('Invalid archive upload.')
    finally:
        if close_source:
            source.close()
 def test_bz2_file(self):
     source_write = tempfile.NamedTemporaryFile(delete=False)
     self.addCleanup(lambda: os.remove(source_write.name))
     source_write.write(bz2.compress(b'contents'))
     source_write.flush()
     source_read = open(source_write.name, 'rb')
     destination = tempfile.NamedTemporaryFile(delete=False)
     self.addCleanup(lambda: os.remove(destination.name))
     un_bz2_file(source_read, destination.name)
     self.assertEqual(destination.read(), b'contents')
     source_write.close()
     source_read.close()
     destination.close()
예제 #4
0
def unpack(ext, source, dest_path):
    """
    Unpack the archive |source| to |dest_path|.
    Note: |source| can be a file handle or a path.
    |ext| contains the extension of the archive.
    """
    if ext != '.zip':
        close_source = False
        try:
            if isinstance(source, str):
                source = open(source, 'rb')
                close_source = True

            if ext == '.tar.gz' or ext == '.tgz':
                un_tar_directory(source, dest_path, 'gz')
            elif ext == '.tar.bz2':
                un_tar_directory(source, dest_path, 'bz2')
            elif ext == '.bz2':
                un_bz2_file(source, dest_path)
            elif ext == '.gz':
                with open(dest_path, 'wb') as f:
                    shutil.copyfileobj(un_gzip_stream(source), f)
            else:
                raise UsageError('Not an archive.')
        except (tarfile.TarError, IOError):
            raise UsageError('Invalid archive upload.')
        finally:
            if close_source:
                source.close()
    else:
        delete_source = False
        try:
            # unzip doesn't accept input from standard input, so we have to save
            # to a temporary file.
            if not isinstance(source, str):
                temp_path = dest_path + '.zip'
                with open(temp_path, 'wb') as f:
                    shutil.copyfileobj(source, f)
                source = temp_path
                delete_source = True

            exitcode = subprocess.call(
                ['unzip', '-q', source, '-d', dest_path])
            if exitcode != 0:
                raise UsageError('Invalid archive upload.')
        finally:
            if delete_source:
                path_util.remove(source)