Skip to content

Commit

Permalink
dra
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Fiers committed Dec 2, 2012
1 parent 24c4aed commit 19f634d
Showing 1 changed file with 92 additions and 134 deletions.
226 changes: 92 additions & 134 deletions moa/plugin/system/adhoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,40 @@

l = moa.logger.getLogger(__name__)

def _checkTitle(t):
def _checkTitle(j, argstitle):
"""
check if there is anything resembling a title in `t` - otherwise
ask the user for one
see if there is one in the job object j, else ask the user
"""
if t is None or str(t).strip() == "":
return moa.ui.askUser("Title: ")
rv = ""

if argstitle:
rv = t
else:
return t
default = ""
if j.conf.is_local('title') and j.conf.title:
default = j.conf.title
while rv == "":
rv = moa.ui.askUser("title", default)

moa.ui.message('Setting "title" to "%s"' % rv)
return rv

@moa.args.argument('-t', '--title', help='A title for this job')
@moa.args.forceable
@moa.args.command
def simple(job, args):
def _get_bash_history_file():
"""
Create a 'simple' adhoc job.
get the proper history file - a local one if it exists - otherwise
use bash_history
"""
hf = os.path.join('.moa', 'local_bash_history')

Simple meaning that no in or output files are tracked. Moa will
query you for a command to execute (the `process` parameter).
if os.path.exists(hf):
return hf
else:
return os.path.join(os.path.expanduser('~'), '.bash_history')

def _createAdhocJob(job, args, template, query=[]):
"""
One function for all adhoc type new job commands
"""

wd = job.wd
Expand All @@ -50,85 +64,70 @@ def simple(job, args):

moa.ui.exitError("Job already exists, use -f to override")

command = moa.ui.askUser('process:\n> ', '')
params = []
for qdata in query:
param, xtra_history = qdata[0], qdata[1]

params = [('process', command)]
v = moa.ui.askUser(param,
job.conf.get(param),
xtra_history)

params.append((param, v))

title = _checkTitle(job, args.title)

#make sure the correct hooks are called
sysConf.pluginHandler.run("preNew")

title = _checkTitle(args.title)

job = moa.job.newJob(
job, template='simple',
title=title,
parameters=params)
job = moa.job.newJob(job, template=template, title=title,
parameters=params)

#make sure the correct hooks are called
sysConf.pluginHandler.run("postNew")


def exclamateNoJob(job, args, command):
"""
Create a "simple" job & set the last command
to the 'process' parameter
@moa.args.argument('-t', '--title', help='A title for this job')
@moa.args.forceable
@moa.args.command
def simple(job, args):
"""
Create a 'simple' adhoc job.
title = _checkTitle(args.title)

job = moa.job.newJob(
job, template='simple',
title=title,
parameters=[('process', command)])


def exclamateInJob(job, args, command):
"""
Reuse the last issued command: set it as the 'process' parameters
in the current job
Simple meaning that no in or output files are tracked. Moa will
query you for a command to execute (the `process` parameter).
"""
moa.ui.fprint("{{bold}}Using command:{{reset}}", f='jinja')
moa.ui.fprint(command)
job.conf.process = command
job.conf.save()

_createAdhocJob(job, args, 'simple',
[ ('process', None)
])

@moa.args.argument('-t', '--title', help='A title for this job')
@moa.args.forceable
@moa.args.commandName('!')
def exclamate(job, args):
@moa.args.commandName('simple!')
def simpleX(job, args):
"""
Create a 'simple' job from the last command issued.
Set the `process` parameter to the last issued command. If a moa
job exists in the current directory, then the `process` parameter
is set without questions. (even if the Moa job in question does
not use the `process` parameter). If no moa job exists, a
`simple` job is created first.
Create a 'simple' adhoc job.
*Note:* This works only when using `bash` and if `moainit` is
sourced properly. `moainit` defines a bash function `_moa_prompt`
that is called every time a command is issued (using
`$PROMPT_COMMAND`). The `_moa_prompt` function takes the last
command from the bash history and stores it in
`~/.config/moa/last.command`. Additionally, the `_moa_prompt`
function stores all commands issued in a Moa directory in
`.moa/local_bash_history`.
Simple meaning that no in or output files are tracked. Moa will
query you for a command to execute (the `process` parameter).
"""

pc = os.environ.get('PROMPT_COMMAND', '')
if not '_moa_prompt' in pc:
moa.ui.exitError("moa is not set up to capture the last command")

histFile = os.path.join(os.path.expanduser('~'), '.moa.last.command')
with open(histFile) as F:
last = F.readlines()[-1].strip()
_createAdhocJob(job, args, 'simple',
[ ('process', _get_bash_history_file())])

if job.isMoa():
exclamateInJob(job, args, last)
else:
exclamateNoJob(job, args, last)
@moa.args.argument('-t', '--title', help='A title for this job')
@moa.args.forceable
@moa.args.commandName('map!')
def createMapX(job, args):
"""
create an adhoc moa 'map' job
Moa will query the user for process, input & output files. An
example session
"""
_createAdhocJob(job, args, 'map', [
( 'process', _get_bash_history_file() ),
('input', None),
('output', None),
])

@moa.args.argument('-t', '--title', help='A title for this job')
@moa.args.forceable
Expand All @@ -140,32 +139,11 @@ def createMap(job, args):
Moa will query the user for process, input & output files. An
example session
"""
wd = job.wd

if (not args.force and
os.path.exists(os.path.join(wd, '.moa', 'template'))):

moa.ui.exitError("Job already exists, use -f to override")


params = []

command = moa.ui.askUser('process:\n> ', '')

params.append(('process', command))


title = _checkTitle(args.title)

input = moa.ui.askUser('input:\n> ', '')
output = moa.ui.askUser('output:\n> ', './*')
params.append(('input', input))
params.append(('output', output))

moa.job.newJob(
job, template='map',
title=title,
parameters=params)
_createAdhocJob(job, args, 'map', [
('process', None ),
('input', None),
('output', None),
])


@moa.args.argument('-t', '--title', help='A title for this job')
Expand Down Expand Up @@ -202,45 +180,25 @@ def createReduce(job, args):
...
"""
wd = job.wd

if (not args.force and
os.path.exists(os.path.join(wd, '.moa', 'template'))):

moa.ui.exitError("Job already exists, use -f to override")

params = []

command = moa.ui.askUser('process:\n> ', '')
_createAdhocJob(job, args, 'reduce', [
('process', None ),
('input', None),
('output', None),
])

params.append(('process', command))

title = _checkTitle(args.title)

input = moa.ui.askUser('input:\n> ', '')
output = moa.ui.askUser('output:\n> ', './output')
params.append(('input', input))
params.append(('output', output))

moa.job.newJob(
job, template='reduce',
title=title,
parameters=params)


### Old adhoc code - still here for historical purposes
def _sourceOrTarget(g):
"""
Determine if this glob is a likely source or
target, depending on where the output is aimed to go
@moa.args.argument('-t', '--title', help='A title for this job')
@moa.args.forceable
@moa.args.commandName('reduce!')
def createReduceX(job, args):
"""
d = g.groups()[0]
if not d:
return 'target'
if d[:2] == './': return 'target'

if d[:2] == '..': return 'source'
if d[0] == '/':
return 'source'
return 'target'
Create a 'reduce' adhoc job using the bash history
This command is exactly the same as moa reduce, but uses the bash
history instead of the moa process history.
"""
_createAdhocJob(job, args, 'reduce', [
('process', _get_bash_history_file()),
('input', None),
('output', None),
])

0 comments on commit 19f634d

Please sign in to comment.