def run(self): if "Running Processes" not in self.rawConfig: self.logger.warning("Could not find Running Processes section in config") return {} if "process_list" not in self.rawConfig["Running Processes"]: self.logger.warning("Could not find list of processes to watch") return {} processes = [] processNames = self.rawConfig["Running Processes"]["process_list"] for processName in processNames.split(","): p = Popen(["pgrep", processName], stderr=PIPE) p.name = processName processes.append(p) Pool(len(processes)).map(self.extractResult, processes) return self.data
def call_compare_using_files(args): """Creates shell command to compare two files using compare_using_files.py script and calls it.""" sample, ref_filename, test_filename, options = args blacklists = guess_blacklists([sample], name2version(ref_filename), name2version(test_filename), options.hlt) command = " compare_using_files.py " command += "%s %s " % (ref_filename, test_filename) command += " -C -R " if options.do_pngs: command += " -p " command += " -o %s " % sample # Change threshold to an experimental and empirical value of 10^-5 command += " --specify_run " command += " -t %s " % options.test_threshold command += " -s %s " % options.stat_test # Inspect the HLT directories if options.hlt: command += " -d HLT " if len(blacklists[sample]) > 0: command += '-B %s ' % blacklists[sample] print "\nExecuting -- %s" % command process = Popen(filter(lambda x: len(x) > 0, command.split(" "))) process.name = sample
def cmd(command): stream = False timeout = 60 output = [] try: process = Popen(shlex.split(command), stdout=PIPE, stderr=STDOUT, stdin=None) process.name = command # start the timer if we specified a timeout if timeout: proctimer = threading.Timer(timeout, _timeout, (process,)) proctimer.start() for line in iter(process.stdout.readline, ''): if stream: print(line.rstrip('\n')) output.append(line.rstrip('\n')) # when finished, get the exit code returncode = process.wait() except OSError as e: output.append(e.strerror.rstrip('\n')) returncode = e.errno except (KeyboardInterrupt, SystemExit): # need to clean up the timing thread here if timeout: proctimer.cancel() raise else: # Stop the timer if timeout: proctimer.cancel() if returncode == -9: output.append("Command '%s' timed out (longer than %ss)" % (command, timeout)) return returncode, '\n'.join(output)
def connect(self): cmd = ['ssh'] cmd.extend(self._flags) cmd.extend([self._host, "-p", self._port, "-l", self._user]) conn = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True) conn.name = self._name conn.agent = self self.connection = conn
def exec(self): if self.is_windows: # TODO: Call scripts one after another terminal_cmd = """matlab -nodesktop -nosplash -r " """ q_cmd = """ cd('{}'),disp(pwd),input('press any key to run...','s'),{},""" # The for loop should be made, now it is wrong, the code after is OK for script_file in self.script_files: # Note that .m should be removed from the filename to run terminal_cmd += q_cmd.format( os.path.dirname(script_file), os.path.basename(script_file[:-2])) self.log_trigger.emit("Exec: {}".format( os.path.relpath(script_file, self._root))) terminal_cmd.rstrip(',') # remove trailing , terminal_cmd += "\"" t = epoch_time() p = Popen(shlex.split(terminal_cmd)) # In some computers p.wait() p.terminate() does not do anything # in other systems, it blocks the program. #p.wait() #p.terminate() # Note that this does not close MATLAB # Use psutil to retrieve all the processes with MATLAB matlab_proc = list( filter(lambda p: "matlab" in p.name().lower(), psutil.process_iter())) # Sometimes more than one matlab process exist if len(matlab_proc) > 1: diff_time = [abs(p.create_time() - t) for p in matlab_proc] # Find the process created almost the same time as t, # Hopefully this is correct always ind_min = min(enumerate(diff_time), key=itemgetter(1))[0] self._processes.append(matlab_proc[ind_min]) elif len(matlab_proc) > 0: # In fact should be 1 self._processes.append(matlab_proc[0]) elif self.is_linux: # Here the scripts should be run terminal_cmd = "gnome-terminal --disable-factory " tab_cmd = "--tab --working-directory='{}' -e " + \ """'bash -c "matlab -nodesktop -nosplash -r "{}";""" + \ """exec bash" ' """ cmd = terminal_cmd for script_file in self.script_files: # script_file is absolute path # Note that .m should be removed from the command of matlab matlab_cmd = script_file[:-2] cmd += tab_cmd.format(os.path.dirname(script_file), os.path.basename(matlab_cmd)) rel_script_path = os.path.relpath(script_file, self._root) self.log_trigger.emit("Exec: {}".format(rel_script_path)) p = Popen(shlex.split(cmd), shell=False, start_new_session=True) self._processes.append(p)
def run(self): if 'Running Processes' not in self.rawConfig: self.logger.warning( 'Could not find Running Processes section in config') return {} if 'process_list' not in self.rawConfig['Running Processes']: self.logger.warning('Could not find list of processes to watch') return {} processes = [] processNames = self.rawConfig['Running Processes']['process_list'] for processName in processNames.split(','): p = Popen(['pgrep', processName], stderr=PIPE) p.name = processName processes.append(p) Pool(len(processes)).map(self.extractResult, processes) return self.data
def _run(command, env=os.environ, timeout=None, log=False, **kwargs): """Given a command, run it. Return a tuple of the return code and any output. :param timeout: If specified, the command will be terminated after timeout seconds. :param log: If True, the _log will be handled by _run. If set, it is mandatory to pass at least a :service: and a :component: parameter. Optionally you can pass :cluster:, :instance: and :loglevel: parameters for logging. We wanted to use plumbum instead of rolling our own thing with subprocess.Popen but were blocked by https://github.com/tomerfiliba/plumbum/issues/162 and our local BASH_FUNC magic. """ output = [] if log: service = kwargs['service'] component = kwargs['component'] cluster = kwargs.get('cluster', ANY_CLUSTER) instance = kwargs.get('instance', ANY_INSTANCE) loglevel = kwargs.get('loglevel', DEFAULT_LOGLEVEL) try: process = Popen(shlex.split(command), stdout=PIPE, stderr=STDOUT, env=env) process.name = command # start the timer if we specified a timeout if timeout: proctimer = threading.Timer(timeout, _timeout, (process, )) proctimer.start() for line in iter(process.stdout.readline, ''): if log: _log( service=service, line=line.rstrip('\n'), component=component, level=loglevel, cluster=cluster, instance=instance, ) output.append(line.rstrip('\n')) # when finished, get the exit code returncode = process.wait() except OSError as e: if log: _log( service=service, line=e.strerror.rstrip('\n'), component=component, level=loglevel, cluster=cluster, instance=instance, ) output.append(e.strerror.rstrip('\n')) returncode = e.errno except (KeyboardInterrupt, SystemExit): # need to clean up the timing thread here if timeout: proctimer.cancel() raise # Stop the timer if timeout: proctimer.cancel() if returncode == -9: output.append("Command '%s' timed out (longer than %ss)" % (command, timeout)) return returncode, '\n'.join(output)
def _run(command, env=os.environ, timeout=None, log=False, stream=False, stdin=None, **kwargs): """Given a command, run it. Return a tuple of the return code and any output. :param timeout: If specified, the command will be terminated after timeout seconds. :param log: If True, the _log will be handled by _run. If set, it is mandatory to pass at least a :service: and a :component: parameter. Optionally you can pass :cluster:, :instance: and :loglevel: parameters for logging. We wanted to use plumbum instead of rolling our own thing with subprocess.Popen but were blocked by https://github.com/tomerfiliba/plumbum/issues/162 and our local BASH_FUNC magic. """ output = [] if log: service = kwargs['service'] component = kwargs['component'] cluster = kwargs.get('cluster', ANY_CLUSTER) instance = kwargs.get('instance', ANY_INSTANCE) loglevel = kwargs.get('loglevel', DEFAULT_LOGLEVEL) try: process = Popen(shlex.split(command), stdout=PIPE, stderr=STDOUT, stdin=stdin, env=env) process.name = command # start the timer if we specified a timeout if timeout: proctimer = threading.Timer(timeout, _timeout, (process,)) proctimer.start() for line in iter(process.stdout.readline, ''): if stream: print(line.rstrip('\n')) if log: _log( service=service, line=line.rstrip('\n'), component=component, level=loglevel, cluster=cluster, instance=instance, ) output.append(line.rstrip('\n')) # when finished, get the exit code returncode = process.wait() except OSError as e: if log: _log( service=service, line=e.strerror.rstrip('\n'), component=component, level=loglevel, cluster=cluster, instance=instance, ) output.append(e.strerror.rstrip('\n')) returncode = e.errno except (KeyboardInterrupt, SystemExit): # need to clean up the timing thread here if timeout: proctimer.cancel() raise # Stop the timer if timeout: proctimer.cancel() if returncode == -9: output.append("Command '%s' timed out (longer than %ss)" % (command, timeout)) return returncode, '\n'.join(output)
def connect(self): conn = Popen(["/bin/sh"], stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True) conn.name = self._name conn.agent = self self.connection = conn