def open_yaml_editor(data, description): if data is None: data = {} fd, path = tempfile.mkstemp() os.close(fd) with open(path, 'w') as output: if len(data) > 0: output.write(yaml.safe_dump(data, default_flow_style=False)) output.write("\n") output.write("# You are editing the configuration for the {}.\n".format(description)) output.write("# Blank lines and lines starting with '#' will be ignored.\n") output.write("# Save and exit to apply changes; exit without saving to discard.\n") # Get modified time before calling editor. orig_mtime = os.path.getmtime(path) editor = os.environ.get("EDITOR", "vim") os.spawnvpe(os.P_WAIT, editor, [editor, path], os.environ) with open(path, 'r') as source: data = source.read() new_data = yaml.safe_load(data) # If result is null, convert to an empty dict before sending to router. if new_data is None: new_data = {} # Check if the file has been modified. new_mtime = os.path.getmtime(path) changed = (new_mtime != orig_mtime) os.remove(path) return new_data, changed
def open_file_with_default_application( file_path ): """ Launch a program to open an arbitrary file. The file will be opened using whatever program is configured on the host as the default program for that type of file. """ norm_path = os.path.normpath( file_path ) if not os.path.exists(norm_path): print("%s does not exist" % file_path) return if os.sys.platform == 'win32': try: os.startfile(norm_path) except WindowsError as msg: print("Error Opening File. " + str(msg)) else: search = os.environ['PATH'].split(':') for path in search: prog = os.path.join(path, 'xdg-open') if os.path.isfile(prog): os.spawnvpe(os.P_NOWAIT, prog, [prog, norm_path], os.environ) return
def edit(ctx): """ Interactively edit the host configuration. """ url = ctx.obj['base_url'] + "/config/hostconfig" req = router_request("GET", url, dump=False) config = req.json() fd, path = tempfile.mkstemp() os.close(fd) with open(path, 'w') as output: output.write(yaml.safe_dump(config, default_flow_style=False)) # Get modified time before calling editor. orig_mtime = os.path.getmtime(path) editor = os.environ.get("EDITOR", "vim") os.spawnvpe(os.P_WAIT, editor, [editor, path], os.environ) with open(path, 'r') as source: data = source.read() config = yaml.safe_load(data) # Check if the file has been modified, and if it has, send the update. new_mtime = os.path.getmtime(path) if new_mtime != orig_mtime: data = { 'config': config } res = router_request("PUT", url, json=data) result = res.json() ctx.invoke(watch, change_id=result['change_id']) os.remove(path)
def sync(self): new=os.path.join(self.root, self.host, "new") current=os.path.join(self.root, self.host, "current") if self.debug: print "running rsync on host", self.host try: if self.conf['map-uid']: ret_val=os.spawnvpe(os.P_WAIT, "rsync", ["rsync", "--archive", "--delete", "--numeric-ids", "--hard-links", "--sparse", "--link-dest="+current, self.host+"::backup/", new], { "USER": self.user, "RSYNC_PASSWORD": self.password } ) else: ret_val=os.spawnvpe(os.P_WAIT, "rsync", ["rsync", "-rlptD", "--delete", "--hard-links", "--sparse", "--link-dest="+current, self.host+"::backup/", new], { "USER": self.user, "RSYNC_PASSWORD": self.password } ) except: ret_val=1 if ret_val > 0: print "cleanup" os.spawnvp(os.P_WAIT, "rm", ["rm", "-r", new]) print self.host,"failed" return stime=time.time() dirbase='/'.join([str(de).zfill(2) for de in time.localtime(stime)[0:3]]) dir=':'.join([str(de).zfill(2) for de in time.localtime(stime)[3:6]]) try: os.makedirs(os.path.join(self.root, self.host, dirbase)) except OSError, oe: if oe.errno != 17: raise
def open_text_editor(data): if data is None: data = "" fd, path = tempfile.mkstemp() os.close(fd) with open(path, 'w') as output: output.write(data) # Get modified time before calling editor. orig_mtime = os.path.getmtime(path) editor = os.environ.get("EDITOR", "vim") os.spawnvpe(os.P_WAIT, editor, [editor, path], os.environ) with open(path, 'r') as source: data = source.read() # Check if the file has been modified, and if it has, send the update. new_mtime = os.path.getmtime(path) if new_mtime == orig_mtime: data = None os.remove(path) return data
def open_file_with_default_application(file_path): """ Launch a program to open an arbitrary file. The file will be opened using whatever program is configured on the host as the default program for that type of file. """ norm_path = os.path.normpath(file_path) if not os.path.exists(norm_path): print("%s does not exist" % file_path) return if os.sys.platform == "win32": try: os.startfile(norm_path) except WindowsError as msg: print("Error Opening File. " + str(msg)) else: search = os.environ["PATH"].split(":") for path in search: prog = os.path.join(path, "xdg-open") if os.path.isfile(prog): os.spawnvpe(os.P_NOWAIT, prog, [prog, norm_path], os.environ) return
def update_alarm(self): # Is the command still being executed? try: self.alarm_alive = not os.waitpid(self.alarm_pid, os.WNOHANG)[0] except OSError: self.alarm_alive = False if self.alarm_enabled: if not self.alarm_running: # alarm_running = False if self.alarm_when: if self.data > self.alarm_limit: self.alarm_running = True commands = self.alarm_command.split(' ') self.alarm_pid = os.spawnvpe(os.P_NOWAIT, commands[0], commands, os.environ) else: if self.data < self.alarm_limit: self.alarm_running = True commands = self.alarm_command.split(' ') self.alarm_pid = os.spawnvpe(os.P_NOWAIT, commands[0], commands, os.environ) else: # alarm_running = True if self.alarm_repeat and not self.alarm_alive: commands = self.alarm_command.split(' ') self.alarm_pid = os.spawnvpe(os.P_NOWAIT, commands[0], commands, os.environ) if self.alarm_when: if self.data < self.alarm_limit: self.alarm_running = False else: if self.data > self.alarm_limit: self.alarm_running = False
def do_start(self, arg): self.get_status() if not self.zd_up: args = [ self.options.python, self.options.zdrun, ] args += self._get_override("-S", "schemafile") args += self._get_override("-C", "configfile") args += self._get_override("-b", "backofflimit") args += self._get_override("-d", "daemon", flag=1) args += self._get_override("-f", "forever", flag=1) args += self._get_override("-s", "sockname") args += self._get_override("-u", "user") if self.options.umask: args += self._get_override("-m", "umask", oct(self.options.umask)) args += self._get_override( "-x", "exitcodes", ",".join(map(str, self.options.exitcodes))) args += self._get_override("-z", "directory") args.extend(self.options.program) if self.options.daemon: flag = os.P_NOWAIT else: flag = os.P_WAIT os.spawnvpe(flag, args[0], args, self.environment()) elif not self.zd_pid: self.send_action("start") else: print "daemon process already running; pid=%d" % self.zd_pid return self.awhile(lambda: self.zd_pid, "daemon process started, pid=%(zd_pid)d")
def winexe(cmd): check_tool('winexe') creds = '%s/%s%%%s' % (CONF['smb_domain'], CONF['smb_user'], CONF['smb_pass']) run = [] run.append(TOOLS['winexe']) if CONF['system']: run.append('--system') run.append('--uninstall') run.append('--interactive=0') run.append('-U') run.append(creds) run.append('//' + CONF['smb_ip']) run.append(cmd) if not cmd.lower().startswith('cmd'): process = subprocess.Popen(run, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT) ret = process.stdout.read() ret = ret.replace('\x00', '') return ret.strip() # For an interactive command line, don't use popen os.spawnvpe(os.P_WAIT, run[0], run, os.environ) return ''
def main(): # dummy zdctl startup of zdrun shutup() file = os.path.normpath(os.path.abspath(sys.argv[0])) tmp = sys.argv[1] dir = os.path.dirname(file) zctldir = os.path.dirname(dir) zdrun = os.path.join(zctldir, 'zdrun.py') donothing = os.path.join(tmp, 'donothing.sh') fd = os.open(donothing, os.O_WRONLY | os.O_CREAT, 0o700) os.write(fd, donothing_contents.encode()) os.close(fd) args = [sys.executable, zdrun] args += [ '-d', '-b', '10', '-s', os.path.join(tmp, 'testsock'), '-x', '0,2', '-z', dir, donothing ] flag = os.P_NOWAIT os.spawnvpe( flag, args[0], args, dict(os.environ, PYTHONPATH=':'.join(sys.path)), ) while 1: # wait to be signaled time.sleep(1)
def winexe(cmd): check_tool('winexe') creds = '%s%%%s' % (CONF['smb_user'], CONF['smb_pass']) run = [] run.append(TOOLS['winexe']) if CONF['system']: run.append('--system') run.append('--uninstall') run.append('--interactive=0') run.append('-U') run.append(creds) run.append('//'+ CONF['smb_ip']) run.append(cmd) if not cmd.lower().startswith('cmd'): process = subprocess.Popen(run, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT) ret = process.stdout.read() ret = ret.replace('\x00', '') return ret.strip() # For an interactive command line, don't use popen os.spawnvpe(os.P_WAIT, run[0], run, os.environ) return ''
def edit(ctx): """ Interactively edit the host configuration. """ url = ctx.obj['base_url'] + "/config/hostconfig" req = router_request("GET", url, dump=False) config = req.json() fd, path = tempfile.mkstemp() os.close(fd) with open(path, 'w') as output: output.write(yaml.safe_dump(config, default_flow_style=False)) # Get modified time before calling editor. orig_mtime = os.path.getmtime(path) editor = os.environ.get("EDITOR", "vim") os.spawnvpe(os.P_WAIT, editor, [editor, path], os.environ) with open(path, 'r') as source: data = source.read() config = yaml.safe_load(data) # Check if the file has been modified, and if it has, send the update. new_mtime = os.path.getmtime(path) if new_mtime != orig_mtime: data = {'config': config} res = router_request("PUT", url, json=data) result = res.json() ctx.invoke(watch, change_id=result['change_id']) os.remove(path)
def __run_command( app, cmd, args ): app.log.info( '%s %s' % (cmd, ' '.join( [str(arg) for arg in args] ) ) ) env = os.environ.copy() cmd = asUtf8( cmd ) args = [asUtf8( str(arg) ) for arg in args] os.spawnvpe( os.P_NOWAIT, cmd, [cmd]+args, env )
def __run_command(app, cmd, args): app.log.info('%s %s' % (cmd, ' '.join([str(arg) for arg in args]))) env = os.environ.copy() cmd = asUtf8(cmd) args = [asUtf8(str(arg)) for arg in args] os.spawnvpe(os.P_NOWAIT, cmd, [cmd] + args, env)
def smb_rdp(): """ <ip> [ user ] [ passwd/nthash ] [ enable | disable ] Open a Remote Desktop session using xfreerdp (Pass-the-Hash = restricted admin) """ if CONF["threaded_mode"]: text("[!] Function not available when running for several hosts.", 1) if 'enable' in sys.argv: set_creds(4) text("[*] %s Updating Registry..." % (CONF["smb_ip"])) winexe('reg add "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server" /v fDenyTSConnections /t REG_DWORD /d 0 /f') smb_fwrule('add', 3389) sys.exit(0) if 'disable' in sys.argv: set_creds(4) text("[*] %s Updating Registry..." % (CONF["smb_ip"])) winexe('reg add "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server" /v fDenyTSConnections /t REG_DWORD /d 1 /f') smb_fwrule('del', 3389); sys.exit(0) set_creds(3) check_tool('xfreerdp') res = screen_resolution() max_res = '%dx%d' % (int(res[0]), int(res[1]) - 50) run = [] run.append(TOOLS['xfreerdp']) run.append('/size:%s' % max_res) run.append('/t:%s' % CONF['smb_ip']) run.append('/v:%s' % CONF['smb_ip']) if '\\' in CONF['smb_user']: tab = CONF['smb_user'].split('\\', 2) run.append('/d:%s' % tab[0]) run.append('/u:%s' % tab[1]) else: run.append('/u:%s' % CONF['smb_user']) if CONF['smb_pass'] == '': text("[!] Note: Pass-the-Hash with RDP only works for local admin accounts and under the restricted admin mode.") run.append('/pth:%s' % CONF['smb_hash']) run.append('/restricted-admin') else: run.append('/p:%s' % CONF['smb_pass']) # Tweak the following to suit your needs run.append("+clipboard") run.append("+home-drive") run.append("-decorations") run.append("/cert-ignore") # baaad. os.spawnvpe(os.P_WAIT, run[0], run, os.environ)
def handle(self): """Handle of the socket request.""" assert(isinstance(self.request, socket.socket)) szSimDir = os.path.join(os.environ['HOME'], 'simulations') if not os.path.exists(szSimDir): os.mkdir(szSimDir) os.chdir(szSimDir) self.request.send("HELO") self.logger.debug('HELO received\n') simname = readPacket(self.request) self.logger.debug('Simname = ' + simname + '\n') self.request.send('FILENAME') i = 0 basename = simname while 1: try: self.logger.debug('Making Directory - ' + simname) os.mkdir(simname) break except: simname = basename + '_' + str(i) i += 1 os.chdir(simname) numfiles = int(readPacket(self.request)) self.logger.debug('Numfiles = ' + str(numfiles)) while numfiles: numfiles -= 1 self.getfile() self.request.send("SFC") hostporthdr = readPacket(self.request) self.logger.debug('HostPort = ' + hostporthdr + '\n') f = open('settings', 'w') f.write(hostporthdr) f.close() szScriptName = readPacket(self.request) self.request.send("HPC") #WARNING: This should be independent of module name. Fix it. shutil.copytree(sys.path[0], './mas_framework') shutil.move('mas_framework/apistub.py', 'mas_framework/api.py') L = ['python', szScriptName] os.spawnvpe(os.P_NOWAIT, L[0], L, os.environ) self.request.close()
def shell(ctx): """ Open a shell inside a chute. This requires you to have enabled SSH access to the device and installed bash inside your chute. """ cmd = ["ssh", "-t", "paradrop@{}".format(ctx.obj['address']), "sudo", "docker", "exec", "-it", ctx.obj['chute'], "/bin/bash"] os.spawnvpe(os.P_WAIT, "ssh", cmd, os.environ)
def monitor(outdir, outname, log=True, plot=True): pid = os.getpid() L = ['psrecord', "%s" % pid, "--interval", "1"] if log: L = L + ["--log", "%s/log_%s_%s.txt" % (outdir, outname, pid)] if plot: L = L + ["--plot", "%s/plot_%s_%s.png" % (outdir, outname, pid)] if not log and not plot: print("Nothing being monitored") else: os.spawnvpe(os.P_NOWAIT, 'psrecord', L, os.environ)
def monitor(out_dir: str, out_name: str, log=True, plot=True): pid = os.getpid() L = ['psrecord', "%s" % pid, "--interval", "1"] if log: L = L + ["--log", os.path.join(out_dir, f"log_{out_name}_{pid}.txt")] if plot: L = L + ["--plot", os.path.join(out_dir, f"plot_{out_name}_{pid}.txt")] if not log and not plot: logging.info("Nothing being monitored") else: os.spawnvpe(os.P_NOWAIT, 'psrecord', L, os.environ)
def main(args, unk_args): credentials = configparser.ConfigParser() credentials.read(os.path.expanduser(args.credentials_file)) config = configparser.ConfigParser() config.read(os.path.expanduser(args.config_file)) extra_env_dict = aws_environs(args, unk_args, config, credentials) current_shell = os.environ.get("SHELL") os.spawnvpe(os.P_WAIT, current_shell, [ current_shell, ], dict(os.environ, **extra_env_dict))
def edit_environment(ctx): """ Interactively edit the chute environment vairables. """ req = router_request("GET", ctx.obj['chute_url'], dump=False) info = req.json() old_environ = info.get('environment', None) if old_environ is None: old_environ = {} fd, path = tempfile.mkstemp() os.close(fd) with open(path, 'w') as output: if len(old_environ) > 0: output.write(yaml.safe_dump(old_environ, default_flow_style=False)) output.write("\n") output.write( "# You are editing the environment variables for the chute {}.\n". format(ctx.obj['chute'])) output.write( "# Blank lines and lines starting with '#' will be ignored.\n") output.write( "# Put each variable on a line with a colon separator, e.g. 'VARIABLE: VALUE'\n" ) output.write( "# Save and exit to apply changes; exit without saving to discard.\n" ) # Get modified time before calling editor. orig_mtime = os.path.getmtime(path) editor = os.environ.get("EDITOR", "vim") os.spawnvpe(os.P_WAIT, editor, [editor, path], os.environ) with open(path, 'r') as source: data = source.read() new_environ = yaml.safe_load(data) # If result is null, convert to an empty dict before sending to router. if new_environ is None: new_environ = {} # Check if the file has been modified, and if it has, send the update. new_mtime = os.path.getmtime(path) if new_mtime != orig_mtime: data = {'environment': new_environ} url = ctx.obj['chute_url'] + "/restart" res = router_request("POST", url, json=data) data = res.json() ctx.invoke(watch, change_id=data['change_id']) os.remove(path)
def shell(ctx): """ Open a shell inside a chute. This requires you to have enabled SSH access to the device and installed bash inside your chute. """ cmd = [ "ssh", "-t", "paradrop@{}".format(ctx.obj['address']), "sudo", "docker", "exec", "-it", ctx.obj['chute'], "/bin/bash" ] os.spawnvpe(os.P_WAIT, "ssh", cmd, os.environ)
def PromptToStartDaemon(): """ Prompt the user to start the daemon """ daemonloc = wpath.sbin + 'wicd' sudo_prog = choose_sudo_prog() if sudo_prog.endswith("gksudo") or sudo_prog.endswith("ktsuss"): msg = '--message' else: msg = '--caption' sudo_args = [sudo_prog, msg, 'Wicd needs to access your computer\'s network cards.', daemonloc] os.spawnvpe(os.P_WAIT, sudo_prog, sudo_args, os.environ)
def console(self): logger.info('Action: console()') try: os.environ['PGPASSWORD'] = self.databaseConf.password cmdline = 'psql -h %(host)s -p %(port)d -U %(user)s %(name)s' % dict( host=self.serverConf.host, port=self.serverConf.port, user=self.databaseConf.user, name=self.databaseConf.name) cmd = cmdline.split() os.spawnvpe(os.P_WAIT, cmd[0], cmd, os.environ) finally: del os.environ['PGPASSWORD']
def console(self): logger.info('Action: console()') try: os.environ['PGPASSWORD'] = self.databaseConf.password cmdline = 'psql -h %(host)s -p %(port)d -U %(user)s %(name)s' % dict( host = self.serverConf.host, port = self.serverConf.port, user = self.databaseConf.user, name = self.databaseConf.name ) cmd = cmdline.split() os.spawnvpe(os.P_WAIT, cmd[0], cmd, os.environ) finally: del os.environ['PGPASSWORD']
def program(*a, **kw): filename = kw['filename'] title = input('Enter title: ').strip() valid_file = False text_to_insert = '' while not valid_file: text_source = input('Do you want to put predefined contents? ' 'Enter path to insert ' '(or just press Enter to open editor): ') if text_source: try: with open(text_source, 'r') as src: text_to_insert = '\n ' + src.read() os.remove(text_source) except FileNotFoundError: print(f'Could not open file: {text_source}\nPlease try again') except EOFError: return else: valid_file = True else: valid_file = True linkformat = date.today().strftime(DATE_FORMAT).lower() richformat = date.today().strftime(DATE_TEXT_FORMAT) rfcformat = formatdate(float(datetime.now().strftime('%s'))) with NamedTemporaryFile() as tempfile: tempfile.write((f'<log-entry id="{linkformat}">\n' f' <h2>{richformat} - <a href="#{linkformat}">' f'{title}</a></h2>{text_to_insert}' '\n</log-entry>\n').encode('utf-8')) tempfile.flush() os.spawnvpe(os.P_WAIT, EDITOR, [EDITOR, tempfile.name], os.environ) tempfile.seek(0) result = [x.decode('utf-8') for x in tempfile.readlines()] with open('.head', 'w') as storage: storage.write(kw['filename'] + '\t\n') storage.write(linkformat + '\t\n') storage.write(rfcformat + '\t\n') storage.write(title + '\t\n') storage.write('\n'.join(result)) feedname = os.path.splitext(filename)[0] os.spawnvpe(os.P_WAIT, 'rw.py', ('rw.py', feedname + '.rss'), os.environ) os.remove('.head') return result
def shell_entry_point(args: List[str]): program = args[0] path = find_in_path(program) remaining_args = args[1:] command = " ".join(args) environment = get_environment() subprocess.run(args) subprocess.check_call(args) subprocess.check_output(args) subprocess.Popen(args) subprocess.getstatusoutput(command) subprocess.getoutput(command) asyncio.subprocess.create_subprocess_exec(program, remaining_args) asyncio.subprocess.create_subprocess_shell(command) # TODO loop = asyncio.get_event_loop() loop.subprocess_exec(get_protocol_factory(), args) loop.subprocess_shell(get_protocol_factory(), command) os.execl(path, *remaining_args) os.execle(path, *remaining_args, environment) os.execlp(program, *remaining_args) os.execlpe(program, *remaining_args, environment) os.execv(path, remaining_args) os.execve(path, remaining_args, environment) os.execvp(program, remaining_args) os.execvpe(program, remaining_args, environment) os.popen(command) # TODO os.posix_spawnp(path, remaining_args, environment) os.posix_spawn(path, remaining_args, environment) os.spawnl(os.P_WAIT, path, *remaining_args) os.spawnle(os.P_WAIT, path, *remaining_args, environment) os.spawnlp(os.P_WAIT, program, *remaining_args) os.spawnlpe(os.P_WAIT, program, *remaining_args, environment) os.spawnv(os.P_WAIT, path, remaining_args) os.spawnve(os.P_WAIT, path, remaining_args, environment) os.spawnvp(os.P_WAIT, program, remaining_args) # TODO os.spawnvpe(os.P_WAIT, program, remaining_args, environment) os.system(command)
def edit_environment(ctx): """ Interactively edit the chute environment vairables. """ req = router_request("GET", ctx.obj['chute_url'], dump=False) info = req.json() old_environ = info.get('environment', None) if old_environ is None: old_environ = {} fd, path = tempfile.mkstemp() os.close(fd) with open(path, 'w') as output: if len(old_environ) > 0: output.write(yaml.safe_dump(old_environ, default_flow_style=False)) output.write("\n") output.write("# You are editing the environment variables for the chute {}.\n" .format(ctx.obj['chute'])) output.write("# Blank lines and lines starting with '#' will be ignored.\n") output.write("# Put each variable on a line with a colon separator, e.g. 'VARIABLE: VALUE'\n") output.write("# Save and exit to apply changes; exit without saving to discard.\n") # Get modified time before calling editor. orig_mtime = os.path.getmtime(path) editor = os.environ.get("EDITOR", "vim") os.spawnvpe(os.P_WAIT, editor, [editor, path], os.environ) with open(path, 'r') as source: data = source.read() new_environ = yaml.safe_load(data) # If result is null, convert to an empty dict before sending to router. if new_environ is None: new_environ = {} # Check if the file has been modified, and if it has, send the update. new_mtime = os.path.getmtime(path) if new_mtime != orig_mtime: data = { 'environment': new_environ } url = ctx.obj['chute_url'] + "/restart" res = router_request("POST", url, json=data) data = res.json() ctx.invoke(watch, change_id=data['change_id']) os.remove(path)
def PromptToStartDaemon(): """ Prompt the user to start the daemon """ daemonloc = wpath.sbin + 'wicd' sudo_prog = choose_sudo_prog() if not sudo_prog: return False if "gksu" in sudo_prog or "ktsuss" in sudo_prog: msg = '--message' else: msg = '--caption' sudo_args = [sudo_prog, msg, _("Wicd needs to access your computer's network cards."), daemonloc] os.spawnvpe(os.P_WAIT, sudo_prog, sudo_args, os.environ) return True
def run(program, args=[], echo=True, env={}): windows = os.name == 'nt' or os.name == 'vista' # Windows doesn't support spawnvp, so we have to locate the binary if windows: program = _findWindowsBinary(program) if not program: raise Exception('Cannot find "' + program + '"') program = toLocalPath(program) argProgram = program if windows: # If the program name contains spaces, we # add quotes around it. if (' ' in argProgram) and not ('"' in argProgram): argProgram = '"' + argProgram + '"' # spawn requires specification of argv[0] # Because the program name may contain spaces, we # add quotes around it. newArgs = [argProgram] + args newEnv = {} newEnv.update(os.environ) newEnv.update(env) if echo: colorPrint(string.join(newArgs), COMMAND_COLOR) if windows: # Windows doesn't support spawnvpe exitcode = os.spawnve(os.P_WAIT, program, newArgs, newEnv) else: exitcode = os.spawnvpe(os.P_WAIT, program, newArgs, newEnv) return exitcode
def run(program, args=[], env={}): program = removeQuotes(to_local_path(program)) # Windows doesn't support spawnvp, so we have to locate the binary if (os.name == 'nt'): program = findBinary(program) # spawn requires specification of argv[0] # Because the program name may contain spaces, we # add quotes around it. args.insert(0, '"' + program + '"') print string.join(args) + '\n' print '\n' if (os.name == 'nt'): # Windows doesn't support spawnvp if env != {}: exitcode = os.spawnve(os.P_WAIT, program, args, env) else: exitcode = os.spawnv(os.P_WAIT, program, args) else: if env != {}: exitcode = os.spawnvpe(os.P_WAIT, program, args, env) else: exitcode = os.spawnvp(os.P_WAIT, program, args) # Since we mutated the list, remove the element # that was inserted. args.pop(0) return exitcode
def __LaunchPointCalc(self, suffix, vars): #Create a unique running dir for this specific point runPath = os.path.join(self.scriptRunPath, "runPoint_"+suffix) os.system("rm -rf " + runPath) os.system("mkdir -p " + runPath) #Copy all necessary files to the point running dir os.system("cp -rf " + os.path.join(self.scriptFilesPath,"*") + " " +runPath) self.AllPaths.append(runPath) self.runningPaths.append(runPath) #Create the input file for this specific point initialXF = open(os.path.join(runPath , "varsPoint.txt"),"w") initialXF.write(str(len(vars))+"\r\n") initialXF.write(str(vars)[1:-1].replace(", ","\r\n")+"\r\n") initialXF.close() #Launch the thread. Don't wait for it to finish. We will check this dir periodically looking for # the rersults file. args = [os.path.join(runPath, self.ScriptFileName),\ os.path.realpath(runPath),\ os.path.join(runPath,"varsPoint.txt"),\ os.path.join(runPath, "resPoint.txt")] if self.verbose > 1: print "Command:",args exitCode = os.spawnvpe(os.P_NOWAIT, os.path.join(runPath, self.ScriptFileName), args, os.environ) if self.verbose > 1: print "Exited code:",exitCode return True
def post(self, request, *args, **kwargs): global pid if pid is not None: return Response(status=400) pid = os.spawnvpe(os.P_NOWAIT, 'python', ['python', RCMD_PROG], os.environ) return Response(status=201)
def spawnSingle(fScript, iJob, dictEnv): if not Globals.fUseXgrid: strScript = fScript == _SCRIPT_EXEC and 'stexec.py' or 'strpt.py' cc = os.spawnvpe(os.P_WAIT, sys.executable, [ os.path.basename(sys.executable), os.path.join(Globals.strLocalPath, strScript) ], os.environ) if cc: raise Common.BiologicError('Stylus failed - error code(%d)' % cc) else: strXgridJob = 'Stylus - %s' % datetime.datetime.now().isoformat() aryXgridEnv = [ _XGRID_ENVIRONMENT % (key, dictEnv[key]) for key in dictEnv.keys() if key in _STYLUS_ENVIRONMENT ] if not Globals.fLaunchJobs and (Globals.strGenomes or Globals.strPlans): aryXgridEnv.append(_XGRID_ENVIRONMENT % ('STYLUS_GRIDARGS', ('-x -l %s %s') % ((Globals.strGenomes and ('-g ' + Globals.strGenomes) or ''), (Globals.strPlans and ('-p ' + Globals.strPlans) or '')))) fd, strJobPath = tempfile.mkstemp('.xml', text=True) os.write(fd, _XGRID_JOB % (strXgridJob, Globals.strXgridNotify and (_XGRID_NOTIFY % Globals.strXgridNotify) or '', Globals.strXgridShell, Globals.strXgridVolume + '/Stylus', fScript == _SCRIPT_EXEC and '--exec' or '--report', '\n'.join(aryXgridEnv))) os.close(fd) strXgridCmd = 'xgrid -h %s %s -job batch %s' % (Globals.strXgridController, (Globals.strXgridPassword and ('-auth Password -p %s' % Globals.strXgridPassword) or ''), strJobPath) cc = os.system(strXgridCmd) os.remove(strJobPath) if cc: raise Common.BiologicError('Xgrid command failed - command(%s) error code(%d)' % (strXgridCmd, cc))
def run(program, args = [], echo = True, env = {}): windows = os.name == 'nt' or os.name == 'vista' # Windows doesn't support spawnvp, so we have to locate the binary if windows: print(program) program = _findWindowsBinary(program) if not program: raise Exception('Cannot find "' + str(program) + '"') program = toLocalPath(program) argProgram = program if windows: argProgram = safeWindowsName(argProgram) # spawn requires specification of argv[0] # Because the program name may contain spaces, we # add quotes around it. newArgs = [argProgram] + args newEnv = {} newEnv.update(os.environ) newEnv.update(env) if echo: colorPrint(' '.join(newArgs), COMMAND_COLOR) if windows: # Windows doesn't support spawnvpe exitcode = os.spawnve(os.P_WAIT, program, newArgs, newEnv) else: exitcode = os.spawnvpe(os.P_WAIT, program, newArgs, newEnv) return exitcode
def runCommand(cmd): '''Run given command interactively. Return command exit code.''' cmd = cmd.split() code = os.spawnvpe(os.P_WAIT, cmd[0], cmd, os.environ) if code == 127: sys.stderr.write('{0}: command not found\n'.format(cmd[0])) return code
def main(): args = [PAGE_SIZE * i for i in [0, 1, 10, 100, 1000]] with tempfile.NamedTemporaryFile() as outfile: test_env = os.environ.copy() test_env['LD_PRELOAD'] = './stackreport.so' test_env['SR_OUTPUT_FILE'] = outfile.name cmd_line = [TEST_PROG] + map(str, args) #cmd_line = 'gdb --args'.split() + cmd_line print 'run: %s' % ' '.join(cmd_line) rc = os.spawnvpe(os.P_WAIT, cmd_line[0], cmd_line, test_env) assert rc == 0, 'unexpected rc: %d' % rc outfile.file.seek(0) usage = parse_usage(outfile.readlines()) print usage for i in range(len(args)): tname = thread_name(i) assert tname in usage print '%s: %u' % (tname, usage[tname]) assert usage[tname] >= args[i] assert usage[tname] - args[i] <= PAGE_SIZE * 4 print 'all OK'
def run(program, args = [], env = {}): program = removeQuotes(to_local_path(program)) # Windows doesn't support spawnvp, so we have to locate the binary if (os.name == 'nt'): program = findBinary(program) # spawn requires specification of argv[0] # Because the program name may contain spaces, we # add quotes around it. args.insert(0, '"' + program + '"') print string.join(args) + '\n' print '\n' if (os.name == 'nt'): # Windows doesn't support spawnvp if env != {}: exitcode = os.spawnve(os.P_WAIT, program, args, env) else: exitcode = os.spawnv(os.P_WAIT, program, args) else: if env != {}: exitcode = os.spawnvpe(os.P_WAIT, program, args, env) else: exitcode = os.spawnvp(os.P_WAIT, program, args) # Since we mutated the list, remove the element # that was inserted. args.pop(0) return exitcode
def cmd(cmd): cmd = cmd.split() code = os.spawnvpe(os.P_WAIT, cmd[0], cmd, os.environ) if code == 127: sys.stderr.write('{0}: command not found\n'.format( cmd[0])) return code
def __LaunchPointCalc(self, suffix, vars): #Create a unique running dir for this specific point runPath = os.path.join(self.scriptRunPath, "runPoint_" + suffix) os.system("rm -rf " + runPath) os.system("mkdir -p " + runPath) #Copy all necessary files to the point running dir os.system("cp -rf " + os.path.join(self.scriptFilesPath, "*") + " " + runPath) self.AllPaths.append(runPath) self.runningPaths.append(runPath) #Create the input file for this specific point initialXF = open(os.path.join(runPath, "varsPoint.txt"), "w") initialXF.write(str(len(vars)) + "\r\n") initialXF.write(str(vars)[1:-1].replace(", ", "\r\n") + "\r\n") initialXF.close() #Launch the thread. Don't wait for it to finish. We will check this dir periodically looking for # the rersults file. args = [os.path.join(runPath, self.ScriptFileName),\ os.path.realpath(runPath),\ os.path.join(runPath,"varsPoint.txt"),\ os.path.join(runPath, "resPoint.txt")] if self.verbose > 1: print "Command:", args exitCode = os.spawnvpe(os.P_NOWAIT, os.path.join(runPath, self.ScriptFileName), args, os.environ) if self.verbose > 1: print "Exited code:", exitCode return True
def run(l, env=None): log("run", l) env = dict(os.environ, **env) if env else None if isinstance(l, list): if env: rc = os.spawnvpe(os.P_WAIT, l[0], l, env) else: rc = os.spawnvp(os.P_WAIT, l[0], l) elif isinstance(l, str): tmp = ['sh', '-c', l] if env: rc = os.spawnvpe(os.P_WAIT, tmp[0], tmp, env) else: rc = os.spawnvp(os.P_WAIT, tmp[0], tmp) log("run", rc=rc) return rc
def execute_command(self, parts, dry_run): """ Execute a command. Parameters ---------- parts : list Sequence of strings constituting a command. dry_run : bool Whether to just log the command instead of executing it. Returns ------- status : int Status code of the executed command or 0 if `dry_run` is `True`. """ if dry_run: self.logger.info("dry-run command '%s'", " ".join(map(str, parts))) return 0 else: # pragma: no cover self.logger.debug("executing command '%s'", " ".join(map(str, parts))) status_code = os.spawnvpe(os.P_WAIT, parts[0], parts, os.environ) if status_code: self.logger.warning("command '%s' returned status code %d", " ".join(map(str, parts)), status_code) return status_code
def run_interactive_command(command): log.debug('Executing inaractive command: {}'.format(command)) cmd = command.split() code = os.spawnvpe(os.P_WAIT, cmd[0], cmd, os.environ) if code == 127: log.error('{0}: command not found'.format(cmd[0]))
def do_start(self, arg): self.get_status() if not self.zd_up: args = [ self.options.python, self.options.interpreter, self.options.zdrun, ] args += self._get_override("-S", "schemafile") args += self._get_override("-C", "configfile") args += self._get_override("-b", "backofflimit") args += self._get_override("-d", "daemon", flag=1) args += self._get_override("-f", "forever", flag=1) args += self._get_override("-s", "sockname") args += self._get_override("-u", "user") if self.options.umask: args += self._get_override("-m", "umask", oct(self.options.umask)) args += self._get_override( "-x", "exitcodes", ",".join(map(str, self.options.exitcodes)) ) args += self._get_override("-z", "directory") args.extend(self.options.program) if self.options.daemon: flag = os.P_NOWAIT else: flag = os.P_WAIT env = self.environment().copy() env.update({'ZMANAGED': '1', }) os.spawnvpe(flag, args[0], args, env) elif not self.zd_pid: self.send_action("start") else: print('daemon process already running; pid={}'.format( self.zd_pid)) return def cond(n=0): return self.zd_pid self.awhile( cond, 'daemon process started, pid=%(zd_pid)d' )
def run_browser(url): import os search = os.environ['PATH'].split(':') for browser in ['firefox','konqueror','epiphany','galeon','mozilla']: for path in search: prog = os.path.join(path,browser) if os.path.isfile(prog): os.spawnvpe(os.P_NOWAIT, prog, [prog, url], os.environ) return # If we did not find a browser in the path, try this try: os.startfile(url) except: pass
def run_browser(url): import os search = os.environ['PATH'].split(':') for browser in ['firefox', 'konqueror', 'epiphany', 'galeon', 'mozilla']: for path in search: prog = os.path.join(path, browser) if os.path.isfile(prog): os.spawnvpe(os.P_NOWAIT, prog, [prog, url], os.environ) return # If we did not find a browser in the path, try this try: os.startfile(url) except: pass
def open_chute_shell(ctx, chute, service): """ Open a shell inside the running chute. CHUTE must be the name of a running chute. This requires you to have enabled SSH access to the device and installed bash inside your chute. Changes made to files inside the chute may not be persistent if the chute or the node is restarted. Only changes to files in the "/data" directory will be preserved. """ container_name = "{}-{}".format(chute, service) cmd = ["ssh", "-t", "paradrop@{}".format(ctx.obj['target']), "sudo", "docker", "exec", "-it", container_name, "/bin/bash"] os.spawnvpe(os.P_WAIT, "ssh", cmd, os.environ)
def createNode(port, x, y): #args = ['./runseed.sh', 'solipsis/core/startSeed.py'] args = ['./runseed.sh'] args += ['-b', '-p', str(port)] args += ['-x', str(x), '-y', str(y), '-f', 'conf/seed.conf'] print args nodePID = os.spawnvpe(os.P_NOWAIT, './runseed.sh', args, os.environ) os.system
def run(l, env=None): """Run a command described by l in environment env""" log("run", l) env = dict(os.environ, **env) if env else None if isinstance(l, list): if env: rc = os.spawnvpe(os.P_WAIT, l[0], l, env) else: rc = os.spawnvp(os.P_WAIT, l[0], l) elif isinstance(l, str): tmp = ['sh', '-c', l] if env: rc = os.spawnvpe(os.P_WAIT, tmp[0], tmp, env) else: rc = os.spawnvp(os.P_WAIT, tmp[0], tmp) log("run", rc=rc) return rc
def load_plugin(self, name, settings): logging.info("Bot.plugin_load %s, %s", name, settings) file_name = "plugins/" + name + "/" + name + ".py" if not os.path.isfile(file_name): logging.error("Unable to load plugin %s", name) else: logging.info("Bot.plugin_load plugin %s, %s, %s, %s", name, self, sys.executable, [sys.executable, file_name]) environment = os.environ environment.update(PYTHONPATH=os.getcwd()) os.spawnvpe( os.P_NOWAIT, sys.executable, args=[sys.executable, file_name, "--socket_path", self.temp_folder], env=environment ) self.plugins.append(PluginInterface(name, self))
def testRunIgnoresParentSignals(self): # Spawn a process which will in turn spawn a zdrun process. # We make sure that the zdrun process is still running even if # its parent process receives an interrupt signal (it should # not be passed to zdrun). tmp = tempfile.mkdtemp() zdrun_socket = os.path.join(tmp, 'testsock') try: zdctlpid = os.spawnvpe( os.P_NOWAIT, sys.executable, [sys.executable, os.path.join(self.here, 'parent.py'), tmp], dict(os.environ, PYTHONPATH=":".join(sys.path), ) ) # Wait for it to start, but no longer than a minute. deadline = time.time() + 60 is_started = False while time.time() < deadline: response = send_action('status\n', zdrun_socket) if response is None: time.sleep(0.05) else: is_started = True break self.assertTrue(is_started, "spawned process failed to start in a minute") # Kill it, and wait a little to ensure it's dead. os.kill(zdctlpid, signal.SIGINT) time.sleep(0.25) # Make sure the child is still responsive. response = send_action('status\n', zdrun_socket, raise_on_error=True) self.assertTrue(b'\n' in response, 'no newline in response: ' + repr(response)) # Kill the process. send_action('exit\n', zdrun_socket) finally: # Remove the tmp directory. # Caution: this is delicate. The code here used to do # shutil.rmtree(tmp), but that suffers a sometimes-fatal # race with zdrun.py. The 'testsock' socket is created # by zdrun in the tmp directory, and zdrun tries to # unlink it. If shutil.rmtree sees 'testsock' too, it # will also try to unlink it, but zdrun may complete # unlinking it before shutil gets to it (there's more # than one process here). So, in effect, we code a # 1-level rmtree inline here, suppressing errors. for fname in os.listdir(tmp): try: os.unlink(os.path.join(tmp, fname)) except os.error: pass os.rmdir(tmp)
def perform_op(ra, ip, op): os.environ["OCF_RA_VERSION_MAJOR"] = "1" os.environ["OCF_RA_VERSION_MINOR"] = "0" os.environ["OCF_ROOT"] = CTSvars.OCF_ROOT_DIR os.environ["OCF_RESOURCE_INSTANCE"] = ip os.environ["OCF_RESOURCE_TYPE"] = ra os.environ["OCF_RESKEY_ip"] = ip os.environ["HA_LOGFILE"] = "/dev/null" os.environ["HA_LOGFACILITY"] = "local7" path = CTSvars.OCF_ROOT_DIR +"/resource.d/heartbeat/" + ra return os.spawnvpe(os.P_WAIT, path, [ra, op], os.environ)