def recompress(self, name): """Recompress archive with given name.""" archive = os.path.join(datadir, name) ext = os.path.splitext(archive)[1] tmpfile = util.tmpfile(suffix=ext) try: shutil.copy(archive, tmpfile) util.run_checked([sys.executable, patool_cmd, "-vv", "--non-interactive", "recompress", tmpfile]) finally: if os.path.exists(tmpfile): os.remove(tmpfile)
def recompress(self, name): """Recompress archive with given name.""" archive = os.path.join(datadir, name) ext = os.path.splitext(archive)[1] tmpfile = util.tmpfile(suffix=ext) try: shutil.copy(archive, tmpfile) util.run_checked( [sys.executable, patool_cmd, "-vv", "recompress", tmpfile]) finally: if os.path.exists(tmpfile): os.remove(tmpfile)
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