()
| 3060 | |
| 3061 | |
| 3062 | def main(): |
| 3063 | import argparse |
| 3064 | |
| 3065 | description = 'A simple command-line interface for tarfile module.' |
| 3066 | parser = argparse.ArgumentParser(description=description, color=True) |
| 3067 | parser.add_argument('-v', '--verbose', action='store_true', default=False, |
| 3068 | help='Verbose output') |
| 3069 | parser.add_argument('--filter', metavar='<filtername>', |
| 3070 | choices=_NAMED_FILTERS, |
| 3071 | help='Filter for extraction') |
| 3072 | |
| 3073 | group = parser.add_mutually_exclusive_group(required=True) |
| 3074 | group.add_argument('-l', '--list', metavar='<tarfile>', |
| 3075 | help='Show listing of a tarfile') |
| 3076 | group.add_argument('-e', '--extract', nargs='+', |
| 3077 | metavar=('<tarfile>', '<output_dir>'), |
| 3078 | help='Extract tarfile into target dir') |
| 3079 | group.add_argument('-c', '--create', nargs='+', |
| 3080 | metavar=('<name>', '<file>'), |
| 3081 | help='Create tarfile from sources') |
| 3082 | group.add_argument('-t', '--test', metavar='<tarfile>', |
| 3083 | help='Test if a tarfile is valid') |
| 3084 | |
| 3085 | args = parser.parse_args() |
| 3086 | |
| 3087 | if args.filter and args.extract is None: |
| 3088 | parser.exit(1, '--filter is only valid for extraction\n') |
| 3089 | |
| 3090 | if args.test is not None: |
| 3091 | src = args.test |
| 3092 | if is_tarfile(src): |
| 3093 | with open(src, 'r') as tar: |
| 3094 | tar.getmembers() |
| 3095 | print(tar.getmembers(), file=sys.stderr) |
| 3096 | if args.verbose: |
| 3097 | print('{!r} is a tar archive.'.format(src)) |
| 3098 | else: |
| 3099 | parser.exit(1, '{!r} is not a tar archive.\n'.format(src)) |
| 3100 | |
| 3101 | elif args.list is not None: |
| 3102 | src = args.list |
| 3103 | if is_tarfile(src): |
| 3104 | with TarFile.open(src, 'r:*') as tf: |
| 3105 | tf.list(verbose=args.verbose) |
| 3106 | else: |
| 3107 | parser.exit(1, '{!r} is not a tar archive.\n'.format(src)) |
| 3108 | |
| 3109 | elif args.extract is not None: |
| 3110 | if len(args.extract) == 1: |
| 3111 | src = args.extract[0] |
| 3112 | curdir = os.curdir |
| 3113 | elif len(args.extract) == 2: |
| 3114 | src, curdir = args.extract |
| 3115 | else: |
| 3116 | parser.exit(1, parser.format_help()) |
| 3117 | |
| 3118 | if is_tarfile(src): |
| 3119 | with TarFile.open(src, 'r:*') as tf: |
no test coverage detected
searching dependent graphs…