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 os |
| 8 import subprocess |
| 9 import sys |
| 10 import tempfile |
| 11 |
| 12 |
| 13 class Checker(object): |
| 14 _common_closure_args = [ |
| 15 "--accept_const_keyword", |
| 16 "--language_in=ECMASCRIPT5", |
| 17 "--summary_detail_level=3", |
| 18 "--warning_level=VERBOSE", |
| 19 "--jscomp_error=accessControls", |
| 20 "--jscomp_error=ambiguousFunctionDecl", |
| 21 "--jscomp_error=checkEventfulObjectDisposal", |
| 22 "--jscomp_error=checkStructDictInheritance", |
| 23 "--jscomp_error=checkTypes", |
| 24 "--jscomp_error=checkVars", |
| 25 "--jscomp_error=constantProperty", |
| 26 "--jscomp_error=deprecated", |
| 27 "--jscomp_error=duplicateMessage", |
| 28 "--jscomp_error=es3", |
| 29 "--jscomp_error=externsValidation", |
| 30 "--jscomp_error=globalThis", |
| 31 "--jscomp_error=invalidCasts", |
| 32 "--jscomp_error=misplacedTypeAnnotation", |
| 33 "--jscomp_error=missingProperties", |
| 34 "--jscomp_error=missingReturn", |
| 35 "--jscomp_error=nonStandardJsDocs", |
| 36 "--jscomp_error=suspiciousCode", |
| 37 "--jscomp_error=undefinedNames", |
| 38 "--jscomp_error=undefinedVars", |
| 39 "--jscomp_error=unknownDefines", |
| 40 "--jscomp_error=uselessCode", |
| 41 "--jscomp_error=visibility", |
| 42 ] |
| 43 |
| 44 _found_java = False |
| 45 |
| 46 _jar_command = [ |
| 47 "java", |
| 48 "-jar", |
| 49 "-Xms1024m", |
| 50 "-server", |
| 51 "-XX:+TieredCompilation" |
| 52 ] |
| 53 |
| 54 def __init__(self, verbose=False): |
| 55 self._current_dir = os.path.dirname(__file__) |
| 56 self._compiler_jar = os.path.join(self._current_dir, "lib", "compiler.jar") |
| 57 self._runner_jar = os.path.join(self._current_dir, "runner", "runner.jar") |
| 58 self._verbose = verbose |
| 59 |
| 60 def _clean_up(self): |
| 61 if hasattr(self, '_args_file'): |
| 62 self._debug("Deleting args file (" + self._args_file.name + ")") |
| 63 os.remove(self._args_file.name) |
| 64 del self._args_file |
| 65 |
| 66 def _debug(self, msg, error=False): |
| 67 if self._verbose: |
| 68 if error: |
| 69 print sys.stderr, "(ERROR) " + msg |
| 70 else: |
| 71 print "(INFO) " + msg |
| 72 |
| 73 def _fatal(self, msg): |
| 74 self._debug(msg, error=True) |
| 75 self._clean_up() |
| 76 sys.exit(1) |
| 77 |
| 78 def _run_command(self, cmd): |
| 79 cmd_str = " ".join(cmd) |
| 80 self._debug("Running command: " + cmd_str) |
| 81 |
| 82 devnull = open(os.devnull, "w") |
| 83 return subprocess.Popen( |
| 84 cmd_str, stdout=devnull, stderr=subprocess.PIPE, shell=True) |
| 85 |
| 86 def _check_java_path(self): |
| 87 if self._found_java: |
| 88 return |
| 89 |
| 90 proc = self._run_command(["which", "java"]) |
| 91 proc.communicate() |
| 92 if proc.returncode == 0: |
| 93 self._found_java = True |
| 94 else: |
| 95 self._fatal("Cannot find java (`which java` => %s)" % proc.returncode) |
| 96 |
| 97 def _run_jar(self, jar, args=[]): |
| 98 self._check_java_path() |
| 99 return self._run_command(self._jar_command + [jar] + args) |
| 100 |
| 101 def check(self, file, depends=[], externs=[]): |
| 102 self._debug("FILE: " + file) |
| 103 |
| 104 if file.endswith("_externs.js"): |
| 105 self._debug("Skipping externs") |
| 106 return |
| 107 |
| 108 j = os.path.join |
| 109 src_dir = j(self._current_dir, "..", "..") |
| 110 # Omit blah_externs.js if we're compiling blah.js. |
| 111 externs = [j(src_dir, e) for e in externs if file != e[:-11] + ".js"] |
| 112 |
| 113 args = ["--js=" + file] + ["--externs=" + e for e in externs] |
| 114 args_file_content = " " + " ".join(self._common_closure_args + args) |
| 115 self._debug("Args: " + args_file_content.strip()) |
| 116 |
| 117 self._args_file = tempfile.NamedTemporaryFile(mode='wt', delete=False) |
| 118 self._debug("Args file: " + self._args_file.name) |
| 119 self._args_file.write(args_file_content) |
| 120 self._args_file.close() |
| 121 |
| 122 runner_args = ["--compiler-args-file=" + self._args_file.name] |
| 123 runner_cmd = self._run_jar(self._runner_jar, args=runner_args) |
| 124 (_, output) = runner_cmd.communicate() |
| 125 output = output.strip() |
| 126 |
| 127 if runner_cmd.returncode: |
| 128 self._fatal(output) |
| 129 else: |
| 130 line_sep = os.linesep + "## " |
| 131 self._debug("Output: " + line_sep + line_sep.join(output.splitlines())) |
| 132 |
| 133 self._clean_up() |
| 134 |
| 135 return runner_cmd.returncode == 0 |
OLD | NEW |