OLD | NEW |
---|---|
1 # Copyright (C) 2014 Google Inc. All rights reserved. | 1 # Copyright (C) 2014 Google Inc. All rights reserved. |
2 # | 2 # |
3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
5 # met: | 5 # met: |
6 # | 6 # |
7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
(...skipping 25 matching lines...) Expand all Loading... | |
36 | 36 |
37 CheckOutput = namedtuple('CheckOutput', ['results', 'has_errors']) | 37 CheckOutput = namedtuple('CheckOutput', ['results', 'has_errors']) |
38 | 38 |
39 | 39 |
40 def _CheckNodeAndNPMModules(input_api, output_api): | 40 def _CheckNodeAndNPMModules(input_api, output_api): |
41 node_script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), "s cripts", "install_node_deps.py") | 41 node_script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), "s cripts", "install_node_deps.py") |
42 process = input_api.subprocess.Popen( | 42 process = input_api.subprocess.Popen( |
43 [input_api.python_executable, node_script_path], stdout=input_api.subpro cess.PIPE, stderr=input_api.subprocess.STDOUT) | 43 [input_api.python_executable, node_script_path], stdout=input_api.subpro cess.PIPE, stderr=input_api.subprocess.STDOUT) |
44 out, _ = process.communicate() | 44 out, _ = process.communicate() |
45 if process.returncode != 0: | 45 if process.returncode != 0: |
46 return CheckOutput([output_api.PresubmitError(out)], has_errors=True) | 46 return [output_api.PresubmitError(out)] |
47 return CheckOutput([output_api.PresubmitNotifyResult(out)], has_errors=False ) | 47 return [output_api.PresubmitNotifyResult(out)] |
48 | 48 |
49 | 49 |
50 def _FormatDevtools(input_api, output_api): | 50 def _FormatDevtools(input_api, output_api): |
51 affected_files = _getAffectedJSFiles(input_api) | 51 check_formatting_process = input_api.subprocess.Popen( |
52 if len(affected_files) == 0: | 52 args=['git', 'cl', 'format', '--js', '--dry-run'], stdout=input_api.subp rocess.PIPE, stderr=input_api.subprocess.STDOUT) |
53 return CheckOutput([], has_errors=False) | 53 check_formatting_process.communicate() |
54 original_sys_path = sys.path | 54 if check_formatting_process.returncode == 0: |
55 try: | |
56 sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPa th(), "scripts")] | |
57 import install_node_deps | |
58 finally: | |
59 sys.path = original_sys_path | |
60 | |
61 node_path, _ = install_node_deps.resolve_node_paths() | |
62 format_path = input_api.os_path.join(input_api.PresubmitLocalPath(), "script s", "format.js") | |
63 glob_arg = "--glob=" + ",".join(affected_files) | |
64 check_formatting_process = _inputPopen(input_api, args=[node_path, format_pa th] + [glob_arg, "--output-replacements-xml"]) | |
65 check_formatting_out, _ = check_formatting_process.communicate() | |
66 if check_formatting_process.returncode != 0: | |
67 return CheckOutput([output_api.PresubmitError(check_formatting_out)], ha s_errors=True) | |
68 if "</replacement>" not in check_formatting_out: | |
69 return CheckOutput([output_api.PresubmitNotifyResult("CL is properly for matted")], has_errors=False) | 55 return CheckOutput([output_api.PresubmitNotifyResult("CL is properly for matted")], has_errors=False) |
70 | |
71 format_args = [node_path, format_path] + [glob_arg] | |
72 format_process = _inputPopen(input_api, format_args) | |
73 format_process_out, _ = format_process.communicate() | |
74 | |
75 # Use eslint to autofix the braces | |
dgozman
2017/02/27 18:36:28
That's unfortunate. Can we print out the command l
chenwilliam
2017/02/28 20:28:38
I can print out "npm run fix-braces" if they get a
| |
76 eslint_path = input_api.os_path.join(input_api.PresubmitLocalPath(), "node_m odules", ".bin", "eslint") | |
77 eslint_process = _inputPopen( | |
78 input_api, | |
79 [node_path, eslint_path, '--no-eslintrc', '--fix', '--env=es6', '--rule= {"curly": [2, "multi-or-nest", "consistent"]}'] + | |
80 affected_files) | |
81 eslint_process.communicate() | |
82 | |
83 # Need to run clang-format again to align the braces | |
84 reformat_process = _inputPopen(input_api, format_args) | |
85 reformat_process.communicate() | |
86 | |
87 return CheckOutput( | 56 return CheckOutput( |
88 [ | 57 [output_api.PresubmitError("ERROR: Found formatting violations.\nPlease run 'git cl format --js'")], has_errors=True) |
dgozman
2017/02/27 18:36:28
The common script say something like "directory X
chenwilliam
2017/02/28 20:28:38
I looked into the precanned check but I'd like to
| |
89 output_api.PresubmitError("ERROR: Found formatting violations.\n" | |
90 "Ran clang-format on files changed in CL\n " | |
91 "Use git status to check the formatting ch anges"), | |
92 output_api.PresubmitError(format_process_out) | |
93 ], | |
94 has_errors=True) | |
95 | 58 |
96 | 59 |
97 def _CheckDevtoolsStyle(input_api, output_api): | 60 def _CheckDevtoolsStyle(input_api, output_api): |
98 affected_front_end_files = _getAffectedFrontEndFiles(input_api) | 61 affected_front_end_files = _getAffectedFrontEndFiles(input_api) |
99 if len(affected_front_end_files) > 0: | 62 if len(affected_front_end_files) > 0: |
100 lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(), "scri pts", "lint_javascript.py") | 63 lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(), "scri pts", "lint_javascript.py") |
101 process = input_api.subprocess.Popen( | 64 process = input_api.subprocess.Popen( |
102 [input_api.python_executable, lint_path] + affected_front_end_files, | 65 [input_api.python_executable, lint_path] + affected_front_end_files, |
103 stdout=input_api.subprocess.PIPE, | 66 stdout=input_api.subprocess.PIPE, |
104 stderr=input_api.subprocess.STDOUT) | 67 stderr=input_api.subprocess.STDOUT) |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
182 if "/deep/" in line: | 145 if "/deep/" in line: |
183 results.append(output_api.PresubmitError(("%s:%d uses /deep/ sel ector") % (f.LocalPath(), line_number))) | 146 results.append(output_api.PresubmitError(("%s:%d uses /deep/ sel ector") % (f.LocalPath(), line_number))) |
184 if "::shadow" in line: | 147 if "::shadow" in line: |
185 results.append(output_api.PresubmitError(("%s:%d uses ::shadow s elector") % (f.LocalPath(), line_number))) | 148 results.append(output_api.PresubmitError(("%s:%d uses ::shadow s elector") % (f.LocalPath(), line_number))) |
186 return results | 149 return results |
187 | 150 |
188 | 151 |
189 def CheckChangeOnUpload(input_api, output_api): | 152 def CheckChangeOnUpload(input_api, output_api): |
190 results = [] | 153 results = [] |
191 | 154 |
192 (node_results, has_errors) = _CheckNodeAndNPMModules(input_api, output_api) | |
193 results.extend(node_results) | |
194 if has_errors: | |
195 results.append(output_api.PresubmitError("ERROR: Bailed out early becaus e error using node.js/npm")) | |
196 return results | |
197 | |
198 (format_results, has_errors) = _FormatDevtools(input_api, output_api) | 155 (format_results, has_errors) = _FormatDevtools(input_api, output_api) |
199 results.extend(format_results) | 156 results.extend(format_results) |
200 if has_errors: | 157 if has_errors: |
201 results.append(output_api.PresubmitError("ERROR: Bailed out early becaus e formatting errors were found")) | 158 results.append(output_api.PresubmitError("ERROR: Bailed out early becaus e formatting errors were found")) |
dgozman
2017/02/27 18:36:28
Do we need to bailout now? Compilation is fast.
chenwilliam
2017/02/28 20:28:38
Removed bailout.
| |
202 return results | 159 return results |
203 | 160 |
161 results.extend(_CheckNodeAndNPMModules(input_api, output_api)) | |
204 results.extend(_CheckDevtoolsStyle(input_api, output_api)) | 162 results.extend(_CheckDevtoolsStyle(input_api, output_api)) |
205 results.extend(_CompileDevtoolsFrontend(input_api, output_api)) | 163 results.extend(_CompileDevtoolsFrontend(input_api, output_api)) |
206 results.extend(_CheckConvertSVGToPNGHashes(input_api, output_api)) | 164 results.extend(_CheckConvertSVGToPNGHashes(input_api, output_api)) |
207 results.extend(_CheckOptimizePNGHashes(input_api, output_api)) | 165 results.extend(_CheckOptimizePNGHashes(input_api, output_api)) |
208 results.extend(_CheckCSSViolations(input_api, output_api)) | 166 results.extend(_CheckCSSViolations(input_api, output_api)) |
209 return results | 167 return results |
210 | 168 |
211 | 169 |
212 def CheckChangeOnCommit(input_api, output_api): | 170 def CheckChangeOnCommit(input_api, output_api): |
213 return [] | 171 return [] |
214 | 172 |
215 | 173 |
216 def _getAffectedFrontEndFiles(input_api): | 174 def _getAffectedFrontEndFiles(input_api): |
217 local_paths = [f.AbsoluteLocalPath() for f in input_api.AffectedFiles() if f .Action() != "D"] | 175 local_paths = [f.AbsoluteLocalPath() for f in input_api.AffectedFiles() if f .Action() != "D"] |
218 devtools_root = input_api.PresubmitLocalPath() | 176 devtools_root = input_api.PresubmitLocalPath() |
219 devtools_front_end = input_api.os_path.join(devtools_root, "front_end") | 177 devtools_front_end = input_api.os_path.join(devtools_root, "front_end") |
220 affected_front_end_files = [ | 178 affected_front_end_files = [ |
221 file_name for file_name in local_paths if devtools_front_end in file_nam e and file_name.endswith(".js") | 179 file_name for file_name in local_paths if devtools_front_end in file_nam e and file_name.endswith(".js") |
222 ] | 180 ] |
223 return [input_api.os_path.relpath(file_name, devtools_root) for file_name in affected_front_end_files] | 181 return [input_api.os_path.relpath(file_name, devtools_root) for file_name in affected_front_end_files] |
224 | |
225 | |
226 def _getAffectedJSFiles(input_api): | |
227 local_paths = [f.AbsoluteLocalPath() for f in input_api.AffectedFiles() if f .Action() != "D"] | |
228 devtools_root = input_api.PresubmitLocalPath() | |
229 devtools_front_end = input_api.os_path.join(devtools_root, "front_end") | |
230 devtools_scripts = input_api.os_path.join(devtools_root, "scripts") | |
231 affected_js_files = [ | |
232 file_name for file_name in local_paths | |
233 if (devtools_front_end in file_name or devtools_scripts in file_name) an d file_name.endswith(".js") | |
234 ] | |
235 return [input_api.os_path.relpath(file_name, devtools_root) for file_name in affected_js_files] | |
236 | |
237 | |
238 def _inputPopen(input_api, args): | |
239 return input_api.subprocess.Popen(args, stdout=input_api.subprocess.PIPE, st derr=input_api.subprocess.STDOUT) | |
OLD | NEW |