diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2013-11-23 23:53:29 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2013-11-23 23:53:29 (GMT) |
commit | d27b455bbcedd232ad8490acff46f532a365be49 (patch) | |
tree | f81a8c6bf695aba1bf93e030a3a25b29a845f140 /Lib/tarfile.py | |
parent | 44e2eaab5491881120aab43e2838da8afe7ab70e (diff) | |
download | cpython-d27b455bbcedd232ad8490acff46f532a365be49.zip cpython-d27b455bbcedd232ad8490acff46f532a365be49.tar.gz cpython-d27b455bbcedd232ad8490acff46f532a365be49.tar.bz2 |
Issue #13477: Added command line interface to the tarfile module.
Original patch by Berker Peksag.
Diffstat (limited to 'Lib/tarfile.py')
-rw-r--r-- | Lib/tarfile.py | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py index 94bd75d..f4df6c7 100644 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -2404,3 +2404,97 @@ def is_tarfile(name): bltn_open = open open = TarFile.open + + +def main(): + import argparse + + description = 'A simple command line interface for tarfile module.' + parser = argparse.ArgumentParser(description=description) + parser.add_argument('-v', '--verbose', action='store_true', default=False, + help='Verbose output') + group = parser.add_mutually_exclusive_group() + group.add_argument('-l', '--list', metavar='<tarfile>', + help='Show listing of a tarfile') + group.add_argument('-e', '--extract', nargs='+', + metavar=('<tarfile>', '<output_dir>'), + help='Extract tarfile into target dir') + group.add_argument('-c', '--create', nargs='+', + metavar=('<name>', '<file>'), + help='Create tarfile from sources') + group.add_argument('-t', '--test', metavar='<tarfile>', + help='Test if a tarfile is valid') + args = parser.parse_args() + + if args.test: + src = args.test + if is_tarfile(src): + with open(src, 'r') as tar: + tar.getmembers() + print(tar.getmembers(), file=sys.stderr) + if args.verbose: + print('{!r} is a tar archive.'.format(src)) + else: + parser.exit(1, '{!r} is not a tar archive.\n'.format(src)) + + elif args.list: + src = args.list + if is_tarfile(src): + with TarFile.open(src, 'r:*') as tf: + tf.list(verbose=args.verbose) + else: + parser.exit(1, '{!r} is not a tar archive.\n'.format(src)) + + elif args.extract: + if len(args.extract) == 1: + src = args.extract[0] + curdir = os.curdir + elif len(args.extract) == 2: + src, curdir = args.extract + else: + parser.exit(1, parser.format_help()) + + if is_tarfile(src): + with TarFile.open(src, 'r:*') as tf: + tf.extractall(path=curdir) + if args.verbose: + if curdir == '.': + msg = '{!r} file is extracted.'.format(src) + else: + msg = ('{!r} file is extracted ' + 'into {!r} directory.').format(src, curdir) + print(msg) + else: + parser.exit(1, '{!r} is not a tar archive.\n'.format(src)) + + elif args.create: + tar_name = args.create.pop(0) + _, ext = os.path.splitext(tar_name) + compressions = { + # gz + 'gz': 'gz', + 'tgz': 'gz', + # xz + 'xz': 'xz', + 'txz': 'xz', + # bz2 + 'bz2': 'bz2', + 'tbz': 'bz2', + 'tbz2': 'bz2', + 'tb2': 'bz2', + } + tar_mode = 'w:' + compressions[ext] if ext in compressions else 'w' + tar_files = args.create + + with TarFile.open(tar_name, tar_mode) as tf: + for file_name in tar_files: + tf.add(file_name) + + if args.verbose: + print('{!r} file created.'.format(tar_name)) + + else: + parser.exit(1, parser.format_help()) + +if __name__ == '__main__': + main() |