2010-02-20 15:37:09 -05:00
|
|
|
#! /usr/bin/python
|
|
|
|
|
|
|
|
# To change this template, choose Tools | Templates
|
|
|
|
# and open the template in the editor.
|
|
|
|
|
|
|
|
__author__="bjschuma"
|
|
|
|
__date__ ="$Feb 20, 2010 2:19:10 PM$"
|
|
|
|
|
|
|
|
from bt.message import *
|
|
|
|
from session import alias
|
|
|
|
from session import manager
|
2010-02-26 00:31:35 -05:00
|
|
|
from session import vars
|
2010-02-20 15:37:09 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def runCmd(input):
|
|
|
|
write("Running command: "+input,2)
|
|
|
|
|
|
|
|
# Find command
|
|
|
|
split = input.split(' ',1)
|
|
|
|
cmd = split[0]
|
|
|
|
args=None
|
|
|
|
if len(split) > 1:
|
|
|
|
args = split[1]
|
|
|
|
|
|
|
|
if (cmd in manager.enabled) == True:
|
|
|
|
if args == None:
|
|
|
|
return manager.enabled[cmd].start()
|
|
|
|
else:
|
|
|
|
return manager.enabled[cmd].start(args.split())
|
2010-02-26 00:31:35 -05:00
|
|
|
else:
|
|
|
|
return input
|
|
|
|
|
|
|
|
|
|
|
|
# Check if we are storing in a variable
|
|
|
|
def varCheck(cmd):
|
|
|
|
split = cmd.split('=', 1)
|
|
|
|
if len(split)==2 and len(split[0].split())==1:
|
2010-02-28 15:44:40 -05:00
|
|
|
var = split[0].strip()
|
|
|
|
if not var[0]=="$":
|
|
|
|
var = "$" + var
|
2010-02-26 00:31:35 -05:00
|
|
|
write("Using variable: "+var, 2)
|
|
|
|
return ( var, split[1].strip() )
|
|
|
|
else:
|
|
|
|
return (None, cmd)
|
|
|
|
|
|
|
|
|
|
|
|
# Replace variables in the command with their real values
|
|
|
|
def varReplace(cmd):
|
2010-02-28 15:44:40 -05:00
|
|
|
for key in vars.keys():
|
|
|
|
v = "`"+key+"`"
|
|
|
|
if cmd.find(v) > -1:
|
|
|
|
new = str(vars[key])
|
|
|
|
write(key + " => " + new, 2)
|
|
|
|
cmd = cmd.replace(v, new)
|
|
|
|
return cmd
|
2010-02-26 00:31:35 -05:00
|
|
|
|
|
|
|
|
|
|
|
# Replace aliases with their real values
|
|
|
|
def aliasReplace(cmd):
|
|
|
|
split = cmd.split()
|
|
|
|
out = ""
|
|
|
|
for index,word in enumerate(split):
|
|
|
|
if index > 0:
|
|
|
|
out += " "
|
|
|
|
if alias.has(word) == True:
|
2010-02-26 00:37:48 -05:00
|
|
|
write(word + " => " + alias[word], 2)
|
2010-02-26 00:31:35 -05:00
|
|
|
out += aliasReplace(alias[word])
|
|
|
|
else:
|
|
|
|
out += word
|
|
|
|
return out
|
2010-02-20 15:37:09 -05:00
|
|
|
|
|
|
|
|
|
|
|
def run(string):
|
|
|
|
split = string.split(";")
|
|
|
|
ans = []
|
2010-02-26 00:31:35 -05:00
|
|
|
for cmd in split:
|
|
|
|
(var,cmd) = varCheck(cmd)
|
|
|
|
cmd = varReplace(cmd)
|
|
|
|
cmd = aliasReplace(cmd)
|
|
|
|
|
|
|
|
if var == None:
|
2010-02-26 00:37:48 -05:00
|
|
|
ans += [ runCmd(cmd) ]
|
2010-02-26 00:31:35 -05:00
|
|
|
else:
|
2010-02-26 00:37:48 -05:00
|
|
|
disable()
|
|
|
|
vars[var] = runCmd(cmd)
|
|
|
|
enable()
|
2010-02-26 00:31:35 -05:00
|
|
|
|
2010-02-21 21:50:02 -05:00
|
|
|
if len(ans) == 1:
|
|
|
|
return ans[0]
|
2010-02-20 15:37:09 -05:00
|
|
|
return ans
|
|
|
|
|
2010-02-21 21:50:02 -05:00
|
|
|
|
2010-02-20 15:37:09 -05:00
|
|
|
def call(string):
|
|
|
|
# disable text printing
|
|
|
|
disable()
|
|
|
|
result = run(string)
|
|
|
|
# enable text printing
|
|
|
|
enable()
|
|
|
|
return result
|
|
|
|
|