Skip to content

Commit

Permalink
adding the 'task' decorator.
Browse files Browse the repository at this point in the history
  • Loading branch information
toumorokoshi committed Jan 12, 2016
1 parent 7f4ff21 commit 0d8713e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
Empty file added tests/tasks/__init__.py
Empty file.
Empty file added tests/tasks/test_tasks.py
Empty file.
19 changes: 12 additions & 7 deletions uranium/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .hooks import Hooks
from .history import History
from .packages import Packages
from .tasks import Tasks
from .environment_variables import EnvironmentVariables
from .lib.script_runner import build_script, get_public_functions
from .lib.asserts import get_assert_function
Expand Down Expand Up @@ -40,6 +41,7 @@ def __init__(self, root, config=None, with_sandbox=True):
self._executables = Executables(root)
self._hooks = Hooks()
self._packages = Packages()
self._tasks = Tasks()
self._envvars = EnvironmentVariables()
self._options = None
self._history = History(
Expand Down Expand Up @@ -79,6 +81,9 @@ def packages(self):
def root(self):
return self._root

def task(self, f):
self._tasks.add(f)

def run(self, options):
if not self._sandbox:
return self._run(options)
Expand Down Expand Up @@ -109,23 +114,23 @@ def _run(self, options):
self._options = None
return code

def _run_script(self, path, directive, override_func=None):
def _run_script(self, path, task_name, override_func=None):
"""
override_func: if this is not None, the _run_script will
execute this function (passing in the script object) instead
of executing the directive.
of executing the task_name.
"""
script = build_script(path, {"build": self})

if override_func:
return override_func(script)

if directive not in script:
raise ScriptException("{0} does not have a {1} function. available public directives: \n{2}".format(
path, directive, _get_formatted_public_directives(script)
if task_name not in script:
raise ScriptException("{0} does not have a {1} function. available public task_names: \n{2}".format(
path, task_name, _get_formatted_public_tasks(script)
))
self.hooks.run("initialize", self)
output = script[directive](build=self)
output = self._tasks.run(build, task_name)
self.hooks.run("finalize", self)
return output

Expand All @@ -140,7 +145,7 @@ def _finalize(self):
self.history.save()


def _get_formatted_public_directives(script):
def _get_formatted_public_tasks(script):
public_directives = get_public_functions(script)

def fmt(func):
Expand Down
7 changes: 7 additions & 0 deletions uranium/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Tasks(dict):

def add(self, f):
self[f.__name__] = f

def run(self, build, name):
return self[name](build)

0 comments on commit 0d8713e

Please sign in to comment.