| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # | 2 # |
| 3 # Copyright 2016 The Chromium Authors. All rights reserved. | 3 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 import os.path as path | 7 import os.path as path |
| 8 import re | 8 import re |
| 9 import subprocess | 9 import subprocess |
| 10 import sys | 10 import sys |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 sys.exit(0) | 22 sys.exit(0) |
| 23 | 23 |
| 24 else: | 24 else: |
| 25 print("Linting only these files:\n %s" % sys.argv[1:]) | 25 print("Linting only these files:\n %s" % sys.argv[1:]) |
| 26 files_to_lint = sys.argv[1:] | 26 files_to_lint = sys.argv[1:] |
| 27 | 27 |
| 28 | 28 |
| 29 is_cygwin = sys.platform == "cygwin" | 29 is_cygwin = sys.platform == "cygwin" |
| 30 | 30 |
| 31 | 31 |
| 32 def popen(arguments): | 32 def popen(arguments, cwd=None): |
| 33 return subprocess.Popen(arguments, stdout=subprocess.PIPE, stderr=subprocess
.STDOUT) | 33 return subprocess.Popen(arguments, cwd=cwd, stdout=subprocess.PIPE, stderr=s
ubprocess.STDOUT) |
| 34 | 34 |
| 35 | 35 |
| 36 def to_platform_path(filepath): | 36 def to_platform_path(filepath): |
| 37 if not is_cygwin: | 37 if not is_cygwin: |
| 38 return filepath | 38 return filepath |
| 39 return re.sub(r"^/cygdrive/(\w)", "\\1:", filepath) | 39 return re.sub(r"^/cygdrive/(\w)", "\\1:", filepath) |
| 40 | 40 |
| 41 | 41 |
| 42 def to_platform_path_exact(filepath): | 42 def to_platform_path_exact(filepath): |
| 43 if not is_cygwin: | 43 if not is_cygwin: |
| 44 return filepath | 44 return filepath |
| 45 output, _ = popen(["cygpath", "-w", filepath]).communicate() | 45 output, _ = popen(["cygpath", "-w", filepath]).communicate() |
| 46 # pylint: disable=E1103 | 46 # pylint: disable=E1103 |
| 47 return output.strip().replace("\\", "\\\\") | 47 return output.strip().replace("\\", "\\\\") |
| 48 | 48 |
| 49 scripts_path = path.dirname(path.abspath(__file__)) | 49 scripts_path = path.dirname(path.abspath(__file__)) |
| 50 devtools_path = path.dirname(scripts_path) | 50 devtools_path = path.dirname(scripts_path) |
| 51 devtools_frontend_path = path.join(devtools_path, "front_end") | 51 devtools_frontend_path = path.join(devtools_path, "front_end") |
| 52 eslint_path = path.join(devtools_path, "node_modules", ".bin", "eslint") | 52 eslint_path = path.join(devtools_path, "node_modules", "eslint", "bin", "eslint.
js") |
| 53 | 53 |
| 54 print("Linting JavaScript with eslint...\n") | 54 print("Linting JavaScript with eslint...\n") |
| 55 | 55 |
| 56 | 56 |
| 57 def js_lint(files_list=None): | 57 def js_lint(files_list=None): |
| 58 eslint_errors_found = False | 58 eslint_errors_found = False |
| 59 if not path.isfile(eslint_path): | 59 if not path.isfile(eslint_path): |
| 60 print("Failed to run eslint, run ./scripts/install_node_deps.py to insta
ll eslint") | 60 print("Failed to run eslint, run ./scripts/install_node_deps.py to insta
ll eslint") |
| 61 eslint_errors_found = True | 61 eslint_errors_found = True |
| 62 return eslint_errors_found | 62 return eslint_errors_found |
| 63 | 63 |
| 64 if files_list is None: | 64 if files_list is None: |
| 65 files_list = [devtools_frontend_path] | 65 files_list = [devtools_frontend_path] |
| 66 files_list = [file_name for file_name in files_list if not file_name.endswit
h(".eslintrc.js")] | 66 files_list = [file_name for file_name in files_list if not file_name.endswit
h(".eslintrc.js")] |
| 67 | 67 |
| 68 eslintconfig_path = path.join(devtools_path, ".eslintrc.js") | 68 eslintconfig_path = path.join(devtools_path, ".eslintrc.js") |
| 69 eslintignore_path = path.join(devtools_path, ".eslintignore") | 69 eslintignore_path = path.join(devtools_path, ".eslintignore") |
| 70 (node_path, _) = install_node_deps.resolve_node_paths() | 70 (node_path, _) = install_node_deps.resolve_node_paths() |
| 71 exec_command = [ | 71 exec_command = [ |
| 72 node_path, | 72 node_path, |
| 73 eslint_path, | 73 eslint_path, |
| 74 "--config", to_platform_path_exact(eslintconfig_path), | 74 "--config", to_platform_path_exact(eslintconfig_path), |
| 75 "--ignore-path", to_platform_path_exact(eslintignore_path), | 75 "--ignore-path", to_platform_path_exact(eslintignore_path), |
| 76 ] + files_list | 76 ] + files_list |
| 77 | 77 |
| 78 eslint_proc = popen(exec_command) | 78 eslint_proc = popen(exec_command, cwd=devtools_path) |
| 79 (eslint_proc_out, _) = eslint_proc.communicate() | 79 (eslint_proc_out, _) = eslint_proc.communicate() |
| 80 if eslint_proc.returncode != 0: | 80 if eslint_proc.returncode != 0: |
| 81 eslint_errors_found = True | 81 eslint_errors_found = True |
| 82 else: | 82 else: |
| 83 print("eslint exited successfully") | 83 print("eslint exited successfully") |
| 84 | 84 |
| 85 print(eslint_proc_out) | 85 print(eslint_proc_out) |
| 86 return eslint_errors_found | 86 return eslint_errors_found |
| 87 | 87 |
| 88 | 88 |
| 89 errors_found = js_lint(files_to_lint) | 89 errors_found = js_lint(files_to_lint) |
| 90 | 90 |
| 91 if errors_found: | 91 if errors_found: |
| 92 print("ERRORS DETECTED") | 92 print("ERRORS DETECTED") |
| 93 sys.exit(1) | 93 sys.exit(1) |
| OLD | NEW |