def execute_command(self, cmdline): """ Scan the given command-line and return a predefined result if *any* word in command position matches one of the keys in the `expected_answer` argument to the class constructor. Note that the parsing of command-line is based on regular expressions and is thus only an approximation at ``sh`` syntax. It will *certainly* fail on some command-lines, but there is no way around this short of writing a complete ``sh`` parser just for this function. (And no, Python's module `shlex` will not do the job -- been there, done that.) """ log.debug("scanning command-line <<<%s>>>", cmdline) for match in self._COMMAND_RE.finditer(cmdline): cmd = match.group("cmd") if cmd in self.expected_answer: reply = self.expected_answer[cmd] log.debug("returning programmed reply for '%s': %s", cmd, reply) return reply # if everything else failed, do run the command-line ... return LocalTransport.execute_command(self, cmdline)
def execute_command(self, cmdline): """ Scan the given command-line and return a predefined result if *any* word in command position matches one of the keys in the `expected_answer` argument to the class constructor. Note that the parsing of command-line is based on regular expressions and is thus only an approximation at ``sh`` syntax. It will *certainly* fail on some command-lines, but there is no way around this short of writing a complete ``sh`` parser just for this function. (And no, Python's module `shlex` will not do the job -- been there, done that.) """ log.debug("scanning command-line <<<%s>>>", cmdline) for match in self._COMMAND_RE.finditer(cmdline): cmd = match.group("cmd") if cmd in self.expected_answer: return self.expected_answer[cmd] # if everything else failed, do run the command-line ... return LocalTransport.execute_command(self, cmdline)
def execute_command(self, command): """parse the command and return fake output and error codes depending on the current suppose status of the job""" commands = [] # There isn't an easy way to split a complex pipeline like # "cmd1 ; cmd2 && cmd3 || cmd4" # We do split the command line, however, but it will work only # in a simple case. Therefore you have to remember that # complex command lines will probably not be handled correctly # by this method. _ = command.split(';') for i in _: commands.extend(i.strip().split('&&')) for cmd in commands: args = cmd.strip().split() if args[0] in self.expected_answer: return self.expected_answer[args[0]] else: continue return LocalTransport.execute_command(self, command)
def __init__(self, expected_answer={}): LocalTransport.__init__(self) self.expected_answer = expected_answer