mirror of
https://github.com/python/cpython.git
synced 2025-11-08 13:42:22 +00:00
Issue #28115: Command-line interface of the zipfile module now uses argparse.
Added support of long options.
This commit is contained in:
parent
e4bdf4fce5
commit
8c9331057d
3 changed files with 69 additions and 65 deletions
|
|
@ -2055,7 +2055,8 @@ class CommandLineTest(unittest.TestCase):
|
||||||
|
|
||||||
def test_test_command(self):
|
def test_test_command(self):
|
||||||
zip_name = findfile('zipdir.zip')
|
zip_name = findfile('zipdir.zip')
|
||||||
out = self.zipfilecmd('-t', zip_name)
|
for opt in '-t', '--test':
|
||||||
|
out = self.zipfilecmd(opt, zip_name)
|
||||||
self.assertEqual(out.rstrip(), b'Done testing')
|
self.assertEqual(out.rstrip(), b'Done testing')
|
||||||
zip_name = findfile('testtar.tar')
|
zip_name = findfile('testtar.tar')
|
||||||
rc, out, err = self.zipfilecmd_failure('-t', zip_name)
|
rc, out, err = self.zipfilecmd_failure('-t', zip_name)
|
||||||
|
|
@ -2067,7 +2068,8 @@ class CommandLineTest(unittest.TestCase):
|
||||||
with zipfile.ZipFile(zip_name, 'r') as tf:
|
with zipfile.ZipFile(zip_name, 'r') as tf:
|
||||||
tf.printdir(t)
|
tf.printdir(t)
|
||||||
expected = t.getvalue().encode('ascii', 'backslashreplace')
|
expected = t.getvalue().encode('ascii', 'backslashreplace')
|
||||||
out = self.zipfilecmd('-l', zip_name,
|
for opt in '-l', '--list':
|
||||||
|
out = self.zipfilecmd(opt, zip_name,
|
||||||
PYTHONIOENCODING='ascii:backslashreplace')
|
PYTHONIOENCODING='ascii:backslashreplace')
|
||||||
self.assertEqual(out, expected)
|
self.assertEqual(out, expected)
|
||||||
|
|
||||||
|
|
@ -2081,8 +2083,9 @@ class CommandLineTest(unittest.TestCase):
|
||||||
f.write('test 2')
|
f.write('test 2')
|
||||||
files = [TESTFN, TESTFNDIR]
|
files = [TESTFN, TESTFNDIR]
|
||||||
namelist = [TESTFN, TESTFNDIR + '/', TESTFNDIR + '/file.txt']
|
namelist = [TESTFN, TESTFNDIR + '/', TESTFNDIR + '/file.txt']
|
||||||
|
for opt in '-c', '--create':
|
||||||
try:
|
try:
|
||||||
out = self.zipfilecmd('-c', TESTFN2, *files)
|
out = self.zipfilecmd(opt, TESTFN2, *files)
|
||||||
self.assertEqual(out, b'')
|
self.assertEqual(out, b'')
|
||||||
with zipfile.ZipFile(TESTFN2) as zf:
|
with zipfile.ZipFile(TESTFN2) as zf:
|
||||||
self.assertEqual(zf.namelist(), namelist)
|
self.assertEqual(zf.namelist(), namelist)
|
||||||
|
|
@ -2093,8 +2096,9 @@ class CommandLineTest(unittest.TestCase):
|
||||||
|
|
||||||
def test_extract_command(self):
|
def test_extract_command(self):
|
||||||
zip_name = findfile('zipdir.zip')
|
zip_name = findfile('zipdir.zip')
|
||||||
|
for opt in '-e', '--extract':
|
||||||
with temp_dir() as extdir:
|
with temp_dir() as extdir:
|
||||||
out = self.zipfilecmd('-e', zip_name, extdir)
|
out = self.zipfilecmd(opt, zip_name, extdir)
|
||||||
self.assertEqual(out, b'')
|
self.assertEqual(out, b'')
|
||||||
with zipfile.ZipFile(zip_name) as zf:
|
with zipfile.ZipFile(zip_name) as zf:
|
||||||
for zi in zf.infolist():
|
for zi in zf.infolist():
|
||||||
|
|
|
||||||
|
|
@ -1951,50 +1951,44 @@ class PyZipFile(ZipFile):
|
||||||
|
|
||||||
|
|
||||||
def main(args=None):
|
def main(args=None):
|
||||||
import textwrap
|
import argparse
|
||||||
USAGE=textwrap.dedent("""\
|
|
||||||
Usage:
|
|
||||||
zipfile.py -l zipfile.zip # Show listing of a zipfile
|
|
||||||
zipfile.py -t zipfile.zip # Test if a zipfile is valid
|
|
||||||
zipfile.py -e zipfile.zip target # Extract zipfile into target dir
|
|
||||||
zipfile.py -c zipfile.zip src ... # Create zipfile from sources
|
|
||||||
""")
|
|
||||||
if args is None:
|
|
||||||
args = sys.argv[1:]
|
|
||||||
|
|
||||||
if not args or args[0] not in ('-l', '-c', '-e', '-t'):
|
description = 'A simple command line interface for zipfile module.'
|
||||||
print(USAGE, file=sys.stderr)
|
parser = argparse.ArgumentParser(description=description)
|
||||||
sys.exit(1)
|
group = parser.add_mutually_exclusive_group()
|
||||||
|
group.add_argument('-l', '--list', metavar='<zipfile>',
|
||||||
|
help='Show listing of a zipfile')
|
||||||
|
group.add_argument('-e', '--extract', nargs=2,
|
||||||
|
metavar=('<zipfile>', '<output_dir>'),
|
||||||
|
help='Extract zipfile into target dir')
|
||||||
|
group.add_argument('-c', '--create', nargs='+',
|
||||||
|
metavar=('<name>', '<file>'),
|
||||||
|
help='Create zipfile from sources')
|
||||||
|
group.add_argument('-t', '--test', metavar='<zipfile>',
|
||||||
|
help='Test if a zipfile is valid')
|
||||||
|
args = parser.parse_args(args)
|
||||||
|
|
||||||
if args[0] == '-l':
|
if args.test is not None:
|
||||||
if len(args) != 2:
|
src = args.test
|
||||||
print(USAGE, file=sys.stderr)
|
with ZipFile(src, 'r') as zf:
|
||||||
sys.exit(1)
|
|
||||||
with ZipFile(args[1], 'r') as zf:
|
|
||||||
zf.printdir()
|
|
||||||
|
|
||||||
elif args[0] == '-t':
|
|
||||||
if len(args) != 2:
|
|
||||||
print(USAGE, file=sys.stderr)
|
|
||||||
sys.exit(1)
|
|
||||||
with ZipFile(args[1], 'r') as zf:
|
|
||||||
badfile = zf.testzip()
|
badfile = zf.testzip()
|
||||||
if badfile:
|
if badfile:
|
||||||
print("The following enclosed file is corrupted: {!r}".format(badfile))
|
print("The following enclosed file is corrupted: {!r}".format(badfile))
|
||||||
print("Done testing")
|
print("Done testing")
|
||||||
|
|
||||||
elif args[0] == '-e':
|
elif args.list is not None:
|
||||||
if len(args) != 3:
|
src = args.list
|
||||||
print(USAGE, file=sys.stderr)
|
with ZipFile(src, 'r') as zf:
|
||||||
sys.exit(1)
|
zf.printdir()
|
||||||
|
|
||||||
with ZipFile(args[1], 'r') as zf:
|
elif args.extract is not None:
|
||||||
zf.extractall(args[2])
|
src, curdir = args.extract
|
||||||
|
with ZipFile(src, 'r') as zf:
|
||||||
|
zf.extractall(curdir)
|
||||||
|
|
||||||
elif args[0] == '-c':
|
elif args.create is not None:
|
||||||
if len(args) < 3:
|
zip_name = args.create.pop(0)
|
||||||
print(USAGE, file=sys.stderr)
|
files = args.create
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
def addToZip(zf, path, zippath):
|
def addToZip(zf, path, zippath):
|
||||||
if os.path.isfile(path):
|
if os.path.isfile(path):
|
||||||
|
|
@ -2007,8 +2001,8 @@ def main(args = None):
|
||||||
os.path.join(path, nm), os.path.join(zippath, nm))
|
os.path.join(path, nm), os.path.join(zippath, nm))
|
||||||
# else: ignore
|
# else: ignore
|
||||||
|
|
||||||
with ZipFile(args[1], 'w') as zf:
|
with ZipFile(zip_name, 'w') as zf:
|
||||||
for path in args[2:]:
|
for path in files:
|
||||||
zippath = os.path.basename(path)
|
zippath = os.path.basename(path)
|
||||||
if not zippath:
|
if not zippath:
|
||||||
zippath = os.path.basename(os.path.dirname(path))
|
zippath = os.path.basename(os.path.dirname(path))
|
||||||
|
|
@ -2016,5 +2010,8 @@ def main(args = None):
|
||||||
zippath = ''
|
zippath = ''
|
||||||
addToZip(zf, path, zippath)
|
addToZip(zf, path, zippath)
|
||||||
|
|
||||||
|
else:
|
||||||
|
parser.exit(2, parser.format_usage())
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
|
||||||
|
|
@ -97,6 +97,9 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #28115: Command-line interface of the zipfile module now uses argparse.
|
||||||
|
Added support of long options.
|
||||||
|
|
||||||
- Issue #18219: Optimize csv.DictWriter for large number of columns.
|
- Issue #18219: Optimize csv.DictWriter for large number of columns.
|
||||||
Patch by Mariatta Wijaya.
|
Patch by Mariatta Wijaya.
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue