Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(35)

Unified Diff: common/eslint/eslint/__init__.py

Issue 2361623007: Add a run_eslint wrapper script (Closed)
Patch Set: Synced to head Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « common/eslint/bin/run_tests ('k') | common/eslint/eslint/smoke_test.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: common/eslint/eslint/__init__.py
diff --git a/common/eslint/eslint/__init__.py b/common/eslint/eslint/__init__.py
new file mode 100644
index 0000000000000000000000000000000000000000..5b2ba5212ccb6f4897f0062303495818a79cc3c4
--- /dev/null
+++ b/common/eslint/eslint/__init__.py
@@ -0,0 +1,82 @@
+# Copyright 2016 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import os
+import subprocess
+import sys
+
+
+_CATAPULT_PATH = os.path.join(
+ os.path.dirname(os.path.abspath(__file__)),
+ os.path.pardir, os.path.pardir, os.path.pardir)
+
+
+def _AddToPathIfNeeded(path):
+ if path not in sys.path:
+ sys.path.insert(0, path)
+
+
+def _UpdateSysPathIfNeeded():
+ _AddToPathIfNeeded(os.path.join(_CATAPULT_PATH, 'common', 'node_runner'))
+ _AddToPathIfNeeded(os.path.join(_CATAPULT_PATH, 'common', 'py_utils'))
+
+
+_UpdateSysPathIfNeeded()
+
+
+import py_utils
+from node_runner import node_util
+
+
+BASE_ESLINT_CMD = [
+ node_util.GetNodePath(),
+ os.path.join(node_util.GetNodeModulesPath(), 'eslint', 'bin', 'eslint.js'),
+ '--color'
+]
+
+
+DEFAULT_ESLINT_CONFIG = os.path.join(
+ py_utils.GetCatapultDir(), 'common', 'eslint', '.eslintrc')
+
+
+DEFAULT_ESLINT_RULES_DIR = os.path.join(
+ py_utils.GetCatapultDir(), 'common', 'eslint', 'rules')
+
+
+def _CreateEslintCommand(config, rulesdir):
+ return BASE_ESLINT_CMD + [
+ '--config', config,
+ '--rulesdir', rulesdir
+ ]
+
+
+def RunEslintOnDirs(dirs,
+ config=DEFAULT_ESLINT_CONFIG,
+ rules_dir=DEFAULT_ESLINT_RULES_DIR):
+ if type(dirs) is not list or len(dirs) == 0:
+ raise ValueError('Must specify a non-empty list of directories to lint.')
+
+ try:
+ find_cmd = ['find'] + dirs + ['-name', '*.html']
+ eslint_cmd = _CreateEslintCommand(config, rules_dir)
+ p1 = subprocess.Popen(find_cmd, stdout=subprocess.PIPE)
+ output = subprocess.check_output(['xargs'] + eslint_cmd, stdin=p1.stdout)
+ p1.wait()
+ return output
+ except subprocess.CalledProcessError as e:
+ return e.output
+
+
+def RunEslintOnFiles(filenames,
+ config=DEFAULT_ESLINT_CONFIG,
+ rules_dir=DEFAULT_ESLINT_RULES_DIR):
+ if type(filenames) is not list or len(filenames) == 0:
+ raise ValueError('Must specify a non-empty list of files to lint.')
+
+ try:
+ eslint_cmd = _CreateEslintCommand(config, rules_dir)
+ return subprocess.check_output(eslint_cmd + filenames,
+ stderr=subprocess.STDOUT)
+ except subprocess.CalledProcessError as e:
+ return e.output
« no previous file with comments | « common/eslint/bin/run_tests ('k') | common/eslint/eslint/smoke_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698