Skip to content

Commit 85bb78c

Browse files
committed
Fix command line
1 parent 0db4967 commit 85bb78c

File tree

2 files changed

+41
-29
lines changed

2 files changed

+41
-29
lines changed

src/isal/igzip.py

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import os
2929

3030
import _compression
31+
import sys
3132

3233
from . import isal_zlib
3334

@@ -228,7 +229,7 @@ def main():
228229
parser.description = (
229230
"A simple command line interface for the igzip module. "
230231
"Acts like igzip.")
231-
parser.add_argument("file")
232+
parser.add_argument("file", nargs="?")
232233
compress_group = parser.add_mutually_exclusive_group()
233234
compress_group.add_argument(
234235
"-0", "--fast", action="store_const", dest="compresslevel",
@@ -254,24 +255,35 @@ def main():
254255

255256
compresslevel = args.compresslevel or _COMPRESS_LEVEL_TRADEOFF
256257

257-
if args.compress:
258-
out_filename = args.file + ".gz"
259-
out_open = functools.partial(open, compresslevel=compresslevel)
260-
in_open = io.open
258+
if args.file is None:
259+
if args.compress:
260+
in_file = sys.stdin.buffer
261+
out_file = IGzipFile(mode="wb", compresslevel=compresslevel,
262+
fileobj=sys.stdout.buffer)
263+
else:
264+
in_file = IGzipFile(mode="rb", fileobj=sys.stdin.buffer)
265+
out_file = sys.stdout.buffer
261266
else:
262-
base, extension = os.path.splitext(args.file)
263-
if extension != ".gz":
264-
raise ValueError("Can only decompress files with a .gz extension")
265-
out_filename = base
266-
out_open = io.open
267-
in_open = open
268-
with in_open(args.file, "rb") as in_file:
269-
with out_open(out_filename, "wb") as out_file:
270-
while True:
271-
block = in_file.read(_BLOCK_SIZE)
272-
if block == b"":
273-
break
274-
out_file.write(block)
267+
if args.compress:
268+
in_file = io.open(args.file, mode="rb")
269+
out_file = open(args.file + ".gz", mode="wb",
270+
compresslevel=compresslevel)
271+
else:
272+
base, extension = os.path.splitext(args.file)
273+
if extension != ".gz":
274+
print(f"filename doesn't end in .gz: {args.file}")
275+
return
276+
in_file = open(args.file, "rb")
277+
out_file = io.open(base, "wb")
278+
try:
279+
while True:
280+
block = in_file.read(_BLOCK_SIZE)
281+
if block == b"":
282+
break
283+
out_file.write(block)
284+
finally:
285+
in_file.close()
286+
out_file.close()
275287

276288

277289
if __name__ == "__main__":

tests/test_gzip_compliance.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ def create_and_remove_directory(directory):
772772
def decorator(function):
773773
@functools.wraps(function)
774774
def wrapper(*args, **kwargs):
775-
os.makedirs(directory)
775+
os.makedirs(directory, exist_ok=True)
776776
try:
777777
return function(*args, **kwargs)
778778
finally:
@@ -789,7 +789,7 @@ def test_decompress_stdin_stdout(self):
789789
with igzip.IGzipFile(fileobj=bytes_io, mode='wb') as igzip_file:
790790
igzip_file.write(self.data)
791791

792-
args = sys.executable, '-m', 'igzip', '-d'
792+
args = sys.executable, '-m', 'isal.igzip', '-d'
793793
with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc:
794794
out, err = proc.communicate(bytes_io.getvalue())
795795

@@ -803,7 +803,7 @@ def test_decompress_infile_outfile(self):
803803

804804
with igzip.open(igzipname, mode='wb') as fp:
805805
fp.write(self.data)
806-
rc, out, err = assert_python_ok('-m', 'igzip', '-d', igzipname)
806+
rc, out, err = assert_python_ok('-m', 'isal.igzip', '-d', igzipname)
807807

808808
with open(os.path.join(TEMPDIR, "testigzip"), "rb") as gunziped:
809809
self.assertEqual(gunziped.read(), self.data)
@@ -814,14 +814,14 @@ def test_decompress_infile_outfile(self):
814814
self.assertEqual(err, b'')
815815

816816
def test_decompress_infile_outfile_error(self):
817-
rc, out, err = assert_python_ok('-m', 'igzip', '-d', 'thisisatest.out')
817+
rc, out, err = assert_python_ok('-m', 'isal.igzip', '-d', 'thisisatest.out')
818818
self.assertIn(b"filename doesn't end in .gz:", out)
819819
self.assertEqual(rc, 0)
820820
self.assertEqual(err, b'')
821821

822822
@create_and_remove_directory(TEMPDIR)
823823
def test_compress_stdin_outfile(self):
824-
args = sys.executable, '-m', 'igzip'
824+
args = sys.executable, '-m', 'isal.igzip'
825825
with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc:
826826
out, err = proc.communicate(self.data)
827827

@@ -837,7 +837,7 @@ def test_compress_infile_outfile_default(self):
837837
with open(local_testigzip, 'wb') as fp:
838838
fp.write(self.data)
839839

840-
rc, out, err = assert_python_ok('-m', 'igzip', local_testigzip)
840+
rc, out, err = assert_python_ok('-m', 'isal.igzip', local_testigzip)
841841

842842
self.assertTrue(os.path.exists(igzipname))
843843
self.assertEqual(out, b'')
@@ -854,7 +854,7 @@ def test_compress_infile_outfile(self):
854854
with open(local_testigzip, 'wb') as fp:
855855
fp.write(self.data)
856856

857-
rc, out, err = assert_python_ok('-m', 'igzip', compress_level, local_testigzip)
857+
rc, out, err = assert_python_ok('-m', 'isal.igzip', compress_level, local_testigzip)
858858

859859
self.assertTrue(os.path.exists(igzipname))
860860
self.assertEqual(out, b'')
@@ -863,13 +863,13 @@ def test_compress_infile_outfile(self):
863863
self.assertFalse(os.path.exists(igzipname))
864864

865865
def test_compress_fast_best_are_exclusive(self):
866-
rc, out, err = assert_python_failure('-m', 'igzip', '--fast', '--best')
867-
self.assertIn(b"error: argument --best: not allowed with argument --fast", err)
866+
rc, out, err = assert_python_failure('-m', 'isal.igzip', '--fast', '--best')
867+
self.assertIn(b"error: argument -3/--best: not allowed with argument -0/--fast", err)
868868
self.assertEqual(out, b'')
869869

870870
def test_decompress_cannot_have_flags_compression(self):
871-
rc, out, err = assert_python_failure('-m', 'igzip', '--fast', '-d')
872-
self.assertIn(b'error: argument -d/--decompress: not allowed with argument --fast', err)
871+
rc, out, err = assert_python_failure('-m', 'isal.igzip', '--fast', '-d')
872+
self.assertIn(b'error: argument -d/--decompress: not allowed with argument -0/--fast', err)
873873
self.assertEqual(out, b'')
874874

875875

0 commit comments

Comments
 (0)