def upload_zmodem(self, filename): """Call external ZMODEM program to upload a file. Return an ExitStatus object that should indicate success or failure. """ sockfd = self.sock.fileno() pread, pwrite = os.pipe() pid = os.fork() if pid == 0: # child os.dup2(sockfd, 0) os.dup2(sockfd, 1) os.dup2(pwrite, 2) os.close(pread) os.close(pwrite) for fd in range(3, 64): try: os.close(fd) except: pass os.write(0, "rz -e -q -s +30\r") # needed with -e flag on sz os.execlp("sz", "sz", "-e", "-q", "-y", "-L", "128", filename) os._exit(1) # not normally reached # parent os.close(pwrite) while True: wpid, es = os.waitpid(pid, 0) if wpid == pid: break errout = os.read(pread, 4096) es = ExitStatus("sz {}".format(filename), es) es.output = errout return es
def upload_zmodem(self, filename): """Call external ZMODEM program to upload a file. Return an ExitStatus object that should indicate success or failure. """ sockfd = self.sock.fileno() pread, pwrite = os.pipe() pid = os.fork() if pid == 0: # child os.dup2(sockfd, 0) os.dup2(sockfd, 1) os.dup2(pwrite, 2) os.close(pread) os.close(pwrite) for fd in xrange(3,64): try: os.close(fd) except: pass os.write(0, "rz -e -q -s +30\r") # needed with -e flag on sz os.execlp("sz", "sz", "-e", "-q", "-y", "-L", "128", filename) os._exit(1) # not normally reached # parent os.close(pwrite) while True: wpid, es = os.waitpid(pid, 0) if wpid == pid: break errout = os.read(pread, 4096) es = ExitStatus("sz {}".format(filename), es) es.output = errout return es
def _proc_status(self, proc, sts): es = ExitStatus(sts, proc.cmdline.split()[0]) proc.set_exitstatus(es) # XXX untested with stopped processes if es.state != ExitStatus.STOPPED: proc.dead() del self._procs[proc.childpid] return es
def _proc_status(self, proc, sts): es = ExitStatus(proc.cmdline, sts) proc.set_exitstatus(es) if es.state != ExitStatus.STOPPED: # XXX untested with stopped processes proc.dead() if proc._async and not proc.closed: poller.unregister(proc) del self._procs[proc.childpid] return es
def waitpid(self, pid, option=0): try: proc = self._procs[pid] except KeyError: logging.warning("Wait on unmanaged process (pid: %s)." % pid) cmdline = ProcStat(pid).cmdline pid, sts = os.waitpid(pid, option) return ExitStatus(cmdline, sts) return self.waitproc(proc)
def upload(self, filename): """Basic upload using cat. """ text = open(filename).read() sockfd = self.sock.fileno() os.write(sockfd, "cat - > {}\r".format(os.path.basename(filename))) os.write(sockfd, text) os.write(sockfd, "\r" + chr(4)) return ExitStatus( "cat", 0) # fake exitstatus to be compatible with upload_zmodem.
def command(self, cmd, timeout=600): "write a shell command and return the output and the exit status." s = self._intf s.send_slow(str(cmd)) s.write("\r") s.readline() # discard echoed command resp = s.read_until(self.PROMPT) s.write("echo $?\r") s.read_until("\n") # consume echo command that was echoed ret = s.wait_for_prompt() ret = ExitStatus(cmd, int(ret) << 8) return ret, resp
def interrupt(self): "Interrupt a running process. Return the exit value." s = self._intf fo = s.fileobject() fo.interrupt() s.wait_for_prompt() s.send("echo $?\r") s.readline() ret = s.readline() s.wait_for_prompt() ret = ExitStatus("<interrupted process>", int(ret) << 8) return ret
def command(self, cmd, timeout=600): "Send a shell command and return the output and the exit status." s = self._intf s.send(str(cmd)) s.send("\r") s.readline() # eat command that was just sent resp = s.wait_for_prompt(timeout) s.send("echo $?\r") s.readline() # eat echo line #ret = s.readline() ret = s.wait_for_prompt() ret = ExitStatus(cmd, int(ret) << 8) return resp, ret
def system(cmd): sts = os.system(cmd) return ExitStatus(cmd, sts)
def system(cmd): """Like os.system(), except returns ExitStatus object.""" sts = os.system(cmd) return ExitStatus(sts, cmd)