Chromium Code Reviews| 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 # TODO: verify windows |
| 52 if len(affected_files) == 0: | 52 check_formatting_process = input_api.subprocess.Popen( |
| 53 return CheckOutput([], has_errors=False) | 53 args=['git', 'cl', '--format', '--js', '--dry-run'], stdout=input_api.su bprocess.PIPE, stderr=input_api.subprocess.STDOUT) |
| 54 original_sys_path = sys.path | 54 check_formatting_process.communicate() |
| 55 try: | 55 if check_formatting_process.returncode == 0: |
| 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) | 56 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 | |
|
chenwilliam
2017/02/25 02:19:22
No more braces autofix, but it'll get caught by es
| |
| 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( | 57 return CheckOutput( |
| 88 [ | 58 [output_api.PresubmitError("ERROR: Found formatting violations.\nPlease run 'git cl format --js'")], has_errors=True) |
| 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 | 59 |
| 96 | 60 |
| 97 def _CheckDevtoolsStyle(input_api, output_api): | 61 def _CheckDevtoolsStyle(input_api, output_api): |
| 98 affected_front_end_files = _getAffectedFrontEndFiles(input_api) | 62 affected_front_end_files = _getAffectedFrontEndFiles(input_api) |
| 99 if len(affected_front_end_files) > 0: | 63 if len(affected_front_end_files) > 0: |
| 100 lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(), "scri pts", "lint_javascript.py") | 64 lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(), "scri pts", "lint_javascript.py") |
| 101 process = input_api.subprocess.Popen( | 65 process = input_api.subprocess.Popen( |
| 102 [input_api.python_executable, lint_path] + affected_front_end_files, | 66 [input_api.python_executable, lint_path] + affected_front_end_files, |
| 103 stdout=input_api.subprocess.PIPE, | 67 stdout=input_api.subprocess.PIPE, |
| 104 stderr=input_api.subprocess.STDOUT) | 68 stderr=input_api.subprocess.STDOUT) |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 182 if "/deep/" in line: | 146 if "/deep/" in line: |
| 183 results.append(output_api.PresubmitError(("%s:%d uses /deep/ sel ector") % (f.LocalPath(), line_number))) | 147 results.append(output_api.PresubmitError(("%s:%d uses /deep/ sel ector") % (f.LocalPath(), line_number))) |
| 184 if "::shadow" in line: | 148 if "::shadow" in line: |
| 185 results.append(output_api.PresubmitError(("%s:%d uses ::shadow s elector") % (f.LocalPath(), line_number))) | 149 results.append(output_api.PresubmitError(("%s:%d uses ::shadow s elector") % (f.LocalPath(), line_number))) |
| 186 return results | 150 return results |
| 187 | 151 |
| 188 | 152 |
| 189 def CheckChangeOnUpload(input_api, output_api): | 153 def CheckChangeOnUpload(input_api, output_api): |
| 190 results = [] | 154 results = [] |
| 191 | 155 |
| 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) | 156 (format_results, has_errors) = _FormatDevtools(input_api, output_api) |
| 199 results.extend(format_results) | 157 results.extend(format_results) |
| 200 if has_errors: | 158 if has_errors: |
| 201 results.append(output_api.PresubmitError("ERROR: Bailed out early becaus e formatting errors were found")) | 159 results.append(output_api.PresubmitError("ERROR: Bailed out early becaus e formatting errors were found")) |
| 202 return results | 160 return results |
| 203 | 161 |
| 162 results.extend(_CheckNodeAndNPMModules(input_api, output_api)) | |
| 204 results.extend(_CheckDevtoolsStyle(input_api, output_api)) | 163 results.extend(_CheckDevtoolsStyle(input_api, output_api)) |
| 205 results.extend(_CompileDevtoolsFrontend(input_api, output_api)) | 164 results.extend(_CompileDevtoolsFrontend(input_api, output_api)) |
| 206 results.extend(_CheckConvertSVGToPNGHashes(input_api, output_api)) | 165 results.extend(_CheckConvertSVGToPNGHashes(input_api, output_api)) |
| 207 results.extend(_CheckOptimizePNGHashes(input_api, output_api)) | 166 results.extend(_CheckOptimizePNGHashes(input_api, output_api)) |
| 208 results.extend(_CheckCSSViolations(input_api, output_api)) | 167 results.extend(_CheckCSSViolations(input_api, output_api)) |
| 209 return results | 168 return results |
| 210 | 169 |
| 211 | 170 |
| 212 def CheckChangeOnCommit(input_api, output_api): | 171 def CheckChangeOnCommit(input_api, output_api): |
| 213 return [] | 172 return [] |
| 214 | 173 |
| 215 | 174 |
| 216 def _getAffectedFrontEndFiles(input_api): | 175 def _getAffectedFrontEndFiles(input_api): |
| 217 local_paths = [f.AbsoluteLocalPath() for f in input_api.AffectedFiles() if f .Action() != "D"] | 176 local_paths = [f.AbsoluteLocalPath() for f in input_api.AffectedFiles() if f .Action() != "D"] |
| 218 devtools_root = input_api.PresubmitLocalPath() | 177 devtools_root = input_api.PresubmitLocalPath() |
| 219 devtools_front_end = input_api.os_path.join(devtools_root, "front_end") | 178 devtools_front_end = input_api.os_path.join(devtools_root, "front_end") |
| 220 affected_front_end_files = [ | 179 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") | 180 file_name for file_name in local_paths if devtools_front_end in file_nam e and file_name.endswith(".js") |
| 222 ] | 181 ] |
| 223 return [input_api.os_path.relpath(file_name, devtools_root) for file_name in affected_front_end_files] | 182 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 |