Skip to content

Commit

Permalink
#34: Reduce Code Complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Lehmann committed Dec 21, 2017
1 parent ecfb965 commit 309e14c
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 44 deletions.
2 changes: 1 addition & 1 deletion pylint.conf
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ max-parents=2
max-public-methods=10

# Maximum number of return / yield for function / method body
max-returns=4
max-returns=2

# Maximum number of statements in function / method body
max-statements=25
Expand Down
19 changes: 10 additions & 9 deletions spline/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ def validate_document(self, document):
self.logger.info("Schema validation for '%s' succeeded", self.definition)
return document

@staticmethod
def find_matrix(document):
"""Find matrix in document."""
return document['matrix'] if 'matrix' in document \
else document['matrix(ordered)'] if 'matrix(ordered)' in document \
else document['matrix(parallel)'] if 'matrix(parallel)' in document \
else None

def run(self):
"""Processing the pipeline."""
self.logger.info("Running with Python %s", sys.version.replace("\n", ""))
Expand All @@ -89,15 +97,8 @@ def run(self):
return

model = {} if 'model' not in document else document['model']
matrix = document['matrix'] if 'matrix' in document \
else document['matrix(ordered)'] if 'matrix(ordered)' in document \
else document['matrix(parallel)'] if 'matrix(parallel)' in document \
else None

hooks = Hooks()
if 'hooks' in document:
if 'cleanup' in document['hooks']:
hooks.cleanup = document['hooks']['cleanup']['script']
matrix = Application.find_matrix(document)
hooks = Hooks(document)

if matrix is None:
pipeline = Pipeline(document['pipeline'], model=model, tags=self.tag_list, hooks=hooks)
Expand Down
6 changes: 5 additions & 1 deletion spline/components/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
class Hooks(object):
"""Pipeline hooks."""

def __init__(self):
def __init__(self, document=None):
"""Initialize hooks."""
self.cleanup = ""

if document is not None and 'hooks' in document:
if 'cleanup' in document['hooks'] and 'script' in document['hooks']['cleanup']:
self.cleanup = document['hooks']['cleanup']['script']
29 changes: 12 additions & 17 deletions spline/components/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,7 @@ def prepare_shell_data(self, shells, key, entry):

def process(self, tasks):
"""Processing a group of tasks."""
self.logger.info("Processing group of tasks")
if self.parallel:
self.logger.info("Run tasks in parallel")

self.logger.info("Processing group of tasks (parallel=%s)", self.parallel)
self.pipeline.data.env_list[2] = {}

output = []
Expand All @@ -99,26 +96,23 @@ def process(self, tasks):
if key == "env":
result = Adapter(self.process_shells(shells))
output += result.output
if not result.success:
return {'success': False, 'output': output}
shells = []
if not result.success:
break

self.pipeline.data.env_list[2].update(entry)
self.logger.debug("Updating environment at level 2 with %s",
self.pipeline.data.env_list[2])
continue

if key in ["shell", "docker(container)"]:
elif key in ["shell", "docker(container)"]:
self.prepare_shell_data(shells, key, entry)
continue

result = Adapter(self.process_shells(shells))
output += result.output
if not result.success:
return {'success': False, 'output': output}
if result.success:
self.event.succeeded()

self.event.succeeded()
return {'success': True, 'output': output}
return {'success': result.success, 'output': output}

def process_shells_parallel(self, shells):
"""Processing a list of shells parallel."""
Expand Down Expand Up @@ -154,11 +148,12 @@ def process_shells_ordered(self, shells):

def process_shells(self, shells):
"""Processing a list of shells."""
result = {'success': True, 'output': []}
if self.parallel and len(shells) > 1:
return self.process_shells_parallel(shells)
if len(shells) > 0:
return self.process_shells_ordered(shells)
return {'success': True, 'output': []}
result = self.process_shells_parallel(shells)
elif len(shells) > 0:
result = self.process_shells_ordered(shells)
return result

def can_process_shell(self, entry):
""":return: True when shell can be executed."""
Expand Down
23 changes: 14 additions & 9 deletions spline/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ def __init__(self, pipeline, model=None, env=None, tags=None, hooks=None):
self.data.env_list[0].update([] if env is None else env)
self.logger = Logger.get_logger(__name__)

def cleanup(self):
"""Run cleanup script of pipeline when hook is configured."""
if self.data.hooks and len(self.data.hooks.cleanup) > 0:
env = self.data.env_list[0].copy()
env.update({'PIPELINE_RESULT': 'SUCCESS'})
env.update({'PIPELINE_SHELL_EXIT_CODE': '0'})
config = ShellConfig(script=self.data.hooks.cleanup, model=self.model, env=env)
cleanup_shell = Bash(config)
for line in cleanup_shell.process():
yield line

def run(self):
"""Processing the whole pipeline definition."""
output = []
Expand All @@ -78,15 +89,9 @@ def run(self):
if not result['success']:
return {'success': False, 'output': output}

if self.data.hooks and len(self.data.hooks.cleanup) > 0:
env = self.data.env_list[0].copy()
env.update({'PIPELINE_RESULT': 'SUCCESS'})
env.update({'PIPELINE_SHELL_EXIT_CODE': '0'})
config = ShellConfig(script=self.data.hooks.cleanup, model=self.model, env=env)
cleanup_shell = Bash(config)
for line in cleanup_shell.process():
output.append(line)
self.logger.info(" | %s", line)
for line in self.cleanup():
output.append(line)
self.logger.info(" | %s", line)

self.event.succeeded()
return {'success': True, 'output': output}
12 changes: 6 additions & 6 deletions spline/tools/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ def __init__(self, data):

def __getattr__(self, key):
"""Organizing walking of dictionaries via direct field access."""
value = None
if key in self.data:
value = self.data[key]
if isinstance(value, dict):
return Adapter(value)
return value
value = Adapter(value)
else:
value = getattr(self.data, key)
if callable(value):
return value
return None
attr = getattr(self.data, key)
if callable(attr):
value = attr
return value

def __str__(self):
"""string representation of the underlying object."""
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ commands = pep8 --max-line-length=110 --ignore=E731 spline tests
commands = pep257 --count spline tests

[tool-flake8]
commands = flake8 --max-line-length=110 --max-complexity=8 --ignore=E731 spline tests
commands = flake8 --max-line-length=110 --max-complexity=6 --ignore=E731 spline tests

[tool-pylint]
commands = pylint --rcfile={toxinidir}/pylint.conf spline tests
Expand Down

0 comments on commit 309e14c

Please sign in to comment.