Skip to content

Commit

Permalink
Merge 2ed0657 into 6f8cdaa
Browse files Browse the repository at this point in the history
  • Loading branch information
leonkozlowski committed Feb 21, 2020
2 parents 6f8cdaa + 2ed0657 commit 6f0dfdc
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ coverage.xml
*.py,cover
.hypothesis/
.pytest_cache/
.coveralls.yml

# Translations
*.mo
Expand Down
19 changes: 8 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Usage: jpl [OPTIONS]

CLI Args:
-v --verbose: Run `jpl` with verbosity.
-s --skip: Skip "PASSED" rules in JiggyPlaybookLint Report.
-s --show: Show "PASSED" rules in JiggyPlaybookLint Report.
-p --playbook: Location of JiggyPlaybook to lint.

Returns:
Expand All @@ -49,7 +49,7 @@ Usage: jpl [OPTIONS]

Options:
-v, --verbose Run `jpl` with verbosity.
-s, --skip Skip `PASSED` rules in jpl report
-s, --show Show `PASSED` rules in jpl report
-p, --playbook TEXT Filepath to Jiggy Playbook [required]
--help Show this message and exit.
```
Expand All @@ -67,22 +67,19 @@ $ jpl -v -p examples/jiggy-playbook.yml

```

Via python client
Via python client with `.validate()`
```python
import jpl

>>> client = jpl.JiggyPlaybookLint(path="path/to/playbook")
>>> client.run()
>>> result = client.validate()
```

