Skip to content
This repository has been archived by the owner on Nov 27, 2018. It is now read-only.

Commit

Permalink
Add a Job method to filter changes coming from a PBChangeSource
Browse files Browse the repository at this point in the history
  • Loading branch information
lv-develer committed Jul 10, 2014
1 parent 2b0d0d5 commit a45a678
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
16 changes: 14 additions & 2 deletions positronic/brain/job/freestyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
from random import randrange

from buildbot.changes.filter import ChangeFilter
from buildbot.changes.svnpoller import SVNPoller
from buildbot.process.properties import Interpolate
from buildbot.changes.svnpoller import SVNPoller
from buildbot.schedulers.basic import SingleBranchScheduler
from buildbot.schedulers.triggerable import Triggerable
from buildbot.status.mail import MailNotifier
Expand All @@ -31,7 +31,7 @@
from positronic.brain.config import BrainConfig, BuildmasterConfig
from positronic.brain.job import Job
from positronic.brain.mail import html_message_formatter
from positronic.brain.utils import has_svn_change_source, hashify, scheduler_name
from positronic.brain.utils import has_svn_change_source, hashify, scheduler_name, is_dir_in_change


class FreestyleJob(Job):
Expand Down Expand Up @@ -95,3 +95,15 @@ def watch(self, url, branch):
treeStableTimer=60,
builderNames=[self.name],
change_filter=ChangeFilter(repository=repo_url)))

def watch_paths(self, paths):
"""
Start the build if an incoming change-set contains files that begin with the given
directory names.
"""
BuildmasterConfig['schedulers'].append(SingleBranchScheduler(
builderNames=[self.name],
change_filter=ChangeFilter(filter_fn=is_dir_in_change),
name=scheduler_name(self, 'filter-' + hashify(''.join(paths))),
treeStableTimer=60))
31 changes: 29 additions & 2 deletions positronic/brain/test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,38 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

from mock import patch

from buildbot.changes.svnpoller import SVNPoller

from positronic.brain.config import BuildmasterConfig
from positronic.brain.utils import get_default_email_address, has_svn_change_source, name, \
scheduler_name
from positronic.brain.utils import append_dir_sep, get_default_email_address, \
has_svn_change_source, name, scheduler_name, is_dir_in_change


def test_append_dir_sep():
assert append_dir_sep('test') == 'test/'
assert append_dir_sep('test/') == 'test/'
assert append_dir_sep(['test']) == ['test/']
assert append_dir_sep(['test/']) == ['test/']
assert append_dir_sep(['test', 'test']) == ['test/', 'test/']
assert append_dir_sep(['test/', 'test']) == ['test/', 'test/']
assert append_dir_sep(['test/', 'test/']) == ['test/', 'test/']


@patch('buildbot.changes.changes.Change')
def test_is_dir_in_change(change):
change.files = ['test_dir1/file1.py', 'test_dir2/file2.py']

assert not is_dir_in_change(change, ['test_bogus'])
assert not is_dir_in_change(change, ['test'])
assert not is_dir_in_change(change, ['test_dir'])
assert not is_dir_in_change(change, ['file1'])
assert not is_dir_in_change(change, ['file1.py'])
assert is_dir_in_change(change, ['test_dir1'])
assert is_dir_in_change(change, ['test_dir2'])
assert is_dir_in_change(change, ['test_dir1', 'test_dir2'])
assert is_dir_in_change(change, ['test_dir2', 'test_dir1'])


def test_has_svn_change_source():
Expand Down
25 changes: 25 additions & 0 deletions positronic/brain/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,31 @@
from positronic.brain.config import BuildmasterConfig


def is_dir_in_change(change, directories):
"""
:param change: A list of path strings
:param directories: A list of path strings
:type change: buildbot.changes.changes.Change
:return: True if a file in `change` starts with a path from `directories`
"""
for change_dirname in append_dir_sep([os.path.dirname(f) for f in change.files]):
for directory in append_dir_sep(directories):
if change_dirname.startswith(directory):
return True
else:
return False


def append_dir_sep(item_or_items):
sep = os.path.sep

if type(item_or_items) is list:
return [i if i.endswith(sep) else i + sep for i in item_or_items]
else:
return item_or_items if item_or_items.endswith(sep) else item_or_items + sep


def abspath(p):
return os.path.abspath(os.path.expanduser(p))

Expand Down

0 comments on commit a45a678

Please sign in to comment.