make-js-file-arrays.py
1 #!/usr/bin/env python 2 # Copyright (C) 2014 Apple Inc. All rights reserved. 3 # 4 # Redistribution and use in source and binary forms, with or without 5 # modification, are permitted provided that the following conditions 6 # are met: 7 # 1. Redistributions of source code must retain the above copyright 8 # notice, this list of conditions and the following disclaimer. 9 # 2. Redistributions in binary form must reproduce the above copyright 10 # notice, this list of conditions and the following disclaimer in the 11 # documentation and/or other materials provided with the distribution. 12 # 13 # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY 14 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 # DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY 17 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20 # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 24 from __future__ import print_function 25 import io 26 import os 27 from optparse import OptionParser 28 import sys 29 from jsmin import jsmin 30 is_3 = sys.version_info >= (3, 0) 31 32 33 def stringifyCodepoint(code): 34 if code < 128: 35 return '{0:d}'.format(code) 36 else: 37 return "'\\x{0:02x}'".format(code) 38 39 40 def chunk(list, chunkSize): 41 for i in range(0, len(list), chunkSize): 42 yield list[i:i + chunkSize] 43 44 45 def main(): 46 parser = OptionParser(usage="usage: %prog [options] header source [input [input...]]") 47 parser.add_option('--no-minify', action='store_true', help='Do not run the input files through jsmin') 48 parser.add_option('-n', '--namespace', help='Namespace to use') 49 (options, arguments) = parser.parse_args() 50 if not options.namespace: 51 print('Error: must provide a namespace') 52 parser.print_usage() 53 exit(-1) 54 if len(arguments) < 3: 55 print('Error: must provide at least 3 arguments') 56 parser.print_usage() 57 exit(-1) 58 59 namespace = options.namespace 60 headerPath = arguments[0] 61 sourcePath = arguments[1] 62 inputPaths = arguments[2:] 63 64 headerFile = open(headerPath, 'w') 65 print('namespace {0:s} {{'.format(namespace), file=headerFile) 66 67 sourceFile = open(sourcePath, 'w') 68 print('#include "{0:s}"'.format(os.path.basename(headerPath)), file=sourceFile) 69 print('namespace {0:s} {{'.format(namespace), file=sourceFile) 70 71 for inputFileName in inputPaths: 72 73 if is_3: 74 inputStream = io.open(inputFileName, encoding='utf-8') 75 else: 76 inputStream = io.FileIO(inputFileName) 77 78 data = inputStream.read() 79 80 if not options.no_minify: 81 characters = jsmin(data) 82 else: 83 characters = data 84 85 if is_3: 86 codepoints = bytearray(characters, encoding='utf-8') 87 else: 88 codepoints = list(map(ord, characters)) 89 90 # Use the size of codepoints instead of the characters 91 # because UTF-8 characters may need more than one byte. 92 size = len(codepoints) 93 94 variableName = os.path.splitext(os.path.basename(inputFileName))[0] 95 96 print('extern const char {0:s}JavaScript[{1:d}];'.format(variableName, size), file=headerFile) 97 print('const char {0:s}JavaScript[{1:d}] = {{'.format(variableName, size), file=sourceFile) 98 99 for codepointChunk in chunk(codepoints, 16): 100 print(' {0:s},'.format(','.join(map(stringifyCodepoint, codepointChunk))), file=sourceFile) 101 102 print('};', file=sourceFile) 103 104 print('}} // namespace {0:s}'.format(namespace), file=headerFile) 105 print('}} // namespace {0:s}'.format(namespace), file=sourceFile) 106 107 if __name__ == '__main__': 108 main()