Skip to content

Commit

Permalink
improved command line interface and added respective tests to what wa…
Browse files Browse the repository at this point in the history
…s needed (something_has_changed method)
  • Loading branch information
hltbra committed May 21, 2010
1 parent f51b5cb commit 5bdf909
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
26 changes: 19 additions & 7 deletions peon/peon.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
import os
import stat
import time
import optparse
from os.path import abspath, dirname, join


_checksum = 0
_pattern = None


class Urgency(object):
Expand Down Expand Up @@ -55,11 +57,13 @@ def checkSumRecursive(directory, pattern='*.py'):
return result


def something_has_changed(dir):
def something_has_changed(dir, pattern='*.py'):
global _checksum
if _checksum == 0:
_checksum = checkSumRecursive(dir)
new_checksum = checkSumRecursive(dir)
global _pattern
if _pattern != pattern:
_pattern = pattern
_checksum = checkSumRecursive(dir, _pattern)
new_checksum = checkSumRecursive(dir, _pattern)
if new_checksum != _checksum:
_checksum = new_checksum
return True
Expand All @@ -72,13 +76,21 @@ def main():
by default it looks for '*.py'.
If something has changes, run a given command or nosetests.
'''
val = 0
command = " ".join(sys.argv[1:]) or 'nosetests'
parser = optparse.OptionParser()
parser.add_option('-d', '--dir', default='.', dest='directory',
help='the directory peon will watch for changes')
parser.add_option('-p', '--pattern', default='*.py', dest='pattern',
help='the glob pattern to watch for changes. '\
'(default is "*.py)"')
options, args = parser.parse_args()
directory = options.directory
pattern = options.pattern
command = ' '.join(args) or 'nosetests'
is_build_broken = False

try:
while True:
if something_has_changed('.'):
if something_has_changed(directory, pattern):
os.system('reset')
status = os.system(command)
if status != 0:
Expand Down
9 changes: 8 additions & 1 deletion tests/something_has_changed_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@ def setUp(self):

def should_be_true_if_there_are_changes_and_false_if_not(self):
before = something_has_changed(TEST_OUTPUT_DIR)
env.writefile('lol.py', 'lol')
env.writefile('dummy.py', 'dummy')
after = something_has_changed(TEST_OUTPUT_DIR)
after |should_be| True
before |should_be| False

def should_be_possible_to_pass_patterns(self):
before = something_has_changed(TEST_OUTPUT_DIR, pattern='*.ext')
env.writefile('myfile.ext', 'my text')
after = something_has_changed(TEST_OUTPUT_DIR, pattern='*.ext')
after |should_be| True
before |should_be| False

0 comments on commit 5bdf909

Please sign in to comment.