def executeAndGetOutput( cmd ): try: import subprocess _p = subprocess.Popen( "%s" % cmd, shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE, close_fds = True ) outData = _p.stdout.read().strip() returnCode = _p.wait() except ImportError: import popen2 _p3 = popen2.Popen3( "%s" % cmd ) outData = _p3.fromchild.read().strip() returnCode = _p3.wait() return ( returnCode, outData )
def __init__(self, command, debugname): self.connectionID = debugname self.invoke = command self.cmd = command self.result = "" self.quit = False self.copyfiles = [] print "Executing %s" % command try: self.subprocess = popen2.Popen3(command) except: raise GtpError(self, "Failed to open pipe")
def spawn_async(cmd): if version_info >= (2, 6, 0): return spawn_async26(cmd) import popen2 try: process = popen2.Popen3(cmd) except: return (None, None) if process is None: return (None, None) process.tochild.close() return (process, process.fromchild)
def main(): from getopt import getopt import popen2 opts, args = getopt(sys.argv[1:], 'u:n:Rt:') opts = dict(opts) exitcode = 0 util = opts.get('-u', './kf_float') try: dims = [int(d) for d in opts['-n'].split(',')] cpx = opts.get('-R') is None fmt = opts.get('-t', 'f') except KeyError: sys.stderr.write(""" usage: compfft.py -n d1[,d2,d3...] : FFT dimension(s) -u utilname : see sample_code/fftutil.c, default = ./kf_float -R : real-optimized version\n""") sys.exit(1) x = fft.make_random(dims) cmd = '%s -n %s ' % (util, ','.join([str(d) for d in dims])) if cpx: xout = FFT.fftnd(x) xout = reshape(xout, (size(xout), )) else: cmd += '-R ' xout = FFT.real_fft(x) proc = popen2.Popen3(cmd, bufsize=len(x)) proc.tochild.write(dopack(x, fmt, cpx)) proc.tochild.close() xoutcomp = dounpack(proc.fromchild.read(), fmt, 1) #xoutcomp = reshape( xoutcomp , dims ) sig = xout * conjugate(xout) sigpow = sum(sig) diff = xout - xoutcomp noisepow = sum(diff * conjugate(diff)) snr = 10 * math.log10(abs(sigpow / noisepow)) if snr < 100: print xout print xoutcomp exitcode = 1 print 'NFFT=%s,SNR = %f dB' % (str(dims), snr) sys.exit(exitcode)
def runCommand(command): """ _runCommand_ Run the command without deadlocking stdou and stderr, echo all output to sys.stdout and sys.stderr Returns the exitCode """ child = popen2.Popen3(command, 1) # capture stdout and stderr from command child.tochild.close() # don't need to talk to child outfile = child.fromchild outfd = outfile.fileno() errfile = child.childerr errfd = errfile.fileno() makeNonBlocking(outfd) # don't deadlock! makeNonBlocking(errfd) outdata = errdata = '' outeof = erreof = 0 while 1: ready = select.select([outfd, errfd], [], []) # wait for input if outfd in ready[0]: try: outchunk = outfile.read() except Exception as ex: msg = "Unable to read stdout chunk... skipping" print msg outchunk = '' if outchunk == '': outeof = 1 sys.stdout.write(outchunk) if errfd in ready[0]: try: errchunk = errfile.read() except Exception as ex: msg = "Unable to read stderr chunk... skipping" print msg, str(ex) errchunk = "" if errchunk == '': erreof = 1 sys.stderr.write(errchunk) if outeof and erreof: break select.select([], [], [], .1) # give a little time for buffers to fill err = child.wait() if os.WIFEXITED(err): return os.WEXITSTATUS(err) elif os.WIFSIGNALED(err): return os.WTERMSIG(err) elif os.WIFSTOPPED(err): return os.WSTOPSIG(err) return err
def generate(self, event): if self.curListPos == 0: # no params to be written self.szDialog(_('No data'), wx.OK | wx.ICON_INFORMATION, _('No data to be written to database.')) elif self.szDialog( _('Are you sure?'), wx.YES_NO | wx.ICON_INFORMATION, _('Do you really want to write all data to database') ) == wx.ID_YES: # temporary file (for szbwriter) fileName = '/tmp/filler-' fileName += os.popen('date +%F-%R-%N').readline().rstrip() + '.tmp' try: outFile = file(fileName, 'w') except: self.szDialog(_('Temporary file'), wx.OK | wx.ICON_ERROR, _('Cannot open temporary file'), ': ', filename) return # writing to file for k, v in self.params.iteritems(): for i in v.range: gen(k, i[3], i[0], i[1], outFile) outFile.close() command = '/opt/szarp/bin/szbwriter -Dlog="/tmp/szbwriter-by-filler"' if "FILLER_COMMAND" in os.environ: command = os.environ["FILLER_COMMAND"] # writing to database writer = popen2.Popen3( command + self.main.options + ' < ' + fileName, True) result = writer.wait() / 256 err = writer.childerr.read().strip() if result == 0: self.szDialog(_('Database'), wx.OK | wx.ICON_INFORMATION, _('Data was successfully written to database.')) self.cleanList() # removing all (written) params elif err == '': self.szDialog(_('Database'), wx.OK|wx.ICON_ERROR, \ _('Error writing database:'),' ','szbwriter',\ ' ',_('returned'),' ', result ,'.\n',\ _('For more informations see'),' ',\ '/tmp/szbwriter-by-filler',' ', _('log file'),'.') else: self.szDialog(_('Database'), wx.OK | wx.ICON_ERROR, err) os.remove(fileName) # removing temporary file
def runCommand(cmd, printout=0, timeout=-1): """ Run command 'cmd'. Returns command stdoutput+stderror string on success, or None if an error occurred. Following recipe on http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52296 """ child = popen2.Popen3(cmd, 1) # capture stdout and stderr from command child.tochild.close() # don't need to talk to child outfile = child.fromchild outfd = outfile.fileno() errfile = child.childerr errfd = errfile.fileno() makeNonBlocking(outfd) # don't deadlock! makeNonBlocking(errfd) outdata = [] errdata = [] outeof = erreof = 0 if timeout > 0 : maxwaittime = time.time() + timeout err = -1 while (timeout == -1 or time.time() < maxwaittime): ready = select.select([outfd,errfd],[],[]) # wait for input if outfd in ready[0]: outchunk = outfile.read() if outchunk == '': outeof = 1 outdata.append(outchunk) if errfd in ready[0]: errchunk = errfile.read() if errchunk == '': erreof = 1 errdata.append(errchunk) if outeof and erreof: err = child.wait() break select.select([],[],[],.1) # give a little time for buffers to fill if err == -1: # kill the pid os.kill (child.pid, 9) err = child.wait() cmd_out = string.join(outdata,"") cmd_err = string.join(errdata,"") if err: return None cmd_out = cmd_out + cmd_err return cmd_out
def start(self): trycount = 0 while trycount < 10: if os.path.exists(self._ctl_path): self._proc = popen2.Popen3(self._ssh_cmd) break logging.info("Waiting for control socket to exist: %s" % self._ctl_path) trycount += 1 time.sleep(1) if not self._proc: fatal("Timeout waiting for master to create control socket: %s" % self._ctl_path)
def isconf(self, args='', quiet=True, rc=None): coverage = os.environ.get('COVERAGE', '') cmd = '%s ../bin/isconf -c simple.conf -q %s' % (coverage, args) print cmd popen = popen2.Popen3(cmd, capturestderr=True) (stdin, stdout, stderr) = (popen.tochild, popen.fromchild, popen.childerr) status = popen.wait() realrc = os.WEXITSTATUS(status) if rc is not None: self.assertEquals(realrc, rc) if quiet: self.failUnless(self.quiet((realrc, stdin, stdout, stderr))) return (realrc, stdin, stdout, stderr)
def getTvtimeVersion(self): helpcmd = '%s --help' % config.TVTIME_CMD child = popen2.Popen3( helpcmd, 1, 100) data = child.childerr.readline() # Just need the first line if data: data = re.search( "^(tvtime: )?Running tvtime (?P<major>\d+).(?P<minor>\d+).(?P<version>\d+).", data ) if data: _debug_("major is: %s" % data.group( "major" )) _debug_("minor is: %s" % data.group( "minor" )) _debug_("version is: %s" % data.group( "version" )) self.major = int(data.group( "major" )) self.minor = int(data.group( "minor" )) self.minorversion = int(data.group( "version" )) child.wait()
def restore(self, dbName, path): path = os.path.abspath(path) if not os.path.isfile(path): raise ValueError, "No such file %r" % path assert self.isRunning() t = time.time() self.dropDB(dbName) self.createDB(dbName) cmd = "%s %s < %s" % (self.mysql, dbName, path) import popen2 p = popen2.Popen3(cmd) p.wait() print >> sys.stderr, "RESTORED %r in %r secs" % (path, time.time() - t)
def findCpuColumnInSarOutput(filename): grepCmd = 'grep "\%user" ' + filename resultsPopen = popen2.Popen3(grepCmd) results = resultsPopen.fromchild.readlines() if ((results == None) or (len(results) == 0)): print "FATAL ERROR: sar output for file: " + filename + " is not in the proper format" sys.exit(2) resultVals = results[0].split() retVal = -1 for i in range(0, len(resultVals)): if (resultVals[i] == "%user"): retVal = i + 1 break return retVal
def __runCommand_redir(self, command, solution=None): global demo_killed tmp_out = [] if "subprocess" in sys.modules or hasattr(popen2, "Popen3"): # The command runs here. # This is done as the object version so I can use the "poll" method # later on to see if it is still running. if "subprocess" in sys.modules: args = os.path.expandvars(command).split() demo_object = self.__popen(args, subprocess.PIPE, subprocess.PIPE) stdin, stdout, stderr = (demo_object.stdin, demo_object.stdout, demo_object.stderr) teststatus = None else: demo_object = popen2.Popen3(command, 1, 1) stdin, stdout, stderr = (demo_object.tochild, demo_object.fromchild, demo_object.childerr) teststatus = -1 if solution is not None: self.__write_constants_solution(stdin, solution) stdin.close() status = demo_object.poll() while status == teststatus: try: line = stdout.readline() sys.stdout.write(line) sys.stdout.flush() tmp_out.append(line) except IOError: demo_killed = 1 status = demo_object.poll() else: stdout, stdin, stderr = popen2.popen3(command) self.__write_constants_solution(stdin, solution) stdin.close() status = 0 line = stdout.readline() # Read the rest of the data from stdout while len(line) > 0: tmp_out.append(line) sys.stdout.write(line) sys.stdout.flush() line = stdout.readline() self.__analyseLog("".join(tmp_out)) sys.stderr.write(stderr.read()) stdout.close() stderr.close() return status
def __init__ (self, cmd, resultHandler, prio=gobject.PRIORITY_LOW): self.lineSplitter = LineSplitter(resultHandler) #print "executing command: %s" % cmd self.popenObj = popen2.Popen3(cmd) self.pipe = self.popenObj.fromchild # make pipe non-blocking: fl = fcntl.fcntl(self.pipe, fcntl.F_GETFL) fcntl.fcntl(self.pipe, fcntl.F_SETFL, fl | os.O_NONBLOCK) #print "(add watch)" gobject.io_add_watch(self.pipe, gobject.IO_IN | gobject.IO_ERR | gobject.IO_HUP, self.onPipeReadable, priority=prio)
def subopt(self, strands, energy_gap, Temp=37.0, multi=" -multi", pseudo="", degenerate="", dangles="some"): self["subopt_composition"] = strands if Temp <= 0: raise ValueError( "The specified temperature must be greater than zero.") if (multi == 1 and pseudo == 1): raise ValueError( "The pseudoknot algorithm does not work with the -multi option." ) #Write input files self._write_input_subopt(strands, energy_gap) #Set arguments material = self["material"] if multi == "": multi = "" if pseudo: pseudo = " -pseudo" if degenerate: degenerate = " -degenerate " dangles = " -dangles " + dangles + " " #Call NuPACK C programs cmd = "subopt" args = " -T " + str( Temp ) + multi + pseudo + " -material " + material + degenerate + dangles + " " output = popen2.Popen3(cmd + args + self.prefix) while output.poll() < 0: try: output.wait() time.sleep(0.001) except: break if debug == 1: print output.fromchild.read() self._read_output_subopt() self._cleanup("subopt") self._cleanup("in") self["program"] = "subopt"
def openPipe(command): '''We need to use the asynchronous version here since we want to avoid blocking reads''' import popen2 pipe = None if hasattr(popen2, 'Popen3'): pipe = popen2.Popen3(command, 1) input = pipe.tochild output = pipe.fromchild err = pipe.childerr else: import os (input, output, err) = os.popen3(command) return (input, output, err, pipe)
def _run_medianfilter(self, medianfilter_cmd): # procedure to run superalign program and check for errors self.logfile.write(medianfilter_cmd) sproc = popen2.Popen3(medianfilter_cmd, 1) output = sproc.fromchild.readlines() errs = sproc.childerr.readlines() if errs: self.logfile.write('medianfilter choked on ' + medianfilter_cmd) for f in errs: self.logfile.write(string.strip(f)) self.errorList.append(('medianfilter', string.strip(f))) sproc.fromchild.close() sproc.childerr.close() return 0
def run(self, command, args): # as we pass popen3 as sequence, it executes monotone with these # arguments - and does not pass them through the shell according # to help(os.popen3) # print(("standalone is running:", command, args)) to_run = self.base_command + [command] + args process = popen2.Popen3(to_run, capturestderr=True) for line in process.fromchild: yield line stderr_data = process.childerr.read() if len(stderr_data) > 0: raise MonotoneException("data on stderr for command '%s': %s" % (command, stderr_data)) terminate_popen3(process)
def __init__(self, host, handler, streams=None, ssh_config=None): # Store information about the peer and self.handler = handler self.host = host # ... initialize the read and write file objects. self.myChild = None if streams: self.rfile = streams[0] self.wfile = streams[1] pass else: self.user, ssh_host = urllib.splituser(self.host) # print self.user + " " + self.host + " " + handler # Use ssh unless we're on Windows with no ssh-agent running. nt = os.name == "nt" use_ssh = not nt or os.environ.has_key("SSH_AGENT_PID") flags = "" if self.user: flags = flags + " -l " + self.user pass if use_ssh and ssh_config: flags = flags + " -F " + ssh_config pass args = flags + " " + ssh_host + " " + handler if use_ssh: cmd = "ssh -x -C -o 'CompressionLevel 5' " + args pass else: # Use the PyTTY plink, equivalent to the ssh command. cmd = "plink -x -C " + args pass if not nt: # Popen3 objects, and the wait method, are Unix-only. self.myChild = popen2.Popen3(cmd, 0) self.rfile = self.myChild.fromchild self.wfile = self.myChild.tochild pass else: # Open the pipe in Binary mode so it doesn't mess with CR-LFs. self.rfile, self.wfile, self.errfile = popen2.popen3(cmd, mode='b') pass # print "wfile", self.wfile, "rfile", self.rfile pass return
def export_PDF(self, complex_ID, name="", filename="temp.pdf", program=None): """Uses Zuker's sir_graph_ng and ps2pdf.exe to convert a secondary structure described in .ct format to a PDF of the RNA""" if program is None: program = self["program"] inputfile = "temp.ct" self.Convert_to_ct(complex_ID, name, inputfile, program) cmd = "sir_graph_ng" #Assumes it's on the path args = "-p" #to PostScript file output = popen2.Popen3(cmd + " " + args + " " + inputfile, "r") output.wait() if debug == 1: print output.fromchild.read() inputfile = inputfile[0:len(inputfile) - 2] + "ps" cmd = "ps2pdf" #Assumes it's on the path output = popen2.Popen3(cmd + " " + inputfile, "r") output.wait() if debug == 1: print output.fromchild.read() outputfile = inputfile[0:len(inputfile) - 2] + "pdf" #Remove the temporary file "temp.ct" if it exists if os.path.exists("temp.ct"): os.remove("temp.ct") #Remove the temporary Postscript file if it exists if os.path.exists(inputfile): os.remove(inputfile) #Rename the output file to the desired filename. if os.path.exists(outputfile): os.rename(outputfile, filename)
def getCommandOutput(command, verbose=False, distinct=False): def makeNonBlocking(fd): fl = fcntl.fcntl(fd, fcntl.F_GETFL) fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NDELAY) if verbose: print command child = popen2.Popen3(command, 1) # capture stdout and stderr from command child.tochild.close() # don't need to talk to child outfile = child.fromchild outfd = outfile.fileno() errfile = child.childerr errfd = errfile.fileno() makeNonBlocking(outfd) # don't deadlock! makeNonBlocking(errfd) outdata = errdata = complete = '' outeof = erreof = 0 while 1: ready = select.select([outfd, errfd], [], []) # wait for input if outfd in ready[0]: outchunk = outfile.read() if verbose: print outchunk, if outchunk == '': outeof = 1 if distinct: outdata = outdata + outchunk else: complete += outchunk if errfd in ready[0]: errchunk = errfile.read() if verbose: print errchunk, if errchunk == '': erreof = 1 if distinct: errdata = errdata + errchunk else: complete += errchunk if outeof and erreof: break select.select([], [], [], .1) # give a little time for buffers to fill if verbose: print "" err = child.wait() if distinct: return err, outdata, errdata else: return err, complete
def execLocalGetResult(__cmd, err=False, __output=None, __err=None): """ exec %__cmd and returns an array ouf output lines either (rc, out, err) or (rc, out) dependent on if err or not. @param __output overwrite the output for this command so that it will be executed. Will only work in Simulated environment """ global __EXEC_REALLY_DO log.debug(__cmd) if __EXEC_REALLY_DO == ASK: __ans = raw_input(__cmd + " (y*,n,c)") if __ans == "c": __EXEC_REALLY_DO = CONTINUE if __ans == "n": if err: return [0, SKIPPED, ""] else: return [0, SKIPPED] elif isSimulate(): if not __err: __err = err return __simret(command=__cmd, output=__output, error=__err, tostderr=False, tostdout=False, asstring=False) if sys.version[:3] < "2.4": import popen2 child = popen2.Popen3(__cmd, err) __rc = child.wait() __rv = child.fromchild.readlines() if err: __err = child.childerr.readlines() return [__rc, __rv, __err] return [__rc, __rv] else: import subprocess p = subprocess.Popen([__cmd], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True) p.wait() __rc = p.returncode __rv = p.stdout.readlines() if err: __err = p.stderr.readlines() return [__rc, __rv, __err] return [__rc, __rv]
def start(self): """Arranca el proceso ameba, instanciando el objeto Popen3. Se utiliza un metodo separado de la instanciacion del AmebaProcess para poder relanzar el proceso ante ciertas condiciones de error.""" mcrRoot = os.path.join(self.props.getProperty("EXEHOME"), self.props.getProperty("MCRROOT")) #mcrPath = "%s/bin/glnx86:%s/runtime/glnx86" % ( mcrRoot , mcrRoot ) mcrPath = "%s" % mcrRoot if os.environ.has_key("LD_LIBRARY_PATH"): mcrPath += ":%s" % os.environ['LD_LIBRARY_PATH'] amebaRoot = os.path.join(self.props.getProperty("EXEHOME"), self.props.getProperty("AMEBADIR")) os.environ['MCRROOT'] = mcrRoot if self.cpuid == -1: os.chdir(amebaRoot + "/" + self.getVersion()) self.child = popen2.Popen3( "LD_LIBRARY_PATH=%s ./ameba.sh" % mcrPath, True) else: os.chdir(amebaRoot + "/" + self.getVersion()) self.child = popen2.Popen3( "LD_LIBRARY_PATH=%s taskset -c %s ./ameba.sh" % (mcrPath, self.cpuid), True) logger.info("Starting ameba %s with pid %s. Octave binary from %s" % (self.getVersion(), self.child.pid, mcrRoot))
def display(self): """ Exec the relevant program and extract the output into the UI """ import popen2 out_format = self.out_format if out_format == 'png': out_format = 'gif' print "%s -T%s -Gstart=rand" % (self.prog, out_format) dot_prog = popen2.Popen3("%s -T%s -Gstart=rand" % (self.prog, out_format)) dot_prog.tochild.write(self.text()) dot_prog.tochild.close() result = dot_prog.fromchild.read() return result
def getNamespaceDict(): # execute SealPluginDump to get list of plugins process = popen2.Popen3('SealPluginDump') output = process.fromchild.read() namespaceDict = {} for line in output.split('\n'): # search for Reflex, global namespace and non templates if line.find('Reflex') !=-1 and line.find(':') ==-1 and line.find('<') ==-1: className = line.replace('LCGReflex/', '').strip() namespaceDict[className] = line.strip() return namespaceDict
def run(self): print 'Content-type: application/octet-stream' print # blank line required cmd = 'mkisofs -r -quiet %s | dd bs=%s skip=%s' % (self.path, self.bs, self.chunk) job = popen2.Popen3(cmd) while 1: data = job.fromchild.read(1024) if not data: break sys.stdout.write(data) job.fromchild.close() job.tochild.close() job.wait()
def runSvnAdmin(cmd): # Spawn the process and get pipes p = popen2.Popen3(svnadmin_bin + ' ' + cmd, 1) # Read all of stderr first se = p.childerr.readlines() # Read all of stdout so = p.fromchild.readlines() # Get the return code rc = p.wait() # Return it all return (rc, so, se)
def __init__(self, cmdline, datafile): self.cmdline = ""; self.retcode = 0; self.fname = datafile; debug("Grabber: calls popen2.Popen3(%s)" % cmdline); self.opener = popen2.Popen3(cmdline); self.file = None; # while True: # wait for the child termination. # ret = self.opener.poll(); # if ret != -1: # break; self.retcode = self.opener.wait(); if self.retcode != 0: raise GrabberException(1); self.file = open(self.fname, "r"); # may raise IOError or OSError
def _restart(self): assert self._lock.locked() self._reset_popen() self._popen = popen2.Popen3( "superexec %s %s" % (CBUS_CLI, self._get_dev_name()), False) line = self._popen.fromchild.readline() if self.debug: print "< %r" % line while (line and line != CBus.PROMPT): line = self._popen.fromchild.readline() if self.debug: print "< %r" % line if self.auto_discover: # This is really a work around to how CBM discovers initial values. self._discover() return
def stdout(app): """ start app and return the stdout """ ret = [] child = popen2.Popen3(app, 1, 100) while (1): data = child.fromchild.readline() if not data: break ret.append(data) child.wait() child.fromchild.close() child.childerr.close() child.tochild.close() return ret