def getZpoolDevs(ml=None, zl=None): mpdevs = [] zpools = [] if not ml or not zl: ml = Popen(['/usr/sbin/mpathadm','list', 'LU'], stdout=PIPE).stdout zl = Popen(['/usr/sbin/zpool','status'], env={'LC_ALL':'C'}, stdout=PIPE).stdout mpdevs = [ (line.strip()) for line in ml.readlines() if 'rdsk' in line] lines = zl.readlines() iter_lines = iter(lines) devpat = compile('(/dev/(r)?dsk/)?(c.*d0)(s[0-9])?') for line in iter_lines: if 'pool:' in line: zd = {} poolname = line.split()[1] zd[poolname] = [] for line in iter_lines: if len(line.split()) > 4: if line.split()[0] in ('errors:'): break if line.split()[0] in (poolname,'mirror-0', 'NAME', 'scan:'): continue if match(devpat, line.split()[0]): for d in mpdevs: if match(devpat, line.split()[0]).groups()[2] == match(devpat, d).groups()[2]: zd[poolname].append(d) break zpools.append(zd) return zpools
def retrieve_spec(target_path,timepoint,left,right): output = np.array([]) result = np.zeros((2, 3)) if (left == True): stdout = Popen(['fslinfo ' + target_path + '/' + timepoint + '_left.nii.gz'], shell=True, stdout=PIPE).stdout for line in stdout.readlines(): if (output.size == 0): output = np.array(line) else: output = np.append(output,line) x = str(output[6]) result[0][0] = x[17:-3] y = str(output[7]) result[0][1] = y[17:-3] z = str(output[8]) result[0][2] = z[17:-3] if (right == True): output = np.array([]) stdout = Popen(['fslinfo ' + target_path + '/' + timepoint + '_right.nii.gz'], shell=True, stdout=PIPE).stdout for line in stdout.readlines(): if (output.size == 0): output = np.array(line) else: output = np.append(output,line) x = str(output[6]) result[1][0] = x[17:-3] y = str(output[7]) result[1][1] = y[17:-3] z = str(output[8]) result[1][2] = z[17:-3] return result
def runshell(input): """ This function just calls popen on the input string, and the stdout is printed. """ import socket from subprocess import Popen, PIPE print "%s executing: %s" % (socket.gethostname(), input) pipe = Popen(input, shell=True, stdout=PIPE).stdout pipe.readlines() return 0
def runshell(input): """ This function just calls popen on the input string, and the stdout is printed. """ from pyina import mpi from subprocess import Popen, PIPE print "%d of %d: executing: %s" % (mpi.world.rank, mpi.world.size, input) pipe = Popen(input, shell=True, stdout=PIPE).stdout pipe.readlines() return 0
class LineReader(object): def __init__(self, fname): if fname.endswith('.gz'): if not os.path.isfile(fname): raise IOError(fname) self.f = Popen(['gunzip', '-c', fname], stdout=PIPE, stderr=PIPE) self.zipped = True else: self.f = open(fname, 'r') self.zipped = False def readlines(self): if self.zipped: for line in self.f.stdout: yield line else: for line in self.f.readlines(): yield line def close(self): if self.zipped: if self.f.poll() == None: os.kill(self.f.pid, signal.SIGHUP) else: self.f.close() def __enter__(self): return self def __exit__(self, type, value, traceback): self.close() def __iter__(self): return self.readlines()
def save(self,filename=None): start = time.time() if filename is None: filename = self._genfilename() cmd = ' '.join([gspath, '-dNOPAUSE', '-q', '-r300', '-sDEVICE=tiffg4', '-dBATCH', '-sOutputFile=%s'%filename, '-sPAPERSIZE=a4', '-dFirstPage=%d'%self._pagenumber, '-dLastPage=%d'%self._pagenumber, self._origfile ]) po = Popen(cmd,shell=True,stdout=PIPE,stderr=STDOUT).stdout for l in po.readlines(): self._l.debug("GS Output:%s",l) po.close() self._file = filename self._l.debug("Saving file %s (duration %f)", filename,(time.time()-start)) return filename
class LineReader: def __init__(self, fname): if fname.endswith(".gz"): if not os.path.isfile(fname): raise IOError(fname) self.f = Popen(["gunzip", "-c", fname], stdout=PIPE, stderr=PIPE) self.zipped = True else: self.f = open(fname, "r") self.zipped = False def readlines(self): if self.zipped: for line in self.f.stdout: yield line else: for line in self.f.readlines(): yield line def close(self): if self.zipped: if self.f.poll() == None: os.kill(self.f.pid, signal.SIGHUP) else: self.f.close() def __enter__(self): return self def __exit__(self, type, value, traceback): self.close() def __iter__(self): return self.readlines()
def fingerprint(source=None): """Create a new fingerprint of current repositories. The source argument is parsed to look for the expected output from a `sync-all` command. If the source is `None` then the `sync-all` command will be run to get the current fingerprint. """ if source is None: if sys.platform == 'win32': # Can't rely on perl being located at `/usr/bin/perl`. sync_all = ["perl", "./sync-all", "log", "-1", "--pretty=oneline"] else: sync_all = ["./sync-all", "log", "-1", "--pretty=oneline"] source = Popen(sync_all, stdout=PIPE).stdout lib = "" commits = {} for line in source.readlines(): if line.startswith("=="): lib = line.split()[1].rstrip(":") lib = "." if lib == "running" else lib # hack for top ghc repo elif re.match("[abcdef0-9]{40}", line): commit = line[:40] commits[lib] = commit return FingerPrint(commits)
def _hostIsUp(host, count=1, timeout=1): """ Executes a ping command and sets the appropriate attribute. Parameters: A host, optional: Amount of pings, Timeout """ thecommand = 'ping -n {0} -w {1} -a {2}'.format(count, timeout, host.ipaddress) results = Popen(thecommand, stdout=PIPE).stdout output = results.readlines() results.close() # Convert the returned bytearrays to strings. output = [item.decode('utf8') for item in output] output = ' '.join(output) if ('Request timed out' in output) or ('Destination net unreachable' in output): if host.isup == True: host.isup = False host.changed = True else: if host.isup == False: host.isup = True host.changed = True
def scanEntries(self): assert self.isArchive() attrs = self.attrs attrs = attrs.dirAttrs attrs.thisCount = 0 attrs.thisSize = 0 attrs.totalCount = 0 attrs.totalSize = 0 pipe = None try: pipe = Popen("7za l \"%s\"" % self.path, shell = True, stdout = PIPE).stdout for line in pipe.readlines(): if not re.match("20", line): continue filename = line[53:-1] entry = Entry(self.volume, self, join(self.path, filename), join(self.volumePath, filename), filename) self.readStoredInfo(entry, line) entry.store() attrs.thisCount += 1 attrs.thisSize += entry.attrs.size except Exception, msg: print "Failed to index %s:" % self.path, msg
def run_one(self, c, g): cmdline = '{0} -c {1} -g {2} -v {3} {4} {5}'.format( svmtrain_exe, c, g, fold, pass_through_string, dataset_pathname) result = Popen(cmdline, shell=True, stdout=PIPE).stdout for line in result.readlines(): if str(line).find("Cross") != -1: return float(line.split()[-1][0:-1])
def scanEntries(self): assert self.isArchive() attrs = self.attrs attrs = attrs.dirAttrs attrs.thisCount = 0 attrs.thisSize = 0 attrs.totalCount = 0 attrs.totalSize = 0 pipe = None try: pipe = Popen("7za l \"%s\"" % self.path, shell=True, stdout=PIPE).stdout for line in pipe.readlines(): if not re.match("20", line): continue filename = line[53:-1] entry = Entry(self.volume, self, join(self.path, filename), join(self.volumePath, filename), filename) self.readStoredInfo(entry, line) entry.store() attrs.thisCount += 1 attrs.thisSize += entry.attrs.size except Exception, msg: print "Failed to index %s:" % self.path, msg
def get_svn_info(): "Check the status of the target directories for svn checkouts" logger(get_func_name() + "()", "debug") global relCfg exit_status = 0 if 'SVN' in relCfg: for line in relCfg['SVN'] : if (os.access(line[0] + "/.svn", os.W_OK) and os.path.isdir(line[0] + "/.svn")): line.append('svn') p = Popen( ('/usr/bin/svn info ' + line[0]), shell=True, stdout=PIPE).stdout plines = filter(None, [pline.strip() for pline in p.readlines()]) for pline in plines: k, v = re.split(': ', pline, 1) if (k == 'URL'): line.append(v) if (k == 'Revision'): line.append(v) elif (os.access(line[0], os.W_OK) and os.path.isdir(line[0])): line.append('dir') else: line.append('bad') exit_status = -1 else: exit_status = -1 return exit_status
def enableUnicode(self): cmd = [ self.p4d, "-r", self.server_root, "-L", "log", "-vserver=3", "-xi" ] f = Popen(cmd, stdout=PIPE).stdout for s in f.readlines(): pass f.close()
def feature_extraction(filepath): features = [] command_to_execute = 'python3 pdfid.py ' + filepath stdout = Popen(command_to_execute, shell=True, stdout=PIPE).stdout output = stdout.readlines() if len(output) == 24: features.append(extract_featues(output)) return features
def run_one(self, prot_p): ###print self.name + ' Running :' + prot_p cmdline = ('{0} {1}'.format(psipred_exe, prot_p)) result = Popen(cmdline,shell=True,stdout=PIPE).stdout for line in result.readlines(): #print line if str(line).find("Finished.") != -1: ###print self.name + ' Finished :' + prot_p return 1
def run_one(self, p_names): cmd_str = '{0} {1} -i {2} {3}' cmdline = cmd_str.format(self.py_exe, self.py_code, self.ppi_p, p_names) print cmdline result = Popen(cmdline, shell=True, stdout=PIPE).stdout for line in result.readlines(): if str(line).find("Finish changing the format to libsvm") != -1: return True
def run_one(self, prot_p): ###print self.name + ' Running :' + prot_p cmdline = "{0} {1}".format(psipred_exe, prot_p) result = Popen(cmdline, shell=True, stdout=PIPE).stdout for line in result.readlines(): # print line if str(line).find("Finished.") != -1: ###print self.name + ' Finished :' + prot_p return 1
def notifier_loop_callback(self, notifier): if len(self.file_events) > 0: for event in self.file_events: self.multi_queue.put(event) self.mmc.touch_index_file() self.file_events = [] #yield to worker thread time.sleep(0) #use items() because we are going to be modifying this #dictionary while iterating over it. for k, pair in self.cookies_IN_MOVED_FROM.items(): event = pair[0] timestamp = pair[1] timestamp_now = time.time() if timestamp_now - timestamp > 5: #in_moved_from event didn't have a corresponding #in_moved_to event in the last 5 seconds. #This means the file was moved to outside of the #watched directories. Let's handle this by deleting #it from the Airtime directory. del self.cookies_IN_MOVED_FROM[k] self.handle_removed_file(False, event.pathname) # we don't want create_dict grow infinitely # this part is like a garbage collector for k, t in self.create_dict.items(): now = time.time() if now - t > 5: # check if file exist # When whole directory is copied to the organized dir, # inotify doesn't fire IN_CLOSE_WRITE, hench we need special way of # handling those cases. We are manully calling handle_created_file # function. if os.path.exists(k): # check if file is open try: command = "lsof " + k #f = os.popen(command) f = Popen(command, shell=True, stdout=PIPE).stdout except Exception, e: self.logger.error('Exception: %s', e) self.logger.error("traceback: %s", traceback.format_exc()) continue if not f.readlines(): self.logger.info("Handling file: %s", k) self.handle_created_file(False, k, os.path.basename(k)) del self.create_dict[k] else: del self.create_dict[k]
def process(self, event): """ Runs the discord bot.run command when called """ print(event.src_path, event.event_type) pipe = Popen('ps ax | grep whosonline', shell=True, stdout=PIPE).stdout output = pipe.readlines()[0].decode("utf-8") if "/bot/whosonline.py" in output: call(['kill', output.split(" ")[0]]) Popen(['python3', '../bot/whosonline.py'])
def run_one(self, prot_p): cmd_str = 'ssh -x {0} "cd {1}; {2} {3}"' cmdline = cmd_str.format(self.host, self.cwd, psipred_exe, prot_p) print cmdline result = Popen(cmdline,shell=True,stdout=PIPE).stdout for line in result.readlines(): #print line if str(line).find("Finished.") != -1: ###print self.name + ' Finished :' + prot_p return 1
def run_one(self, p_names): py_path = "export PYTHONPATH=/home/ba2g09/workspace/summer_internship;" cmd_str = 'ssh -x {0} "' + py_path + 'cd {1}; {2} {3} -i {4} {5}"' cmdline = cmd_str.format(self.host, self.cwd, self.py_exe, self.py_code, self.ppi_p, p_names) print cmdline result = Popen(cmdline, shell=True, stdout=PIPE).stdout for line in result.readlines(): if str(line).find("Finish changing the format to libsvm") != -1: return True
def run_one(self, prot_p): cmd_str = 'ssh -x {0} "cd {1}; {2} {3}"' cmdline = cmd_str.format(self.host, self.cwd, psipred_exe, prot_p) print cmdline result = Popen(cmdline, shell=True, stdout=PIPE).stdout for line in result.readlines(): # print line if str(line).find("Finished.") != -1: ###print self.name + ' Finished :' + prot_p return 1
def stanfordTag(text): text = text.replace('\n','. ') text = text.encode('ascii','ignore') data=open(directory+'data.txt','w') print(text,end='',file=data) data.close() file=Popen("cd "+STANFORD_PATH+"; ./stanford-postagger.sh models/wsj-0-18-bidirectional-nodistsim.tagger "+directory+"data.txt", shell=True, stdout=PIPE).stdout sentences=file.readlines() postagged = [[tuple(word.split('_')) for word in sentence] for sentence in [sent.split(' ') for sent in sentences]] return postagged
def notifier_loop_callback(self, notifier): if len(self.file_events) > 0: for event in self.file_events: self.multi_queue.put(event) self.mmc.touch_index_file() self.file_events = [] #yield to worker thread time.sleep(0) #use items() because we are going to be modifying this #dictionary while iterating over it. for k, pair in self.cookies_IN_MOVED_FROM.items(): event = pair[0] timestamp = pair[1] timestamp_now = time.time() if timestamp_now - timestamp > 5: #in_moved_from event didn't have a corresponding #in_moved_to event in the last 5 seconds. #This means the file was moved to outside of the #watched directories. Let's handle this by deleting #it from the Airtime directory. del self.cookies_IN_MOVED_FROM[k] self.handle_removed_file(False, event.pathname) # we don't want create_dict grow infinitely # this part is like a garbage collector for k, t in self.create_dict.items(): now = time.time() if now - t > 5: # check if file exist # When whole directory is copied to the organized dir, # inotify doesn't fire IN_CLOSE_WRITE, hench we need special way of # handling those cases. We are manully calling handle_created_file # function. if os.path.exists(k): # check if file is open try: command = "lsof "+k #f = os.popen(command) f = Popen(command, shell=True, stdout=PIPE).stdout except Exception, e: self.logger.error('Exception: %s', e) self.logger.error("traceback: %s", traceback.format_exc()) continue if not f.readlines(): self.logger.info("Handling file: %s", k) self.handle_created_file(False, k, os.path.basename(k)) del self.create_dict[k] else: del self.create_dict[k]
def scanEntries(self): assert self.isArchive() attrs = self.attrs attrs = attrs.dirAttrs attrs.thisCount = 0 attrs.thisSize = 0 attrs.totalCount = 0 attrs.totalSize = 0 pipe = None path = None skip = False try: pipe = Popen("hdiutil imageinfo \"%s\"" % self.path, shell=True, stdout=PIPE).stdout for line in pipe.readlines(): if re.search("Software License Agreement: true", line) or \ (not opts.openEncryptedImages and re.search("Encrypted: true", line)): skip = True pipe.close() return if not skip: pipe = Popen( "hdiutil attach \"%s\" -readonly %s" % (self.path, "-mountrandom /tmp -noverify -noautofsck"), shell=True, stdout=PIPE).stdout for line in pipe.readlines(): match = re.search("(/tmp/.+)", line) if match: path = match.group(1) break except Exception, msg: print "Failed to index %s:" % self.path, msg
def fix_offsets(): libc_printf_offset_cmd = Popen("gdb -batch -x offsets/libc_printf.gdb libc.so.6", shell=True, stdout=PIPE, stderr=STDOUT, bufsize=1024).stdout libc_printf_offset_stdout = libc_printf_offset_cmd.readlines() libc_printf_offset = re.match("\$1 = {<text variable, no debug info>} (.*) <printf>\\n", libc_printf_offset_stdout[0]).group(1) vuln_printf_offset_cmd = Popen("gdb -batch -x offsets/vuln_printf.gdb vuln", shell=True, stdout=PIPE, stderr=STDOUT, bufsize=1024).stdout vuln_printf_offset_stdout = vuln_printf_offset_cmd.readlines() vuln_printf_offset = re.match("\$1 = {<text variable, no debug info>} (.*) <printf>\\n", vuln_printf_offset_stdout[3]).group(1) readelf_vuln_bss_cmd = Popen("readelf -S vuln | grep bss", shell=True, stdout=PIPE, stderr=STDOUT, bufsize=1024).stdout readelf_vuln_bss_stdout = readelf_vuln_bss_cmd.readlines() readelf_vuln_bss_addr = readelf_vuln_bss_stdout[0].split(' ')[27] return int(vuln_printf_offset, 16) - int(libc_printf_offset, 16), int(readelf_vuln_bss_addr, 16)
def check_dmesg_for_ooops(): ''' check for ooops in dmesg output ''' oops_messages = ['BUG: unable to handle kernel paging request at', 'general protection fault'] cmd_ = Popen('dmesg', shell=True, stdout=PIPE, close_fds=True).stdout for line in cmd_.readlines(): if hasattr(line, 'decode'): line = line.decode() if any(mes in line for mes in oops_messages): return True return False
def main(name): socket = generate_map(name)['networks'] while True: pipe = Popen(['iwlist', 'wlan0', 'scan'], stdout = PIPE).stdout cells = parse_cells(pipe.readlines()) for cell in cells: pair = [cell['Address'] + '_' + cell['Name'], int(cell['Signal'][0:-3])] print pair socket.send(dumps(pair)) print sleep(0.5)
def run_one(self, c, g): cmdstr = 'ssh -x {0} "cd {1}; {2} -c {3} -g {4} -v {5} {6} {7}"' # cmdstr = "ssh -x -t -t -L8081:localhost:10000 \ # cluster \'ssh -x -L10000:localhost:80 \ # {0} \"cd {1}; {2} -c {3} -g {4} -v {5} {6} {7}\"\'" cmdline = cmdstr.format(self.host, self.cwd, svmtrain_exe, c, g, fold, pass_through_string, dataset_pathname) result = Popen(cmdline, shell=True, stdout=PIPE).stdout for line in result.readlines(): if str(line).find("Cross") != -1: return float(line.split()[-1][0:-1])
def run_one(self, c, g): cmdline = 'ssh -x -t -t {0} "cd {1}; {2}"'.format\ (self.host,self.cwd,self.get_cmd(c,g)) result = Popen(cmdline, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE).stdout for line in result.readlines(): if str(line).find('Cross') != -1: return float(line.split()[-1][0:-1])
def run_one(self, c, g): cmdline = self.get_cmd(c, g) print cmdline, "cmdline" # cmdline: /home/pio/yast/yast/learner/learner_impl.py -c 1.0 -v 5 -s 4 sample_2_train.svm result = Popen(cmdline, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE).stdout for line in result.readlines(): if str(line).find('Cross') != -1: return float(line.split()[-1][0:-1])
def call_svn_cmd(path, cmd, opts): """string, string, string -> string Call a svn command and return an xml string """ stringlist = [] cli_out = Popen("svn %s %s '%s' 2> /dev/null" % (cmd, opts, path), shell=True, stdout=PIPE, close_fds=True).stdout try: stringlist = cli_out.readlines() xml_string = "".join(stringlist) return xml_string except: return None
def check_dmesg_for_ooops(): ''' check for ooops in dmesg output ''' oops_messages = [ 'BUG: unable to handle kernel paging request at', 'general protection fault' ] cmd_ = Popen('dmesg', shell=True, stdout=PIPE, close_fds=True).stdout for line in cmd_.readlines(): if hasattr(line, 'decode'): line = line.decode() if any(mes in line for mes in oops_messages): return True return False
def main(name): socket = generate_map(name)['networks'] while True: pipe = Popen(['iwlist', 'wlan0', 'scan'], stdout = PIPE).stdout cells = parse_cells(pipe.readlines()) for cell in cells: try: sig = int(cell['Signal'][0:-3]) except: sig = int(cell['Signal'][0:-4]) pair = [cell['Address'] + '_' + cell['Name'], sig] socket.send(dumps(pair)) sleep(1.0)
def exeComm(cmd, std): try: retline = [] cmdList = cmd.split() if std == 1: ret = Popen(cmdList, stdout=PIPE, stderr=open("/dev/null")).stdout elif std == 2: ret = Popen(cmdList, stderr=PIPE, stdout=open("/dev/null")).stderr for x in ret.readlines(): retline.append(x.strip('\t').strip('\n')) return retline except IOError as Ierr: sys.stderr.write("%s not found : " % cmd + str(Ierr) + "\n")
def exeComm(cmd, std): try: retline = [] cmdList = cmd.split() if std == 1: ret = Popen(cmdList, stdout=PIPE, stderr=open("/dev/null")).stdout elif std == 2: ret = Popen(cmdList, stderr=PIPE, stdout=open("/dev/null")).stderr for x in ret.readlines(): retline.append(x.strip("\t").strip("\n")) return retline except IOError as Ierr: sys.stderr.write("%s not found : " % cmd + str(Ierr) + "\n")
def gethosts(opts,parser): hosts=[] if opts.file is not None: f=open(opts.file,'r') hosts=f.readlines() else: if opts.group is not None: f=Popen(('opsfree','-l','-g',opts.group),stdout=PIPE).stdout hosts=f.readlines() else: parser.error("Please input hosts. -f or -g options") sys.exit() return hosts
def gethosts(opts, parser): hosts = [] if opts.file is not None: f = open(opts.file, 'r') hosts = f.readlines() else: if opts.group is not None: f = Popen(('opsfree', '-l', '-g', opts.group), stdout=PIPE).stdout hosts = f.readlines() else: parser.error("Please input hosts. -f or -g options") sys.exit() return hosts
def scanEntries(self): assert self.isArchive() attrs = self.attrs attrs = attrs.dirAttrs attrs.thisCount = 0 attrs.thisSize = 0 attrs.totalCount = 0 attrs.totalSize = 0 pipe = None path = None skip = False try: pipe = Popen("hdiutil imageinfo \"%s\"" % self.path, shell = True, stdout = PIPE).stdout for line in pipe.readlines(): if re.search("Software License Agreement: true", line) or \ (not opts.openEncryptedImages and re.search("Encrypted: true", line)): skip = True pipe.close() return if not skip: pipe = Popen("hdiutil attach \"%s\" -readonly %s" % (self.path, "-mountrandom /tmp -noverify -noautofsck"), shell = True, stdout = PIPE).stdout for line in pipe.readlines(): match = re.search("(/tmp/.+)", line) if match: path = match.group(1) break except Exception, msg: print "Failed to index %s:" % self.path, msg
def getZpoolDevs(ml=None, zl=None): mpdevs = [] zpools = [] if not ml or not zl: ml = Popen(['/usr/sbin/mpathadm', 'list', 'LU'], stdout=PIPE).stdout zl = Popen(['/usr/sbin/zpool', 'status'], env={ 'LC_ALL': 'C' }, stdout=PIPE).stdout mpdevs = [(line.strip()) for line in ml.readlines() if 'rdsk' in line] lines = zl.readlines() iter_lines = iter(lines) devpat = compile('(/dev/(r)?dsk/)?(c.*d0)(s[0-9])?') for line in iter_lines: if 'pool:' in line: zd = {} poolname = line.split()[1] zd[poolname] = [] for line in iter_lines: if len(line.split()) > 4: if line.split()[0] in ('errors:'): break if line.split()[0] in (poolname, 'mirror-0', 'NAME', 'scan:'): continue if match(devpat, line.split()[0]): for d in mpdevs: if match(devpat, line.split()[0]).groups()[2] == match( devpat, d).groups()[2]: zd[poolname].append(d) break zpools.append(zd) return zpools
def main(name): socket = generate_map(name)['networks'] while True: pipe = Popen(['iwlist', 'wlan0', 'scan'], stdout=PIPE).stdout cells = parse_cells(pipe.readlines()) for cell in cells: pair = [ cell['Address'] + '_' + cell['Name'], int(cell['Signal'][0:-3]) ] print pair socket.send(dumps(pair)) print sleep(0.5)
def get_usb_instances(): "funtion to find a list of instance IDs corresponding to USB port" # Not currently used cmd = "devcon.exe find usb*" s = Popen(cmd, shell=True, stdout=PIPE).stdout lines = s.readlines() for l in lines: line = l.decode() m = re.search('(USB[0-9a-zA-Z\&\\\_]+).+USB Serial Converter A', line) if m: instance = m.group(1) instances.append(instance) print(instance)
def run_quota(mp, fgroups): """Run the quota command and parse output. Checks both user and group quotas. Ignores file limits, just number of bytes. """ from subprocess import Popen, PIPE devnull = open(os.devnull, 'w') p = Popen(['/usr/bin/quota', '-ugwp'], stdout=PIPE, stderr=devnull).stdout fs = "" myquota = [] bs = 1024 # BLOCK_SIZE from sys/mount.h for line in p.readlines(): if line.find("Disk quotas for") != -1: eind = line.find(' (') ls = line[:eind].split()[3:] ug = ls[0] # 'user' or 'group' usrgrpname = " ".join(ls[1:]) # Name of user/group curlist = {} myquota.append((ug, usrgrpname, curlist)) elif line.find(" Filesystem blocks quota limit") == -1: ls = line.split() # If over block quota, there will be a '*', remove it qb = int(ls[1].replace('*', '')) * bs curlist[map_fs(ls[0], mp)[0]] = (qb, int(ls[2]) * bs, \ int(ls[3]) * bs, int(ls[4])) p.close() devnull.close() done_mp = set() for q in myquota: for e in q[2]: done_mp.add(e) q_to_del = [] myuid = os.geteuid() for q in myquota: if myuid != 0 and q[0] == 'group' and q[1] in fgroups: q_to_del.append(q) for q in q_to_del: myquota.remove(q) for q in myquota: mp_to_del = [] for e in q[2]: if not os.access(e, os.R_OK): mp_to_del.append(e) for e in mp_to_del: del q[2][e] print_quota(myquota) return done_mp
def run_quota(mp, fgroups): """Run the quota command and parse output. Checks both user and group quotas. Ignores file limits, just number of bytes. """ from subprocess import Popen, PIPE devnull = open(os.devnull, "w") p = Popen(["/usr/bin/quota", "-ugwp"], stdout=PIPE, stderr=devnull).stdout fs = "" myquota = [] bs = 1024 # BLOCK_SIZE from sys/mount.h for line in p.readlines(): if line.find("Disk quotas for") != -1: eind = line.find(" (") ls = line[:eind].split()[3:] ug = ls[0] # 'user' or 'group' usrgrpname = " ".join(ls[1:]) # Name of user/group curlist = {} myquota.append((ug, usrgrpname, curlist)) elif line.find(" Filesystem blocks quota limit") == -1: ls = line.split() # If over block quota, there will be a '*', remove it qb = int(ls[1].replace("*", "")) * bs curlist[map_fs(ls[0], mp)[0]] = (qb, int(ls[2]) * bs, int(ls[3]) * bs, int(ls[4])) p.close() devnull.close() done_mp = set() for q in myquota: for e in q[2]: done_mp.add(e) q_to_del = [] myuid = os.geteuid() for q in myquota: if myuid != 0 and q[0] == "group" and q[1] in fgroups: q_to_del.append(q) for q in q_to_del: myquota.remove(q) for q in myquota: mp_to_del = [] for e in q[2]: if not os.access(e, os.R_OK): mp_to_del.append(e) for e in mp_to_del: del q[2][e] print_quota(myquota) return done_mp
def main(remove=False): pipe = Popen('bzr ls --ignored', shell=True, stdout=PIPE).stdout print 'rm *.pyc' for line in pipe.readlines(): file = line.strip() if os.path.isfile(file): if not file.endswith('.pyc'): print 'rm %s' % file if remove: os.remove(file) elif os.path.isdir(file): print 'rmtree %s' % file if remove: shutil.rmtree(file) pipe.close()
def call_list(cmd, **keyargs): """ Executes a command and return its stdout as a list. """ keyargs.update({ 'shell' : True, 'stdout' : PIPE }) o = Popen(cmd, **keyargs).stdout if ( o != None ): retval = o.readlines() else: retval = [] retval = map(lambda line:line.strip(), retval) return retval
def get_scores(motif, file): from subprocess import Popen, PIPE from tempfile import NamedTemporaryFile pwm = NamedTemporaryFile() pwm.write(motif.to_pwm()) pwm.flush() cmd = "pwmscan.py -i %s -p %s -c 0.0" % (file, pwm.name) out = Popen(cmd, shell=True, stdout=PIPE).stdout vals = [] for line in out.readlines(): vals.append(float(line.split("\t")[5])) return vals
def main(name): socket = generate_map(name)['networks'] while True: try: pipe = Popen(['iwlist', 'wlan0', 'scan'], stdout=PIPE).stdout cells = parse_cells(pipe.readlines()) for cell in cells: try: sig = int(cell['Signal'][0:-3]) except: sig = int(cell['Signal'][0:-4]) pair = [cell['Address'] + '_' + cell['Name'], sig] socket.send(dumps(pair)) sleep(1.0) except: pass
def load(self, filt=""): self.filter=filt self.store.clear() try: fd = Popen("semodule -l", shell=True, stdout=PIPE).stdout l = fd.readlines() fd.close() for i in l: module, ver, newline = i.split('\t') if not (self.match(module, filt) or self.match(ver, filt)): continue it = self.store.append() self.store.set_value(it, 0, module.strip()) self.store.set_value(it, 1, ver.strip()) except: pass self.view.get_selection().select_path ((0,))
def installGBUpdate(): upgb = Popen("sudo operator cat " + gbupdatelist, shell=True, stdout=PIPE, close_fds=True) for ports in upgb.stdout.readlines(): nprtlist = ports.rstrip().split() nprtsname = nprtlist[0] nprtversion = nprtlist[1] pkgquery = Popen("sudo operator pkg query '%n %v'| grep " + nprtsname, shell=True, stdout=PIPE, close_fds=True) pkglist = pkgquery.readlines()[0].rstrip().split() prtversion = pkglist[1] if float(prtversion) < float(nprtversion): findport = "find /usr/ports/ -name " + nprtsname portdir = Popen(findport, shell=True, stdout=PIPE, close_fds=True) chdir(portdir.stdout.readlines()[0].rstrip()) call("sudo operator make reinstall clean", shell=True, stdout=PIPE, close_fds=True)
def load(self, filter=""): self.filter = filter self.store.clear() try: fd = Popen("semodule -lfull", shell=True, stdout=PIPE).stdout l = fd.readlines() fd.close() for i in l: priority, module, kind = i.decode('utf-8').split() if not (self.match(module, filter) or self.match(priority, filter)): continue iter = self.store.append() self.store.set_value(iter, 0, module.strip()) self.store.set_value(iter, 1, priority.strip()) self.store.set_value(iter, 2, kind.strip()) except: pass self.view.get_selection().select_path((0,))
def addItem(self): """Add Items from Locate command.""" start_time = datetime.now().second self.stringlist.clear() lineText = self.lineEdit.text() if len(lineText) and str(lineText).strip() not in self.history: self.history.append(lineText + "\n") self.historyCurrentItem = 1 self.saveHistory() self.historyCurrentItem = self.historyCurrentItem - 1 command = "ionice --ignore --class 3 chrt --idle 0 " # Nice CPU / IO command += "locate --ignore-case --existing --quiet --limit 9999 {}" condition = str(self.applet.configurations.readEntry("Home")) == "true" if len(str(lineText).strip()) and condition: command_to_run = command.format( # Only Search inside Home folders path.join(path.expanduser("~"), "*{}*".format(lineText))) else: command_to_run = command.format(lineText) locate_output = Popen(command_to_run, shell=True, stdout=PIPE).stdout results = tuple(locate_output.readlines()) banned = self.applet.configurations.readEntry("Banned") banned_regex_pattern = str(banned).strip().lower().replace(" ", "|") for item in results: if not search(banned_regex_pattern, str(item)): # banned words self.stringlist.append(item[:-1]) purge() # Purge RegEX Cache self.model.setStringList(self.stringlist) self.treeview.nativeWidget().resizeColumnToContents(0) number_of_results = len(results) if number_of_results: # if tems found Focus on item list self.lineEdit.nativeWidget().clear() self.label.setText("Found {} results on {} seconds !".format( number_of_results, abs(datetime.now().second - start_time))) self.resize(500, 12 * number_of_results) self.treeview.nativeWidget().show() self.treeview.nativeWidget().setFocus() else: # if no items found Focus on LineEdit self.label.setText("Search") self.resize(self.minimumSize()) self.treeview.nativeWidget().hide() self.lineEdit.nativeWidget().selectAll() self.lineEdit.nativeWidget().setFocus()
def get_length_of_mpg(fname='%s/netflix/mpg/test_roku_0.mpg' % HOMEDIR): ''' get length of mpg/avi/mp4 with avconv ''' if not os.path.exists(fname): return -1 command = 'avconv -i %s 2>&1' % fname cmd_ = Popen(command, shell=True, stdout=PIPE, close_fds=True).stdout nsecs = 0 for line in cmd_.readlines(): if hasattr(line, 'decode'): line = line.decode() _line = line.split() if _line[0] == 'Duration:': items = _line[1].strip(',').split(':') try: nhour = int(items[0]) nmin = int(items[1]) nsecs = int(float(items[2])) + nmin*60 + nhour*60*60 except ValueError: nsecs = -1 return nsecs