|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# |
| 4 | +# This script converts a file's ASCII data into a C char array: |
| 5 | +# |
| 6 | +# const char script_gen[] = { ........ } |
| 7 | +# |
| 8 | + |
| 9 | +# This script was written to be compatible with Python 3.4 |
| 10 | + |
| 11 | +import argparse |
| 12 | +import collections |
| 13 | +import os |
| 14 | +import pathlib |
| 15 | +import subprocess |
| 16 | +import sys |
| 17 | + |
| 18 | +Uglify = collections.namedtuple("Uglify", ['content', 'used_uglifyjs']) |
| 19 | + |
| 20 | + |
| 21 | +def main(): |
| 22 | + args = parse_args() |
| 23 | + minified_result = uglifyjs(args.input) |
| 24 | + write_minified(args.output, minified_result) |
| 25 | + |
| 26 | + |
| 27 | +def write_minified(output_path, minified_result): |
| 28 | + """Write the minified output, escaping things as necessary |
| 29 | +
|
| 30 | + :param output_path: A pathlib.Path() object of the output file. |
| 31 | + :param minified_result: An Uglify() object containing the content to write. |
| 32 | + """ |
| 33 | + with output_path.open('w') as out_file: |
| 34 | + last_char = "" |
| 35 | + print('"', end='', file=out_file) |
| 36 | + for char in minified_result.content: |
| 37 | + # Escape quote symbols |
| 38 | + if char in ('"', ): |
| 39 | + print(r'\"', end='', file=out_file) |
| 40 | + elif last_char == "\\": |
| 41 | + # Special case for new line and carriage return that can appear |
| 42 | + # in a string e.g. print("some string\n"); |
| 43 | + # This is needed because BASH reads the "\n" as two characters, |
| 44 | + # but in a C string it is just one character. |
| 45 | + if char in ('n', 'r'): |
| 46 | + # NOTE(jlvillal): If not 'n' or 'r' we print nothing. This |
| 47 | + # is how 'convert.sh' did it. |
| 48 | + print('\\{}'.format(char), end='', file=out_file) |
| 49 | + elif char == "\n": |
| 50 | + if not minified_result.used_uglifyjs: |
| 51 | + print(r'\n"', end='\n"', file=out_file) |
| 52 | + else: |
| 53 | + print(char, end='', file=out_file) |
| 54 | + last_char = char |
| 55 | + print('"', end='', file=out_file) |
| 56 | + |
| 57 | + |
| 58 | +def uglifyjs(input_path): |
| 59 | + """Run 'uglifyjs' on the input file |
| 60 | +
|
| 61 | + :param input_path: A pathlib.Path() object of the input file. |
| 62 | + :returns: An Uglify() object |
| 63 | + """ |
| 64 | + filename = str(input_path) |
| 65 | + # NOTE(jlvillal): The docs say that '-nc' is the same as '--no-copyright' |
| 66 | + # but in testing it is not. |
| 67 | + # https://github.com/mishoo/UglifyJS/blob/2bc1d02363db3798d5df41fb5059a19edca9b7eb/README.html#L481-L483 |
| 68 | + ugl_args = ["-nc", "-mt"] |
| 69 | + |
| 70 | + # uglifyjs seems to have vastly different options between versions, this |
| 71 | + # should work with both |
| 72 | + cmd_line = ['uglifyjs', '--version'] |
| 73 | + try: |
| 74 | + result = subprocess.call(cmd_line, stdout=subprocess.DEVNULL) |
| 75 | + except FileNotFoundError: |
| 76 | + # We don't have uglifyjs, so return the contents of the file |
| 77 | + return Uglify(input_path.read_text(), False) |
| 78 | + |
| 79 | + if result == 0: |
| 80 | + # We have newer uglifyjs |
| 81 | + cmd_line = ['uglifyjs', filename] + ugl_args |
| 82 | + else: |
| 83 | + # Older uglifyjs |
| 84 | + cmd_line = ['uglifyjs'] + ugl_args + [filename] |
| 85 | + try: |
| 86 | + output = subprocess.check_output(cmd_line) |
| 87 | + except subprocess.CalledProcessError as exc: |
| 88 | + print("ERROR: Minification failed!") |
| 89 | + sys.exit(exc.returncode) |
| 90 | + return Uglify(output.decode('utf-8'), True) |
| 91 | + |
| 92 | + |
| 93 | +def parse_args(): |
| 94 | + parser = argparse.ArgumentParser( |
| 95 | + description=("Runs minifier on a JavaScript file and writes it out in " |
| 96 | + "a C string format.")) |
| 97 | + parser.add_argument("input", metavar='INPUT_FILE') |
| 98 | + parser.add_argument("output", metavar='OUTPUT_FILE') |
| 99 | + args = parser.parse_args() |
| 100 | + # Make all paths absolute and expand any "~/" usage. |
| 101 | + for arg_name in ('input', 'output'): |
| 102 | + setattr(args, arg_name, pathlib.Path( |
| 103 | + os.path.abspath(os.path.expanduser(getattr(args, arg_name))))) |
| 104 | + if not args.input.is_file(): |
| 105 | + print("ERROR: The INPUT_FILE {!r} does not exist.".format( |
| 106 | + str(args.input))) |
| 107 | + parser.print_help() |
| 108 | + sys.exit(1) |
| 109 | + return args |
| 110 | + |
| 111 | + |
| 112 | +if '__main__' == __name__: |
| 113 | + sys.exit(main()) |
0 commit comments