-
Notifications
You must be signed in to change notification settings - Fork 7
/
__main__.py
104 lines (85 loc) · 3.11 KB
/
__main__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# Copyright (c) 2016 Iotic Labs Ltd. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://github.com/Iotic-Labs/py-lz4framed/blob/master/LICENSE
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""(de)compresses to/from lz4-framed data"""
from __future__ import print_function
from sys import argv, stderr
from .compat import STDIN_RAW, STDOUT_RAW
from . import Compressor, Decompressor, Lz4FramedError, Lz4FramedNoDataError, get_block_size
def __error(*args, **kwargs):
print(*args, file=stderr, **kwargs)
def do_compress(in_stream, out_stream):
read = in_stream.read
read_size = get_block_size()
try:
with Compressor(out_stream) as compressor:
try:
while True:
compressor.update(read(read_size))
# empty read result supplied to update()
except Lz4FramedNoDataError:
pass
# input stream exception
except EOFError:
pass
except Lz4FramedError as ex:
__error('Compression error: %s' % ex)
return 8
return 0
def do_decompress(in_stream, out_stream):
write = out_stream.write
try:
for chunk in Decompressor(in_stream):
write(chunk)
except Lz4FramedError as ex:
__error('Compression error: %s' % ex)
return 8
return 0
__ACTION = frozenset(('compress', 'decompress'))
def main(): # noqa (complexity)
if not (3 <= len(argv) <= 4 and argv[1] in __ACTION):
print("""USAGE: lz4framed (compress|decompress) (INFILE|-) [OUTFILE]
(De)compresses an lz4 frame. Input is read from INFILE unless set to '-', in
which case stdin is used. If OUTFILE is not specified, output goes to stdout.""", file=stderr)
return 1
compress = (argv[1] == 'compress')
in_file = out_file = None
try:
# input
if argv[2] == '-':
in_stream = STDIN_RAW
else:
try:
in_stream = in_file = open(argv[2], 'rb')
except IOError as ex:
__error('Failed to open input file for reading: %s' % ex)
return 2
# output
if len(argv) == 3:
out_stream = STDOUT_RAW
else:
try:
out_stream = out_file = open(argv[3], 'ab')
except IOError as ex:
__error('Failed to open output file for appending: %s' % ex)
return 4
return (do_compress if compress else do_decompress)(in_stream, out_stream)
except IOError as ex:
__error('I/O failure: %s' % ex)
finally:
if in_file:
in_file.close()
if out_file:
out_file.close()
if __name__ == "__main__":
exit(main())