Cosimo Lupo | b33e10e | 2015-03-16 17:56:10 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Alex Nicksay | 4651f7c | 2016-12-12 04:28:15 -0500 | [diff] [blame] | 2 | """Compression/decompression utility using the Brotli algorithm.""" |
Cosimo Lupo | b33e10e | 2015-03-16 17:56:10 +0000 | [diff] [blame] | 3 | |
| 4 | from __future__ import print_function |
Cosimo Lupo | 32c44ec | 2015-05-08 14:17:20 +0100 | [diff] [blame] | 5 | import argparse |
Cosimo Lupo | b33e10e | 2015-03-16 17:56:10 +0000 | [diff] [blame] | 6 | import sys |
| 7 | import os |
Cosimo Lupo | b316cb7 | 2015-04-16 12:41:40 +0100 | [diff] [blame] | 8 | import platform |
Cosimo Lupo | b33e10e | 2015-03-16 17:56:10 +0000 | [diff] [blame] | 9 | |
Alex Nicksay | 4651f7c | 2016-12-12 04:28:15 -0500 | [diff] [blame] | 10 | import brotli |
Cosimo Lupo | b33e10e | 2015-03-16 17:56:10 +0000 | [diff] [blame] | 11 | |
Cosimo Lupo | 4106a40 | 2015-05-08 15:46:56 +0100 | [diff] [blame] | 12 | # default values of encoder parameters |
| 13 | DEFAULT_PARAMS = { |
Cosimo Lupo | c93c0da | 2015-05-11 11:10:48 +0100 | [diff] [blame] | 14 | 'mode': brotli.MODE_GENERIC, |
Cosimo Lupo | 4106a40 | 2015-05-08 15:46:56 +0100 | [diff] [blame] | 15 | 'quality': 11, |
| 16 | 'lgwin': 22, |
| 17 | 'lgblock': 0, |
Cosimo Lupo | b33e10e | 2015-03-16 17:56:10 +0000 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | |
| 21 | def get_binary_stdio(stream): |
| 22 | """ Return the specified standard input, output or errors stream as a |
| 23 | 'raw' buffer object suitable for reading/writing binary data from/to it. |
| 24 | """ |
Alex Nicksay | 4651f7c | 2016-12-12 04:28:15 -0500 | [diff] [blame] | 25 | assert stream in ['stdin', 'stdout', 'stderr'], 'invalid stream name' |
Cosimo Lupo | b33e10e | 2015-03-16 17:56:10 +0000 | [diff] [blame] | 26 | stdio = getattr(sys, stream) |
| 27 | if sys.version_info[0] < 3: |
| 28 | if sys.platform == 'win32': |
| 29 | # set I/O stream binary flag on python2.x (Windows) |
Cosimo Lupo | b316cb7 | 2015-04-16 12:41:40 +0100 | [diff] [blame] | 30 | runtime = platform.python_implementation() |
Alex Nicksay | 4651f7c | 2016-12-12 04:28:15 -0500 | [diff] [blame] | 31 | if runtime == 'PyPy': |
Cosimo Lupo | b316cb7 | 2015-04-16 12:41:40 +0100 | [diff] [blame] | 32 | # the msvcrt trick doesn't work in pypy, so I use fdopen |
Alex Nicksay | 4651f7c | 2016-12-12 04:28:15 -0500 | [diff] [blame] | 33 | mode = 'rb' if stream == 'stdin' else 'wb' |
Cosimo Lupo | b316cb7 | 2015-04-16 12:41:40 +0100 | [diff] [blame] | 34 | stdio = os.fdopen(stdio.fileno(), mode, 0) |
| 35 | else: |
| 36 | # this works with CPython -- untested on other implementations |
| 37 | import msvcrt |
| 38 | msvcrt.setmode(stdio.fileno(), os.O_BINARY) |
Cosimo Lupo | b33e10e | 2015-03-16 17:56:10 +0000 | [diff] [blame] | 39 | return stdio |
| 40 | else: |
| 41 | # get 'buffer' attribute to read/write binary data on python3.x |
| 42 | if hasattr(stdio, 'buffer'): |
| 43 | return stdio.buffer |
| 44 | else: |
Alex Nicksay | 4651f7c | 2016-12-12 04:28:15 -0500 | [diff] [blame] | 45 | orig_stdio = getattr(sys, '__%s__' % stream) |
Cosimo Lupo | b33e10e | 2015-03-16 17:56:10 +0000 | [diff] [blame] | 46 | return orig_stdio.buffer |
| 47 | |
| 48 | |
Cosimo Lupo | 82ffc4d | 2015-10-05 18:57:32 +0100 | [diff] [blame] | 49 | def main(args=None): |
Cosimo Lupo | b33e10e | 2015-03-16 17:56:10 +0000 | [diff] [blame] | 50 | |
Cosimo Lupo | 32c44ec | 2015-05-08 14:17:20 +0100 | [diff] [blame] | 51 | parser = argparse.ArgumentParser( |
Alex Nicksay | 4651f7c | 2016-12-12 04:28:15 -0500 | [diff] [blame] | 52 | prog=os.path.basename(__file__), description=__doc__) |
| 53 | parser.add_argument( |
| 54 | '--version', action='version', version=brotli.__version__) |
| 55 | parser.add_argument( |
| 56 | '-i', |
| 57 | '--input', |
| 58 | metavar='FILE', |
| 59 | type=str, |
| 60 | dest='infile', |
| 61 | help='Input file', |
| 62 | default=None) |
| 63 | parser.add_argument( |
| 64 | '-o', |
| 65 | '--output', |
| 66 | metavar='FILE', |
| 67 | type=str, |
| 68 | dest='outfile', |
| 69 | help='Output file', |
| 70 | default=None) |
| 71 | parser.add_argument( |
| 72 | '-f', |
| 73 | '--force', |
| 74 | action='store_true', |
| 75 | help='Overwrite existing output file', |
| 76 | default=False) |
| 77 | parser.add_argument( |
| 78 | '-d', |
| 79 | '--decompress', |
| 80 | action='store_true', |
| 81 | help='Decompress input file', |
| 82 | default=False) |
Cosimo Lupo | 4106a40 | 2015-05-08 15:46:56 +0100 | [diff] [blame] | 83 | params = parser.add_argument_group('optional encoder parameters') |
Alex Nicksay | 4651f7c | 2016-12-12 04:28:15 -0500 | [diff] [blame] | 84 | params.add_argument( |
| 85 | '-m', |
| 86 | '--mode', |
| 87 | metavar='MODE', |
| 88 | type=int, |
| 89 | choices=[0, 1, 2], |
| 90 | help='The compression mode can be 0 for generic input, ' |
| 91 | '1 for UTF-8 encoded text, or 2 for WOFF 2.0 font data. ' |
| 92 | 'Defaults to 0.') |
| 93 | params.add_argument( |
| 94 | '-q', |
| 95 | '--quality', |
| 96 | metavar='QUALITY', |
| 97 | type=int, |
| 98 | choices=list(range(0, 12)), |
| 99 | help='Controls the compression-speed vs compression-density ' |
| 100 | 'tradeoff. The higher the quality, the slower the ' |
| 101 | 'compression. Range is 0 to 11. Defaults to 11.') |
| 102 | params.add_argument( |
| 103 | '--lgwin', |
| 104 | metavar='LGWIN', |
| 105 | type=int, |
| 106 | choices=list(range(10, 25)), |
| 107 | help='Base 2 logarithm of the sliding window size. Range is ' |
| 108 | '10 to 24. Defaults to 22.') |
| 109 | params.add_argument( |
| 110 | '--lgblock', |
| 111 | metavar='LGBLOCK', |
| 112 | type=int, |
| 113 | choices=[0] + list(range(16, 25)), |
| 114 | help='Base 2 logarithm of the maximum input block size. ' |
| 115 | 'Range is 16 to 24. If set to 0, the value will be set based ' |
| 116 | 'on the quality. Defaults to 0.') |
Cosimo Lupo | 4106a40 | 2015-05-08 15:46:56 +0100 | [diff] [blame] | 117 | # set default values using global DEFAULT_PARAMS dictionary |
| 118 | parser.set_defaults(**DEFAULT_PARAMS) |
Cosimo Lupo | 32c44ec | 2015-05-08 14:17:20 +0100 | [diff] [blame] | 119 | |
Cosimo Lupo | 82ffc4d | 2015-10-05 18:57:32 +0100 | [diff] [blame] | 120 | options = parser.parse_args(args=args) |
Cosimo Lupo | b33e10e | 2015-03-16 17:56:10 +0000 | [diff] [blame] | 121 | |
| 122 | if options.infile: |
| 123 | if not os.path.isfile(options.infile): |
Cosimo Lupo | 32c44ec | 2015-05-08 14:17:20 +0100 | [diff] [blame] | 124 | parser.error('file "%s" not found' % options.infile) |
Alex Nicksay | 4651f7c | 2016-12-12 04:28:15 -0500 | [diff] [blame] | 125 | with open(options.infile, 'rb') as infile: |
Cosimo Lupo | b33e10e | 2015-03-16 17:56:10 +0000 | [diff] [blame] | 126 | data = infile.read() |
| 127 | else: |
| 128 | if sys.stdin.isatty(): |
| 129 | # interactive console, just quit |
Cosimo Lupo | 32c44ec | 2015-05-08 14:17:20 +0100 | [diff] [blame] | 130 | parser.error('no input') |
Cosimo Lupo | b33e10e | 2015-03-16 17:56:10 +0000 | [diff] [blame] | 131 | infile = get_binary_stdio('stdin') |
| 132 | data = infile.read() |
| 133 | |
| 134 | if options.outfile: |
| 135 | if os.path.isfile(options.outfile) and not options.force: |
Cosimo Lupo | 32c44ec | 2015-05-08 14:17:20 +0100 | [diff] [blame] | 136 | parser.error('output file exists') |
Alex Nicksay | 4651f7c | 2016-12-12 04:28:15 -0500 | [diff] [blame] | 137 | outfile = open(options.outfile, 'wb') |
Cosimo Lupo | b33e10e | 2015-03-16 17:56:10 +0000 | [diff] [blame] | 138 | else: |
| 139 | outfile = get_binary_stdio('stdout') |
| 140 | |
| 141 | try: |
| 142 | if options.decompress: |
Eugene Kliuchnikov | d63e8f7 | 2017-08-04 10:02:56 +0200 | [diff] [blame] | 143 | data = brotli.decompress(data) |
Cosimo Lupo | b33e10e | 2015-03-16 17:56:10 +0000 | [diff] [blame] | 144 | else: |
Cosimo Lupo | 4106a40 | 2015-05-08 15:46:56 +0100 | [diff] [blame] | 145 | data = brotli.compress( |
Alex Nicksay | 4651f7c | 2016-12-12 04:28:15 -0500 | [diff] [blame] | 146 | data, |
| 147 | mode=options.mode, |
| 148 | quality=options.quality, |
| 149 | lgwin=options.lgwin, |
Eugene Kliuchnikov | d63e8f7 | 2017-08-04 10:02:56 +0200 | [diff] [blame] | 150 | lgblock=options.lgblock) |
Cosimo Lupo | b33e10e | 2015-03-16 17:56:10 +0000 | [diff] [blame] | 151 | except brotli.error as e: |
Alex Nicksay | 4651f7c | 2016-12-12 04:28:15 -0500 | [diff] [blame] | 152 | parser.exit(1, |
| 153 | 'bro: error: %s: %s' % (e, options.infile or 'sys.stdin')) |
Cosimo Lupo | b33e10e | 2015-03-16 17:56:10 +0000 | [diff] [blame] | 154 | |
| 155 | outfile.write(data) |
| 156 | outfile.close() |
| 157 | |
| 158 | |
Cosimo Lupo | b33e10e | 2015-03-16 17:56:10 +0000 | [diff] [blame] | 159 | if __name__ == '__main__': |
Cosimo Lupo | 32c44ec | 2015-05-08 14:17:20 +0100 | [diff] [blame] | 160 | main() |