Skip to content

Commit

Permalink
Bug 1650834 - Make tools/lint code black compatible r=ahal
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvestre committed Jul 9, 2020
1 parent 2bf6820 commit faf9894
Show file tree
Hide file tree
Showing 49 changed files with 1,085 additions and 887 deletions.
275 changes: 163 additions & 112 deletions tools/lint/android/lints.py

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions tools/lint/black.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ black:
- taskcluster/docker/funsize-update-generator
- testing/condprofile/condprof
- tools/crashreporter/system-symbols
- tools/lint/python/black.py
- tools/lint/test/test_black.py
- tools/lint/
- tools/tryselect/selectors/scriptworker.py
exclude:
- layout/style/ServoCSSPropList.mako.py
Expand Down
39 changes: 22 additions & 17 deletions tools/lint/clang-format/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ def parse_issues(config, output, paths, log):
for line in output:
match = diff_line.match(line)
file, line_no, col, diff, diff2 = match.groups()
log.debug("file={} line={} col={} diff={} diff2={}".format(
file, line_no, col, diff, diff2))
log.debug(
"file={} line={} col={} diff={} diff2={}".format(
file, line_no, col, diff, diff2
)
)
d = diff + "\n" + diff2
res = {
"path": file,
Expand Down Expand Up @@ -78,34 +81,34 @@ def get_clang_format_binary():

clang_tools_path = os.path.join(get_state_dir(), "clang-tools")
bin_path = os.path.join(clang_tools_path, "clang-tidy", "bin")
return os.path.join(bin_path, "clang-format" + substs.get('HOST_BIN_SUFFIX', ''))
return os.path.join(bin_path, "clang-format" + substs.get("HOST_BIN_SUFFIX", ""))


def is_ignored_path(ignored_dir_re, topsrcdir, f):
# Remove up to topsrcdir in pathname and match
if f.startswith(topsrcdir + '/'):
match_f = f[len(topsrcdir + '/'):]
if f.startswith(topsrcdir + "/"):
match_f = f[len(topsrcdir + "/") :]
else:
match_f = f
return re.match(ignored_dir_re, match_f)


def remove_ignored_path(paths, topsrcdir, log):
path_to_third_party = os.path.join(topsrcdir, '.clang-format-ignore')
path_to_third_party = os.path.join(topsrcdir, ".clang-format-ignore")

ignored_dir = []
with open(path_to_third_party, 'r') as fh:
with open(path_to_third_party, "r") as fh:
for line in fh:
# In case it starts with a space
line = line.strip()
# Remove comments and empty lines
if line.startswith('#') or len(line) == 0:
if line.startswith("#") or len(line) == 0:
continue
# The regexp is to make sure we are managing relative paths
ignored_dir.append(r"^[\./]*" + line.rstrip())

# Generates the list of regexp
ignored_dir_re = '(%s)' % '|'.join(ignored_dir)
ignored_dir_re = "(%s)" % "|".join(ignored_dir)

path_list = []
for f in paths:
Expand All @@ -119,13 +122,13 @@ def remove_ignored_path(paths, topsrcdir, log):


def lint(paths, config, fix=None, **lintargs):
log = lintargs['log']
paths = list(expand_exclusions(paths, config, lintargs['root']))
log = lintargs["log"]
paths = list(expand_exclusions(paths, config, lintargs["root"]))

# We ignored some specific files for a bunch of reasons.
# Not using excluding to avoid duplication
if lintargs.get('use_filters', True):
paths = remove_ignored_path(paths, lintargs['root'], log)
if lintargs.get("use_filters", True):
paths = remove_ignored_path(paths, lintargs["root"], log)

# An empty path array can occur when the user passes in `-n`. If we don't
# return early in this case, rustfmt will attempt to read stdin and hang.
Expand All @@ -146,18 +149,20 @@ def lint(paths, config, fix=None, **lintargs):
else:
cmd_args.append("--dry-run")
base_command = cmd_args + paths
log.debug("Command: {}".format(' '.join(cmd_args)))
log.debug("Command: {}".format(" ".join(cmd_args)))
output = run_process(config, base_command)
output_list = []

if len(output) % 3 != 0:
raise Exception("clang-format output should be a multiple of 3. Output: %s" % output)
raise Exception(
"clang-format output should be a multiple of 3. Output: %s" % output
)

for i in range(0, len(output), 3):
# Merge the element 3 by 3 (clang-format output)
line = output[i]
line += ";" + output[i+1]
line += ";" + output[i+2]
line += ";" + output[i + 1]
line += ";" + output[i + 2]
output_list.append(line)

if fix:
Expand Down
41 changes: 22 additions & 19 deletions tools/lint/clippy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@ def parse_issues(log, config, issues, path, onlyIn):
try:
detail = json.loads(six.ensure_text(issue))
if "message" in detail:
p = detail['target']['src_path']
p = detail["target"]["src_path"]
detail = detail["message"]
if "level" in detail:
if ((detail["level"] == "error" or detail["level"] == "failure-note")
and not detail["code"]):
log.debug("Error outside of clippy."
"This means that the build failed. Therefore, skipping this")
if (
detail["level"] == "error" or detail["level"] == "failure-note"
) and not detail["code"]:
log.debug(
"Error outside of clippy."
"This means that the build failed. Therefore, skipping this"
)
log.debug("File = {} / Detail = {}".format(p, detail))
continue
# We are in a clippy warning
Expand All @@ -61,13 +64,17 @@ def parse_issues(log, config, issues, path, onlyIn):
# [], 'code': None, 'level': 'warning', 'message':
# '5 warnings emitted', 'spans': []}
# if this is the case, skip it
log.debug("Skipping the summary line {} for file {}".format(detail, p))
log.debug(
"Skipping the summary line {} for file {}".format(detail, p)
)
continue

l = detail["spans"][0]
if onlyIn and onlyIn not in p:
# Case when we have a .rs in the include list in the yaml file
log.debug("{} is not part of the list of files '{}'".format(p, onlyIn))
log.debug(
"{} is not part of the list of files '{}'".format(p, onlyIn)
)
continue
res = {
"path": p,
Expand Down Expand Up @@ -119,13 +126,9 @@ def get_clippy_version(log, binary):
# --version failed, clippy isn't installed.
return False

log.debug(
"Found version: {}".format(
output
)
)
log.debug("Found version: {}".format(output))

version = re.findall(r'(\d+-\d+-\d+)', output)[0].replace("-", ".")
version = re.findall(r"(\d+-\d+-\d+)", output)[0].replace("-", ".")
version = StrictVersion(version)
return version

Expand Down Expand Up @@ -160,11 +163,11 @@ def lint(paths, config, fix=None, **lintargs):

if not cargo:
print(CARGO_NOT_FOUND)
if 'MOZ_AUTOMATION' in os.environ:
if "MOZ_AUTOMATION" in os.environ:
return 1
return []

min_version_str = config.get('min_clippy_version')
min_version_str = config.get("min_clippy_version")
min_version = StrictVersion(min_version_str)
actual_version = get_clippy_version(log, cargo)
log.debug(
Expand All @@ -183,13 +186,13 @@ def lint(paths, config, fix=None, **lintargs):
cmd_args_common = ["--manifest-path"]
cmd_args_clippy = [
cargo,
'clippy',
'--message-format=json',
"clippy",
"--message-format=json",
]

lock_files_to_delete = []
for p in paths:
lock_file = os.path.join(p, 'Cargo.lock')
lock_file = os.path.join(p, "Cargo.lock")
if not os.path.exists(lock_file):
lock_files_to_delete.append(lock_file)

Expand Down Expand Up @@ -218,7 +221,7 @@ def lint(paths, config, fix=None, **lintargs):
# Make sure that we don't display that either
onlyIn = p

cargo_files = get_ancestors_by_name('Cargo.toml', p, lintargs['root'])
cargo_files = get_ancestors_by_name("Cargo.toml", p, lintargs["root"])
p = cargo_files[0]

log.debug("Path translated to = {}".format(p))
Expand Down
4 changes: 2 additions & 2 deletions tools/lint/cpp/mingw-capitalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
from mozlint.types import LineType

here = os.path.abspath(os.path.dirname(__file__))
HEADERS_FILE = os.path.join(here, 'mingw-headers.txt')
HEADERS_FILE = os.path.join(here, "mingw-headers.txt")
# generated by cd mingw-w64/mingw-w64-headers &&
# find . -name "*.h" | xargs -I bob -- basename bob | sort | uniq)


class MinGWCapitalization(LineType):
def __init__(self, *args, **kwargs):
super(MinGWCapitalization, self).__init__(*args, **kwargs)
with open(HEADERS_FILE, 'r') as fh:
with open(HEADERS_FILE, "r") as fh:
self.headers = fh.read().strip().splitlines()
self.regex = re.compile("^#include\s*<(" + "|".join(self.headers) + ")>")

Expand Down
69 changes: 40 additions & 29 deletions tools/lint/eslint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ def setup(root, **lintargs):

def lint(paths, config, binary=None, fix=None, setup=None, **lintargs):
"""Run eslint."""
log = lintargs['log']
setup_helper.set_project_root(lintargs['root'])
log = lintargs["log"]
setup_helper.set_project_root(lintargs["root"])
module_path = setup_helper.get_project_root()

# Valid binaries are:
Expand All @@ -63,35 +63,44 @@ def lint(paths, config, binary=None, fix=None, setup=None, **lintargs):
print(ESLINT_NOT_FOUND_MESSAGE)
return 1

extra_args = lintargs.get('extra_args') or []
extra_args = lintargs.get("extra_args") or []
exclude_args = []
for path in config.get('exclude', []):
exclude_args.extend(['--ignore-pattern', os.path.relpath(path, lintargs['root'])])

cmd_args = [binary,
os.path.join(module_path, "node_modules", "eslint", "bin", "eslint.js"),
# This keeps ext as a single argument.
'--ext', '[{}]'.format(','.join(config['extensions'])),
'--format', 'json',
'--no-error-on-unmatched-pattern',
] + extra_args + exclude_args + paths
log.debug("Command: {}".format(' '.join(cmd_args)))
for path in config.get("exclude", []):
exclude_args.extend(
["--ignore-pattern", os.path.relpath(path, lintargs["root"])]
)

cmd_args = (
[
binary,
os.path.join(module_path, "node_modules", "eslint", "bin", "eslint.js"),
# This keeps ext as a single argument.
"--ext",
"[{}]".format(",".join(config["extensions"])),
"--format",
"json",
"--no-error-on-unmatched-pattern",
]
+ extra_args
+ exclude_args
+ paths
)
log.debug("Command: {}".format(" ".join(cmd_args)))

# eslint requires that --fix be set before the --ext argument.
if fix:
cmd_args.insert(2, '--fix')
cmd_args.insert(2, "--fix")

shell = False
if os.environ.get('MSYSTEM') in ('MINGW32', 'MINGW64'):
if os.environ.get("MSYSTEM") in ("MINGW32", "MINGW64"):
# The eslint binary needs to be run from a shell with msys
shell = True
encoding = 'utf-8'
encoding = "utf-8"

orig = signal.signal(signal.SIGINT, signal.SIG_IGN)
proc = subprocess.Popen(cmd_args,
shell=shell,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
proc = subprocess.Popen(
cmd_args, shell=shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
signal.signal(signal.SIGINT, orig)

try:
Expand All @@ -118,16 +127,18 @@ def lint(paths, config, binary=None, fix=None, setup=None, **lintargs):

results = []
for obj in jsonresult:
errors = obj['messages']
errors = obj["messages"]

for err in errors:
err.update({
'hint': err.get('fix'),
'level': 'error' if err['severity'] == 2 else 'warning',
'lineno': err.get('line') or 0,
'path': obj['filePath'],
'rule': err.get('ruleId'),
})
err.update(
{
"hint": err.get("fix"),
"level": "error" if err["severity"] == 2 else "warning",
"lineno": err.get("line") or 0,
"path": obj["filePath"],
"rule": err.get("ruleId"),
}
)
results.append(result.from_config(config, **err))

return results
Loading

0 comments on commit faf9894

Please sign in to comment.