OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 import argparse | |
7 import sys | |
8 import re | |
9 | |
10 IMPORT_TEMPLATE = '<import src="/%s.sky" as="%s" />' | |
11 PREAMBLE_TEMPLATE = '<script>' | |
12 POSTAMBLE_TEMPLATE = ' module.exports = exports;\n</script>' | |
13 | |
14 class Import(object): | |
15 def __init__(self, path, name): | |
16 self.path = path | |
17 self.name = name | |
18 | |
19 class Module(object): | |
20 def __init__(self): | |
21 self.name = "" | |
22 self.imports = [] | |
23 self.body = "" | |
24 | |
25 def Serialize(module): | |
26 lines = [] | |
27 for i in module.imports: | |
28 lines.append(IMPORT_TEMPLATE % (i.path, i.name)) | |
29 lines.append(PREAMBLE_TEMPLATE) | |
30 lines.append(module.body) | |
31 lines.append(POSTAMBLE_TEMPLATE) | |
32 return "\n".join(lines) | |
33 | |
34 name_regex = re.compile(r'define\("([^"]+)"') | |
35 import_regex = re.compile(r' +"([^"]+)",') | |
36 begin_body_regexp = re.compile(r', function\(([^)]*)\)') | |
37 end_body_regexp = re.compile(r'return exports') | |
38 | |
39 def AddImportNames(module, unparsed_names): | |
40 names = [n.strip() for n in unparsed_names.split(',')] | |
41 for i in range(len(module.imports)): | |
42 module.imports[i].name = names[i] | |
43 | |
44 def RewritePathNames(path): | |
45 return path.replace("mojo/public/js", "mojo/public/sky") \ | |
46 .replace("mojo/services/public/js", "mojo/services/public/sky") | |
47 | |
48 def Parse(amd_module): | |
49 module = Module() | |
50 body_lines = [] | |
51 state = "name" | |
52 for line in amd_module.splitlines(): | |
53 if state == "name": | |
54 m = name_regex.search(line) | |
55 if m: | |
56 module.name = m.group(1) | |
57 m = begin_body_regexp.search(line) | |
58 if m: | |
59 AddImportNames(module, m.group(1)) | |
60 state = "body" | |
61 else: | |
62 state = "imports" | |
63 continue | |
64 if state == "imports": | |
65 m = import_regex.search(line) | |
66 if m: | |
67 module.imports.append(Import(RewritePathNames(m.group(1)), None)) | |
68 continue | |
69 m = begin_body_regexp.search(line) | |
70 if m: | |
71 AddImportNames(module, m.group(1)) | |
72 state = "body" | |
73 continue | |
74 raise Exception("Unknown import declaration:" + line) | |
75 if state == "body": | |
76 if end_body_regexp.search(line): | |
77 module.body = "\n".join(body_lines) | |
78 return module | |
79 body_lines.append(line) | |
80 continue | |
81 raise Exception("Unknown parser state") | |
82 raise Exception("End of file reached with finding a module") | |
83 | |
84 def main(): | |
85 parser = argparse.ArgumentParser() | |
86 parser.add_argument("--input") | |
87 parser.add_argument("--output") | |
88 args = parser.parse_args() | |
89 | |
90 module = None | |
91 with open(args.input, "r") as input_file: | |
92 module = Parse(input_file.read()) | |
93 | |
94 with open(args.output, "w+") as output_file: | |
95 output_file.write(Serialize(module)) | |
96 | |
97 return 0 | |
98 | |
99 if __name__ == "__main__": | |
100 sys.exit(main()) | |
OLD | NEW |