Skip to content

Commit

Permalink
adding the once rule to uranium.
Browse files Browse the repository at this point in the history
  • Loading branch information
toumorokoshi committed Feb 29, 2016
1 parent 3ad34b4 commit 744f657
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
]

setup(name='uranium',
version='0.2.29',
version='0.2.31b',
description='a build system for python',
long_description=open('README.rst').read(),
author='Yusuke Tsutsumi',
Expand Down
18 changes: 18 additions & 0 deletions tests/rules/test_once.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from uranium.rules import rule, Once


def test_only_runs_once(tmpdir, build):

g = []

tmpdir.join("foo.txt").write("foo")

@build.task
@rule(Once())
def main(build):
g.append("ran")

build.run_task("main")
assert "ran" in g
build.run_task("main")
assert len(g) == 1
3 changes: 2 additions & 1 deletion uranium/rules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from ..experimental import experimental
from .base import RuleBase
from .was_changed import WasChanged
from .once import Once

__all__ = ["RuleBase", "WasChanged", "rule"]
__all__ = ["Once", "RuleBase", "WasChanged", "rule"]


@experimental
Expand Down
31 changes: 31 additions & 0 deletions uranium/rules/once.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from .base import RuleBase
KEY = "_uranium.rules.once"


class Once(RuleBase):
"""
Once is a rule that activates if the task has never run.
It will not run again, unless the uranium history is cleaned.
.. code:: python
import subprocess
from uranium import rule
from uranium.rules import WasChanged
# only run tests if the code changed.
@rule(Once())
def test(build):
build.packages.install("pytest")
return subprocess.call(["py.test", build.root])
"""

def before(self, build):
return build.history.get(self.key)

def after(self, build):
build.history[self.key] = True

@property
def key(self):
return "{0}.{1}".format(KEY, self.func.__name__)

0 comments on commit 744f657

Please sign in to comment.