Skip to content

Commit

Permalink
Normalize displayed file paths
Browse files Browse the repository at this point in the history
Avoids displaying a mix of relative and absolute paths in tool output.

Signed-off-by: Sorin Sbarnea <ssbarnea@redhat.com>
  • Loading branch information
ssbarnea authored and webknjaz committed Nov 3, 2019
1 parent 5453d07 commit cad1804
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
6 changes: 4 additions & 2 deletions lib/ansiblelint/__init__.py
Expand Up @@ -260,7 +260,7 @@ def run(self):
continue
if playbook[1] == 'role':
continue
files.append({'path': playbook[0], 'type': playbook[1]})
files.append({'path': ansiblelint.utils.normpath(playbook[0]), 'type': playbook[1]})
visited = set()
while (visited != self.playbooks):
for arg in self.playbooks - visited:
Expand All @@ -280,7 +280,9 @@ def run(self):
files = [x for x in files if x['path'] not in self.checked_files]
for file in files:
if self.verbosity > 0:
print("Examining %s of type %s" % (file['path'], file['type']))
print("Examining %s of type %s" % (
ansiblelint.utils.normpath(file['path']),
file['type']))
matches.extend(self.rules.run(file, tags=set(self.tags),
skip_list=self.skip_list))
# update list of checked files
Expand Down
5 changes: 3 additions & 2 deletions lib/ansiblelint/__main__.py
Expand Up @@ -30,6 +30,7 @@
import six
from ansiblelint import default_rulesdir, RulesCollection, Runner
from ansiblelint.version import __version__
from ansiblelint.utils import normpath
import yaml
import os

Expand Down Expand Up @@ -176,7 +177,7 @@ def main():
skip.update(str(s).split(','))
options.skip_list = frozenset(skip)

playbooks = set(args)
playbooks = sorted(set(args))
matches = list()
checked_files = set()
for playbook in playbooks:
Expand All @@ -185,7 +186,7 @@ def main():
options.verbosity, checked_files)
matches.extend(runner.run())

matches.sort(key=lambda x: (x.filename, x.linenumber, x.rule.id))
matches.sort(key=lambda x: (normpath(x.filename), x.linenumber, x.rule.id))

for match in matches:
print(formatter.format(match, options.colored))
Expand Down
13 changes: 7 additions & 6 deletions lib/ansiblelint/formatters/__init__.py
Expand Up @@ -2,6 +2,7 @@
from ansible import color
except ImportError:
from ansible.utils import color
from ansiblelint.utils import normpath


class Formatter(object):
Expand All @@ -12,7 +13,7 @@ def format(self, match, colored=False):
color.ANSIBLE_COLOR = True
return formatstr.format(color.stringc(u"[{0}]".format(match.rule.id), 'bright red'),
color.stringc(match.message, 'red'),
color.stringc(match.filename, 'blue'),
color.stringc(normpath(match.filename), 'blue'),
color.stringc(str(match.linenumber), 'cyan'),
color.stringc(u"{0}".format(match.line), 'purple'))
else:
Expand All @@ -30,10 +31,10 @@ def format(self, match, colored=False):
if colored:
color.ANSIBLE_COLOR = True
return formatstr.format(color.stringc(u"[{0}]".format(match.rule.id), 'bright red'),
color.stringc(match.filename, 'blue'),
color.stringc(normpath(match.filename), 'blue'),
color.stringc(str(match.linenumber), 'cyan'))
else:
return formatstr.format(match.rule.id, match.filename,
return formatstr.format(match.rule.id, normpath(match.filename),
match.linenumber)


Expand All @@ -43,12 +44,12 @@ def format(self, match, colored=False):
formatstr = u"{0}:{1}: [{2}] {3}"
if colored:
color.ANSIBLE_COLOR = True
return formatstr.format(color.stringc(match.filename, 'blue'),
return formatstr.format(color.stringc(normpath(match.filename), 'blue'),
color.stringc(str(match.linenumber), 'cyan'),
color.stringc(u"E{0}".format(match.rule.id), 'bright red'),
color.stringc(u"{0}".format(match.message), 'red'))
else:
return formatstr.format(match.filename,
return formatstr.format(normpath(match.filename),
match.linenumber,
"E" + match.rule.id,
match.message)
Expand All @@ -59,7 +60,7 @@ class ParseableSeverityFormatter(object):
def format(self, match, colored=False):
formatstr = u"{0}:{1}: [{2}] [{3}] {4}"

filename = match.filename
filename = normpath(match.filename)
linenumber = str(match.linenumber)
rule_id = u"E{0}".format(match.rule.id)
severity = match.rule.severity
Expand Down
10 changes: 10 additions & 0 deletions lib/ansiblelint/utils.py
Expand Up @@ -732,3 +732,13 @@ def get_rule_skips_from_line(line):
noqa_text = line.split('# noqa')[1]
rule_id_list = noqa_text.split()
return rule_id_list


def normpath(path):
"""
Normalize a path in order to provide a more consistent output.
Currently it generates a relative path but in the future we may want to
make this user configurable.
"""
return os.path.relpath(path)

0 comments on commit cad1804

Please sign in to comment.