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 from checker import Checker as Checker |
| 8 import os |
| 9 try: |
| 10 import json |
| 11 except: |
| 12 import simplejson as json |
| 13 |
| 14 |
| 15 |
| 16 class Module(object): |
| 17 def __init__(self, name, sources, depends=[], externs=[]): |
| 18 self._name = name |
| 19 self._sources = sources |
| 20 self._depends = depends |
| 21 self._externs = externs |
| 22 |
| 23 def name(self): |
| 24 return self._name |
| 25 |
| 26 def sources(self): |
| 27 return self._sources |
| 28 |
| 29 # TODO(dbeam): support depending on other modules/dependency flattening. |
| 30 def depends(self): |
| 31 return self._depends |
| 32 |
| 33 def externs(self): |
| 34 return self._externs |
| 35 |
| 36 @staticmethod |
| 37 def from_dict(d): |
| 38 keys = d.keys() |
| 39 |
| 40 required = ["name", "sources"] |
| 41 assert all(r in keys for r in required), "Module missing name or sources" |
| 42 |
| 43 allowed = required + ["depends", "externs"] |
| 44 assert all(k in allowed for k in keys), "Module has unknown key" |
| 45 |
| 46 depends = d["depends"] if "depends" in d else [] |
| 47 externs = d["externs"] if "externs" in d else [] |
| 48 return Module(d["name"], d["sources"], depends=depends, externs=externs) |
| 49 |
| 50 |
| 51 # TODO(dbeam): should ModuleParser be internal to ModuleCompiler or should we |
| 52 # pass Modules into ModuleCompiler.compile()? Maybe this is fine? |
| 53 class ModuleParser(object): |
| 54 _cache = {} |
| 55 |
| 56 def __init__(self, verbose=False): |
| 57 self._verbose = verbose |
| 58 |
| 59 def parse(self, file_path): |
| 60 if file_path in self._cache: |
| 61 print "(INFO) Found module file %s in the cache" % file_path |
| 62 return self._cache[file_path] |
| 63 |
| 64 file = open(file_path, "r") |
| 65 data = json.load(file) |
| 66 file.close() |
| 67 |
| 68 if self._verbose: |
| 69 pretty_json = json.dumps(data, indent=2, separators=(',', ': ')).strip() |
| 70 print "(INFO) Layout: " + os.linesep + pretty_json + os.linesep |
| 71 |
| 72 self._cache[file_path] = [Module.from_dict(m) for m in data] |
| 73 return self._cache[file_path] |
| 74 |
| 75 |
| 76 class ModuleCompiler(object): |
| 77 _checker = None |
| 78 _parser = None |
| 79 |
| 80 def __init__(self, verbose=False): |
| 81 self._verbose = verbose |
| 82 |
| 83 def _debug(self, msg, prefix="(INFO) ", suffix=""): |
| 84 if self._verbose: |
| 85 print prefix + msg.strip() + suffix |
| 86 |
| 87 def compile(self, module_file): |
| 88 self._debug("MODULE FILE: " + module_file, prefix="") |
| 89 |
| 90 # NOTE: It's possible by unlikely that |_checker| or |_parser|'s verbosity |
| 91 # isn't the same as |self._verbose| due to this class being called with |
| 92 # verbose=False then verbose=True in the same program. |
| 93 self._parser = self._parser or ModuleParser(verbose=self._verbose) |
| 94 self._checker = self._checker or Checker(verbose=self._verbose) |
| 95 |
| 96 module_dir = os.path.dirname(module_file) |
| 97 modules = self._parser.parse(module_file) |
| 98 |
| 99 for m in modules: |
| 100 self._debug("MODULE: " + m.name(), prefix="", suffix=os.linesep) |
| 101 |
| 102 for s in m.sources(): |
| 103 f = os.path.join(module_dir, s) |
| 104 depends = [os.path.join(module_dir, d) for d in m.depends()] |
| 105 externs = [os.path.join(module_dir, e) for e in m.externs()] |
| 106 self._checker.check(f, depends=depends, externs=externs) |
| 107 |
| 108 if s != m.sources()[-1]: |
| 109 self._debug(os.linesep, prefix="") |
| 110 |
| 111 if m != modules[-1]: |
| 112 self._debug(os.linesep, prefix="") |
| 113 |
| 114 |
| 115 def main(opts): |
| 116 module_compiler = ModuleCompiler(verbose=opts.verbose) |
| 117 for module_file in opts.module_file: |
| 118 module_compiler.compile(module_file) |
| 119 if module_file != opts.module_file[-1]: |
| 120 print |
| 121 |
| 122 |
| 123 if __name__ == "__main__": |
| 124 parser = argparse.ArgumentParser( |
| 125 description="Typecheck JavaScript using Closure compiler") |
| 126 parser.add_argument("-v", "--verbose", action="store_true", |
| 127 help="Show more information as this script runs") |
| 128 parser.add_argument("module_file", nargs=argparse.ONE_OR_MORE, |
| 129 help="Path to a modules file to check") |
| 130 main(parser.parse_args()) |
OLD | NEW |