diff options
author | noah <noah@656d521f-e311-0410-88e0-e7920216d269> | 2007-06-27 23:50:30 +0000 |
---|---|---|
committer | noah <noah@656d521f-e311-0410-88e0-e7920216d269> | 2007-06-27 23:50:30 +0000 |
commit | 5be939e901a3633adb04e234aca4e8995613a7f4 (patch) | |
tree | d98d0b52fe3d2e123b6ef2ef2bc3ac4dfa036347 | |
parent | 9397f4733605a7b36722a36e03dcb5c95239ea28 (diff) | |
download | pexpect-5be939e901a3633adb04e234aca4e8995613a7f4.tar.gz |
Added Andrew Stone's code.
git-svn-id: http://pexpect.svn.sourceforge.net/svnroot/pexpect/trunk@480 656d521f-e311-0410-88e0-e7920216d269
-rw-r--r-- | pexpect/psh.py | 146 |
1 files changed, 139 insertions, 7 deletions
diff --git a/pexpect/psh.py b/pexpect/psh.py index 6737c90..ac5f761 100644 --- a/pexpect/psh.py +++ b/pexpect/psh.py @@ -4,23 +4,155 @@ combines Pexpect and wraps many Standard Python Library functions. $Id$ """ -import pexpect, os, sys +import pexpect, os, sys, re +from types import * + +class ExceptionPsh(pexpect.ExceptionPexpect): + + """Raised for pxssh exceptions. + """ + +class ExceptionErrorCode(ExceptionPsh): + + """Raised when an program returns an error code + """ + + def __init__(self, string, err_code, cmd_output): + + ExceptionPsh.__init__(self,string) + self.error = err_code + self.output = cmd_output class psh (object): - def __init__ (self): - self.cwd = os.getcwd() + def __init__ (self,exp): + + self.exp = exp + self.default_timeout = 30 # Seconds + def ls (self, path=''): + + fileStr = self.run("ls %s" % path) + return fileStr.split() + def cd (self, path='-'): + + return self.run("cd %s" % path) + def rm (self, path=''): + + return self.run("/bin/rm -f %s" % path) + def cp (self, path_from='', path_to=''): + + return self.run("/bin/cp %s %s" % (path_from, path_to)) + def mv (self, path_from='', path_to=''): + + return self.run("/bin/mv %s %s" % (path_from, path_to)) + def pwd (self): + + return self.run("/bin/pwd") + def which (self, exe_name): - def chown (self, path, user='', group='', recurse=False): + + return self.run("/usr/bin/which %s" % exe_name) + + def chown (self, path, user='', group=None, recurse=False): + + xtra_flags = "" + if recurse: xtra_flags = "-R" + if group: group = ':' + group + else: group = "" + + return self.run("/bin/chmod %s %s%s %s" % (recurse,user,group,path)) + def chmod (self, path, perms='', recurse=False): + + xtra_flags = "" + if recurse: xtra_flags = "-R" + return self.run("/usr/bin/chmod %s %s %s" % (xtra_flags, perms, path)) + def chattr (self, path, attrs='', recurse=False): + + xtra_flags = "" + if recurse: xtra_flags = "-R" + return self.run("/usr/bin/chattr %s %s %s" % (xtra_flags, attrs, path)) + def cat (self, path): - def run (self, cmd): - def pipe (self, cmd, string_to_send): - + + return self.run("/bin/cat %s" % path) + + def run (self, cmd, stim_resp_dict = {}, timeout=None): + + (ret, output) = self.run_raw(cmd, stim_resp_dict, timeout) + if ret == 0: return output + raise ExceptionErrorCode("Running command [%s] returned error [%d]" % (cmd,ret), ret, output) + + def run_raw(self, cmd, stim_resp_dict=None, timeout=None): + + if not timeout: timeout = self.default_timeout + + def cmd_exp_loop(param): + if type(param) is DictType: + param = (param,) + for item in param: + if type(item) is type(()) or type(item) is type([]): + cmd_exp_loop(item) + elif type(item) is type(""): + self.exp.send(item) + elif type(item) is type({}): + dict = item + while(1): + stimulus = dict.keys() + idx = self.exp.expect_exact(stimulus, timeout) + keys = dict.keys() + respond = dict[keys[idx]] + if type(respond) is type({}) or type(respond) is type(()) or type(item) is type([]): + cmd_exp_loop(respond) + if type(respond) is type(""): + self.exp.send(respond) + elif type(respond) is InstanceType and Exception in inspect.getmro(respond.__class__): + raise respond + elif type(respond) is type(0): + return (respond, self.exp.before) + elif respond is None: + break + else: + self.exp.send(str(respond)) + + if stim_resp_dict == None: + stim_resp_dict = {} + + self.exp.sendline("") + if not self.exp.prompt(): raise SessionException("No prompt") + self.exp.sendline(cmd) + self.exp.expect_exact([cmd]) + stim_resp_dict[re.compile(self.exp.PROMPT)] = None + cmd_exp_loop(stim_resp_dict) + + output = self.exp.before + # Get the return code + self.exp.sendline("echo $?") + self.exp.expect_exact(["echo $?"]) + if not self.exp.prompt(): raise SessionException("No prompt", 0, self.exp.before) + try: + reg = re.compile("^(\d+)") + s = self.exp.before.strip() + #print s + #pdb.set_trace() + s = reg.search(s).groups()[0] + error_code = int(s) + except ValueError: + log.error("Cannot parse %s into an int!" % self.exp.before) + raise + + if not output[0:2] == '\r\n': + log.warning("Returned output lacks leading \\r\\n which may indicate a tae error") + log.debug2("Offending output string: [%s]" % output) + return (error_code, output) + else: + return(error_code, output[2:]) + +# def pipe (self, cmd, string_to_send): |