Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(285)

Side by Side Diff: mojo/public/sky/convert_amd_modules_to_sky.py

Issue 814543006: Move //mojo/{public, edk} underneath //third_party (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/public/sky/BUILD.gn ('k') | mojo/public/tests/DEPS » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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())
OLDNEW
« no previous file with comments | « mojo/public/sky/BUILD.gn ('k') | mojo/public/tests/DEPS » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698