-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
modifying how rules work a bit, adding WasChanged.
- Loading branch information
1 parent
e8ca736
commit 325dd87
Showing
7 changed files
with
134 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,4 @@ python: | |
- "2.7" | ||
- "3.3" | ||
install: "pip install -r requirements.txt" | ||
script: "uranium test" | ||
script: "uranium test" |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import functools | ||
from abc import ABCMeta, abstractmethod | ||
from ..experimental import experimental | ||
|
||
|
||
class RuleBase(object): | ||
""" | ||
an example of a rule. | ||
* func gets set during the initialization process. | ||
""" | ||
func = None | ||
|
||
__metaclass__ = ABCMeta | ||
|
||
@abstractmethod | ||
def before(self, build): | ||
pass | ||
|
||
@abstractmethod | ||
def after(self, build): | ||
pass | ||
|
||
|
||
@experimental | ||
def rule(rule_object): | ||
""" | ||
the rule decorator is used to add a rule function to the task. | ||
the rule function should accept a build object, and should return | ||
True in the case where a build is required. | ||
multiple rules can be added to a task. If any of the rules return false, | ||
the task will be re-executed. | ||
""" | ||
key = "_uranium_rules" | ||
|
||
def decorator(f): | ||
rule_object.func = f | ||
|
||
if hasattr(f, key): | ||
getattr(f, key).append(rule_object) | ||
return f | ||
|
||
rules = [rule_object] | ||
|
||
@functools.wraps(f) | ||
def func(build): | ||
if all((r.before(build) for r in rules)): | ||
return 0 | ||
return_value = f(build) | ||
for r in rules: | ||
r.after(build) | ||
return return_value | ||
|
||
setattr(func, key, rules) | ||
|
||
return func | ||
|
||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import os | ||
import time | ||
from . import RuleBase | ||
KEY = "_uranium.rules.was_changed" | ||
|
||
|
||
class WasChanged(RuleBase): | ||
|
||
def __init__(self, path): | ||
self._path = path | ||
|
||
def before(self, build): | ||
previous_timestamp = build.history.get(self.key) | ||
if not previous_timestamp: | ||
return False | ||
current_timestamp = self._get_timestamp(self.path) | ||
return current_timestamp > previous_timestamp | ||
|
||
def after(self, build): | ||
current_timestamp = self._get_timestamp(self.path) | ||
build.history[self.key] = current_timestamp | ||
|
||
@property | ||
def key(self): | ||
return "{}.{}.{}".format( | ||
KEY, self._path, self.func.__name__ | ||
) | ||
|
||
@classmethod | ||
def _get_timestamp(cls, path): | ||
""" return the timestamp as a UTC datetime object. """ | ||
if os.path.isdir(path): | ||
return cls._get_dir_timestamp(path) | ||
else: | ||
return os.path.getmtime(path) | ||
|
||
@classmethod | ||
def _get_dir_timestamp(cls, path): | ||
oldest_timestamp = time.time() | ||
for root, _, filename in os.walk(path): | ||
f_path = os.path.join(root, filename) | ||
ts = os.path.getmtime(f_path) | ||
if ts < oldest_timestamp: | ||
oldest_timestamp = ts | ||
return oldest_timestamp |