Chromium Code Reviews| 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 | |
| 8 import os.path as path | 7 import os.path as path |
| 9 import re | 8 import re |
| 10 import subprocess | 9 import subprocess |
| 11 import sys | 10 import sys |
| 12 | 11 |
| 12 import install_node_deps | |
| 13 | |
| 13 files_to_lint = None | 14 files_to_lint = None |
| 14 | 15 |
| 15 if len(sys.argv) >= 2: | 16 if len(sys.argv) >= 2: |
| 16 if sys.argv[1] == "--help": | 17 if sys.argv[1] == "--help": |
| 17 print("Usage: %s [file|dir|glob]*" % path.basename(sys.argv[0])) | 18 print("Usage: %s [file|dir|glob]*" % path.basename(sys.argv[0])) |
| 18 print | 19 print |
| 19 print(" [file|dir|glob]* Path or glob to run eslint on.") | 20 print(" [file|dir|glob]* Path or glob to run eslint on.") |
| 20 print(" If absent, the entire frontend will be checked .") | 21 print(" If absent, the entire frontend will be checked .") |
| 21 sys.exit(0) | 22 sys.exit(0) |
| 22 | 23 |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 41 def to_platform_path_exact(filepath): | 42 def to_platform_path_exact(filepath): |
| 42 if not is_cygwin: | 43 if not is_cygwin: |
| 43 return filepath | 44 return filepath |
| 44 output, _ = popen(["cygpath", "-w", filepath]).communicate() | 45 output, _ = popen(["cygpath", "-w", filepath]).communicate() |
| 45 # pylint: disable=E1103 | 46 # pylint: disable=E1103 |
| 46 return output.strip().replace("\\", "\\\\") | 47 return output.strip().replace("\\", "\\\\") |
| 47 | 48 |
| 48 scripts_path = path.dirname(path.abspath(__file__)) | 49 scripts_path = path.dirname(path.abspath(__file__)) |
| 49 devtools_path = path.dirname(scripts_path) | 50 devtools_path = path.dirname(scripts_path) |
| 50 devtools_frontend_path = path.join(devtools_path, "front_end") | 51 devtools_frontend_path = path.join(devtools_path, "front_end") |
| 51 | 52 eslint_path = path.join(devtools_path, "node_modules", ".bin", "eslint") |
|
lushnikov
2016/10/05 16:22:49
who's responsible for installing eslint?
chenwilliam
2016/10/05 17:59:19
'npm install' is run as part of the PRESUBMIT scri
| |
| 52 | |
| 53 # Based on http://stackoverflow.com/questions/377017/test-if-executable-exists-i n-python. | |
| 54 def which(program): | |
| 55 def is_exe(fpath): | |
| 56 return path.isfile(fpath) and os.access(fpath, os.X_OK) | |
| 57 | |
| 58 fpath, fname = path.split(program) | |
| 59 if fpath: | |
| 60 if is_exe(program): | |
| 61 return program | |
| 62 else: | |
| 63 for part in os.environ["PATH"].split(os.pathsep): | |
| 64 part = part.strip("\"") | |
| 65 exe_file = path.join(part, program) | |
| 66 if is_exe(exe_file): | |
| 67 return exe_file | |
| 68 return None | |
| 69 | |
| 70 | 53 |
| 71 print("Linting JavaScript with eslint...\n") | 54 print("Linting JavaScript with eslint...\n") |
| 72 | 55 |
| 73 | 56 |
| 74 def js_lint(files_list=None): | 57 def js_lint(files_list=None): |
| 75 eslint_errors_found = False | 58 eslint_errors_found = False |
| 76 | 59 if not path.isfile(eslint_path): |
| 77 eslint_path = which("eslint") | 60 print("Failed to run eslint, run ./scripts/install_node_deps.py to insta ll eslint") |
| 78 if not eslint_path: | 61 eslint_errors_found = True |
| 79 print("!! NOTE: Skipping JavaScript linting because eslint is not instal led.") | |
| 80 print("!! npm install -g eslint") | |
| 81 eslint_errors_found = False # Linting is opt-in for now, so this is a s oft failure | |
| 82 return eslint_errors_found | 62 return eslint_errors_found |
| 83 | 63 |
| 84 if files_list is None: | 64 if files_list is None: |
| 85 files_list = [devtools_frontend_path] | 65 files_list = [devtools_frontend_path] |
| 86 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")] |
| 87 | 67 |
| 88 eslintconfig_path = path.join(devtools_path, "front_end/.eslintrc.js") | 68 eslintconfig_path = path.join(devtools_path, "front_end/.eslintrc.js") |
| 89 eslintignore_path = path.join(devtools_path, "front_end/.eslintignore") | 69 eslintignore_path = path.join(devtools_path, "front_end/.eslintignore") |
| 70 (node_path, _) = install_node_deps.resolve_node_paths() | |
| 90 exec_command = [ | 71 exec_command = [ |
| 72 node_path, | |
| 91 eslint_path, | 73 eslint_path, |
| 92 "--config", to_platform_path_exact(eslintconfig_path), | 74 "--config", to_platform_path_exact(eslintconfig_path), |
| 93 "--ignore-path", to_platform_path_exact(eslintignore_path), | 75 "--ignore-path", to_platform_path_exact(eslintignore_path), |
| 94 ] + files_list | 76 ] + files_list |
| 95 | 77 |
| 96 eslint_proc = popen(exec_command) | 78 eslint_proc = popen(exec_command) |
| 97 (eslint_proc_out, _) = eslint_proc.communicate() | 79 (eslint_proc_out, _) = eslint_proc.communicate() |
| 98 if eslint_proc.returncode != 0: | 80 if eslint_proc.returncode != 0: |
| 99 eslint_errors_found = True | 81 eslint_errors_found = True |
| 100 else: | 82 else: |
| 101 print("eslint exited successfully") | 83 print("eslint exited successfully") |
| 102 | 84 |
| 103 print(eslint_proc_out) | 85 print(eslint_proc_out) |
| 104 return eslint_errors_found | 86 return eslint_errors_found |
| 105 | 87 |
| 106 | 88 |
| 107 errors_found = js_lint(files_to_lint) | 89 errors_found = js_lint(files_to_lint) |
| 108 | 90 |
| 109 if errors_found: | 91 if errors_found: |
| 110 print("ERRORS DETECTED") | 92 print("ERRORS DETECTED") |
| 111 sys.exit(1) | 93 sys.exit(1) |
| 112 else: | |
| 113 print("OK") | |
| OLD | NEW |