| 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_directive = re.compile( | 15 re_directive = re.compile( |
| 16 r'^#(library|import|source|native|resource)\([\'"]([^\'"]*)[\'"](.*)\);$') | 16 r'^(library|import|part|native|resource)\s+(.*);$') |
| 17 | 17 |
| 18 class Library(object): | 18 class Library(object): |
| 19 def __init__(self, name, imports, sources, natives, code): | 19 def __init__(self, name, imports, sources, natives, code): |
| 20 self.name = name | 20 self.name = name |
| 21 self.imports = imports | 21 self.imports = imports |
| 22 self.sources = sources | 22 self.sources = sources |
| 23 self.natives = natives | 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 natives = [] |
| 35 inlinecode = [] | 35 inlinecode = [] |
| 36 if exists(library): | 36 if exists(library): |
| 37 # TODO(sigmund): stop parsing when import/source | 37 # TODO(sigmund): stop parsing when import/source |
| 38 for line in fileinput.input(library): | 38 for line in fileinput.input(library): |
| 39 match = re_directive.match(line) | 39 match = re_directive.match(line) |
| 40 if match: | 40 if match: |
| 41 directive = match.group(1) | 41 directive = match.group(1) |
| 42 if directive == 'library': | 42 if directive == 'library': |
| 43 assert libraryname is None | 43 assert libraryname is None |
| 44 libraryname = match.group(2) | 44 libraryname = match.group(2) |
| 45 elif directive == 'source': | 45 elif directive == 'part': |
| 46 sources.append(match.group(2)) | 46 suffix = match.group(2) |
| 47 if not suffix.startswith('of '): |
| 48 sources.append(match.group(2).strip('"\'')) |
| 47 elif directive == 'import': | 49 elif directive == 'import': |
| 48 imports.append((match.group(2), match.group(3))) | 50 imports.append(match.group(2)) |
| 49 elif directive == 'native': | |
| 50 natives.append(match.group(2)) | |
| 51 elif directive == 'resource': | |
| 52 # currently ignored | |
| 53 pass | |
| 54 else: | 51 else: |
| 55 raise 'unknown directive %s' % directive | 52 raise Exception('unknown directive %s in %s' % (directive, line)) |
| 56 else: | 53 else: |
| 57 inlinecode.append(line) | 54 inlinecode.append(line) |
| 58 fileinput.close() | 55 fileinput.close() |
| 59 return Library(libraryname, imports, sources, natives, inlinecode) | 56 return Library(libraryname, imports, sources, natives, inlinecode) |
| 60 | 57 |
| 61 def normjoin(*args): | 58 def normjoin(*args): |
| 62 return os.path.normpath(os.path.join(*args)) | 59 return os.path.normpath(os.path.join(*args)) |
| 63 | 60 |
| 64 def mergefiles(srcs, dstfile): | 61 def mergefiles(srcs, dstfile): |
| 65 for src in srcs: | 62 for src in srcs: |
| 66 with open(src, 'r') as s: | 63 with open(src, 'r') as s: |
| 67 dstfile.write(s.read()) | 64 for line in s: |
| 65 if not line.startswith('part of '): |
| 66 dstfile.write(line) |
| 68 | 67 |
| 69 def copyfile(src, dst): | 68 def copyfile(src, dst): |
| 70 if not exists(dirname(dst)): | 69 if not exists(dirname(dst)): |
| 71 os.makedirs(dirname(dst)) | 70 os.makedirs(dirname(dst)) |
| 72 with open(src, 'r') as s: | 71 with open(src, 'r') as s: |
| 73 with open(dst, 'w') as d: | 72 with open(dst, 'w') as d: |
| 74 d.write(s.read()) | 73 d.write(s.read()) |
| 75 | 74 |
| 76 def main(outdir = None, *inputs): | 75 def main(outdir = None, *inputs): |
| 77 if not outdir or not inputs: | 76 if not outdir or not inputs: |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 # Ensure output directory exists | 117 # Ensure output directory exists |
| 119 outpath = join(outdir, lib[1:] if isabs(lib) else lib) | 118 outpath = join(outdir, lib[1:] if isabs(lib) else lib) |
| 120 dstpath = dirname(outpath) | 119 dstpath = dirname(outpath) |
| 121 if not exists(dstpath): | 120 if not exists(dstpath): |
| 122 os.makedirs(dstpath) | 121 os.makedirs(dstpath) |
| 123 | 122 |
| 124 | 123 |
| 125 # Create file containing all imports, and inlining all sources | 124 # Create file containing all imports, and inlining all sources |
| 126 with open(outpath, 'w') as f: | 125 with open(outpath, 'w') as f: |
| 127 if library.name: | 126 if library.name: |
| 128 f.write("#library('%s');\n\n" % library.name) | 127 f.write("library %s;\n\n" % library.name) |
| 129 else: | 128 else: |
| 130 f.write("#library('%s');\n\n" % basename(lib)) | 129 f.write("library %s;\n\n" % basename(lib)) |
| 131 for (importfile, optional_prefix) in library.imports: | 130 for importfile in library.imports: |
| 132 f.write("#import('%s'%s);\n" % (importfile, optional_prefix)) | 131 f.write("import %s;\n" % (importfile)) |
| 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)) | |
| 141 f.write('%s' % (''.join(library.code))) | 132 f.write('%s' % (''.join(library.code))) |
| 142 mergefiles([normjoin(dirname(lib), s) for s in library.sources], f) | 133 mergefiles([normjoin(dirname(lib), s) for s in library.sources], f) |
| 143 | 134 |
| 144 for (i, prefix) in library.imports: | 135 for suffix in library.imports: |
| 145 if not i.startswith('dart:'): | 136 m = re.match(r'[\'"]([^\'"]+)[\'"](\s+as\s+\w+)?$', suffix) |
| 146 worklist.append(normjoin(dirname(lib), i)); | 137 uri = m.group(0) |
| 138 if not uri.startswith('dart:'): |
| 139 worklist.append(normjoin(dirname(lib), uri)); |
| 147 | 140 |
| 148 return 0 | 141 return 0 |
| 149 | 142 |
| 150 if __name__ == '__main__': | 143 if __name__ == '__main__': |
| 151 sys.exit(main(*sys.argv[1:])) | 144 sys.exit(main(*sys.argv[1:])) |
| OLD | NEW |