ansible-later/ansiblelater/utils/rulehelper.py

141 lines
4.5 KiB
Python
Raw Normal View History

2019-04-10 14:50:48 +02:00
"""Abstracted methods to simplify role writeup."""
2018-12-19 11:19:07 +01:00
import codecs
from collections import defaultdict
import yaml
2018-12-19 11:19:07 +01:00
from yamllint import linter
from yamllint.config import YamlLintConfig
2019-04-05 22:05:06 +02:00
from ansiblelater.command.candidates import Error
from ansiblelater.exceptions import LaterAnsibleError
from ansiblelater.exceptions import LaterError
from .yamlhelper import action_tasks
from .yamlhelper import normalize_task
from .yamlhelper import normalized_yaml
from .yamlhelper import parse_yaml_linenumbers
2018-12-19 11:19:07 +01:00
def get_tasks(candidate, settings):
errors = []
try:
with codecs.open(candidate.path, mode="rb", encoding="utf-8") as f:
2018-12-19 11:19:07 +01:00
yamllines = parse_yaml_linenumbers(f, candidate.path)
except LaterError as ex:
e = ex.original
2020-04-11 16:20:41 +02:00
errors.append(Error(e.problem_mark.line + 1, "syntax error: {msg}".format(msg=e.problem)))
2018-12-19 11:19:07 +01:00
except LaterAnsibleError as e:
2020-04-11 16:20:41 +02:00
errors.append(Error(e.line, "syntax error: {msg}".format(e.message)))
2018-12-19 11:19:07 +01:00
return yamllines, errors
def get_action_tasks(candidate, settings):
tasks = []
errors = []
try:
with codecs.open(candidate.path, mode="rb", encoding="utf-8") as f:
2018-12-19 11:19:07 +01:00
yamllines = parse_yaml_linenumbers(f, candidate.path)
if yamllines:
tasks = action_tasks(yamllines, candidate)
except LaterError as ex:
e = ex.original
2020-04-11 16:20:41 +02:00
errors.append(Error(e.problem_mark.line + 1, "syntax error: {msg}".format(msg=e.problem)))
2018-12-19 11:19:07 +01:00
except LaterAnsibleError as e:
2020-04-11 16:20:41 +02:00
errors.append(Error(e.line, "syntax error: {}".format(e.message)))
2018-12-19 11:19:07 +01:00
return tasks, errors
def get_normalized_task(task, candidate, settings):
normalized = None
errors = []
try:
2019-04-03 17:42:46 +02:00
normalized = normalize_task(task, candidate.path, settings["ansible"]["custom_modules"])
2018-12-19 11:19:07 +01:00
except LaterError as ex:
e = ex.original
2020-04-11 16:20:41 +02:00
errors.append(Error(e.problem_mark.line + 1, "syntax error: {msg}".format(msg=e.problem)))
2018-12-19 11:19:07 +01:00
except LaterAnsibleError as e:
2020-04-11 16:20:41 +02:00
errors.append(Error(e.line, "syntax error: {msg}".format(msg=e.message)))
2018-12-19 11:19:07 +01:00
return normalized, errors
def get_normalized_tasks(candidate, settings):
normalized = []
errors = []
try:
with codecs.open(candidate.path, mode="rb", encoding="utf-8") as f:
2018-12-19 11:19:07 +01:00
yamllines = parse_yaml_linenumbers(f, candidate.path)
if yamllines:
tasks = action_tasks(yamllines, candidate)
for task in tasks:
# An empty `tags` block causes `None` to be returned if
# the `or []` is not present - `task.get("tags", [])`
2018-12-19 11:19:07 +01:00
# does not suffice.
if "skip_ansible_lint" in (task.get("tags") or []):
2018-12-19 11:19:07 +01:00
# No need to normalize_task if we are skipping it.
continue
normalized.append(
2020-04-05 14:33:43 +02:00
normalize_task(task, candidate.path, settings["ansible"]["custom_modules"])
)
2018-12-19 11:19:07 +01:00
except LaterError as ex:
e = ex.original
2020-04-11 16:20:41 +02:00
errors.append(Error(e.problem_mark.line + 1, "syntax error: {msg}".format(msg=e.problem)))
2018-12-19 11:19:07 +01:00
except LaterAnsibleError as e:
2020-04-11 16:20:41 +02:00
errors.append(Error(e.line, "syntax error: {msg}".format(msg=e.message)))
2018-12-19 11:19:07 +01:00
return normalized, errors
def get_normalized_yaml(candidate, settings, options=None):
errors = []
if not options:
options = defaultdict(dict)
options.update(remove_empty=True)
options.update(remove_markers=True)
try:
yamllines = normalized_yaml(candidate.path, options)
except LaterError as ex:
e = ex.original
2020-04-11 16:20:41 +02:00
errors.append(Error(e.problem_mark.line + 1, "syntax error: {msg}".format(msg=e.problem)))
2018-12-19 11:19:07 +01:00
except LaterAnsibleError as e:
2020-04-11 16:20:41 +02:00
errors.append(Error(e.line, "syntax error: {msg}".format(msg=e.message)))
2018-12-19 11:19:07 +01:00
return yamllines, errors
def get_raw_yaml(candidate, settings):
content = None
errors = []
try:
with codecs.open(candidate.path, mode="rb", encoding="utf-8") as f:
2018-12-19 11:19:07 +01:00
content = yaml.safe_load(f)
except LaterError as ex:
e = ex.original
2020-04-11 16:20:41 +02:00
errors.append(Error(e.problem_mark.line + 1, "syntax error: {msg}".format(msg=e.problem)))
2018-12-19 11:19:07 +01:00
return content, errors
def run_yamllint(path, options="extends: default"):
2018-12-19 11:19:07 +01:00
errors = []
try:
with codecs.open(path, mode="rb", encoding="utf-8") as f:
2018-12-19 11:19:07 +01:00
for problem in linter.run(f, YamlLintConfig(options)):
errors.append(Error(problem.line, problem.desc))
except LaterError as ex:
e = ex.original
2020-04-11 16:20:41 +02:00
errors.append(Error(e.problem_mark.line + 1, "syntax error: {msg}".format(msg=e.problem)))
2018-12-19 11:19:07 +01:00
return errors