Skip to content
Snippets Groups Projects
Commit 115f346c authored by Andrew Halberstadt's avatar Andrew Halberstadt
Browse files

Bug 1392795 - [yamllint] Group paths to lint by their closest config and run...

Bug 1392795 - [yamllint] Group paths to lint by their closest config and run each config group separately, r=dustin

This makes configuration files for yamllint work a bit better. It's still not perfect, but it's an improvement
on the current situation.

MozReview-Commit-ID: IKxgQm1a7bP

--HG--
extra : rebase_source : 051fafe21337f0557ee39ec71c90e74fd61d3da7
parent fff59923
No related branches found
No related tags found
No related merge requests found
---
ignore: |
*node_modules*
extends: default
--- ---
ignore: |
*node_modules*
extends: default extends: default
......
...@@ -6,6 +6,7 @@ import re ...@@ -6,6 +6,7 @@ import re
import os import os
import signal import signal
import subprocess import subprocess
from collections import defaultdict
import which import which
from mozprocess import ProcessHandlerMixin from mozprocess import ProcessHandlerMixin
...@@ -114,13 +115,35 @@ def gen_yamllint_args(cmdargs, paths=None, conf_file=None): ...@@ -114,13 +115,35 @@ def gen_yamllint_args(cmdargs, paths=None, conf_file=None):
args = cmdargs[:] args = cmdargs[:]
if isinstance(paths, basestring): if isinstance(paths, basestring):
paths = [paths] paths = [paths]
if conf_file: if conf_file and conf_file != 'default':
return args + ['-c', conf_file] + paths return args + ['-c', conf_file] + paths
return args + paths return args + paths
def lint(files, config, **lintargs): def ancestors(path):
while path:
yield path
(path, child) = os.path.split(path)
if child == "":
break
def get_relevant_configs(name, path, root):
"""Returns a list of configuration files that exist in `path`'s ancestors,
sorted from closest->furthest.
"""
configs = []
for path in ancestors(path):
if path == root:
break
config = os.path.join(path, name)
if os.path.isfile(config):
configs.append(config)
return configs
def lint(files, config, **lintargs):
if not reinstall_yamllint(): if not reinstall_yamllint():
print(YAMLLINT_INSTALL_ERROR) print(YAMLLINT_INSTALL_ERROR)
return 1 return 1
...@@ -138,17 +161,12 @@ def lint(files, config, **lintargs): ...@@ -138,17 +161,12 @@ def lint(files, config, **lintargs):
# Run any paths with a .yamllint file in the directory separately so # Run any paths with a .yamllint file in the directory separately so
# it gets picked up. This means only .yamllint files that live in # it gets picked up. This means only .yamllint files that live in
# directories that are explicitly included will be considered. # directories that are explicitly included will be considered.
no_config = [] paths_by_config = defaultdict(list)
for f in files: for f in files:
yamllint_config = os.path.join(f, '.yamllint') conf_files = get_relevant_configs('.yamllint', f, config['root'])
if not os.path.isfile(yamllint_config): paths_by_config[conf_files[0] if conf_files else 'default'].append(f)
no_config.append(f)
continue for conf_file, paths in paths_by_config.items():
run_process(config, run_process(config, gen_yamllint_args(cmdargs, conf_file=conf_file, paths=paths))
gen_yamllint_args(cmdargs, conf_file=yamllint_config, paths=f))
if no_config:
run_process(config,
gen_yamllint_args(cmdargs, paths=no_config))
return results return results
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment