|
@@ -12,7 +12,6 @@ dependencies such as flake8/pep8.
|
|
|
"""
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
-
|
|
|
import os
|
|
|
import sys
|
|
|
import subprocess
|
|
@@ -44,8 +43,11 @@ def get_files(path):
|
|
|
|
|
|
|
|
|
def get_modified_files(path):
|
|
|
- return [s for s in check_output(
|
|
|
- ['git', 'diff-index', '--cached', '--name-only', 'HEAD']).split('\n') if s]
|
|
|
+ return [
|
|
|
+ s
|
|
|
+ for s in check_output(['git', 'diff-index', '--cached', '--name-only', 'HEAD'])
|
|
|
+ .split('\n') if s
|
|
|
+ ]
|
|
|
|
|
|
|
|
|
def get_files_for_list(file_list):
|
|
@@ -65,20 +67,14 @@ def get_files_for_list(file_list):
|
|
|
def get_js_files(file_list=None):
|
|
|
if file_list is None:
|
|
|
file_list = ['tests/js', 'src/sentry/static/sentry/app']
|
|
|
- return [
|
|
|
- x for x in get_files_for_list(file_list)
|
|
|
- if x.endswith(('.js', '.jsx'))
|
|
|
- ]
|
|
|
+ return [x for x in get_files_for_list(file_list) if x.endswith(('.js', '.jsx'))]
|
|
|
return file_list
|
|
|
|
|
|
|
|
|
def get_python_files(file_list=None):
|
|
|
if file_list is None:
|
|
|
file_list = ['src', 'tests']
|
|
|
- return [
|
|
|
- x for x in get_files_for_list(file_list)
|
|
|
- if x.endswith('.py')
|
|
|
- ]
|
|
|
+ return [x for x in get_files_for_list(file_list) if x.endswith('.py')]
|
|
|
|
|
|
|
|
|
def py_lint(file_list):
|
|
@@ -93,8 +89,7 @@ def py_lint(file_list):
|
|
|
|
|
|
def js_lint(file_list=None):
|
|
|
|
|
|
- project_root = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir,
|
|
|
- os.pardir)
|
|
|
+ project_root = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, os.pardir)
|
|
|
eslint_path = os.path.join(project_root, 'node_modules', '.bin', 'eslint')
|
|
|
|
|
|
if not os.path.exists(eslint_path):
|
|
@@ -107,8 +102,9 @@ def js_lint(file_list=None):
|
|
|
|
|
|
has_errors = False
|
|
|
if js_file_list:
|
|
|
- status = Popen([eslint_path, '--config', eslint_config, '--ext', '.jsx', '--fix']
|
|
|
- + js_file_list).wait()
|
|
|
+ status = Popen(
|
|
|
+ [eslint_path, '--config', eslint_config, '--ext', '.jsx', '--fix'] + js_file_list
|
|
|
+ ).wait()
|
|
|
has_errors = status != 0
|
|
|
|
|
|
return has_errors
|
|
@@ -129,14 +125,19 @@ def yarn_check(file_list):
|
|
|
return False
|
|
|
|
|
|
if 'package.json' in file_list and 'yarn.lock' not in file_list:
|
|
|
- echo(style("""
|
|
|
+ echo(
|
|
|
+ style(
|
|
|
+ """
|
|
|
Warning: package.json modified without accompanying yarn.lock modifications.
|
|
|
|
|
|
If you updated a dependency/devDependency in package.json, you must run `yarn install` to update the lockfile.
|
|
|
|
|
|
To skip this check, run:
|
|
|
|
|
|
-$ SKIP_YARN_CHECK=1 git commit [options]""", fg='yellow'))
|
|
|
+$ SKIP_YARN_CHECK=1 git commit [options]""",
|
|
|
+ fg='yellow'
|
|
|
+ )
|
|
|
+ )
|
|
|
return True
|
|
|
|
|
|
return False
|
|
@@ -147,13 +148,14 @@ def js_format(file_list=None):
|
|
|
We only format JavaScript code as part of this pre-commit hook. It is not part
|
|
|
of the lint engine.
|
|
|
"""
|
|
|
- project_root = os.path.join(os.path.dirname(
|
|
|
- __file__), os.pardir, os.pardir, os.pardir)
|
|
|
- prettier_path = os.path.join(
|
|
|
- project_root, 'node_modules', '.bin', 'prettier')
|
|
|
+ project_root = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, os.pardir)
|
|
|
+ prettier_path = os.path.join(project_root, 'node_modules', '.bin', 'prettier')
|
|
|
|
|
|
if not os.path.exists(prettier_path):
|
|
|
- echo('[sentry.lint] Skipping JavaScript formatting because prettier is not installed.', err=True)
|
|
|
+ echo(
|
|
|
+ '[sentry.lint] Skipping JavaScript formatting because prettier is not installed.',
|
|
|
+ err=True
|
|
|
+ )
|
|
|
return False
|
|
|
|
|
|
# Get Prettier version from package.json
|
|
@@ -161,42 +163,39 @@ def js_format(file_list=None):
|
|
|
package_json_path = os.path.join(project_root, 'package.json')
|
|
|
with open(package_json_path) as package_json:
|
|
|
try:
|
|
|
- package_version = json.load(package_json)[
|
|
|
- 'devDependencies']['prettier']
|
|
|
+ package_version = json.load(package_json)['devDependencies']['prettier']
|
|
|
except KeyError:
|
|
|
echo('!! Prettier missing from package.json', err=True)
|
|
|
return False
|
|
|
|
|
|
- prettier_version = subprocess.check_output(
|
|
|
- [prettier_path, '--version']).rstrip()
|
|
|
+ prettier_version = subprocess.check_output([prettier_path, '--version']).rstrip()
|
|
|
if prettier_version != package_version:
|
|
|
echo(
|
|
|
- '[sentry.lint] Prettier is out of date: {} (expected {}). Please run `yarn install`.'.format(
|
|
|
- prettier_version,
|
|
|
- package_version),
|
|
|
- err=True)
|
|
|
+ '[sentry.lint] Prettier is out of date: {} (expected {}). Please run `yarn install`.'.
|
|
|
+ format(prettier_version, package_version),
|
|
|
+ err=True
|
|
|
+ )
|
|
|
return False
|
|
|
|
|
|
js_file_list = get_js_files(file_list)
|
|
|
- return run_formatter([prettier_path,
|
|
|
- '--write',
|
|
|
- '--single-quote',
|
|
|
- '--bracket-spacing=false',
|
|
|
- '--print-width=90',
|
|
|
- '--jsx-bracket-same-line=true'],
|
|
|
- js_file_list)
|
|
|
+ return run_formatter(
|
|
|
+ [
|
|
|
+ prettier_path, '--write', '--single-quote', '--bracket-spacing=false',
|
|
|
+ '--print-width=90', '--jsx-bracket-same-line=true'
|
|
|
+ ], js_file_list
|
|
|
+ )
|
|
|
|
|
|
|
|
|
def py_format(file_list=None):
|
|
|
try:
|
|
|
- __import__('autopep8')
|
|
|
+ __import__('yapf')
|
|
|
except ImportError:
|
|
|
- echo('[sentry.lint] Skipping Python autoformat because autopep8 is not installed.', err=True)
|
|
|
+ echo('[sentry.lint] Skipping Python autoformat because yapf is not installed.', err=True)
|
|
|
return False
|
|
|
|
|
|
py_file_list = get_python_files(file_list)
|
|
|
|
|
|
- return run_formatter(['autopep8', '--in-place', '-j0'], py_file_list)
|
|
|
+ return run_formatter(['yapf', '--in-place', '-p'], py_file_list)
|
|
|
|
|
|
|
|
|
def run_formatter(cmd, file_list, prompt_on_changes=True):
|
|
@@ -226,10 +225,11 @@ def run_formatter(cmd, file_list, prompt_on_changes=True):
|
|
|
secho('Stage this patch and continue? [Y/n] ', bold=True)
|
|
|
if fp.readline().strip().lower() != 'y':
|
|
|
echo(
|
|
|
- '[sentry.lint] Aborted! Changes have been applied but not staged.', err=True)
|
|
|
+ '[sentry.lint] Aborted! Changes have been applied but not staged.',
|
|
|
+ err=True
|
|
|
+ )
|
|
|
sys.exit(1)
|
|
|
- status = subprocess.Popen(
|
|
|
- ['git', 'update-index', '--add'] + file_list).wait()
|
|
|
+ status = subprocess.Popen(['git', 'update-index', '--add'] + file_list).wait()
|
|
|
has_errors = status != 0
|
|
|
return has_errors
|
|
|
|
|
@@ -239,10 +239,7 @@ def run(file_list=None, format=True, lint=True, js=True, py=True, yarn=True):
|
|
|
old_sysargv = sys.argv
|
|
|
|
|
|
try:
|
|
|
- sys.argv = [
|
|
|
- os.path.join(os.path.dirname(__file__),
|
|
|
- os.pardir, os.pardir, os.pardir)
|
|
|
- ]
|
|
|
+ sys.argv = [os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, os.pardir)]
|
|
|
results = []
|
|
|
|
|
|
# packages
|