OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import os |
| 6 import subprocess |
| 7 import sys |
| 8 |
| 9 |
| 10 _CATAPULT_PATH = os.path.join( |
| 11 os.path.dirname(os.path.abspath(__file__)), |
| 12 os.path.pardir, os.path.pardir, os.path.pardir) |
| 13 |
| 14 |
| 15 def _AddToPathIfNeeded(path): |
| 16 if path not in sys.path: |
| 17 sys.path.insert(0, path) |
| 18 |
| 19 |
| 20 def _UpdateSysPathIfNeeded(): |
| 21 _AddToPathIfNeeded(os.path.join(_CATAPULT_PATH, 'common', 'node_runner')) |
| 22 _AddToPathIfNeeded(os.path.join(_CATAPULT_PATH, 'common', 'py_utils')) |
| 23 |
| 24 |
| 25 _UpdateSysPathIfNeeded() |
| 26 |
| 27 |
| 28 import py_utils |
| 29 from node_runner import node_util |
| 30 |
| 31 |
| 32 BASE_ESLINT_CMD = [ |
| 33 node_util.GetNodePath(), |
| 34 os.path.join(node_util.GetNodeModulesPath(), 'eslint', 'bin', 'eslint.js'), |
| 35 '--color' |
| 36 ] |
| 37 |
| 38 |
| 39 DEFAULT_ESLINT_CONFIG = os.path.join( |
| 40 py_utils.GetCatapultDir(), 'common', 'eslint', '.eslintrc') |
| 41 |
| 42 |
| 43 DEFAULT_ESLINT_RULES_DIR = os.path.join( |
| 44 py_utils.GetCatapultDir(), 'common', 'eslint', 'rules') |
| 45 |
| 46 |
| 47 def _CreateEslintCommand(config, rulesdir): |
| 48 return BASE_ESLINT_CMD + [ |
| 49 '--config', config, |
| 50 '--rulesdir', rulesdir |
| 51 ] |
| 52 |
| 53 |
| 54 def RunEslintOnDirs(dirs, |
| 55 config=DEFAULT_ESLINT_CONFIG, |
| 56 rules_dir=DEFAULT_ESLINT_RULES_DIR): |
| 57 if type(dirs) is not list or len(dirs) == 0: |
| 58 raise ValueError('Must specify a non-empty list of directories to lint.') |
| 59 |
| 60 try: |
| 61 find_cmd = ['find'] + dirs + ['-name', '*.html'] |
| 62 eslint_cmd = _CreateEslintCommand(config, rules_dir) |
| 63 p1 = subprocess.Popen(find_cmd, stdout=subprocess.PIPE) |
| 64 output = subprocess.check_output(['xargs'] + eslint_cmd, stdin=p1.stdout) |
| 65 p1.wait() |
| 66 return output |
| 67 except subprocess.CalledProcessError as e: |
| 68 return e.output |
| 69 |
| 70 |
| 71 def RunEslintOnFiles(filenames, |
| 72 config=DEFAULT_ESLINT_CONFIG, |
| 73 rules_dir=DEFAULT_ESLINT_RULES_DIR): |
| 74 if type(filenames) is not list or len(filenames) == 0: |
| 75 raise ValueError('Must specify a non-empty list of files to lint.') |
| 76 |
| 77 try: |
| 78 eslint_cmd = _CreateEslintCommand(config, rules_dir) |
| 79 return subprocess.check_output(eslint_cmd + filenames, |
| 80 stderr=subprocess.STDOUT) |
| 81 except subprocess.CalledProcessError as e: |
| 82 return e.output |
OLD | NEW |