def remove_repository(self, __name): # ---------- # Get locations # --------------- syslink_location = "{}/{}".format(self.__syslink_folder, __name.lower()) repository_location = "{}/{}".format(self.__installation_folder, __name) # ---------- # Get commands # --------------- rm_syslink = "rm -f {}".format(syslink_location) rm_repo = "rm -rf {}".format(repository_location) # ---------- # Remove Syslink # --------------- try: self.qprint("{}{}[*] Removing syslink...{}".format( color.BOLD, color.OKBLUE, color.ENDC)) subprocess.check_output(shsplit(rm_syslink)) except subprocess.CalledProcessError: self.qprint("{}{}[*] Cannot remove {} syslink{}".format( color.BOLD, color.FAIL, syslink_location, color.ENDC)) # ---------- # Remove Repository # --------------- try: self.qprint("{}{}[*] Removing repository...{}".format( color.BOLD, color.OKBLUE, color.ENDC)) subprocess.check_output(shsplit(rm_repo)) except subprocess.CalledProcessError: self.qprint("{}{}[*] Cannot remove {} repository{}".format( color.BOLD, color.FAIL, repository_location, color.ENDC))
def configure_init_script(self): # ---------- # Set commands # --------------- chm = "chmod +x {}".format(self.__initscript) chw = "chown {} {}".format(self.__info.get_username(), self.__initscript) # ---------- # Touch the file # --------------- open(self.__initscript, "w").close() # ---------- # Handle exceptions # --------------- try: with open(self.__info.get_devnull(), "w") as FNULL: self.qprint("{}{}[*] Touching {} script...{}".format( color.BOLD, color.OKBLUE, self.__initscript, color.ENDC)) # ---------- # Executing commands # --------------- subprocess.check_output(shsplit(chm), stderr=FNULL) subprocess.check_output(shsplit(chw), stderr=FNULL) except subprocess.CalledProcessError: self.qprint("{}{}[!] Cannot touch {} script{}".format( color.BOLD, color.FAIL, self.__initscript, color.ENDC))
def ms_to_hscan(msout, focalpos, local=False): '''Use ms output to run H-scan. msout: ms output (e.g. from subpop ms) focalpos: Focal position of SNP on 1mb chrom. NOTE: Should multiply ms position by 1 million and round to nearest integer. ''' # Write msout to temporary file temp = os.getcwd()+'/temp.'+str(randint(1e8, 999999999))+'.txt' f = open(temp,'w') f.write(msout) f.close() # Run H-scan and collect output hformat = '/'.join(os.getcwd().split('/')[:-1])+'/bin/H-scan ' if local: hformat = 'H-scan' args1 = shsplit(hformat+' -m -i '+temp+' -l '+str(focalpos)+' -r '+str(focalpos)) args2 = shsplit('tail -n1') p1 = Popen(args1, stdout=PIPE, stderr=PIPE) p2 = Popen(args2, stdin = p1.stdout, stdout=PIPE, stderr=PIPE) p1.stdout.close() p1.stderr.close() #This stderr from H-scan hout, herr = p2.communicate() os.remove(temp) return hout.split('\t'), herr #(pos, H), error
def ms_to_hscan(msout, focalpos, local=False): '''Use ms output to run H-scan. msout: ms output (e.g. from subpop ms) focalpos: Focal position of SNP on 1mb chrom. NOTE: Should multiply ms position by 1 million and round to nearest integer. ''' # Write msout to temporary file temp = os.getcwd() + '/temp.' + str(randint(1e8, 999999999)) + '.txt' f = open(temp, 'w') f.write(msout) f.close() # Run H-scan and collect output hformat = '/'.join(os.getcwd().split('/')[:-1]) + '/bin/H-scan ' if local: hformat = 'H-scan' args1 = shsplit(hformat + ' -m -i ' + temp + ' -l ' + str(focalpos) + ' -r ' + str(focalpos)) args2 = shsplit('tail -n1') p1 = Popen(args1, stdout=PIPE, stderr=PIPE) p2 = Popen(args2, stdin=p1.stdout, stdout=PIPE, stderr=PIPE) p1.stdout.close() p1.stderr.close() #This stderr from H-scan hout, herr = p2.communicate() os.remove(temp) return hout.split('\t'), herr #(pos, H), error
def _filter_magics(self, code): magics = { 'fcflags': [], 'ldflags': [], 'module': [], 'args': [], 'compiler': ['gfortran', '.f90'], 'fig': False, 'fig_arg': [], 'image': [], } for line in code.splitlines(): if line.strip().startswith('%'): key, value = line.strip().strip('%').split(":", 1) key = key.lower() if key in ['ldflags', 'fcflags', 'args']: magics[key] = shsplit(value) elif key in ['module']: magics[key] = shsplit(value) magics['ldflags'].append('-c') elif key in ['compiler']: for i, item in enumerate(shsplit(value)): magics[key][i] = item elif key in ['fig']: magics[key] = True magics['fig_arg'] = value elif key in ['image']: magics[key] = shsplit(value) else: pass # need to add exception handling return magics
def clearImages(self, camera_buffer, camera_path, camera): # If the user specified the need to remove images post-processing # then clear the image folder from images in the buffer. if self.config.clear_images: clean_files = [] for buffer_entry in camera_buffer[camera['uuid']]: clean_files.append(os.path.join(camera_path, buffer_entry)) print("<> INFO: Deleting {}".format(clean_files)) call(shsplit('rm -f') + shsplit(' '.join(clean_files)))
def get_terminal_size_tput(): try: from subprocess import check_call from shlex import split as shsplit x = int(check_call(shsplit('tput cols'))) y = int(check_call(shsplit('tput lines'))) return (x, y) except: pass return None
def fromLine(cls, commandStr, flagsStr): commandParts = shsplit(commandStr) flags = shsplit(flagsStr) env = {} while commandParts: if "=" in commandParts[0]: name, value = commandParts[0].split("=", 1) del commandParts[0] env[name] = value else: return cls(env, commandParts[0], list(fixArgs(commandParts[1:] + flags))) else: raise ValueError('No command specified in "%s"' % commandStr)
def fromLine(cls, commandStr, flagsStr): commandParts = shsplit(commandStr) flags = shsplit(flagsStr) env = {} while commandParts: if '=' in commandParts[0]: name, value = commandParts[0].split('=', 1) del commandParts[0] env[name] = value else: return cls(env, commandParts[0], list(fixArgs(commandParts[1:] + flags))) else: raise ValueError('No command specified in "%s"' % commandStr)
def __init__(self, sfs_path, csv_file): os.chdir(sfs_path) self.ud = Factory(csv_file) self.sfs = subprocess.Popen(shsplit("java -jar sfs.jar")) os.chdir("..") tries = 0 success = False while tries < self.MAX_TRIES: try: xdt = subprocess.run( shsplit( 'xdotool search --onlyvisible --name "{}"'.format( "Shop Floor Simulator" ) ), stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) if xdt.stdout is None: tries += 1 else: win = int(xdt.stdout) # print(win) xdt = subprocess.run( shsplit("xdotool windowmove {} 1500 300".format(win)), stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) success = True break except Exception: tries += 1 # print(e) if not success: print("Could not reposition SFS window!") else: print("SFS window repositioned!") self.mb_client = ModbusTcpClient("127.0.0.1", 5502) if self.mb_client.connect(): print("Connected to server!") else: print("Could not connect to server!")
def captureStdout(log, commandLine): '''Run a command and capture what it writes to stdout. If the command fails or writes something to stderr, that is logged. Returns the captured string, or None if the command failed. ''' # TODO: This is a modified copy-paste from compilers._Command. commandParts = shsplit(commandLine) env = dict(environ) while commandParts: if '=' in commandParts[0]: name, value = commandParts[0].split('=', 1) del commandParts[0] env[name] = value else: break else: raise ValueError( 'No command specified in "%s"' % commandLine ) if msysActive() and commandParts[0] != 'sh': commandParts = [ msysShell(), '-c', shjoin(commandParts) ] try: proc = Popen( commandParts, bufsize = -1, env = env, stdin = None, stdout = PIPE, stderr = PIPE, ) except OSError, ex: print >> log, 'Failed to execute "%s": %s' % (commandLine, ex) return None
def run(command): """ Execute a command safely and returns its results. Returns a namedtuple with the following attributes: - ``stdout``: Standard output of the command as UTF-8. - ``stderr``: Standard error of the command as UTF-8. - ``returncode``: Return code of the command. """ if isinstance(command, str): command = shsplit(command) try: process = Popen(command, stdin=DEVNULL, stderr=PIPE, stdout=PIPE, env={ 'LC_ALL': 'C', 'LANG': 'C' }) stdout, stderr = process.communicate() except OSError: # E.g. command not found log.debug(format_exc()) return None return CommandResult(stdout=stdout.decode('utf-8').strip(), stderr=stderr.decode('utf-8').strip(), returncode=process.returncode)
def __init__(self, cmd, argmap={}, kwargmap={}, fatalError=False): ''' Initialize the HabisRemoteCommand instance with the provided string command. Optional argmap and kwargmap should be dictionaries whose keys distinguish different option sets. Each value in kwargmap should be a kwargs dictionary. Each value in argmap should either be a list of positional arguments or else a single string; if the value is a string, it will be split into string arguments using shlex.split(). The boolean value fatalError is captured as self.fatalError for record keeping. ''' from shlex import split as shsplit if not isinstance(cmd, str): raise ValueError('Command must be a string') self.cmd = cmd self.fatalError = fatalError # Copy the kwargmap dictionaries self.kwargmap = kwargmap.copy() # Copy the argmap tuples or strings self.argmap = {} for key, args in argmap.items(): if isinstance(args, str): args = shsplit(args) self.argmap[key] = tuple(args)
def cmd_prefix(): """ Determine if the current user can run privileged commands and thus determines the command prefix to use. :rtype: str :return: The prefix to use for privileged commands. """ # Return cache if set if hasattr(cmd_prefix, 'prefix'): return cmd_prefix.prefix if getuid() == 0: # We better do not allow root raise RuntimeError( 'Please do not run as root. ' 'Please configure the ip command for root execution.' ) # cmd_prefix.prefix = '' # return cmd_prefix.prefix with open(devnull, 'w') as f: cmd = shsplit('sudo --non-interactive ip link show') if call(cmd, stdout=f, stderr=f) != 0: raise RuntimeError( 'Please configure the ip command for root execution.' ) cmd_prefix.prefix = 'sudo --non-interactive ' return cmd_prefix.prefix
def write_context_to_file(file_name, cmds): msg = None return_code = 0 o_file = open(file_name, 'w+') try: proc = Popen( args=shsplit(cmds), bufsize=8192, stdout=o_file, stderr=PIPE, close_fds=True, ) std_err_msg = proc.communicate()[1] assert proc.returncode == 0, std_err_msg print(file_name) #if proc.returncode != 0: # std_err_msg = proc.communicate()[1] except OSError as e: msg = """Subprocess execute failed Error message:{0:s} """.format(e.strerror) return_code = -1 except Exception as e: msg = "{0:s}Error code: {1:d}".format(e.message, proc.returncode) return_code = -1 finally: o_file.close() if return_code == 0: return 0 print(msg) sys.exit(-1)
def notify_post_build(self): """ Get notified that the post build stage of the topology build was reached. """ super(OpenvSwitchNode, self).notify_post_build() # FIXME: this is a workaround self._docker_exec( "sed -i -e 's/port = 9001/port = 127.0.0.1:9001/g' " "-e 's/nodaemon=true/nodaemon=false/g' " "/etc/supervisord.conf" ) self._supervisord = Popen(shsplit( 'docker exec {} supervisord'.format(self.container_id) )) # Wait for the configure-ovs script to exit by polling supervisorctl config_timeout = 100 i = 0 while i < config_timeout: config_status = self._docker_exec( 'supervisorctl status configure-ovs' ) if 'EXITED' not in config_status: sleep(0.1) else: break i += 1 if i == config_timeout: raise RuntimeError('configure-ovs did not exit!')
def notify_post_build(self): """ Get notified that the post build stage of the topology build was reached. """ super(OpenvSwitchNode, self).notify_post_build() # FIXME: this is a workaround self._docker_exec("sed -i -e 's/port = 9001/port = 127.0.0.1:9001/g' " "-e 's/nodaemon=true/nodaemon=false/g' " "/etc/supervisord.conf") self._supervisord = Popen( shsplit('docker exec {} supervisord'.format(self.container_id))) # Wait for the configure-ovs script to exit by polling supervisorctl config_timeout = 100 i = 0 while i < config_timeout: config_status = self._docker_exec( 'supervisorctl status configure-ovs') if 'EXITED' not in config_status: sleep(0.1) else: break i += 1 if i == config_timeout: raise RuntimeError('configure-ovs did not exit!')
def captureStdout(log, commandLine): '''Run a command and capture what it writes to stdout. If the command fails or writes something to stderr, that is logged. Returns the captured string, or None if the command failed. ''' # TODO: This is a modified copy-paste from compilers._Command. commandParts = shsplit(commandLine) env = dict(environ) while commandParts: if '=' in commandParts[0]: name, value = commandParts[0].split('=', 1) del commandParts[0] env[name] = value else: break else: raise ValueError('No command specified in "%s"' % commandLine) if msysActive() and commandParts[0] != 'sh': commandParts = [msysShell(), '-c', shjoin(commandParts)] try: proc = Popen( commandParts, bufsize=-1, env=env, stdin=None, stdout=PIPE, stderr=PIPE, ) except OSError, ex: print >> log, 'Failed to execute "%s": %s' % (commandLine, ex) return None
def send_sequences(self): # Send the sequence files by directory unique_dirs = set() for f in self.sequence_files: basedir, filename = split(f) unique_dirs.add(basedir) # Set the ASCP password to the one in the Qiita config, but remember # the old pass so that we can politely reset it old_ascp_pass = environ.get('ASPERA_SCP_PASS', '') environ['ASPERA_SCP_PASS'] = qiita_config.ebi_seq_xfer_pass for unique_dir in unique_dirs: # Get the list of FASTQ files to submit fastqs = glob(join(unique_dir, '*.fastq.gz')) ascp_command = 'ascp -QT -k2 -L- {0} {1}@{2}:/.'.format( ' '.join(fastqs), qiita_config.ebi_seq_xfer_user, qiita_config.ebi_seq_xfer_url) # Generate the command using shlex.split so that we don't have to # pass shell=True to subprocess.call ascp_command_parts = shsplit(ascp_command) # Don't leave the password lingering in the environment if there # is any error try: call(ascp_command_parts) finally: environ['ASPERA_SCP_PASS'] = old_ascp_pass
def cmd_prefix(): """ Determine if the current user can run privileged commands and thus determines the command prefix to use. :rtype: str :return: The prefix to use for privileged commands. """ # Return cache if set if hasattr(cmd_prefix, 'prefix'): return cmd_prefix.prefix if getuid() == 0: # We better do not allow root raise RuntimeError( 'Please do not run as root. ' 'Please configure the ip command for root execution.') # cmd_prefix.prefix = '' # return cmd_prefix.prefix with open(devnull, 'w') as f: cmd = shsplit('sudo --non-interactive ip link show') if call(cmd, stdout=f, stderr=f) != 0: raise RuntimeError( 'Please configure the ip command for root execution.') cmd_prefix.prefix = 'sudo --non-interactive ' return cmd_prefix.prefix
def create_syslink(self, __name, __script): # ---------- # Get Main Script Path # --------------- repository_script = "{}/{}/{}".format(self.__installation_folder, __name, __script) __name = __name.lower() # ---------- # Syslink Command # --------------- cmd = "ln -s {} {}/{}".format(repository_script, self.__syslink_folder, __name) # ---------- # Handle exception # --------------- try: self.qprint("{}{}[*] Creating a syslink...{}".format( color.BOLD, color.OKBLUE, color.ENDC)) # ---------- # Create Syslink # --------------- with open(self.__info.get_devnull(), "w") as FNULL: subprocess.check_output(shsplit(cmd), stderr=FNULL) except subprocess.CalledProcessError: self.qprint("{}{}[!] Cannot create a syslink{}".format( color.BOLD, color.FAIL, color.ENDC))
def complete_cd(self, text, line, begidx, endidx): args = shsplit(line) if len(args) >= 3: return [] else: cand = self._complete(text) cand = [e for e in cand if e.endswith("/")] return cand
def write_to_phuser_report_file(self, cmd_string): # Write to phuser file self._phuser_report_tempfile.seek(0) self._phuser_report_tempfile.truncate() p = Popen(shsplit(cmd_string), stdout=PIPE, stderr=PIPE) # TODO: Error messages from amuser should be communicated to the user r = p.communicate() self._phuser_report_tempfile.seek(0) return self._phuser_report_tempfile
def remove_scripts(self, __scripts): self.qprint("{}{}[*] Running uninstaller scripts...{}".format( color.BOLD, color.OKBLUE, color.ENDC)) for cmd in __scripts: try: subprocess.check_output(shsplit(cmd)) except subprocess.CalledProcessError: self.qprint("{}[!] Error running: {}{}{}".format( color.FAIL, color.WARNING, cmd, color.ENDC))
def recv_args(inst: str, query: str) -> None: param = shsplit(query) if not comp(len(param), len(param_names)): if not info: print("USAGE: {} {}".format(name, "".join(param_names))) else: print("USAGE: {}".format(info)) return func(inst, query)
def run_macs2ms(input, local=False): '''Run ms 'input' in the shell and gather input. input: macs command line string. local: Specifies if local machine has msformatter in path (True) or in directory (False). ''' msformat = '/'.join(os.getcwd().split('/')[:-1])+'/bin/msformatter ' if local: msformat = 'msformatter' args = shsplit(input) args2 = shsplit(msformat) p1 = Popen(args, stdout=PIPE, stderr=PIPE) p2 = Popen(args2, stdin = p1.stdout, stdout=PIPE, stderr=PIPE) p1.stdout.close() p1.stderr.close() #This stderr is the error output from macs, which gives the trees. msout, mserr = p2.communicate() return msout, mserr
def run_macs2ms(input, local=False): '''Run ms 'input' in the shell and gather input. input: macs command line string. local: Specifies if local machine has msformatter in path (True) or in directory (False). ''' msformat = '/'.join(os.getcwd().split('/')[:-1]) + '/bin/msformatter ' if local: msformat = 'msformatter' args = shsplit(input) args2 = shsplit(msformat) p1 = Popen(args, stdout=PIPE, stderr=PIPE) p2 = Popen(args2, stdin=p1.stdout, stdout=PIPE, stderr=PIPE) p1.stdout.close() p1.stderr.close( ) #This stderr is the error output from macs, which gives the trees. msout, mserr = p2.communicate() return msout, mserr
def py_import(): args = shsplit(vim.eval('a:args')) import_path = args.pop() name = next(get_project().search(import_path), None) if name is None: echo_highlight('Cannot find %s in your project or on sys.path!' % import_path) else: cmd_args = ' '.join([a.replace(' ', '\\ ') for a in args]) _goto_specific_name(name, options=cmd_args)
def addTimestamp(self, image_path): # Add timestamp into jpg if self.convert_path: overlay_text = shsplit("{} {} -pointsize 36 -fill white " "-stroke black -annotate +40+40 '{}' " "{}".format( self.convert_path, image_path, self.now_time('%Y-%m-%d %H:%M:%S'), image_path)) call(overlay_text)
def _docker_exec(self, command): """ Execute a command inside the docker. :param str command: The command to execute. """ return check_output(shsplit( 'docker exec {container_id} {command}'.format( container_id=self.container_id, command=command.strip() ) )).decode('utf8')
def _parse_line(self, line): args = shsplit(line) eargs = [] children = self._env.get_current_list()[0] for e in args: m = fnmatch.filter(children, e) if len(m) == 0: # if e does not match any file, use without change eargs.append(e) else: eargs.extend(m) return eargs
def github(self, __name, __author): # ---------- # Get GitHub Link # ---------------- github_link = "https://github.com/{}/{}".format(__author, __name) # ---------- # Git Clone Command # ---------------- repository_folder = "{}/{}".format(self.__installation_folder, __name) git_command = "git clone {} {}".format(github_link, repository_folder) # ---------- # Check if Repository is already installed # ---------------- with open(self.__info.get_devnull(), 'w') as FNULL: if self.__info.is_dir(repository_folder): rm_old_repo_cmd = "rm -rf {}".format(repository_folder) # ---------- # Handle Errors # ---------------- try: self.qprint( "{}{}[*] Removing old installations...{}".format( color.BOLD, color.OKBLUE, color.ENDC)) subprocess.check_output(shsplit(rm_old_repo_cmd), stderr=FNULL) except subprocess.CalledProcessError: self.qprint( "{}{}[*] Failed to remove old repositories{}".format( color.BOLD, color.FAIL, color.ENDC)) # ---------- # Handle Errors # ---------------- try: self.qprint("{}{}[*] Clonning GitHub repository...{}".format( color.BOLD, color.OKBLUE, color.ENDC)) subprocess.check_output(shsplit(git_command), stderr=FNULL) except subprocess.CalledProcessError: raise ImportError("Failed to clone GitHub repository!")
def captureStdout(log, commandLine): '''Run a command and capture what it writes to stdout. If the command fails or writes something to stderr, that is logged. Returns the captured string, or None if the command failed. ''' # TODO: This is a modified copy-paste from compilers._Command. commandParts = shsplit(commandLine) env = dict(environ) env['LC_ALL'] = 'C.UTF-8' while commandParts: if '=' in commandParts[0]: name, value = commandParts[0].split('=', 1) del commandParts[0] env[name] = value else: break else: raise ValueError('No command specified in "%s"' % commandLine) if msysActive() and commandParts[0] != 'sh': commandParts = [msysShell(), '-c', shjoin(commandParts)] try: proc = Popen( commandParts, bufsize=-1, env=env, stdin=None, stdout=PIPE, stderr=PIPE, ) except OSError as ex: print('Failed to execute "%s": %s' % (commandLine, ex), file=log) return None stdoutdata, stderrdata = proc.communicate() if stderrdata: severity = 'warning' if proc.returncode == 0 else 'error' log.write('%s executing "%s"\n' % (severity.capitalize(), commandLine)) # pylint 0.18.0 somehow thinks stderrdata is a list, not a string. # pylint: disable-msg=E1103 stderrdata = stderrdata.decode('utf-8').replace('\r', '') log.write(stderrdata) if not stderrdata.endswith('\n'): log.write('\n') if proc.returncode == 0: return stdoutdata.decode('utf-8') else: print('Execution failed with exit code %d' % proc.returncode, file=log) return None
def parse_input(self): if self.s == "": self.r = [] return args = shsplit(self.s) eargs = [args[0]] for a in args[1:]: g = glob(a) if len(g) == 0: eargs.extend([a]) else: eargs.extend(g) self.r = eargs return
def send_xml(self): # Send the XML files curl_command = self.generate_curl_command() curl_command_parts = shsplit(curl_command) temp_fd, temp_fp = mkstemp() call(curl_command_parts, stdout=temp_fd) close(temp_fd) with open(temp_fp, 'U') as curl_output_f: curl_result = curl_output_f.read() study_accession = None submission_accession = None if 'success="true"' in curl_result: LogEntry.create('Runtime', curl_result) print curl_result print "SUCCESS" accessions = search( '<STUDY accession="(?P<study>.+?)".*?' '<SUBMISSION accession="(?P<submission>.+?)"', curl_result) if accessions is not None: study_accession = accessions.group('study') submission_accession = accessions.group('submission') LogEntry.create('Runtime', "Study accession:\t%s" % study_accession) LogEntry.create( 'Runtime', "Submission accession:\t%s" % submission_accession) print "Study accession:\t", study_accession print "Submission accession:\t", submission_accession else: LogEntry.create('Runtime', ("However, the accession numbers " "could not be found in the output " "above.")) print( "However, the accession numbers could not be found in " "the output above.") else: LogEntry.create('Fatal', curl_result) print curl_result print "FAILED" return (study_accession, submission_accession)
def py_import(): # args are the same as for the :edit command args = shsplit(vim.eval('a:args')) import_path = args.pop() text = 'import %s' % import_path scr = jedi.Script(text, 1, len(text), '', environment=get_environment()) try: completion = scr.goto_assignments()[0] except IndexError: echo_highlight('Cannot find %s in sys.path!' % import_path) else: if completion.column is None: # Python modules always have a line number. echo_highlight('%s is a builtin module.' % import_path) else: cmd_args = ' '.join([a.replace(' ', '\\ ') for a in args]) new_buffer(completion.module_path, cmd_args)
def py_import(): # args are the same as for the :edit command args = shsplit(vim.eval("a:args")) import_path = args.pop() text = "import %s" % import_path scr = jedi.Script(text, 1, len(text), "") try: completion = scr.goto_assignments()[0] except IndexError: echo_highlight("Cannot find %s in sys.path!" % import_path) else: if completion.in_builtin_module(): echo_highlight("%s is a builtin module." % import_path) else: cmd_args = " ".join([a.replace(" ", "\\ ") for a in args]) new_buffer(completion.module_path, cmd_args)
def _docker_exec(self, command): """ Execute a command inside the docker. :param str command: The command to execute. """ log.debug('[{}]._docker_exec(\'{}\') ::'.format( self._container_id, command)) response = check_output( shsplit('docker exec {container_id} {command}'.format( container_id=self._container_id, command=command.strip()))).decode('utf8') log.debug(response) return response
def py_import(): # args are the same as for the :edit command args = shsplit(vim.eval('a:args')) import_path = args.pop() text = 'import %s' % import_path scr = jedi.Script(text, 1, len(text), '') try: completion = scr.goto_assignments()[0] except IndexError: echo_highlight('Cannot find %s in sys.path!' % import_path) else: if completion.in_builtin_module(): echo_highlight('%s is a builtin module.' % import_path) else: cmd_args = ' '.join([a.replace(' ', '\\ ') for a in args]) new_buffer(completion.module_path, cmd_args)
def _setup_system(self): """ Setup the P4 image for testing. #. Create the CPU virtual eth pair used by the P4 switch #. Bring up the interfaces required to run the switch #. Run the switch program """ if self.metadata.get('autostart', True): self._docker_exec( 'ip link add name veth250 type veth peer name veth251' ) self._docker_exec('ip link set dev veth250 up') self._docker_exec('ip link set dev veth251 up') # Bring up all interfaces # FIXME: attach only interfaces brought up by the user for portlbl in self.ports: self.set_port_state(portlbl, True) args = [ '/p4factory/targets/switch/behavioral-model', '--no-veth', '--no-pcap' ] # set openflow controller IP if necessary if self.metadata.get('ofip', None) is not None: args.extend(['--of-ip', self._ofip]) # ifaces are ready in post_build # start the switch program with options: # -i 1 -i 2 -i 3 -i 4 ... for iface in self.ports.values(): args.extend(['-i', iface]) # redirect stdout & stderr to log file # run in background args.extend(['&>/tmp/switch.log', '&']) # run command # TODO: check for posible error code self._bm_daemon = Popen(shsplit( 'docker exec {} '.format(self.container_id) + ' '.join(args) ))
def send_xml(self): # Send the XML files curl_command = self.generate_curl_command() curl_command_parts = shsplit(curl_command) temp_fd, temp_fp = mkstemp() call(curl_command_parts, stdout=temp_fd) close(temp_fd) with open(temp_fp, 'U') as curl_output_f: curl_result = curl_output_f.read() study_accession = None submission_accession = None if 'success="true"' in curl_result: LogEntry.create('Runtime', curl_result) print curl_result print "SUCCESS" accessions = search('<STUDY accession="(?P<study>.+?)".*?' '<SUBMISSION accession="(?P<submission>.+?)"', curl_result) if accessions is not None: study_accession = accessions.group('study') submission_accession = accessions.group('submission') LogEntry.create('Runtime', "Study accession:\t%s" % study_accession) LogEntry.create('Runtime', "Submission accession:\t%s" % submission_accession) print "Study accession:\t", study_accession print "Submission accession:\t", submission_accession else: LogEntry.create('Runtime', ("However, the accession numbers " "could not be found in the output " "above.")) print ("However, the accession numbers could not be found in " "the output above.") else: LogEntry.create('Fatal', curl_result) print curl_result print "FAILED" return (study_accession, submission_accession)
def install(self, __repository): # ---------- # User Security Confirmation # ---------------- if self.confirm_commands(__repository['name'], __repository['install'][0]['Linux']): # ---------- # Git Clone # ---------------- self.github(__repository['name'], __repository['author']) # ---------- # Change owner and directory # ---------------- self.configure_new_folder(__repository['name']) # ---------- # Create and reset initperal.sh if it is needed # ---------------- if self.init_script_needed: self.configure_init_script() # ---------- # Installation Commands # ---------------- self.qprint("{}{}[*] Running Setup Scripts...{}".format( color.BOLD, color.OKBLUE, color.ENDC)) for cmd in __repository['install'][0]['Linux']: with open(self.__info.get_devnull(), "w") as FNULL: cmd = self.custom_command(cmd) if cmd is not None: # ---------- # Handle execution errors # ---------------- try: subprocess.check_output(shsplit(cmd), stderr=FNULL) except subprocess.CalledProcessError: self.qprint("{}[!] Error running: {}{}{}".format( color.FAIL, color.WARNING, cmd, color.ENDC)) # ---------- # Check for Syslink # ---------------- if self.__syslink_flag: if self.confirm_syslink(__repository['name']): # ---------- # Create Syslink # ---------------- self.create_syslink(__repository['name'], __repository['main-script'])
def _execute(command): """ Execute a command safely and returns its output as ``utf-8``. """ try: p = Popen( shsplit(command), stdin=DEVNULL, stderr=DEVNULL, stdout=PIPE, env={'LC_ALL': 'C', 'LANG': 'C'} ) pstdout, _ = p.communicate() except OSError: # E.g. command not found log.debug(format_exc()) return None return pstdout.decode('utf-8')
def _setup_system(self): """ Setup the controller image for testing. #. Bring up the ports connecting to the datapaths #. Run ryu-manager """ # Ryu should be started by Topology if self.metadata.get('autostart', True): # Get the app file (or default) if self.app_name is not None: app_path = '/tmp/' + path.basename(self.app_name) else: app_path = '/root/ryu-master/ryu/app/simple_switch.py' # run ryu app using ryu-manager self._supervisord = Popen(shsplit( 'docker exec {} ' 'sh -c "RYU_COMMAND=\'/root/ryu-master/bin/ryu-manager {} ' '--verbose\' supervisord"'.format( self.container_id, app_path ) )) # Wait for ryu-manager to start config_timeout = 100 i = 0 while i < config_timeout: config_status = self._docker_exec( 'supervisorctl status ryu-manager' ) if 'RUNNING' not in config_status: sleep(0.1) else: break i += 1 if i == config_timeout: raise RuntimeError( 'ryu-manager did not reach RUNNING state on supervisor!' )
def privileged_cmd(commands_tpl, **kwargs): """ Run a privileged command. This function will replace the tokens in the template with the provided keyword arguments, then it will split the lines of the template, strip them and execute them using the prefix for privileged commands, checking that the command returns 0. :param str commands_tpl: Single line or multiline template for commands. :param dict kwargs: Replacement tokens. """ prefix = cmd_prefix() for command in commands_tpl.format(**kwargs).splitlines(): command = command.strip() if command: check_call(shsplit(prefix + command))
def _docker_exec(self, command): """ Execute a command inside the docker. :param str command: The command to execute. """ log.debug( '[{}]._docker_exec({}) ::'.format(self.container_id, command) ) response = check_output(shsplit( 'docker exec {container_id} {command}'.format( container_id=self.container_id, command=command.strip() ) )).decode('utf8') log.debug(response) return response
def _setup_system(self): """ Setup the controller image for testing. #. Bring up the ports connecting to the datapaths #. Run ryu-manager """ # Check if ryu should be started by Topology if not self._autostart: return # run ryu app using ryu-manager self._supervisord = Popen(shsplit( 'docker exec {} ' 'sh -c "RYU_COMMAND=\'/root/ryu-master/bin/ryu-manager {} ' '--verbose\' supervisord"'.format( self.container_id, self.app ) )) # Wait for ryu-manager to start config_timeout = 100 i = 0 while i < config_timeout: config_status = self._docker_exec( 'supervisorctl status ryu-manager' ) if 'RUNNING' not in config_status: sleep(0.1) else: break i += 1 if i == config_timeout: raise RuntimeError( 'ryu-manager did not reach RUNNING state on supervisor!' )
def prince_convert(self): ''' Call the actual Prince command to convert to PDF ''' from os import makedirs from os.path import dirname, join, exists from calibre.ptempfile import PersistentTemporaryFile from calibre.constants import DEBUG from shlex import split as shsplit # All files are relative to the OPF location opf_dir = dirname(self.opf) base_dir = dirname(self.pdf_file) base_dir = join(opf_dir, base_dir) try: makedirs(base_dir) except BaseException: if not exists(base_dir): raise # Create a temporary CSS file with the box contents custom_CSS = PersistentTemporaryFile() custom_CSS.write(unicode(self.css1.toPlainText())) custom_CSS.close() # Create a temporary file with the list of input files file_list = PersistentTemporaryFile() for item in self.oeb.spine: file_list.write(item.href + "\n") file_list.close() # Build the command line command = prefs['prince_exe'] args = ['-v'] if self.prince_file: args.append('-s') args.append(self.prince_file) args.append('-s') args.append(custom_CSS.name) args.append('-l') args.append(file_list.name) args.append('-o') args.append(self.pdf_file) # Additional command-line arguments args.extend(shsplit(self.args.text())) # Hide the convert button and show a busy indicator self.convert.setEnabled(False) self.progress_bar = QProgressBar() self.progress_bar.setRange(0,0) self.progress_bar.setValue(0) self.l.addWidget(self.progress_bar) # Run the command and return the path to the PDF file if DEBUG: print(_('Converting book...')) process = QProcess(self) process.setWorkingDirectory(opf_dir) process.setProcessChannelMode(QProcess.MergedChannels); process.error.connect(self.error) process.finished.connect(self.end) self.process = process if DEBUG: from subprocess import list2cmdline line = list2cmdline([command] + args) print(_('Command line: %s') % line) process.start(command, args)
def run_ms(input): '''Run ms 'input' in the shell and gather input.''' args = shsplit(input) process = Popen(args, stdout = PIPE, stderr = PIPE) return process.communicate()
def run(self): # Check if file insertion is enabled if not self.state.document.settings.file_insertion_enabled: msg = ( 'File and URL access deactivated. ' 'Ignoring directive "{}".'.format(self.name) ) warning = nodes.warning( '', self.state_machine.reporter.warning( '', nodes.literal_block('', msg), line=self.lineno ) ) return [warning] # Define plantuml file name if len(self.arguments) > 0: fname = self.arguments[0] else: fname = '{:06d}'.format(self.lineno) fname = join(self.uml_out_dir, fname) # Create images output folder mkpath(abspath(dirname(fname))) # Write plantuml content uml_file = fname + '.uml' if self.content: with open(uml_file, 'wb') as fd: fd.write('@startuml\n') try: fd.write( self.state_machine.document.settings.plantuml_hdr ) fd.write('\n') except AttributeError: pass fd.write('\n'.join(self.content)) fd.write('\n@enduml\n') # Execute plantuml call # Commented because plantuml doesn't have and output flag # image_file = fname + self.uml_out_ext plantuml_cmd = 'plantuml' try: plantuml_cmd = self.state_machine.document.settings.plantuml_cmd except AttributeError: pass try: code = call(shsplit( '{} {} "{}"'.format(plantuml_cmd, self.uml_cmd_args, uml_file) )) if code != 0: raise Exception('plantuml call returned {}.'.format(code)) except: msg = format_exc() error = nodes.error( '', self.state_machine.reporter.error( '', nodes.literal_block('', msg), line=self.lineno ) ) return [error] # Default to align center if not 'align' in self.options: self.options['align'] = 'center' # Run Image directive self.arguments = [fname + self.uml_emb_ext] return Image.run(self)
def normalizeWhitespace(expression): return shjoin(shsplit(expression))