Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 2 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 3 # for details. All rights reserved. Use of this source code is governed by a | 3 # for details. All rights reserved. Use of this source code is governed by a |
| 4 # BSD-style license that can be found in the LICENSE file. | 4 # BSD-style license that can be found in the LICENSE file. |
| 5 """Used to merge and copy dart source files for deployment to AppEngine""" | 5 """Used to merge and copy dart source files for deployment to AppEngine""" |
| 6 | 6 |
| 7 import fileinput | 7 import fileinput |
| 8 import sys | 8 import sys |
| 9 import shutil | 9 import shutil |
| 10 import os | 10 import os |
| 11 import re | 11 import re |
| 12 from os.path import abspath, basename, dirname, exists, isabs, join | 12 from os.path import abspath, basename, dirname, exists, isabs, join |
| 13 from glob import glob | 13 from glob import glob |
| 14 | 14 |
| 15 re_library = re.compile(r'^#library\([\'"](.*)[\'"]\);$') | 15 re_directive = re.compile( |
| 16 re_import = re.compile(r'^#import\([\'"](.*)[\'"]\);$') | 16 r'^#(library|import|source|native|resource)\([\'"]([^\'"]*)[\'"](.*)\);$') |
|
vsm
2011/10/26 19:52:29
Is "resource" standard?
Siggi Cherem (dart-lang)
2011/10/26 20:03:12
yes - is not used extensively, but it is supported
| |
| 17 re_source = re.compile(r'^#source\([\'"](.*)[\'"]\);$') | |
| 18 | 17 |
| 19 class Library(object): | 18 class Library(object): |
| 20 def __init__(self, name, imports, sources, code): | 19 def __init__(self, name, imports, sources, natives, code): |
| 21 self.name = name | 20 self.name = name |
| 22 self.imports = imports | 21 self.imports = imports |
| 23 self.sources = sources | 22 self.sources = sources |
| 23 self.natives = natives | |
| 24 self.code = code | 24 self.code = code |
| 25 | 25 |
| 26 def parseLibrary(library): | 26 def parseLibrary(library): |
| 27 """ Parses a .dart source file that is the root of a library, and returns | 27 """ Parses a .dart source file that is the root of a library, and returns |
| 28 information about it: the name, the imports, included sources, and any | 28 information about it: the name, the imports, included sources, and any |
| 29 code in the file. | 29 code in the file. |
| 30 """ | 30 """ |
| 31 libraryname = None | 31 libraryname = None |
| 32 imports = [] | 32 imports = [] |
| 33 sources = [] | 33 sources = [] |
| 34 natives = [] | |
| 34 inlinecode = [] | 35 inlinecode = [] |
| 35 if exists(library): | 36 if exists(library): |
| 36 # TODO(sigmund): stop parsing when import/source | 37 # TODO(sigmund): stop parsing when import/source |
| 37 for line in fileinput.input(library): | 38 for line in fileinput.input(library): |
| 38 match = re_import.match(line) | 39 match = re_directive.match(line) |
| 39 if match: | 40 if match: |
| 40 imports.append(match.group(1)) | 41 directive = match.group(1) |
| 42 if directive == 'library': | |
| 43 assert libraryname is None | |
| 44 libraryname = match.group(2) | |
| 45 elif directive == 'source': | |
| 46 sources.append(match.group(2)) | |
| 47 elif directive == 'import': | |
| 48 imports.append((match.group(2), match.group(3))) | |
| 49 elif directive == 'native': | |
| 50 natives.append(match.group(2)) | |
| 51 elif directive == 'resource': | |
| 52 # currently ignored | |
| 53 pass | |
| 54 else: | |
| 55 raise 'unknown directive %s' % directive | |
| 41 else: | 56 else: |
| 42 match = re_source.match(line) | 57 inlinecode.append(line) |
| 43 if match: | |
| 44 sources.append(match.group(1)) | |
| 45 else: | |
| 46 match = re_library.match(line) | |
| 47 if match: | |
| 48 assert libraryname is None | |
| 49 libraryname = match.group(1) | |
| 50 else: | |
| 51 inlinecode.append(line) | |
| 52 | |
| 53 fileinput.close() | 58 fileinput.close() |
| 54 return Library(libraryname, imports, sources, inlinecode) | 59 return Library(libraryname, imports, sources, natives, inlinecode) |
| 55 | 60 |
| 56 def normjoin(*args): | 61 def normjoin(*args): |
| 57 return os.path.normpath(os.path.join(*args)) | 62 return os.path.normpath(os.path.join(*args)) |
| 58 | 63 |
| 59 def mergefiles(srcs, dstfile): | 64 def mergefiles(srcs, dstfile): |
| 60 for src in srcs: | 65 for src in srcs: |
| 61 with open(src, 'r') as s: | 66 with open(src, 'r') as s: |
| 62 dstfile.write(s.read()) | 67 dstfile.write(s.read()) |
| 63 | 68 |
| 69 def copyfile(src, dst): | |
| 70 if not exists(dirname(dst)): | |
| 71 os.makedirs(dirname(dst)) | |
| 72 with open(src, 'r') as s: | |
| 73 with open(dst, 'w') as d: | |
| 74 d.write(s.read()) | |
| 75 | |
| 64 def main(outdir = None, *inputs): | 76 def main(outdir = None, *inputs): |
| 65 if not outdir or not inputs: | 77 if not outdir or not inputs: |
| 66 print "Usage: %s OUTDIR INPUTS" % args[0] | 78 print "Usage: %s OUTDIR INPUTS" % args[0] |
| 67 print " OUTDIR is the war directory to copy to" | 79 print " OUTDIR is the war directory to copy to" |
| 68 print " INPUTS is a list of files or patterns used to specify the input" | 80 print " INPUTS is a list of files or patterns used to specify the input" |
| 69 print " .dart files" | 81 print " .dart files" |
| 70 print "This script should be run from the client root directory." | 82 print "This script should be run from the client root directory." |
| 71 print "Files will be merged and copied to: OUTDIR/relative-path-of-file," | 83 print "Files will be merged and copied to: OUTDIR/relative-path-of-file," |
| 72 print "except for dart files with absolute paths, which will be copied to" | 84 print "except for dart files with absolute paths, which will be copied to" |
| 73 print " OUTDIR/absolute-path-as-directories" | 85 print " OUTDIR/absolute-path-as-directories" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 109 if not exists(dstpath): | 121 if not exists(dstpath): |
| 110 os.makedirs(dstpath) | 122 os.makedirs(dstpath) |
| 111 | 123 |
| 112 | 124 |
| 113 # Create file containing all imports, and inlining all sources | 125 # Create file containing all imports, and inlining all sources |
| 114 with open(outpath, 'w') as f: | 126 with open(outpath, 'w') as f: |
| 115 if library.name: | 127 if library.name: |
| 116 f.write("#library('%s');\n\n" % library.name) | 128 f.write("#library('%s');\n\n" % library.name) |
| 117 else: | 129 else: |
| 118 f.write("#library('%s');\n\n" % basename(lib)) | 130 f.write("#library('%s');\n\n" % basename(lib)) |
| 119 for importfile in library.imports: | 131 for (importfile, optional_prefix) in library.imports: |
| 120 f.write("#import('%s');\n" % importfile) | 132 f.write("#import('%s'%s);\n" % (importfile, optional_prefix)) |
| 133 for native in library.natives: | |
| 134 if isabs(native): | |
| 135 npath = native[1:] | |
| 136 else: | |
| 137 npath = native | |
| 138 f.write("#native('%s');\n" % npath) | |
| 139 copyfile(normjoin(dirname(lib), native), | |
| 140 join(dirname(outpath), npath)) | |
| 121 f.write('%s' % (''.join(library.code))) | 141 f.write('%s' % (''.join(library.code))) |
| 122 mergefiles([normjoin(dirname(lib), s) for s in library.sources], f) | 142 mergefiles([normjoin(dirname(lib), s) for s in library.sources], f) |
| 123 | 143 |
| 124 worklist.extend([normjoin(dirname(lib), i) for i in library.imports]) | 144 for (i, prefix) in library.imports: |
| 145 if not i.startswith('dart:'): | |
| 146 worklist.append(normjoin(dirname(lib), i)); | |
| 125 | 147 |
| 126 return 0 | 148 return 0 |
| 127 | 149 |
| 128 if __name__ == '__main__': | 150 if __name__ == '__main__': |
| 129 sys.exit(main(*sys.argv[1:])) | 151 sys.exit(main(*sys.argv[1:])) |
| OLD | NEW |