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