Beispiel #1
0
def get_tar_mode (compression):
    """Determine tarfile open mode according to the given compression."""
    if compression == 'gzip':
        return 'w:gz'
    if compression == 'bzip2':
        return 'w:bz2'
    if compression:
        msg = 'pytarfile does not support %s for tar compression'
        util.log_error(msg % compression)
    # no compression
    return 'w'
Beispiel #2
0
def run_diff(args):
    """Show differences between two archives."""
    try:
        res = patoolib.diff_archives(args.archive1,
                                     args.archive2,
                                     verbosity=args.verbosity,
                                     interactive=args.interactive)
    except PatoolError as msg:
        log_error("error showing differences between %s and %s: %s" %
                  (args.archive1, args.archive2, msg))
        res = 2
    return res
Beispiel #3
0
def run_recompress(args):
    """Recompress an archive to smaller size."""
    res = 0
    try:
        patoolib.recompress_archive(args.archive,
                                    verbosity=args.verbosity,
                                    interactive=args.interactive,
                                    password=args.password)
    except PatoolError as msg:
        log_error("error recompressing %s: %s" % (args.archive, msg))
        res = 1
    return res
Beispiel #4
0
def run_repack(args):
    """Repackage one archive in another format."""
    res = 0
    try:
        patoolib.repack_archive(args.archive_src,
                                args.archive_dst,
                                verbosity=args.verbosity,
                                interactive=args.interactive)
    except PatoolError as msg:
        log_error("error repacking %s: %s" % (args.archive_src, msg))
        res = 1
    return res
Beispiel #5
0
def run_search(args):
    """Search for pattern in given archive."""
    try:
        res = patoolib.search_archive(args.pattern,
                                      args.archive,
                                      verbosity=args.verbosity,
                                      interactive=args.interactive,
                                      password=args.password)
    except PatoolError as msg:
        log_error("error searching %s: %s" % (args.archive, msg))
        res = 2
    return res
Beispiel #6
0
def run_create(args):
    """Create an archive from given files."""
    res = 0
    try:
        patoolib.create_archive(args.archive,
                                args.filename,
                                verbosity=args.verbosity,
                                interactive=args.interactive,
                                password=args.password)
    except PatoolError as msg:
        log_error("error creating %s: %s" % (args.archive, msg))
        res = 1
    return res
Beispiel #7
0
def run_test(args):
    """Test files in archive(s)."""
    res = 0
    for archive in args.archive:
        try:
            patoolib.test_archive(archive,
                                  verbosity=args.verbosity,
                                  interactive=args.interactive,
                                  password=args.password)
        except PatoolError as msg:
            log_error("error testing %s: %s" % (archive, msg))
            res += 1
    return res
Beispiel #8
0
def run_extract(args):
    """Extract files from archive(s)."""
    res = 0
    for archive in args.archive:
        try:
            patoolib.extract_archive(archive,
                                     verbosity=args.verbosity,
                                     interactive=args.interactive,
                                     outdir=args.outdir,
                                     password=args.password)
        except PatoolError as msg:
            log_error("error extracting %s: %s" % (archive, msg))
            res += 1
    return res
Beispiel #9
0
def run_list(args):
    """List files in archive(s)."""
    res = 0
    for archive in args.archive:
        try:
            # increase default verbosity since the listing output should be visible
            verbosity = args.verbosity + 1
            patoolib.list_archive(archive,
                                  verbosity=verbosity,
                                  interactive=args.interactive,
                                  password=args.password)
        except PatoolError as msg:
            log_error("error listing %s: %s" % (archive, msg))
            res += 1
    return res
Beispiel #10
0
def handle_archive (archive, command, *args, **kwargs):
    """Handle archive file command; with nice error reporting."""
    try:
        if command == "diff":
            res = _diff_archives(archive, args[0])
        elif command == "repack":
            res = _repack_archive(archive, args[0])
        else:
            _handle_archive(archive, command, *args, **kwargs)
            res = 0
    except KeyboardInterrupt:
        util.log_error("aborted")
        res = 1
    except util.PatoolError, msg:
        util.log_error(msg)
        res = 1
Beispiel #11
0
def main():
    """Parse options and execute commands."""
    try:
        argparser = create_argparser()
        args = argparser.parse_args()
        if args.command is None:
            # Python 3.3.1 under linux allows an empty command somehow
            argparser.error("too few arguments")
        # run subcommand function
        res = globals()["run_%s" % args.command](args)
    except KeyboardInterrupt:
        log_error("aborted")
        res = 1
    except Exception:
        log_internal_error()
        res = 2
    return res
Beispiel #12
0
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
Beispiel #13
0
def rmtree_log_error (func, path, exc):
    """Error function for shutil.rmtree(). Raises a PatoolError."""
    msg = "Error in %s(%s): %s" % (func.__name__, path, str(exc[1]))
    util.log_error(msg)