2019-04-04 14:06:18 +00:00
|
|
|
"""Checks related to ansible task files."""
|
2018-12-19 10:19:07 +00:00
|
|
|
|
2019-04-04 14:06:18 +00:00
|
|
|
import re
|
2018-12-19 10:19:07 +00:00
|
|
|
from collections import defaultdict
|
|
|
|
|
2019-04-10 14:09:37 +00:00
|
|
|
from ansiblelater.command.candidates import Error
|
|
|
|
from ansiblelater.command.candidates import Result
|
2020-04-05 11:57:36 +00:00
|
|
|
from ansiblelater.utils.rulehelper import get_normalized_tasks
|
2018-12-19 10:19:07 +00:00
|
|
|
from ansiblelater.utils.rulehelper import get_normalized_yaml
|
|
|
|
|
|
|
|
|
|
|
|
def check_line_between_tasks(candidate, settings):
|
|
|
|
options = defaultdict(dict)
|
|
|
|
options.update(remove_empty=False)
|
|
|
|
options.update(remove_markers=False)
|
|
|
|
|
2020-04-05 11:57:36 +00:00
|
|
|
lines, line_errors = get_normalized_yaml(candidate, settings, options)
|
|
|
|
tasks, task_errors = get_normalized_tasks(candidate, settings)
|
2018-12-19 10:19:07 +00:00
|
|
|
description = "missing task separation (required: 1 empty line)"
|
|
|
|
|
2020-04-05 11:57:36 +00:00
|
|
|
task_regex = re.compile(r"-\sname:(.*)")
|
2018-12-19 10:19:07 +00:00
|
|
|
prevline = "#file_start_marker"
|
|
|
|
|
|
|
|
allowed_prevline = ["---", "tasks:", "pre_tasks:", "post_tasks:", "block:"]
|
|
|
|
|
2020-04-05 11:57:36 +00:00
|
|
|
errors = task_errors + line_errors
|
2018-12-19 10:19:07 +00:00
|
|
|
if not errors:
|
2019-01-28 10:04:44 +00:00
|
|
|
for i, line in lines:
|
2018-12-19 10:19:07 +00:00
|
|
|
match = task_regex.search(line)
|
|
|
|
if match and prevline:
|
2020-04-05 11:57:36 +00:00
|
|
|
name = match.group(1).strip()
|
|
|
|
|
|
|
|
if not any(task.get("name") == name for task in tasks):
|
|
|
|
continue
|
|
|
|
|
2018-12-19 10:19:07 +00:00
|
|
|
if not any(item in prevline for item in allowed_prevline):
|
2019-01-28 10:04:44 +00:00
|
|
|
errors.append(Error(i, description))
|
2020-04-05 11:57:36 +00:00
|
|
|
|
2018-12-19 10:19:07 +00:00
|
|
|
prevline = line.strip()
|
|
|
|
|
|
|
|
return Result(candidate.path, errors)
|