Skip to content

Commit

Permalink
moa unset -r implemented + a general framework for recursive execucti…
Browse files Browse the repository at this point in the history
…on in moa.utils.moaRecursiveWalk - in preparation for [#4]
  • Loading branch information
mfiers committed Apr 2, 2011
1 parent fe5c52e commit b3bf9a9
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 21 deletions.
3 changes: 1 addition & 2 deletions lib/python/moa/jobConf.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def __init__(self, job):

def save(self):
self.job.checkConfDir()
self.jobConf.save(self.jobConfFile, self.doNotSave)
self.localConf.save(self.jobConfFile, self.doNotSave)

def setInJobConf(self, key):
if self.jobConf.has_key(key):
Expand Down Expand Up @@ -139,7 +139,6 @@ def __getitem__(self, key):
def __delitem__(self, key):
del(self.localConf[key])


def __setitem__(self, key, value):
if key in self.job.template.parameters.keys():
pd = self.job.template.parameters[key]
Expand Down
84 changes: 67 additions & 17 deletions lib/python/moa/plugin/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Control job configuration
"""
import os

import moa.ui
import moa.utils
Expand All @@ -25,14 +26,17 @@ def defineCommands(data):
'usage' : 'moa set [KEY] [KEY=VALUE]',
'call' : configSet,
'needsJob' : True,
'log' : True
'log' : True,
'unittest' : TESTSET
}

data['commands']['unset'] = {
'desc' : 'Remove a variable',
'call' : configUnset,
'usage' : 'moa unset KEY',
'needsJob' : True,
'log' : True
'log' : True,
'unittest' : TESTUNSET
}

data['commands']['show'] = {
Expand Down Expand Up @@ -75,24 +79,40 @@ def configShow(data):
moa.ui.fprint("{{red}}%s\t%s{{reset}}" % (
p, job.conf[p]), f='jinja')

def _unsetCallback(wd, data):
"""
Does the actual unset..
"""
job = moa.job.Job(wd)
print "unsetting", " ".join(data.unset), "in", wd
for u in data.unset:
try:
del job.conf[u]
except KeyError:
pass
job.conf.save()

def configUnset(data):
"""
Remove a configured parameter from this job. In the parameter was
defined by the job template, it reverts back to the default
value. If it was an ad-hoc parameter, it is lost from the
configuration.
"""
options = data['options']

job = data['job']
for a in data['newargs']:
data.unset = []
for a in data.newargs:
if '=' in a:
l.error("Invalid argument to unset %s" % a)
moa.ui.exitError("Invalid argument to unset %s" % a)
else:
l.debug("Unsetting %s" % a)
del job.conf[a]
job.conf.save()
data.unset.append(a)
if not data.unset:
moa.ui.exitError("Nothing to unset")

moa.utils.moaRecursiveWalk(
data.wd, _unsetCallback, data, options.recursive)


def configSet(data):
"""
This command can be used in a number of ways::
Expand Down Expand Up @@ -121,14 +141,44 @@ def configSet(data):
else:
key,val = a.split('=',1)
job.conf[key] = val

job.conf.save()

TESTSCRIPT = """
moa new adhoc -t 'something'
moa set title='something else'
moa set undefvar='somewhat'
moa set adhoc_mode=par
moa show || exer moa show does not seem to work
moa show | grep -q 'title[[:space:]\+]something else' || exer title is not set properly

##### TESTSCRIPTS

TESTUNSET = """
moa simple -t test -- echo
moa show | grep -Pq 'title\ttest'
moa unset title
moa show | grep -Pqv 'title\ttest'
# recursive unset
moa set aaa=bbb
moa show | grep -Pq 'aaa\tbbb'
mkdir 10.sub
cd 10.sub
moa simple -t sub -- echo
moa set aaa=bbb
moa show | grep -Pq 'aaa\tbbb'
cd ..
moa unset -r aaa
moa show | grep -Pqv 'aaa\tbbb'
cd 10.sub
moa show | grep -Pqv 'aaa\tbbb'
"""

TESTSET = """
moa simple -t test -- echo
moa show | grep -Pq 'title\ttest'
moa show | grep -Pqv 'title\totherwise'
moa set title=otherwise
moa show | grep -Pqv 'title\ttest'
moa show | grep -Pq 'title\totherwise'
moa set dummy=bla
moa show | grep -Pq 'dummy\tbla'
#now try recursive loading of variables
mkdir 10.subdir
cd 10.subdir
moa simple -t subtest -- echo
moa show | grep -Pq 'dummy\tbla'
moa show | grep -Pqv 'title\ttest'
"""
4 changes: 2 additions & 2 deletions lib/python/moa/plugin/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,15 @@ def rawParameters(data):

TESTOUT = '''
moa simple -t "test" -- echo "something"
moa run
moa run >/dev/null 2>/dev/null
out=`moa out`
[[ "$out" =~ "something" ]] || (echo "Moa out failed" ; false)
'''

TESTERR = '''
moa simple -t "test" --np
moa set process='echo "something" >&2'
moa run
moa run >/dev/null 2>/dev/null
err=`moa err`
[[ "$err" =~ "something" ]] || (echo "Moa err failed" ; false)
'''
26 changes: 26 additions & 0 deletions lib/python/moa/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,32 @@
import moa.utils
import moa.logger as l


def moaRecursiveWalk(wd, callback, data, recursive=True):
"""
Function used in recursive moa commands -
this function takes the 'wd', walks through all subdirectories
(excluding those starting with a '.') and running the callback
function on that directory.
If recursive == False - just execute the callback & return
"""
if not recursive:
callback(wd, data)
return

for path, dirs, files in os.walk(wd):
if '.moa' in dirs:

#potentially a moa dir
#call the callback
callback(path, data)

#remove all '.' directories -
toRemove = [x for x in dirs if x[0] == '.']
[dirs.remove(t) for t in toRemove]

@contextlib.contextmanager
def flock(path, waitDelay=.1, maxWait=100):
"""
Expand Down

0 comments on commit b3bf9a9

Please sign in to comment.