```bash
__ ______ __
/\ \ /\ == \ /\ \
_\_\ \ \ \ _-/ \ \ \____
/\_____\ \ \_\ \ \_____\
\/_____/iggy \/_/laybook \/_____/inter
The `validate()` method will return a `tuple`

[F01] FunctionSourceExists - get-weekday: FAILED Declared path to function: `examples.utils.dates.GetWeekdayTask` does not exist.
The tuple denotes the validity of the playbook and the failing rules as `JiggyRule` objects
```bash
(False, [<JiggyRule: F01>, <JiggyRule: F01>, <JiggyRule: F01>, <JiggyRule: F01>, <JiggyRule: F01>, <JiggyRule: F01>])
```

## Overview
Expand Down
49 changes: 34 additions & 15 deletions jpl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,55 @@
class JiggyPlaybookLint(object): # pragma no cover
"""Runner for jpl."""

def __init__(self, path: str, skip=None):
def __init__(self, path: str, allow_warning=False, show=None):
self.playbook = self._read(path)
self.skip = skip
self.aw = allow_warning
self.show = show
self.exe = ["PASSED"]

def run(self):
"""Runner for JiggyPlaybookLinter."""
pbe = PlayBookExists()
exists = pbe.run(self.playbook)
if exists == "FAILED":
return [(exists, pbe, None)]
playbook_exists = PlayBookExists()
playbook_exists.run(self.playbook)
if playbook_exists.mark == "FAILED":
return [playbook_exists]

linted = []
jiggy_response = []
for rule in rules:
init_rule = rule()
init_rule.run(playbook=self.playbook)

_status = init_rule.run(playbook=self.playbook)

linted.append((_status, init_rule, None))
jiggy_response.append(init_rule)

for task in self.playbook.get("pipeline", {}).get("tasks"):
for rule in task_rules:
init_rule = rule()
init_rule.run(playbook=task)

jiggy_response.append(init_rule)

if self.show:
jiggy_response = [rule for rule in jiggy_response if rule.mark != "PASSED"]

return jiggy_response

def validate(self) -> tuple:
"""
Executor for in runtime JiggyPlaybookLint validation and response structure
Returns:
(tuple) - (is_valid, failures)
is_valid = bool
failures = list [JiggyRule] with mark FAILED
"""
response = self.run()

_status = init_rule.run(playbook=task)
linted.append((_status, init_rule, task.get("name")))
if self.aw:
self.exe = self.exe.append("WARNING")

if self.skip:
linted = [rule for rule in linted if rule[0] != "PASSED"]
prevent = list(filter(lambda rule: rule.mark not in self.exe, response))

return linted
return (prevent is None, prevent)

@staticmethod
def _read(path: str): # pragma no cover
Expand Down
22 changes: 11 additions & 11 deletions jpl/cli/main_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@
)
@click.option(
"-s",
"--skip",
"--show",
required=False,
is_flag=True,
default=True,
help="Skip `PASSED` rules in jpl report",
help="Show `PASSED` rules in jpl report",
)
@click.option("-p", "--playbook", required=True, help="Filepath to Jiggy Playbook")
def lint(verbose, skip, playbook):
def lint(verbose, show, playbook):
"""Click CLI entrypoint to run JiggyPlaybookLint.
CLI Args:
-v --verbose: Run `jpl` with verbosity.
-s --skip: Skip "PASSED" rules in JiggyPlaybookLint Report.
-s --show: Show "PASSED" rules in JiggyPlaybookLint Report.
-p --playbook: Location of JiggyPlaybook to lint.
Expand All @@ -43,7 +43,7 @@ def lint(verbose, skip, playbook):
\/_____/iggy \/_/laybook \/_____/inter
"""
)
linted = jpl.JiggyPlaybookLint(path=playbook, skip=skip).run()
linted = jpl.JiggyPlaybookLint(path=playbook, show=show).run()

generate_jpl_report(linted=linted, verbose=verbose)

Expand All @@ -59,26 +59,26 @@ def generate_jpl_report(linted: list, verbose=None): # pragma no cover
Returns:
click.echo - `with style`
"""
for mark, rule, task_name in linted:
for rule in linted:
rule_meta = "[{}] {}:".format(rule.rule, rule.__class__.__name__)
if task_name:
if rule.task:
rule_meta = "[{}] {} - {}:".format(
rule.rule, rule.__class__.__name__, task_name
rule.rule, rule.__class__.__name__, rule.task
)

if verbose == 1:
click.echo(
"{:<50}{:<20} {}".format(
rule_meta,
click.style(mark, fg=MARK_TO_COLOR.get(mark)),
click.style(rule.mark, fg=MARK_TO_COLOR.get(rule.mark)),
rule.message,
)
)
elif verbose > 1:
click.echo(
"{:<50}{:<20} {} \nPriority: `{}` - Description: `{}`\n".format(
rule_meta,
click.style(mark, fg=MARK_TO_COLOR.get(mark)),
click.style(rule.mark, fg=MARK_TO_COLOR.get(rule.mark)),
rule.message,
rule.priority,
rule.description,
Expand All @@ -87,7 +87,7 @@ def generate_jpl_report(linted: list, verbose=None): # pragma no cover
else:
click.echo(
"{:<50}{:<50}".format(
rule_meta, click.style(mark, fg=MARK_TO_COLOR.get(mark))
rule_meta, click.style(rule.mark, fg=MARK_TO_COLOR.get(rule.mark))
)
)

Expand Down
4 changes: 2 additions & 2 deletions jpl/lint.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import jpl

if __name__ == "__main__":
client = jpl.JiggyPlaybookLint(path="examples/jiggy-playbook-flawed.yml")
result = client.run()
client = jpl.JiggyPlaybookLint(path="examples/jiggy-playbook.yml")
result = client.validate()
5 changes: 5 additions & 0 deletions jpl/rules/pipeline/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class PipelineHasRunner(JiggyRule):
commands = ["command", "core"]
mark = "PASSED"
message = ""
task = None

def validate_runner(self, playbook: dict) -> str:
pipeline = playbook.get("pipeline", {})
Expand All @@ -54,6 +55,7 @@ class RunnerIsSupported(JiggyRule):
commands = ["command", "core"]
mark = "PASSED"
message = ""
task = None

def validate_runner_supported(self, playbook: dict) -> str:
pipeline = playbook.get("pipeline", {})
Expand All @@ -78,6 +80,7 @@ class SecretsHasMetadata(JiggyRule):
commands = ["command", "core"]
mark = "PASSED"
message = ""
task = None

def validate_secrets(self, playbook: dict) -> str:
"""Check `secrets` metadata if exists for mandatory fields."""
Expand Down Expand Up @@ -110,6 +113,7 @@ class SecretsLocationExists(JiggyRule):
commands = ["command", "core"]
mark = "PASSED"
message = ""
task = None

def validate_secrets_exists(self, playbook: dict) -> str:
"""Check `secrets.location` exists."""
Expand Down Expand Up @@ -140,6 +144,7 @@ class PipelineHasTasks(JiggyRule):
commands = ["command", "core"]
mark = "PASSED"
message = ""
task = None

def validate_pipeline_has_tasks(self, playbook: dict) -> str:
"""Check `JiggyPlaybook.pipeline.tasks` exists."""
Expand Down
5 changes: 5 additions & 0 deletions jpl/rules/playbook/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class PlayBookExists(JiggyRule):
commands = ["command", "core"]
mark = "PASSED"
message = ""
task = None

def exists(self, playbook: dict) -> str:
if not playbook:
Expand All @@ -32,6 +33,7 @@ class PlaybookHasName(JiggyRule):
commands = ["command", "core"]
mark = "PASSED"
message = ""
task = None

def has_name(self, playbook: dict) -> str:
if not playbook.get("name", "").strip():
Expand All @@ -53,6 +55,7 @@ class PlaybookHasAuthor(JiggyRule):
commands = ["command", "core"]
mark = "PASSED"
message = ""
task = None

def has_author(self, playbook: dict) -> str:
if not playbook.get("author"):
Expand All @@ -74,6 +77,7 @@ class PlaybookHasDescription(JiggyRule):
commands = ["command", "core"]
mark = "PASSED"
message = ""
task = None

def has_desc(self, playbook: dict) -> str:
if not playbook.get("description"):
Expand All @@ -95,6 +99,7 @@ class PlaybookHasVersion(JiggyRule):
commands = ["command", "core"]
mark = "PASSED"
message = ""
task = None

def has_version(self, playbook: dict) -> str:
if not playbook.get("version"):
Expand Down
8 changes: 8 additions & 0 deletions jpl/rules/task/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ class FunctionSourceExists(JiggyRule):
commands = ["command", "core"]
mark = "PASSED"
message = ""
task = None

def function_exists(self, task: dict) -> str:
self.task = task.get("name")
this_func = task.get("function", {})
function_loc = this_func.get("source")
if function_loc:
Expand Down Expand Up @@ -46,9 +48,11 @@ class ParamHasType(JiggyRule):
commands = ["command", "core"]
mark = "PASSED"
message = ""
task = None

def param_has_attr_type(self, task: dict) -> str:
"""Validate functino.params has `type` if exists."""
self.task = task.get("name")
this_func = task.get("function", {})
params = this_func.get("params", [])
if params:
Expand Down Expand Up @@ -76,9 +80,11 @@ class ParamHasValue(JiggyRule):
commands = ["command", "core"]
mark = "PASSED"
message = ""
task = None

def param_has_attr_value(self, task: dict) -> str:
"""Validate functino.params has `value` if exists."""
self.task = task.get("name")
this_func = task.get("function", {})
params = this_func.get("params", [])
if params:
Expand Down Expand Up @@ -106,9 +112,11 @@ class FunctionOutputIsSingular(JiggyRule):
commands = ["command", "core"]
mark = "PASSED"
message = ""
task = None

def validate_func_has_value(self, task: dict) -> str:
"""Validate functino.params has type and value if exists."""
self.task = task.get("name")
this_func = task.get("function", {})
output = this_func.get("output")
if output:
Expand Down
6 changes: 6 additions & 0 deletions jpl/rules/task/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ class TaskHasName(JiggyRule):
commands = ["command", "core"]
mark = "PASSED"
message = ""
task = None

def has_name(self, task):
self.task = task.get("name")
if not task.get("name"):
self.mark = "FAILED"
self.message = "Task `name` has not been declared."
Expand All @@ -32,8 +34,10 @@ class TaskHasDescription(JiggyRule):
commands = ["command", "core"]
mark = "PASSED"
message = ""
task = None

def has_desc(self, task):
self.task = task.get("name")
if not task.get("description"):
self.mark = "WARNING"
self.message = "Task `description` has not been declared."
Expand All @@ -53,8 +57,10 @@ class TaskHasFunction(JiggyRule):
commands = ["command", "core"]
mark = "PASSED"
message = ""
task = None

def has_function(self, task):
self.task = task.get("name")
if not task.get("function"):
self.mark = "FAILED"
self.message = "Task `function` has not been declared."
Expand Down
1 change: 1 addition & 0 deletions jpl/utils/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


def file_exists(filepath: str):
"""Validate the existence of a filepath."""
return os.path.exists(filepath)


Expand Down

0 comments on commit 6f0dfdc

Please sign in to comment.