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

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: rebase onto master 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
« no previous file with comments | « no previous file | third_party/closure_compiler/coding_conventions_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 121
122 self._debug("Deleting temporary files: " + ", ".join(self._temp_files)) 122 self._debug("Deleting temporary files: " + ", ".join(self._temp_files))
123 for f in self._temp_files: 123 for f in self._temp_files:
124 os.remove(f) 124 os.remove(f)
125 self._temp_files = [] 125 self._temp_files = []
126 126
127 def _debug(self, msg, error=False): 127 def _debug(self, msg, error=False):
128 if self._verbose: 128 if self._verbose:
129 print "(INFO) " + msg 129 print "(INFO) " + msg
130 130
131 def _fatal(self, msg): 131 def _error(self, msg):
132 print >> sys.stderr, "(FATAL) " + msg 132 print >> sys.stderr, "(ERROR) " + msg
133 self._clean_up() 133 self._clean_up()
134 sys.exit(1)
135 134
136 def _run_command(self, cmd): 135 def _run_command(self, cmd):
137 cmd_str = " ".join(cmd) 136 cmd_str = " ".join(cmd)
138 self._debug("Running command: " + cmd_str) 137 self._debug("Running command: " + cmd_str)
139 138
140 devnull = open(os.devnull, "w") 139 devnull = open(os.devnull, "w")
141 return subprocess.Popen( 140 return subprocess.Popen(
142 cmd_str, stdout=devnull, stderr=subprocess.PIPE, shell=True) 141 cmd_str, stdout=devnull, stderr=subprocess.PIPE, shell=True)
143 142
144 def _check_java_path(self): 143 def _check_java_path(self):
145 if self._found_java: 144 if not self._found_java:
146 return 145 proc = self._run_command(["which", "java"])
146 proc.communicate()
147 if proc.returncode == 0:
148 self._found_java = True
149 else:
150 self._error("Cannot find java (`which java` => %s)" % proc.returncode)
147 151
148 proc = self._run_command(["which", "java"]) 152 return self._found_java
149 proc.communicate()
150 if proc.returncode == 0:
151 self._found_java = True
152 else:
153 self._fatal("Cannot find java (`which java` => %s)" % proc.returncode)
154 153
155 def _run_jar(self, jar, args=[]): 154 def _run_jar(self, jar, args=[]):
156 self._check_java_path() 155 self._check_java_path()
157 return self._run_command(self._jar_command + [jar] + args) 156 return self._run_command(self._jar_command + [jar] + args)
158 157
159 def _fix_line_number(self, match): 158 def _fix_line_number(self, match):
160 real_file = self._flattener.get_file_from_line(match.group(1)) 159 real_file = self._flattener.get_file_from_line(match.group(1))
161 return "%s:%d" % (os.path.abspath(real_file.file), real_file.line_number) 160 return "%s:%d" % (os.path.abspath(real_file.file), real_file.line_number)
162 161
163 def _fix_up_error(self, error): 162 def _fix_up_error(self, error):
(...skipping 10 matching lines...) Expand all
174 contents = ("\n" + "## ").join("\n\n".join(errors).splitlines()) 173 contents = ("\n" + "## ").join("\n\n".join(errors).splitlines())
175 return "## " + contents if contents else "" 174 return "## " + contents if contents else ""
176 175
177 def _create_temp_file(self, contents): 176 def _create_temp_file(self, contents):
178 with tempfile.NamedTemporaryFile(mode='wt', delete=False) as tmp_file: 177 with tempfile.NamedTemporaryFile(mode='wt', delete=False) as tmp_file:
179 self._temp_files.append(tmp_file.name) 178 self._temp_files.append(tmp_file.name)
180 tmp_file.write(contents) 179 tmp_file.write(contents)
181 return tmp_file.name 180 return tmp_file.name
182 181
183 def check(self, file, depends=[], externs=[]): 182 def check(self, file, depends=[], externs=[]):
183 if not self._check_java_path():
184 return 1, ""
185
184 self._debug("FILE: " + file) 186 self._debug("FILE: " + file)
185 187
186 if file.endswith("_externs.js"): 188 if file.endswith("_externs.js"):
187 self._debug("Skipping externs: " + file) 189 self._debug("Skipping externs: " + file)
188 return 190 return
189 191
190 self._file_arg = file 192 self._file_arg = file
191 193
192 tmp_dir = tempfile.gettempdir() 194 tmp_dir = tempfile.gettempdir()
193 rel_path = lambda f: os.path.join(os.path.relpath(os.getcwd(), tmp_dir), f) 195 rel_path = lambda f: os.path.join(os.path.relpath(os.getcwd(), tmp_dir), f)
(...skipping 15 matching lines...) Expand all
209 211
210 runner_args = ["--compiler-args-file=" + args_file] 212 runner_args = ["--compiler-args-file=" + args_file]
211 runner_cmd = self._run_jar(self._runner_jar, args=runner_args) 213 runner_cmd = self._run_jar(self._runner_jar, args=runner_args)
212 (_, stderr) = runner_cmd.communicate() 214 (_, stderr) = runner_cmd.communicate()
213 215
214 errors = stderr.strip().split("\n\n") 216 errors = stderr.strip().split("\n\n")
215 self._debug("Summary: " + errors.pop()) 217 self._debug("Summary: " + errors.pop())
216 218
217 output = self._format_errors(map(self._fix_up_error, errors)) 219 output = self._format_errors(map(self._fix_up_error, errors))
218 if runner_cmd.returncode: 220 if runner_cmd.returncode:
219 self._fatal("Error in: " + file + ("\n" + output if output else "")) 221 self._error("Error in: " + file + ("\n" + output if output else ""))
220 elif output: 222 elif output:
221 self._debug("Output: " + output) 223 self._debug("Output: " + output)
222 224
223 self._clean_up() 225 self._clean_up()
224 226
225 return runner_cmd.returncode == 0 227 return runner_cmd.returncode, output
OLDNEW
« no previous file with comments | « no previous file | third_party/closure_compiler/coding_conventions_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698