Skip to content

Commit

Permalink
Added ignore functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
CalumJEadie committed Sep 18, 2012
1 parent 71d0970 commit 1c4f317
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 15 deletions.
13 changes: 10 additions & 3 deletions README.md
Expand Up @@ -13,8 +13,8 @@ Calum J. Eadie (www.calumjeadie.com)
The build script is written in pure Python and microbuild takes care of managing
any dependancies between tasks and generating a command line interface.

Tasks are just regular Python functions marked with the `@task` decorator. Dependancies
are specified with `@task` too.
Tasks are just regular Python functions marked with the `@task()` decorator. Dependancies
are specified with `@task()` too. Tasks can be ignored with the `@ignore` decorator.

After defining all tasks `build(sys.modules[__name__],sys.argv[1:])` is called to
run the build.
Expand All @@ -33,6 +33,7 @@ run the build.
"""Generate HTML."""
print "Generating HTML..."

@ignore
@task(clean)
def images():
"""Prepare images."""
Expand Down Expand Up @@ -67,10 +68,16 @@ are extracted from function docstrings.
Dependancies between tasks are taken care of too.

$ ./example.py android
[ example.py - Starting task "clean" ]
Cleaning build directory...
[ example.py - Completed task "clean" ]
[ example.py - Starting task "html" ]
Generating HTML...
Preparing images...
[ example.py - Completed task "html" ]
[ example.py - Ignoring task "images" ]
[ example.py - Starting task "android" ]
Packaging android app...
[ example.py - Completed task "android" ]

## Installation

Expand Down
3 changes: 2 additions & 1 deletion example.py
@@ -1,7 +1,7 @@
#!/usr/bin/python

import sys
from microbuild.microbuild import task,build
from microbuild.microbuild import task,ignore,build

@task()
def clean():
Expand All @@ -13,6 +13,7 @@ def html():
"""Generate HTML."""
print "Generating HTML..."

@ignore
@task(clean)
def images():
"""Prepare images."""
Expand Down
41 changes: 30 additions & 11 deletions microbuild/microbuild.py
Expand Up @@ -80,18 +80,24 @@ def _run(module,logger,task,completed_tasks):
# Perform current task, if need to.
if task not in completed_tasks:

logger.info("Starting task \"%s\"" % task.__name__)

try:
# Run task.
task()
except:
logger.critical("Error in task \"%s\"" % task.__name__)
traceback.print_exc()
logger.critical("Build aborted")
sys.exit()
if task.is_ignorable():

logger.info("Completed task \"%s\"" % task.__name__)
logger.info("Ignoring task \"%s\"" % task.__name__)

else:

logger.info("Starting task \"%s\"" % task.__name__)

try:
# Run task.
task()
except:
logger.critical("Error in task \"%s\"" % task.__name__)
traceback.print_exc()
logger.critical("Build aborted")
sys.exit()

logger.info("Completed task \"%s\"" % task.__name__)

completed_tasks.add(task)

Expand Down Expand Up @@ -185,6 +191,13 @@ def __call__(self,func):

# Abbreviate for convenience.
task = _TaskDecorator

def ignore(obj):
"""
Decorator to specify that a task should be ignored.
"""
obj.ignorable = True
return obj

class Task(object):

Expand All @@ -207,6 +220,12 @@ def is_task(cls,obj):
Returns true is an object is a build task.
"""
return isinstance(obj,cls)

def is_ignorable(self):
"""
Returns true if task can be ignored.
"""
return ( hasattr(self,'ignorable') and self.ignorable == True ) or ( hasattr(self.func,'ignorable') and self.func.ignorable == True )

def _get_tasks(module):
"""
Expand Down
30 changes: 30 additions & 0 deletions microbuild/tests/build_scripts/ignore_after.py
@@ -0,0 +1,30 @@
import sys
from ... import microbuild

@microbuild.task()
def clean():
"""Clean build directory."""

print "clean"

@microbuild.task(clean)
def html():
"""Generate HTML."""

print "html"

@microbuild.task(clean)
@microbuild.ignore
def images():
"""Prepare images. Should be ignored."""

raise Exception("This task should have been ignored.")

@microbuild.task(clean,html,images)
def android():
"""Package Android app."""

print "android"

if __name__ == "__main__":
microbuild.build(sys.modules[__name__],sys.argv[1:])
30 changes: 30 additions & 0 deletions microbuild/tests/build_scripts/ignore_before.py
@@ -0,0 +1,30 @@
import sys
from ... import microbuild

@microbuild.task()
def clean():
"""Clean build directory."""

print "clean"

@microbuild.task(clean)
def html():
"""Generate HTML."""

print "html"

@microbuild.ignore
@microbuild.task(clean)
def images():
"""Prepare images. Should be ignored."""

raise Exception("This task should have been ignored.")

@microbuild.task(clean,html,images)
def android():
"""Package Android app."""

print "android"

if __name__ == "__main__":
microbuild.build(sys.modules[__name__],sys.argv[1:])
17 changes: 17 additions & 0 deletions microbuild/tests/microbuild.py
Expand Up @@ -42,6 +42,23 @@ def test_3(self):
with self.assertRaises(microbuild.TaskDecorationException) as cm:
import build_scripts.annotation_misuse_3
print cm.exception

class TestIgnore(unittest.TestCase):

def test_ignore_before(self):
import build_scripts.ignore_before
microbuild.build(build_scripts.ignore_before,["android"])

def test_ignore_after(self):
import build_scripts.ignore_after
microbuild.build(build_scripts.ignore_after,["android"])

class TestRuntimeError(unittest.TestCase):

def test_no_exception_raised(self):
import build_scripts.runtime_error
with self.assertRaises(SystemExit):
microbuild.build(build_scripts.runtime_error,["android"])

if __name__ == "__main__":
unittest.main(verbosity=2)

0 comments on commit 1c4f317

Please sign in to comment.