Skip to content

bpo-29958: Minor improvements to zipfile and tarfile CLI. #944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2450,11 +2450,11 @@ def is_tarfile(name):
def main():
import argparse

description = 'A simple command line interface for tarfile module.'
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 = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-l', '--list', metavar='<tarfile>',
help='Show listing of a tarfile')
group.add_argument('-e', '--extract', nargs='+',
Expand All @@ -2467,7 +2467,7 @@ def main():
help='Test if a tarfile is valid')
args = parser.parse_args()

if args.test:
if args.test is not None:
src = args.test
if is_tarfile(src):
with open(src, 'r') as tar:
Expand All @@ -2478,15 +2478,15 @@ def main():
else:
parser.exit(1, '{!r} is not a tar archive.\n'.format(src))

elif args.list:
elif args.list is not None:
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:
elif args.extract is not None:
if len(args.extract) == 1:
src = args.extract[0]
curdir = os.curdir
Expand All @@ -2508,7 +2508,7 @@ def main():
else:
parser.exit(1, '{!r} is not a tar archive.\n'.format(src))

elif args.create:
elif args.create is not None:
tar_name = args.create.pop(0)
_, ext = os.path.splitext(tar_name)
compressions = {
Expand All @@ -2534,8 +2534,5 @@ def main():
if args.verbose:
print('{!r} file created.'.format(tar_name))

else:
parser.exit(1, parser.format_help())

if __name__ == '__main__':
main()
10 changes: 10 additions & 0 deletions Lib/test/test_tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2167,6 +2167,16 @@ def make_simple_tarfile(self, tar_name):
for tardata in files:
tf.add(tardata, arcname=os.path.basename(tardata))

def test_bad_use(self):
rc, out, err = self.tarfilecmd_failure()
self.assertEqual(out, b'')
self.assertIn(b'usage', err.lower())
self.assertIn(b'error', err.lower())
self.assertIn(b'required', err.lower())
rc, out, err = self.tarfilecmd_failure('-l', '')
self.assertEqual(out, b'')
self.assertNotEqual(err.strip(), b'')

def test_test_command(self):
for tar_name in testtarnames:
for opt in '-t', '--test':
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_zipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2140,6 +2140,16 @@ def zipfilecmd(self, *args, **kwargs):
def zipfilecmd_failure(self, *args):
return script_helper.assert_python_failure('-m', 'zipfile', *args)

def test_bad_use(self):
rc, out, err = self.zipfilecmd_failure()
self.assertEqual(out, b'')
self.assertIn(b'usage', err.lower())
self.assertIn(b'error', err.lower())
self.assertIn(b'required', err.lower())
rc, out, err = self.zipfilecmd_failure('-l', '')
self.assertEqual(out, b'')
self.assertNotEqual(err.strip(), b'')

def test_test_command(self):
zip_name = findfile('zipdir.zip')
for opt in '-t', '--test':
Expand Down
7 changes: 2 additions & 5 deletions Lib/zipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1965,9 +1965,9 @@ def _compile(file, optimize=-1):
def main(args=None):
import argparse

description = 'A simple command line interface for zipfile module.'
description = 'A simple command-line interface for zipfile module.'
parser = argparse.ArgumentParser(description=description)
group = parser.add_mutually_exclusive_group()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-l', '--list', metavar='<zipfile>',
help='Show listing of a zipfile')
group.add_argument('-e', '--extract', nargs=2,
Expand Down Expand Up @@ -2022,8 +2022,5 @@ def addToZip(zf, path, zippath):
zippath = ''
addToZip(zf, path, zippath)

else:
parser.exit(2, parser.format_usage())

if __name__ == "__main__":
main()