示例#1
0
 def test_extract(self):
     tmpdir = util.tmpdir(dir=basedir)
     try:
         archive = os.path.join(datadir, "t .7z")
         util.run_checked([sys.executable, patool_cmd, "-vv", "extract", "--outdir", tmpdir, archive])
     finally:
         shutil.rmtree(tmpdir)
示例#2
0
 def test_extract(self):
     tmpdir = util.tmpdir(dir=basedir)
     try:
         archive = os.path.join(datadir, "t .7z")
         util.run_checked([sys.executable, patool_cmd, "-vv", "extract", "--outdir", tmpdir, archive])
     finally:
         shutil.rmtree(tmpdir)
示例#3
0
 def test_create(self):
     tmpdir = util.tmpdir(dir=basedir)
     try:
         files = [os.path.join(datadir, "t"), os.path.join(datadir, "t.txt")]
         archive = os.path.join(tmpdir, "t.7z")
         cmd = [sys.executable, patool_cmd, "-vv", "create", archive]
         cmd.extend(files)
         util.run_checked(cmd)
     finally:
         shutil.rmtree(tmpdir)
示例#4
0
 def repack(self, name1, name2):
     """Repack archive with name1 to archive with name2."""
     archive1 = os.path.join(datadir, name1)
     tmpdir = util.tmpdir()
     try:
         archive2 = os.path.join(tmpdir, name2)
         util.run_checked([sys.executable, patool_cmd, "-vv", "--non-interactive", "repack", archive1, archive2])
         util.run_checked([sys.executable, patool_cmd, "--non-interactive", "diff", archive1, archive2])
     finally:
         shutil.rmtree(tmpdir)
示例#5
0
 def repack(self, name1, name2):
     """Repack archive with name1 to archive with name2."""
     archive1 = os.path.join(datadir, name1)
     tmpdir = util.tmpdir()
     try:
         archive2 = os.path.join(tmpdir, name2)
         util.run_checked([sys.executable, patool_cmd, "-vv", "repack", archive1, archive2])
         util.run_checked([sys.executable, patool_cmd, "diff", archive1, archive2])
     finally:
         shutil.rmtree(tmpdir)
示例#6
0
def _repack_archive (archive1, archive2, **kwargs):
    """Repackage an archive to a different format."""
    tmpdir = util.tmpdir()
    try:
        _handle_archive(archive1, 'extract', outdir=tmpdir, **kwargs)
        archive = os.path.abspath(archive2)
        files = tuple(os.listdir(tmpdir))
        os.chdir(tmpdir)
        _handle_archive(archive, 'create', *files, **kwargs)
        return 0
    finally:
        shutil.rmtree(tmpdir, onerror=rmtree_log_error)
示例#7
0
def _diff_archives (archive1, archive2, **kwargs):
    """Show differences between two archives."""
    if util.is_same_file(archive1, archive2):
        msg = "no differences found: archive `%s' and `%s' are the same files"
        print msg % (archive1, archive2)
        return 0
    diff = util.find_program("diff")
    if not diff:
        msg = "The diff(1) program is required for showing archive differences, please install it."
        raise util.PatoolError(msg)
    tmpdir1 = util.tmpdir()
    try:
        path1 = _handle_archive(archive1, 'extract', outdir=tmpdir1, **kwargs)
        tmpdir2 = util.tmpdir()
        try:
            path2 = _handle_archive(archive2, 'extract', outdir=tmpdir2, **kwargs)
            return util.run([diff, "-urN", path1, path2])
        finally:
            shutil.rmtree(tmpdir2, onerror=rmtree_log_error)
    finally:
        shutil.rmtree(tmpdir1, onerror=rmtree_log_error)
示例#8
0
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