OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import argparse | 6 import argparse |
7 import ast | 7 import ast |
8 from checker import Checker as Checker | 8 from checker import Checker as Checker |
9 import os | 9 import os |
10 import sys | |
11 | |
10 | 12 |
11 class Module(object): | 13 class Module(object): |
12 def __init__(self, name, sources, depends=[], externs=[]): | 14 def __init__(self, name, sources, depends=[], externs=[]): |
13 self.name = name | 15 self.name = name |
14 self.sources = sources | 16 self.sources = sources |
15 # TODO(dbeam): support depending on other modules/dependency flattening. | 17 # TODO(dbeam): support depending on other modules/dependency flattening. |
16 self.depends = depends | 18 self.depends = depends |
17 self.externs = externs | 19 self.externs = externs |
18 | 20 |
19 @staticmethod | 21 @staticmethod |
(...skipping 14 matching lines...) Expand all Loading... | |
34 # TODO(dbeam): should ModuleParser be internal to ModuleCompiler or should we | 36 # TODO(dbeam): should ModuleParser be internal to ModuleCompiler or should we |
35 # pass Modules into ModuleCompiler.compile()? Maybe this is fine? | 37 # pass Modules into ModuleCompiler.compile()? Maybe this is fine? |
36 class ModuleParser(object): | 38 class ModuleParser(object): |
37 _cache = {} | 39 _cache = {} |
38 | 40 |
39 def __init__(self, verbose=False): | 41 def __init__(self, verbose=False): |
40 self._verbose = verbose | 42 self._verbose = verbose |
41 | 43 |
42 def parse(self, file_path): | 44 def parse(self, file_path): |
43 if file_path in self._cache: | 45 if file_path in self._cache: |
44 print "(INFO) Found module file %s in the cache" % file_path | 46 print "(INFO) Found module file %s in the cache" % file_path |
45 return self._cache[file_path] | 47 return self._cache[file_path] |
46 | 48 |
47 file = open(file_path, "r") | 49 file = open(file_path, "r") |
48 file_content = file.read() | 50 file_content = file.read() |
49 data = ast.literal_eval(file_content) | 51 data = ast.literal_eval(file_content) |
50 file.close() | 52 file.close() |
51 | 53 |
52 if self._verbose: | 54 if self._verbose: |
53 print "(INFO) Layout: " + os.linesep + file_content + os.linesep | 55 print "(INFO) Layout: " + os.linesep + file_content + os.linesep |
54 | 56 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
86 rel_path = lambda f: os.path.join(here_to_module_dir, f) | 88 rel_path = lambda f: os.path.join(here_to_module_dir, f) |
87 | 89 |
88 modules = self._parser.parse(module_file) | 90 modules = self._parser.parse(module_file) |
89 | 91 |
90 for m in modules: | 92 for m in modules: |
91 self._debug("MODULE: " + m.name, prefix="", suffix=os.linesep) | 93 self._debug("MODULE: " + m.name, prefix="", suffix=os.linesep) |
92 | 94 |
93 for s in m.sources: | 95 for s in m.sources: |
94 depends = [rel_path(d) for d in m.depends] | 96 depends = [rel_path(d) for d in m.depends] |
95 externs = [rel_path(e) for e in m.externs] | 97 externs = [rel_path(e) for e in m.externs] |
96 self._checker.check(rel_path(s), depends=depends, externs=externs) | 98 success, _ = self._checker.check(rel_path(s), depends=depends, |
Dan Beam
2014/07/29 23:49:08
exit_code, _ = ...
Vitaly Pavlenko
2014/07/30 00:01:37
Done.
| |
99 externs=externs) | |
100 if not success: | |
Dan Beam
2014/07/29 23:49:08
if exit_code:
sys.exit(exit_code)
Vitaly Pavlenko
2014/07/30 00:01:37
Done.
| |
101 sys.exit(1) | |
97 | 102 |
98 if s != m.sources[-1]: | 103 if s != m.sources[-1]: |
99 self._debug(os.linesep, prefix="") | 104 self._debug(os.linesep, prefix="") |
100 | 105 |
101 if m != modules[-1]: | 106 if m != modules[-1]: |
102 self._debug(os.linesep, prefix="") | 107 self._debug(os.linesep, prefix="") |
103 | 108 |
104 | 109 |
105 def main(opts): | 110 def main(opts): |
106 module_compiler = ModuleCompiler(verbose=opts.verbose) | 111 module_compiler = ModuleCompiler(verbose=opts.verbose) |
107 for module_file in opts.module_file: | 112 for module_file in opts.module_file: |
108 module_compiler.compile(module_file) | 113 module_compiler.compile(module_file) |
109 | 114 |
110 | 115 |
111 if __name__ == "__main__": | 116 if __name__ == "__main__": |
112 parser = argparse.ArgumentParser( | 117 parser = argparse.ArgumentParser( |
113 description="Typecheck JavaScript using Closure compiler") | 118 description="Typecheck JavaScript using Closure compiler") |
114 parser.add_argument("-v", "--verbose", action="store_true", | 119 parser.add_argument("-v", "--verbose", action="store_true", |
115 help="Show more information as this script runs") | 120 help="Show more information as this script runs") |
116 parser.add_argument("module_file", nargs=argparse.ONE_OR_MORE, | 121 parser.add_argument("module_file", nargs=argparse.ONE_OR_MORE, |
117 help="Path to a modules file to check") | 122 help="Path to a modules file to check") |
118 main(parser.parse_args()) | 123 main(parser.parse_args()) |
OLD | NEW |