OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 # for details. All rights reserved. Use of this source code is governed by a |
| 3 # BSD-style license that can be found in the LICENSE file. |
| 4 # |
| 5 # This python script creates a tar archive and a C++ source file which contains |
| 6 # the tar archive as an array of bytes. |
| 7 |
| 8 import os |
| 9 import sys |
| 10 from os.path import join, splitext |
| 11 import time |
| 12 from optparse import OptionParser |
| 13 from datetime import date |
| 14 import tarfile |
| 15 import tempfile |
| 16 |
| 17 def makeArchive(tar_path, client_root, files): |
| 18 tar = tarfile.open(tar_path, mode='w') |
| 19 for input_file_name in files: |
| 20 # Chop off client_root. |
| 21 archive_file_name = input_file_name[ len(client_root) : ] |
| 22 # Open input file and add it to the archive. |
| 23 with open(input_file_name, 'rb') as input_file: |
| 24 tarInfo = tarfile.TarInfo(name=archive_file_name) |
| 25 input_file.seek(0,2) |
| 26 tarInfo.size = input_file.tell() |
| 27 input_file.seek(0) |
| 28 tar.addfile(tarInfo, fileobj=input_file) |
| 29 tar.close() |
| 30 |
| 31 def writeCCFile(output_file, |
| 32 outer_namespace, |
| 33 inner_namespace, |
| 34 name, |
| 35 tar_archive, |
| 36 ): |
| 37 cc_text = ''' |
| 38 // Copyright (c) %d, the Dart project authors. Please see the AUTHORS file |
| 39 // for details. All rights reserved. Use of this source code is governed by a |
| 40 // BSD-style license that can be found in the LICENSE file. |
| 41 |
| 42 ''' % date.today().year |
| 43 cc_text += 'namespace %s {\n' % outer_namespace |
| 44 if inner_namespace != None: |
| 45 cc_text += 'namespace %s {\n' % inner_namespace |
| 46 cc_text += '\n\n' |
| 47 # Write the archive. |
| 48 cc_text += 'static const char %s_[] = {\n ' % name |
| 49 lineCounter = 0 |
| 50 for byte in tar_archive: |
| 51 cc_text += r" '\x%02x'," % ord(byte) |
| 52 lineCounter += 1 |
| 53 if lineCounter == 10: |
| 54 cc_text += '\n ' |
| 55 lineCounter = 0 |
| 56 if lineCounter != 0: |
| 57 cc_text += '\n ' |
| 58 cc_text += '};\n' |
| 59 cc_text += '\nunsigned int %s_len = %d;\n' % (name, len(tar_archive)) |
| 60 cc_text += '\nconst char* %s = %s_;\n' % (name, name) |
| 61 if inner_namespace != None: |
| 62 cc_text += '} // namespace %s\n' % inner_namespace |
| 63 cc_text += '} // namespace %s\n' % outer_namespace |
| 64 |
| 65 open(output_file, 'w').write(cc_text) |
| 66 |
| 67 def main(args): |
| 68 try: |
| 69 # Parse input. |
| 70 parser = OptionParser() |
| 71 parser.add_option("--output", |
| 72 action="store", type="string", |
| 73 help="output file name") |
| 74 parser.add_option("--tar_output", |
| 75 action="store", type="string", |
| 76 help="tar output file name") |
| 77 parser.add_option("--outer_namespace", |
| 78 action="store", type="string", |
| 79 help="outer C++ namespace", |
| 80 default="dart") |
| 81 parser.add_option("--inner_namespace", |
| 82 action="store", type="string", |
| 83 help="inner C++ namespace", |
| 84 default="bin") |
| 85 parser.add_option("--name", |
| 86 action="store", type="string", |
| 87 help="name of tar archive symbol") |
| 88 parser.add_option("--client_root", |
| 89 action="store", type="string", |
| 90 help="root directory client resources") |
| 91 |
| 92 (options, args) = parser.parse_args() |
| 93 if not options.output: |
| 94 sys.stderr.write('--output not specified\n') |
| 95 return -1 |
| 96 if not options.tar_output: |
| 97 sys.stderr.write('--tar_output not specified\n') |
| 98 return -1 |
| 99 if not options.name: |
| 100 sys.stderr.write('--name not specified\n') |
| 101 return -1 |
| 102 if not options.client_root: |
| 103 sys.stderr.write('--client_root not specified') |
| 104 return -1 |
| 105 |
| 106 files = [ ] |
| 107 |
| 108 for dirname, dirnames, filenames in os.walk(options.client_root): |
| 109 # strip out all dot files. |
| 110 filenames = [f for f in filenames if not f[0] == '.'] |
| 111 dirnames[:] = [d for d in dirnames if not d[0] == '.'] |
| 112 for f in filenames: |
| 113 src_path = os.path.join(dirname, f) |
| 114 if (os.path.isdir(src_path)): |
| 115 continue |
| 116 files.append(src_path) |
| 117 |
| 118 # Write out archive. |
| 119 makeArchive(options.tar_output, options.client_root, files) |
| 120 |
| 121 # Read it back in. |
| 122 with open(options.tar_output, 'rb') as tar_file: |
| 123 tar_archive = tar_file.read() |
| 124 |
| 125 # Write CC file. |
| 126 writeCCFile(options.output, |
| 127 options.outer_namespace, |
| 128 options.inner_namespace, |
| 129 options.name, |
| 130 tar_archive) |
| 131 return 0 |
| 132 |
| 133 except Exception, inst: |
| 134 sys.stderr.write('create_resources.py exception\n') |
| 135 sys.stderr.write(str(inst)) |
| 136 sys.stderr.write('\n') |
| 137 return -1 |
| 138 |
| 139 if __name__ == '__main__': |
| 140 sys.exit(main(sys.argv)) |
OLD | NEW |