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

Side by Side Diff: third_party/closure_compiler/checker.py

Issue 421253006: Add ChromeCodingConvention.java to Closure Compiler to preserve getInstance() type (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@A_typechecking_about
Patch Set: fatal -> error Created 6 years, 4 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
OLDNEW
1 # Copyright 2014 The Chromium Authors. All rights reserved. 1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 from collections import defaultdict 5 from collections import defaultdict
6 import os 6 import os
7 import re 7 import re
8 import subprocess 8 import subprocess
9 import sys 9 import sys
10 import tempfile 10 import tempfile
11 11
12 12
13 13
14 class LineNumber(object): 14 class LineNumber(object):
15 def __init__(self, file, line_number): 15 def __init__(self, file, line_number):
16 self.file = file 16 self.file = file
17 self.line_number = int(line_number) 17 self.line_number = int(line_number)
18 18
19 19
20 class FileCache(object): 20 class FileCache(object):
21 _cache = defaultdict(str) 21 _cache = defaultdict(str)
22 22
23 def _read(self, file): 23 def _read(self, file):
24 file = os.path.abspath(file) 24 file = os.path.abspath(file)
25 self._cache[file] = self._cache[file] or open(file, "r").read() 25 self._cache[file] = self._cache[file] or open(file, "r").read()
26 return self._cache[file] 26 return self._cache[file]
27 27
28 @staticmethod 28 @staticmethod
29 def read(file): 29 def read(file):
30 return FileCache()._read(file) 30 return FileCache()._read(file)
31 31
32 32
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 118
119 self._debug("Deleting temporary files: " + ", ".join(self._temp_files)) 119 self._debug("Deleting temporary files: " + ", ".join(self._temp_files))
120 for f in self._temp_files: 120 for f in self._temp_files:
121 os.remove(f) 121 os.remove(f)
122 self._temp_files = [] 122 self._temp_files = []
123 123
124 def _debug(self, msg, error=False): 124 def _debug(self, msg, error=False):
125 if self._verbose: 125 if self._verbose:
126 print "(INFO) " + msg 126 print "(INFO) " + msg
127 127
128 def _fatal(self, msg): 128 def _error(self, msg):
129 print >> sys.stderr, "(FATAL) " + msg 129 print >> sys.stderr, "(ERROR) " + msg
130 self._clean_up() 130 self._clean_up()
131 sys.exit(1)
132 131
133 def _run_command(self, cmd): 132 def _run_command(self, cmd):
134 cmd_str = " ".join(cmd) 133 cmd_str = " ".join(cmd)
135 self._debug("Running command: " + cmd_str) 134 self._debug("Running command: " + cmd_str)
136 135
137 devnull = open(os.devnull, "w") 136 devnull = open(os.devnull, "w")
138 return subprocess.Popen( 137 return subprocess.Popen(
139 cmd_str, stdout=devnull, stderr=subprocess.PIPE, shell=True) 138 cmd_str, stdout=devnull, stderr=subprocess.PIPE, shell=True)
140 139
141 def _check_java_path(self): 140 def _check_java_path(self):
142 if self._found_java: 141 if not self._found_java:
143 return 142 proc = self._run_command(["which", "java"])
143 proc.communicate()
144 if proc.returncode == 0:
145 self._found_java = True
146 else:
147 self._error("Cannot find java (`which java` => %s)" % proc.returncode)
144 148
145 proc = self._run_command(["which", "java"]) 149 return self._found_java
146 proc.communicate()
147 if proc.returncode == 0:
148 self._found_java = True
149 else:
150 self._fatal("Cannot find java (`which java` => %s)" % proc.returncode)
151 150
152 def _run_jar(self, jar, args=[]): 151 def _run_jar(self, jar, args=[]):
153 self._check_java_path() 152 self._check_java_path()
154 return self._run_command(self._jar_command + [jar] + args) 153 return self._run_command(self._jar_command + [jar] + args)
155 154
156 def _fix_line_number(self, match): 155 def _fix_line_number(self, match):
157 real_file = self._flattener.get_file_from_line(match.group(1)) 156 real_file = self._flattener.get_file_from_line(match.group(1))
158 return "%s:%d" % (os.path.abspath(real_file.file), real_file.line_number) 157 return "%s:%d" % (os.path.abspath(real_file.file), real_file.line_number)
159 158
160 def _fix_up_error(self, error): 159 def _fix_up_error(self, error):
(...skipping 10 matching lines...) Expand all
171 contents = ("\n" + "## ").join("\n\n".join(errors).splitlines()) 170 contents = ("\n" + "## ").join("\n\n".join(errors).splitlines())
172 return "## " + contents if contents else "" 171 return "## " + contents if contents else ""
173 172
174 def _create_temp_file(self, contents): 173 def _create_temp_file(self, contents):
175 with tempfile.NamedTemporaryFile(mode='wt', delete=False) as tmp_file: 174 with tempfile.NamedTemporaryFile(mode='wt', delete=False) as tmp_file:
176 self._temp_files.append(tmp_file.name) 175 self._temp_files.append(tmp_file.name)
177 tmp_file.write(contents) 176 tmp_file.write(contents)
178 return tmp_file.name 177 return tmp_file.name
179 178
180 def check(self, file, depends=[], externs=[]): 179 def check(self, file, depends=[], externs=[]):
180 if not self._check_java_path():
181 return 1, ""
182
181 self._debug("FILE: " + file) 183 self._debug("FILE: " + file)
182 184
183 if file.endswith("_externs.js"): 185 if file.endswith("_externs.js"):
184 self._debug("Skipping externs: " + file) 186 self._debug("Skipping externs: " + file)
185 return 187 return
186 188
187 self._file_arg = file 189 self._file_arg = file
188 190
189 tmp_dir = tempfile.gettempdir() 191 tmp_dir = tempfile.gettempdir()
190 rel_path = lambda f: os.path.join(os.path.relpath(os.getcwd(), tmp_dir), f) 192 rel_path = lambda f: os.path.join(os.path.relpath(os.getcwd(), tmp_dir), f)
(...skipping 14 matching lines...) Expand all
205 self._debug("Args file: " + args_file) 207 self._debug("Args file: " + args_file)
206 208
207 runner_args = ["--compiler-args-file=" + args_file] 209 runner_args = ["--compiler-args-file=" + args_file]
208 runner_cmd = self._run_jar(self._runner_jar, args=runner_args) 210 runner_cmd = self._run_jar(self._runner_jar, args=runner_args)
209 (_, stderr) = runner_cmd.communicate() 211 (_, stderr) = runner_cmd.communicate()
210 212
211 errors = stderr.strip().split("\n\n") 213 errors = stderr.strip().split("\n\n")
212 self._debug("Summary: " + errors.pop()) 214 self._debug("Summary: " + errors.pop())
213 215
214 output = self._format_errors(map(self._fix_up_error, errors)) 216 output = self._format_errors(map(self._fix_up_error, errors))
217
215 if runner_cmd.returncode: 218 if runner_cmd.returncode:
216 self._fatal("Error in: " + file + ("\n" + output if output else "")) 219 self._error("Error in: " + file + ("\n" + output if output else ""))
217 elif output: 220 elif output:
218 self._debug("Output: " + output) 221 self._debug("Output: " + output)
219 222
220 self._clean_up() 223 self._clean_up()
221 224
222 return runner_cmd.returncode == 0 225 return runner_cmd.returncode, output
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698