def list_tar (archive, compression, cmd, **kwargs): """List a TAR archive with the tarfile Python module.""" verbose = kwargs['verbose'] if verbose: util.log_info('listing %s...' % archive) tfile = tarfile.open(archive) try: tfile.list(verbose=verbose) finally: tfile.close() return None
def create_zip (archive, compression, cmd, *args, **kwargs): """Create a ZIP archive with the zipfile Python module.""" verbose = kwargs['verbose'] if verbose: util.log_info('creating %s...' % archive) zfile = zipfile.ZipFile(archive, 'w') try: for filename in args: zfile.write(filename) finally: zfile.close() return None
def list_zip (archive, compression, cmd, **kwargs): """List member of a ZIP archive with the zipfile Python module.""" verbose = kwargs['verbose'] if verbose: util.log_info('listing %s...' % archive) zfile = zipfile.ZipFile(archive, "r") try: for name in zfile.namelist(): util.log_info('member %s' % name) finally: zfile.close() return None
def _handle_archive (archive, command, *args, **kwargs): """Handle archive command; raising PatoolError on errors.""" check_archive_arguments(archive, command, *args) format, compression = kwargs.get("format"), kwargs.get("compression") if format is None: format, compression = get_archive_format(archive) check_archive_format(format, compression) check_archive_command(command) config_kwargs = clean_config_keys(kwargs) config = parse_config(archive, format, compression, command, **config_kwargs) # check if archive already exists if command == 'create' and os.path.exists(archive): raise util.PatoolError("archive `%s' already exists" % archive) program = config['program'] get_archive_cmdlist = get_archive_cmdlist_func(program, command, format) # prepare keyword arguments for command list cmd_kwargs = dict(verbose=config['verbose']) origarchive = None if command == 'extract': if "outdir" in kwargs: cmd_kwargs["outdir"] = kwargs["outdir"] do_cleanup_outdir = False else: cmd_kwargs['outdir'] = util.tmpdir(dir=os.getcwd()) do_cleanup_outdir = True elif command == 'create' and os.path.basename(program) == 'arc' and \ ".arc" in archive and not archive.endswith(".arc"): # the arc program mangles the archive name if it contains ".arc" origarchive = archive archive = util.tmpfile(dir=os.path.dirname(archive), suffix=".arc") try: cmdlist = get_archive_cmdlist(archive, compression, program, *args, **cmd_kwargs) if cmdlist: # an empty command list means the get_archive_cmdlist() function # already handled the command (eg. when it's a builting Python # function) run_archive_cmdlist(cmdlist) if command == 'extract': if do_cleanup_outdir: target, msg = cleanup_outdir(cmd_kwargs["outdir"]) util.log_info("%s extracted to %s" % (archive, msg)) else: target, msg = cmd_kwargs["outdir"], "`%s'" % cmd_kwargs["outdir"] return target elif command == 'create' and origarchive: shutil.move(archive, origarchive) finally: if command == "extract": try: os.rmdir(cmd_kwargs["outdir"]) except OSError: pass
def create_tar (archive, compression, cmd, *args, **kwargs): """Create a TAR archive with the tarfile Python module.""" verbose = kwargs['verbose'] if verbose: util.log_info('creating %s...' % archive) mode = get_tar_mode(compression) tfile = tarfile.open(archive, mode) try: for filename in args: tfile.add(filename) finally: tfile.close() return None
def extract_tar (archive, compression, cmd, **kwargs): """Extract a TAR archive with the tarfile Python module.""" verbose = kwargs['verbose'] outdir = kwargs['outdir'] # XXX honor outdir if verbose: util.log_info('extracting %s...' % archive) tfile = tarfile.open(archive) try: tfile.extractall() finally: tfile.close() if verbose: util.log_info('... extracted to %s' % outdir) return None
def extract_zip (archive, compression, cmd, **kwargs): """Extract a ZIP archive with the zipfile Python module.""" verbose = kwargs['verbose'] outdir = kwargs['outdir'] # XXX honor outdir if verbose: util.log_info('extracting %s...' % archive) zfile = zipfile.ZipFile(archive) try: zfile.extractall() finally: zfile.close() if verbose: util.log_info('... extracted to %s' % outdir) return None
def create_bzip2 (archive, compression, cmd, *args, **kwargs): """Create a BZIP2 archive with the bz2 Python module.""" verbose = kwargs['verbose'] if verbose: util.log_info('creating %s...' % archive) if len(args) > 1: util.log_error('multi-file compression not supported in Python bz2') bz2file = bz2.BZ2File(archive, 'wb') try: filename = args[0] srcfile = open(filename) try: data = srcfile.read(READ_SIZE_BYTES) while data: bz2file.write(data) data = srcfile.read(READ_SIZE_BYTES) if verbose: util.log_info('... added %s' % filename) finally: srcfile.close() finally: bz2file.close() return None
def extract_bzip2 (archive, compression, cmd, **kwargs): """Extract a BZIP2 archive with the bz2 Python module.""" verbose = kwargs['verbose'] outdir = kwargs['outdir'] # XXX honor outdir if verbose: util.log_info('extracting %s...' % archive) targetname = util.get_single_outfile(kwargs['outdir'], archive) bz2file = bz2.BZ2File(archive) try: targetfile = open(targetname, 'wb') try: data = bz2file.read(READ_SIZE_BYTES) while data: targetfile.write(data) data = bz2file.read(READ_SIZE_BYTES) finally: targetfile.close() finally: bz2file.close() if verbose: util.log_info('... extracted to %s' % targetname) return None