def start(self): """Start the AIPS TV server.""" # Check if we already started the AIPS TV. if self._lock_pid and self._server_pid: raise RuntimeError, "the AIPS TV has already been started" # Create an environment for the AIPS TV processes. env = os.environ.copy() env['TVDEV'] = 'TVDEV01' if self._domain == socket.AF_INET: env['TVDEV01'] = 'sssin:localhost' else: env['TVLOK'] = 'TVLOK01' env['TVDEV01'] = self._address env['TVLOK01'] = self._address.replace('DEV', 'LOK') pass # Start the AIPS TV lock daemon. file = env['LOAD'] + '/TVSERV.EXE' self._lock_pid = os.spawnve(os.P_NOWAIT, file, ['TVSERVER'], env) # Start the AIPS TV server. file = env['LOAD'] + '/XAS' self._server_pid = os.spawnve(os.P_NOWAIT, file, ['XAS'], env) # Wait until the TV server has been started. time.sleep(2) return
def unpack (self, dry_run, verbose, callback, dest = None, filelist = None, list_files = False): if not dry_run: self.verify_md5_sig () self.environ['SHELL'] = self.bash_path if verbose: if dry_run: callback ('msg', '$ ' + self.bash_path + ' ' + os.path.normpath (self.path) + '\n', 2) else: if os.spawnve (os.P_WAIT, self.bash_path, [self.bash_path, '--noprofile', '--norc', self.path], self.environ): prog.ProgArchiveError ('error executing patch script ' + os.path.normpath (self.path)) else: if dry_run: callback ('msg', '$ ' + self.bash_path + ' ' + os.path.normpath (self.path) + ' >/dev/null\n', 2) else: if os.spawnve (os.P_WAIT, self.bash_path, [self.bash_path, '--noprofile', '--norc', self.path], self.environ): prog.ProgArchiveError ('error executing patch script ' + os.path.normpath (self.path))
def start(self, imports=None): """Start cherryd in a subprocess.""" cherrypy._cpserver.wait_for_free_port(self.host, self.port) args = [sys.executable, os.path.join(thisdir, '..', 'cherryd'), '-c', self.config_file, '-p', self.pid_file] if not isinstance(imports, (list, tuple)): imports = [imports] for i in imports: if i: args.append('-i') args.append(i) if self.daemonize: args.append('-d') env = os.environ.copy() # Make sure we import the cherrypy package in which this module is defined. grandparentdir = os.path.abspath(os.path.join(thisdir, '..', '..')) if env.get('PYTHONPATH', ''): env['PYTHONPATH'] = os.pathsep.join((grandparentdir, env['PYTHONPATH'])) else: env['PYTHONPATH'] = grandparentdir if self.wait: self.exit_code = os.spawnve(os.P_WAIT, sys.executable, args, env) else: os.spawnve(os.P_NOWAIT, sys.executable, args, env) cherrypy._cpserver.wait_for_occupied_port(self.host, self.port) # Give the engine a wee bit more time to finish STARTING if self.daemonize: time.sleep(2) else: time.sleep(1)
def processJSONevent(self, jsonstr): 'Process a single JSON event from out input stream' eventobj = pyConfigContext(jsonstr) aobj = eventobj['associatedobject'] aobjclass = aobj['nodetype'] eventtype = AssimEvent.eventtypenames[eventobj['eventtype']] env = {} # Initialize the child environment with our current environment for item in os.environ: env[item] = os.environ[item] # Add in things in 'extrainfo' (if any) if 'extrainfo' in eventobj and eventobj['extrainfo'] is not None: extrastuff = eventobj['extrainfo'] for extra in extrastuff.keys(): evextra = extrastuff[extra] env['ASSIM_%s' % extra] = str(evextra) # Add all the scalars in the associated object for attr in aobj.keys(): avalue = aobj[attr] if isinstance(avalue, (str, unicode, int, float, long, bool)): env['ASSIM_%s' % attr] = str(avalue) env['ASSIM_JSONobj'] = str(jsonstr) # It's an event we want our scripts to know about... # So, let them know! if DEBUG: print >> sys.stderr, 'TO RUN: %s' % (str(self.listscripts())) for script in self.listscripts(): args = [script, eventtype, aobjclass] if DEBUG: print >> sys.stderr, 'STARTING SCRIPT: %s' % (str(args)) os.spawnve(os.P_WAIT, script, args, env) if DEBUG: print >> sys.stderr, 'SCRIPT %s IS NOW DONE' % (str(args))
def trace(target): # create output directory rootdir = os.getcwd() tracedir = os.path.join(rootdir, target) if not os.path.exists(tracedir): os.mkdir(tracedir) # get tcl script list f = open(SCRIPT_LIST_FILE, 'r') scripts = f.readlines() f.close() # get java path # spawnvpe is not supported in MS Windows java = os.path.join(os.path.join(os.getenv('JAVA_HOME'), 'bin'), 'java') # run script file for line in scripts: tclfile = string.strip(line) # if not marked as inactive if not tclfile[0] == '#': tracefile = os.path.join(tracedir, \ os.path.basename(tclfile) + '.trace') os.chdir(os.path.dirname(tclfile)) print ' - running: ' + os.path.basename(tclfile) run_args = ['java', 'drcl.ruv.System', '-ue', os.path.basename(tclfile), \ '-testout', tracefile] os.spawnve(os.P_WAIT, java, run_args, os.environ) os.chdir(rootdir)
def PreView3D (): misc.SetSlang() if misc.isSlang == 1: PSLCreate(mode = 1) if misc.solverName != "": os.spawnve(os.P_WAIT, misc.solverName, [misc.solverName, misc.solverFileName], os.environ)
def start(self, imports = None): cherrypy._cpserver.wait_for_free_port(self.host, self.port) args = [sys.executable, os.path.join(thisdir, '..', 'cherryd'), '-c', self.config_file, '-p', self.pid_file] if not isinstance(imports, (list, tuple)): imports = [imports] for i in imports: if i: args.append('-i') args.append(i) if self.daemonize: args.append('-d') env = os.environ.copy() grandparentdir = os.path.abspath(os.path.join(thisdir, '..', '..')) if env.get('PYTHONPATH', ''): env['PYTHONPATH'] = os.pathsep.join((grandparentdir, env['PYTHONPATH'])) else: env['PYTHONPATH'] = grandparentdir if self.wait: self.exit_code = os.spawnve(os.P_WAIT, sys.executable, args, env) else: os.spawnve(os.P_NOWAIT, sys.executable, args, env) cherrypy._cpserver.wait_for_occupied_port(self.host, self.port) if self.daemonize: time.sleep(2) else: time.sleep(1)
def run_viewer(self,marguments=[]): myarg=[os.path.join(self.mypythonbase,'osgviewer')] for i in marguments: myarg.append(i) print self.myenv print "-->",myarg print os.environ os.spawnve(os.P_NOWAIT,os.path.join(self.mypythonbase,'osgviewer'), myarg,self.myenv)
def Post (): if misc.postName == "": return opts, arg = getopt.getopt(sys.argv[1:], "", ["post"]) # print opts, arg for o, a in opts: if o =="--post": os.spawnve(os.P_WAIT, misc.postName, [misc.postName, misc.postFileName], os.environ)
def uninterruptible_spawnve (mode, file, args, env): try: if env is None: os.spawnv (mode, file, args) else: os.spawnve (mode, file, args, env) except os.error, (err, errstr): if err != errno.EINTR: raise
def create_spawnve(original_name): """ os.spawnve(mode, path, args, env) os.spawnvpe(mode, file, args, env) """ def new_spawnve(mode, path, args, env): import os return getattr(os, original_name)(mode, path, patch_args(args), env) return new_spawnve
def post_fork_child(self): """Post-fork() child callback for ProcessManager.daemon_spawn().""" entry_point = '{}:launch'.format(__name__) exec_env = combined_dict(os.environ, dict(PANTS_ENTRYPOINT=entry_point)) # Pass all of sys.argv so that we can proxy arg flags e.g. `-ldebug`. cmd = [sys.executable] + sys.argv self._logger.debug('cmd is: PANTS_ENTRYPOINT={} {}'.format(entry_point, ' '.join(cmd))) # TODO: Improve error handling on launch failures. os.spawnve(os.P_NOWAIT, sys.executable, cmd, env=exec_env)
def Run (): # gen = af.PSLGenerator () # file_name = sys.argv[0] + ".psl" # file_name = file_name.replace(".py.",".") # gen.SaveToFile (doc, file_name) # cwd = file_name #test # print cwd # slang = os.getenv("DINSYS")+"\dinama\pradis32\slang" if misc.isSlang == 1: PSLCreate() if misc.solverName != "": os.spawnve(os.P_WAIT, misc.solverName, [misc.solverName, misc.solverFileName], os.environ)
def Simulation_Scheme(Scheme_Name): # функция запуска моделирования схемы Position =Scheme_Name.rfind('.') Extension=Scheme_Name[Position:] Name=Scheme_Name[:Position] if Extension=='.sch': Dat=Name_Name+'.sch.psl.DAT' elif Extension=='.py': os.spawnve(os.P_WAIT, os.getenv("DINSYS")+"\Python24\python", [os.getenv("DINSYS")+"\Python24\python", Scheme_Name], os.environ) Dat_Name=Name+'.psl.DAT' elif Extension=='.psl': os.spawnve(os.P_WAIT, os.getenv("DINSYS")+"\dinama\pradis32\slang", [os.getenv("DINSYS")+"\dinama\pradis32\slang", Scheme_Name], os.environ) Dat_Name=Name+'.psl.DAT' return Dat_Name
def openPythonWindow(self, event=None): '''Open Python's Idle debugger in a separate process.''' try: idlelib_path = imp.find_module('idlelib')[1] except ImportError: g.es_print('idlelib not found: can not open a Python window.') return idle = g.os_path_join(idlelib_path, 'idle.py') args = [sys.executable, idle] if 1: # Use present environment. os.spawnv(os.P_NOWAIT, sys.executable, args) else: # Use a pristine environment. os.spawnve(os.P_NOWAIT, sys.executable, args, os.environ)
def spawnve(mode, file, args, env): spawn_lock.acquire() try: if mode == os.P_WAIT: ret = os.spawnve(os.P_NOWAIT, file, args, env) else: ret = os.spawnve(mode, file, args, env) finally: spawn_lock.release() if mode == os.P_WAIT: pid, status = os.waitpid(ret, 0) ret = status >> 8 return ret
def make_jks_trust_store(self): """ Prepare a JKS TrustStore, for the CA. """ keytool = system.which('keytool') argv = [keytool, "-import", "-trustcacerts", "-noprompt", "-alias", "mercuryCA", "-file", self.ca_certificate_path(), "-keystore", self.__jks_path('mercury-ca'), "-storetype", "JKS", "-storepass", "mercury"] if keytool != None: if os.spawnve(os.P_WAIT, argv[0], argv, os.environ) == 0: return self.__bks_path('mercury-ca') else: argv[0] = "keytool" print "Could not compile the JKS trust store, because keytool could not be located on your system." print "Run:" print " ".join(argv) return False
def unpack (self, dry_run, verbose, callback, dest = None, filelist = None, list_files = False): if not dry_run: self.verify_md5_sig () _gzip_path = misclib.find_path_prog ('gzip', self.environ) if not _gzip_path: raise prog.ProgProgramNotFound ('gzip') _gtar_path = misclib.find_path_prog ('gtar', self.environ) if not _gtar_path: raise prog.ProgProgramNotFound ('gtar') if verbose: _gtar_flags = 'zxvf' else: _gtar_flags = 'zxf' if verbose: callback ('msg', '$ ' + _gtar_path + ' ' + _gtar_flags + ' ' + os.path.normpath (self.path) + '\n', 2) if not dry_run: if os.spawnve (os.P_WAIT, _gtar_path, [_gtar_path, _gtar_flags, self.path], self.environ): raise prog.ProgArchiveError ('error using gtar to unpack source')
def spawnve(space, mode, path, w_args, w_env): args = [space.str0_w(w_arg) for w_arg in space.unpackiterable(w_args)] env = _env2interp(space, w_env) try: ret = os.spawnve(mode, path, args, env) except OSError, e: raise wrap_oserror(space, e)
def spawn(*argv, **kwargs): """ Spawn a process """ if _sys.platform == 'win32': newargv = [] for arg in argv: if not arg or ' ' in arg or arg.startswith('"'): arg = '"%s"' % arg.replace('"', '\\"') newargv.append(arg) argv = newargv env = kwargs.get('env') if env is None: env = _os.environ echo = kwargs.get('echo') if echo: print ' '.join(argv) filepipe = kwargs.get('filepipe') if filepipe: return _filepipespawn( kwargs.get('stdin'), kwargs.get('stdout'), argv, env ) pipe = kwargs.get('stdout') if pipe: return _pipespawn(argv, env) pid = _os.spawnve(_os.P_NOWAIT, argv[0], argv, env) return _os.waitpid(pid, 0)[1]
def make_bks_key_store(self, cn, p12_path, export_password, store_password, key_password): """ Prepare a BouncyCastle KeyStore from a PKCS12 bundle. """ keytool = system.which('keytool') argv = [keytool, "-importkeystore", "-deststorepass", store_password, "-destkeypass", key_password, "-destkeystore", self.__bks_path(cn), "-deststoretype", "BKS", "-provider", "org.bouncycastle.jce.provider.BouncyCastleProvider", "-providerpath", os.path.abspath(os.path.join(os.path.dirname(__file__), "bcprov-ext-jdk15on-1.46.jar")), "-srckeystore", p12_path, "-srcstoretype", "PKCS12", "-srcstorepass", export_password, "-alias", "mercury"] if keytool != None: if os.spawnve(os.P_WAIT, argv[0], argv, os.environ) == 0: return self.__bks_path(cn) else: argv[0] = "keytool" print "Could not compile the BKS keystore, because keytool could not be located on your system." print "Run:" print " ".join(argv) return False
def make_bks_trust_store(self): """ Prepare a BouncyCastle TrustStore, for the CA. """ keytool = system.which('keytool') argv = [keytool, "-import", "-trustcacerts", "-noprompt", "-alias", "mercuryCA", "-file", self.ca_certificate_path(), "-keystore", self.__bks_path('mercury-ca'), "-storetype", "BKS", "-storepass", "mercury", "-provider", "org.bouncycastle.jce.provider.BouncyCastleProvider", "-providerpath", os.path.abspath(os.path.join(os.path.dirname(__file__), "bcprov-ext-jdk15on-1.46.jar"))] if keytool != None: if os.spawnve(os.P_WAIT, argv[0], argv, os.environ) == 0: return self.__bks_path('mercury-ca') else: argv[0] = "keytool" print "Could not compile the BKS trust store, because keytool could not be located on your system." print "Run:" print " ".join(argv) return False
def executePreload(thrille_root, preload, binary, binflags): os.environ["LD_PRELOAD"] = os.path.join(thrille_root, "bin", preload) binarydir, bin = os.path.split(binary) thing = os.spawnve(os.P_NOWAIT, binary, binflags, os.environ) pid, exit = os.waitpid(thing, 0) del os.environ["LD_PRELOAD"] return exit
def unpack (self, dry_run, verbose, callback, dest = None, filelist = None, list_files = False): if not dry_run: self.verify_md5_sig () _gpatch_path = misclib.find_path_prog ('gpatch', self.environ) if not _gpatch_path: raise prog.ProgProgramNotFound ('gpatch') if not dry_run: if not os.path.exists (self.build_prefix): raise prog.ProgArchiveError ('build prefix "' + build_prefix + '" does not exist. maybe you \ specified the wrong build directory in the build-name element?') arg = self.arg if not arg: arg = '-p0' if verbose: callback ('msg', '$ ' + _gpatch_path + ' ' + arg + ' -i ' + os.path.normpath (self.path) + '\n', 2) if not dry_run: if os.spawnve (os.P_WAIT, _gpatch_path, [_gpatch_path, arg, '-i', self.path], self.environ): raise prog.ProgArchiveError ('error using gpatch to patch source')
def unpack (self, dry_run, verbose, callback, dest = None, filelist = [], list_files = True): if not dry_run: self.verify_md5_sig () _unzip_flags = [] _unzip_flags_string = '' if not list_files: _unzip_flags = ['-q'] _unzip_flags_string += ' -q' if dest: _unzip_flags += ['-d', dest] _unzip_flags_string += ' -d ' + dest _filelist_string = '' if filelist: _filelist_string = ' ' + string.join (filelist, ' ') if verbose: callback ('msg', '$ ' + unzip_prog + _unzip_flags_string + ' -o ' + os.path.normpath (self.path) + _filelist_string + '\n', 2) if not dry_run: if os.spawnve (os.P_WAIT, unzip_prog, [unzip_prog] + _unzip_flags + ['-o', self.path] + filelist, self.environ): raise prog.ProgArchiveError ('error unzip to unpack source')
def make_jks_trust_store(self): """ Prepare a JKS TrustStore, for the CA. """ keytool = Configuration.executable("keytool") argv = [ keytool, "-import", "-trustcacerts", "-noprompt", "-alias", "andsploitCA", "-file", self.ca_certificate_path(), "-keystore", self.__jks_path("andsploit-ca"), "-storetype", "JKS", "-storepass", "andsploit", ] if keytool != None: if os.spawnve(os.P_WAIT, argv[0], argv, os.environ) == 0: return self.__bks_path("andsploit-ca") else: argv[0] = "keytool" print "Could not compile the JKS trust store, because keytool could not be located on your system." print "Run:" print " ".join(argv) return False
def spawn_unittest_runner(self, testables, extra_arguments=None): """Spawn test runner process""" args = [sys.executable, '-m', 'modipyd.tools.unittest_runner'] if extra_arguments: args.extend(extra_arguments) if self.test_runner: args.extend(['-r', self.test_runner]) for t in testables: args.append(t.filename) args = [str(arg) for arg in args] if sys.platform == "win32": # Avoid argument parsing problem in # windows, DOS platform args = ['"%s"' % arg for arg in args] # Manipulate PYTHONPATH environment variable so that # the unittest runner can find an appropriate modipyd package. environ = os.environ.copy() path = os.path.join(os.path.dirname(modipyd.__file__), '..') try: environ['PYTHONPATH'] += (':' + path) except KeyError: environ['PYTHONPATH'] = path LOGGER.debug( "Spawn test runner process: PYTHONPATH=%s %s" % (path, ' '.join(args))) return os.spawnve(os.P_WAIT, sys.executable, args, environ)
def piped_spawn(sh, escape, cmd, args, env, stdout, stderr): # There is no direct way to do that in python. What we do # here should work for most cases: # In case stdout (stderr) is not redirected to a file, # we redirect it into a temporary file tmpFileStdout # (tmpFileStderr) and copy the contents of this file # to stdout (stderr) given in the argument if not sh: sys.stderr.write("scons: Could not find command interpreter, is it in your PATH?\n") return 127 else: # one temporary file for stdout and stderr tmpFileStdout = os.path.normpath(tempfile.mktemp()) tmpFileStderr = os.path.normpath(tempfile.mktemp()) # check if output is redirected stdoutRedirected = 0 stderrRedirected = 0 for arg in args: # are there more possibilities to redirect stdout ? if arg.find(">", 0, 1) != -1 or arg.find("1>", 0, 2) != -1: stdoutRedirected = 1 # are there more possibilities to redirect stderr ? if arg.find("2>", 0, 2) != -1: stderrRedirected = 1 # redirect output of non-redirected streams to our tempfiles if stdoutRedirected == 0: args.append(">" + str(tmpFileStdout)) if stderrRedirected == 0: args.append("2>" + str(tmpFileStderr)) # actually do the spawn try: args = [sh, "/C", escape(" ".join(args))] ret = os.spawnve(os.P_WAIT, sh, args, env) except OSError, e: # catch any error try: ret = exitvalmap[e[0]] except KeyError: sys.stderr.write("scons: unknown OSError exception code %d - %s: %s\n" % (e[0], cmd, e[1])) if stderr is not None: stderr.write("scons: %s: %s\n" % (cmd, e[1])) # copy child output from tempfiles to our streams # and do clean up stuff if stdout is not None and stdoutRedirected == 0: try: stdout.write(open(tmpFileStdout, "r").read()) os.remove(tmpFileStdout) except (IOError, OSError): pass if stderr is not None and stderrRedirected == 0: try: stderr.write(open(tmpFileStderr, "r").read()) os.remove(tmpFileStderr) except (IOError, OSError): pass return ret
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 spawn(*argv, **kwargs): """ Spawn a process """ if _sys.platform == "win32": newargv = [] for arg in argv: if not arg or " " in arg or arg.startswith('"'): arg = '"%s"' % arg.replace('"', '\\"') newargv.append(arg) argv = newargv env = kwargs.get("env") if env is None: env = _os.environ echo = kwargs.get("echo") if echo: print(" ".join(argv)) filepipe = kwargs.get("filepipe") if filepipe: return _filepipespawn(kwargs.get("stdin"), kwargs.get("stdout"), argv, env) pipe = kwargs.get("stdout") if pipe: return _pipespawn(argv, env) pid = _os.spawnve(_os.P_NOWAIT, argv[0], argv, env) return _os.waitpid(pid, 0)[1]
def restart_with_reloader(cls): while True: args = [sys.executable] + ['-W%s' % o for o in sys.warnoptions] + sys.argv new_environ = os.environ.copy() new_environ['MAIN_THREAD'] = 'true' exit_code = os.spawnve(os.P_WAIT, sys.executable, args, new_environ) if exit_code != 3: return exit_code
def restart_with_reloader(): while True: args = [sys.executable] + sys.argv if sys.platform == "win32": args = ['"%s"' % arg for arg in args] new_environ = os.environ.copy() new_environ["RUN_MAIN"] = 'true' exit_code = os.spawnve(os.P_WAIT, sys.executable, args, new_environ) if exit_code != 3: return exit_code
def restart_with_reloader(): while True: args = [sys.executable] + ['-W%s' % o for o in sys.warnoptions] + sys.argv if _win: args = ['"%s"' % arg for arg in args] new_environ = os.environ.copy() new_environ["RUN_MAIN"] = 'true' exit_code = os.spawnve(os.P_WAIT, sys.executable, args, new_environ) if exit_code != EXIT_CODE: return exit_code
def post_fork_child(self): """Post-fork() child callback for ProcessManager.daemon_spawn().""" spawn_control_env = { DAEMON_ENTRYPOINT: f"{self._daemon_entrypoint}:launch_new_pantsd_instance", # The daemon should run under the same sys.path as us; so we ensure # this. NB: It will scrub PYTHONPATH once started to avoid infecting # its own unrelated subprocesses. "PYTHONPATH": os.pathsep.join(sys.path), } exec_env = {**os.environ, **spawn_control_env} # Pass all of sys.argv so that we can proxy arg flags e.g. `-ldebug`. cmd = [sys.executable] + sys.argv spawn_control_env_vars = " ".join(f"{k}={v}" for k, v in spawn_control_env.items()) cmd_line = " ".join(cmd) logger.debug(f"pantsd command is: {spawn_control_env_vars} {cmd_line}") # TODO: Improve error handling on launch failures. os.spawnve(os.P_NOWAIT, sys.executable, cmd, env=exec_env)
def post_fork_child(self): """Post-fork() child callback for ProcessManager.daemon_spawn().""" spawn_control_env = dict( PANTS_ENTRYPOINT=f'{__name__}:launch', # The daemon should run under the same sys.path as us; so we ensure # this. NB: It will scrub PYTHONPATH once started to avoid infecting # its own unrelated subprocesses. PYTHONPATH=os.pathsep.join(sys.path)) exec_env = combined_dict(os.environ, spawn_control_env) # Pass all of sys.argv so that we can proxy arg flags e.g. `-ldebug`. cmd = [sys.executable] + sys.argv spawn_control_env_vars = ' '.join( f'{k}={v}' for k, v in spawn_control_env.items()) cmd_line = ' '.join(cmd) self._logger.debug(f'cmd is: {spawn_control_env_vars} {cmd_line}') # TODO: Improve error handling on launch failures. os.spawnve(os.P_NOWAIT, sys.executable, cmd, env=exec_env)
def restart_with_reloader(): while True: args = [sys.executable] + ['-W%s' % o for o in sys.warnoptions] + sys.argv if sys.platform == "win32": args = ['"%s"' % arg for arg in args] new_environ = os.environ.copy() new_environ["RUN_MAIN"] = 'true' # 'os.P_WAIT' will wait until process exit. exit_code = os.spawnve(os.P_WAIT, sys.executable, args, new_environ) if exit_code != 3: return exit_code
def executeSchedule(thrille_root, binary, binflags): os.environ['LD_PRELOAD'] = \ os.path.join(thrille_root, "bin", "libstrictserial.so") binarydir, bin = os.path.split(binary) binflags.insert(0, bin) print "Spawning:" print "\t", binary print "\t", binflags thing = os.spawnve(os.P_NOWAIT, binary, binflags, os.environ) pid, exit = os.waitpid(thing, 0) del os.environ['LD_PRELOAD']
def main(): # start process env = dict(os.environ) env.update({ 'G_MESSAGES_DEBUG': 'all', 'XDG_DATA_DIRS': '.', }) pid1 = os.spawnve(os.P_NOWAIT, './lunadhtd', '-b org.manuel.LunaDHTTest -p 17768'.split(), env) pid2 = os.spawnve(os.P_NOWAIT, './lunadhtd', [], env) time.sleep(1) # create connections bus = dbus.SessionBus(mainloop=DBusGMainLoop()) bus_name = 'org.manuel.LunaDHT' dht = bus.get_object(bus_name, '/org/manuel/LunaDHT') dht.join("::1", 7786, dbus_interface='org.manuel.LunaDHT') for i in range(5): dht.put(0xDEAD, "\0foo\0", "\0bar\0", 60*60, dbus_interface='org.manuel.LunaDHT') time.sleep(1) res = dht.get(0xDEAD, "\0foo\0", dbus_interface='org.manuel.LunaDHT') if len(res) > 0: actual_result = ''.join([chr(x) for x in res[0]]) expected_result = "\0bar\0" if actual_result == expected_result: print 'Test suceeded.' os.kill(pid1, signal.SIGTERM) os.kill(pid2, signal.SIGTERM) os.wait() os.wait() return raise 'Test failed.' main_loop.run()
def legacy_spawn_apis(proc, args): """ Deprecated APIs, but still possible attacks """ os.execl(proc, args) os.execl(proc, args) os.execle(proc, args) os.execlp(proc, args) os.execlpe(proc, args) os.execv(proc, args) os.execve(proc, args) os.execvp(proc, args) os.execvpe(proc, args) os.spawnl(proc, args) os.spawnle(proc, args) os.spawnlp(proc, args) os.spawnlpe(proc, args) os.spawnv(proc, args) os.spawnve(proc, args) os.spawnvp(proc, args) os.spawnvpe(proc, args)
def restart_with_reloader(): while True: args = [ sys.executable ] + sys.argv # ['/usr/bin/python2.7', '/path/examples/manage.py'] if sys.platform == "win32": args = ['"%s"' % arg for arg in args] new_environ = os.environ.copy() new_environ["RUN_MAIN"] = 'true' exit_code = os.spawnve(os.P_WAIT, sys.executable, args, new_environ) if exit_code != 3: return exit_code
def _restart_application_with_autoreload(): while True: new_environ = os.environ.copy() new_environ['RUN_MAIN_APP'] = 'true' exit_code = os.spawnve( os.P_WAIT, sys.executable, [sys.executable] + sys.argv, new_environ ) if exit_code != RELOAD_EXIT_CODE: return exit_code
def restart_with_reloader(): """Spawn a new Python interpreter with the same arguments as this one, but running the reloader thread.""" while True: print '* Restarting with reloader...' args = [sys.executable] + sys.argv if sys.platform == 'win32': args = ['"%s"' % arg for arg in args] new_environ = os.environ.copy() new_environ['RUN_MAIN'] = 'true' exit_code = os.spawnve(os.P_WAIT, sys.executable, args, new_environ) if exit_code != 3: return exit_code
def do_spawn(x=None): from main.settings import WindowsStarterFileName starter_filepath = os.path.join(bpio.getExecutableDir(), WindowsStarterFileName()) lg.out(0, "bpmain.main bitstarter.exe path: %s " % starter_filepath) if not os.path.isfile(starter_filepath): lg.out(0, "ERROR: %s not found\n" % starter_filepath) bpio.shutdown() return 1 cmdargs = [os.path.basename(starter_filepath), 'uninstall'] lg.out(0, "bpmain.main os.spawnve cmdargs=" + str(cmdargs)) ret = os.spawnve(os.P_DETACH, starter_filepath, cmdargs, os.environ) bpio.shutdown() return ret
def __init__(self, argv, env, stdio = False): if isIronPython: self.__pid = os.spawnve(os.P_NOWAIT, argv[0], argv, dict(env)) else: self.__proc = subprocess.Popen( argv, env = dict(env), shell = False, stdin = subprocess.PIPE if stdio else None, stdout = subprocess.PIPE if stdio else None, stderr = subprocess.PIPE if stdio else None )
def main(): # special preprocessing of command line arguments. # because setting the LD_LIBRARY_PATH only works correctly for new # processes, as the loader for the current one does not re-check the # variable. therefore we have to restart ourselves. do this now before any # other actions have been done, such as writing to stdout/stderr. library_path = None for x in sys.argv: if x.startswith('-l=') or x.startswith('--library-path='): _, library_path = x.split('=', 1) sys.argv.remove(x) break if x == '-l' or x == '--library-path': index = sys.argv.index(x) library_path = sys.argv.pop(index+1) sys.argv.remove(x) break if library_path is not None: #~ print sys.argv if sys.platform == 'win32': os.environ['PATH'] = '%s;%s' % (library_path, os.environ.get('PATH', '')) os.environ['LIBMSPGCC_PATH'] = library_path else: os.environ['LD_LIBRARY_PATH'] = library_path os.environ['LIBMSPGCC_PATH'] = library_path # start a new process if hasattr(sys, "frozen"): # py2exe sys.exit(os.spawnve(os.P_WAIT, sys.executable, [sys.executable] + sys.argv[1:], os.environ)) else: # XXX this does only work if module is in pythons search path.. if sys.platform == 'win32': os.spawnve(os.P_WAIT, sys.executable, [sys.executable, '-m', 'msp430.jtag.target'] + sys.argv[1:], os.environ) else: os.execve(sys.executable, [sys.executable, '-m', 'msp430.jtag.target'] + sys.argv[1:], os.environ) return # run the main application jtag_target = JTAG() jtag_target.main()
def start(self, imports=None): """Start cherryd in a subprocess.""" cherrypy._cpserver.wait_for_free_port(self.host, self.port) args = [ sys.executable, os.path.join(thisdir, '..', 'cherryd'), '-c', self.config_file, '-p', self.pid_file ] if not isinstance(imports, (list, tuple)): imports = [imports] for i in imports: if i: args.append('-i') args.append(i) if self.daemonize: args.append('-d') env = os.environ.copy() # Make sure we import the cherrypy package in which this module is defined. grandparentdir = os.path.abspath(os.path.join(thisdir, '..', '..')) if env.get('PYTHONPATH', ''): env['PYTHONPATH'] = os.pathsep.join( (grandparentdir, env['PYTHONPATH'])) else: env['PYTHONPATH'] = grandparentdir if self.wait: self.exit_code = os.spawnve(os.P_WAIT, sys.executable, args, env) else: os.spawnve(os.P_NOWAIT, sys.executable, args, env) cherrypy._cpserver.wait_for_occupied_port(self.host, self.port) # Give the engine a wee bit more time to finish STARTING if self.daemonize: time.sleep(2) else: time.sleep(1)
def _restart_with_reloader(): while True: args = [sys.executable] + sys.argv if sys.platform == 'win32': args = ['"%s"' % arg for arg in args] new_environ = os.environ.copy() new_environ['RUN_MAIN'] = 'true' # This call reinvokes ourself and goes into the other branch of main as # a new process. exit_code = os.spawnve(os.P_WAIT, sys.executable, args, new_environ) if exit_code != 3: return exit_code
def spawn(sh, escape, cmd, args, env): if not sh: sys.stderr.write( "scons: Could not find command interpreter, is it in your PATH?\n") return 127 else: try: args = [sh, '/C', escape(string.join(args))] ret = os.spawnve(os.P_WAIT, sh, args, env) except OSError, e: ret = exitvalmap[e[0]] sys.stderr.write("scons: %s: %s\n" % (cmd, e[1])) return ret
def restart_with_reloader(): ''' This method will spawn a new child when the exit code is 3. ''' while True: args = [sys.executable] + sys.argv if sys.platform == "win32": args = ['"%s"' % arg for arg in args] new_environ = os.environ.copy() new_environ["RUN_MAIN"] = 'true' exit_code = os.spawnve(os.P_WAIT, sys.executable, args, new_environ) if exit_code != 3: break sys.exit(exit_code)
def _restart_with_reloader(self): while True: args = [sys.executable] + sys.argv if sys.platform == "win32": args = ['"%s"' % arg for arg in args] new_environ = os.environ.copy() new_environ["RUN_MAIN"] = "true" exit_code = os.spawnve(os.P_WAIT, sys.executable, args, new_environ) if exit_code != 3: if exit_code != 0: yn = raw_input('Server terminated abnormally, restart? [Yn]') if not yn or yn.upper() == 'Y': continue return exit_code
def start(self): """Start the AIPS TV server.""" # Check if we already started the AIPS TV. if self._lock_pid and self._server_pid: raise RuntimeError, "the AIPS TV has already been started" # Create an environment for the AIPS TV processes. env = os.environ.copy() env['TVDEV'] = 'TVDEV01' env['TVDEV01'] = 'sssin:localhost' # Start the AIPS TV lock daemon. file = env['LOAD'] + '/TVSERV.EXE' self._lock_pid = os.spawnve(os.P_NOWAIT, file, ['TVSERVER'], env) # Start the AIPS TV server. file = env['LOAD'] + '/XAS' self._server_pid = os.spawnve(os.P_NOWAIT, file, ['XAS'], env) # Wait until the TV server has been started. time.sleep(2) return
def restart_with_reloader(): global pid while True: args = [sys.executable] + sys.argv if sys.platform == "win32": args = ['"%s"' % arg for arg in args] new_environ = os.environ.copy() new_environ["RUN_MAIN"] = 'true' pid = os.spawnve(os.P_NOWAIT, sys.executable, args, new_environ) _, exit_code = os.waitpid(pid, 0) exit_code = exit_code >> 8 # exit status is in the high byte if exit_code != 3: return exit_code
def run_command(cmd, verbose=False, env=None, cwd=None): if verbose: print 'In %r: %s' % (cwd or '.', cmd) if env is not None: new_env = {} for k, v in env.iteritems(): new_env[str(k)] = str(v) env = new_env shell = True if 0 and ('python.exe' in cmd and 'install' in cmd) or ( 'svn.exe' in cmd and '--version' not in cmd): oldcwd = os.getcwd() path, args = splitcmd(cmd) print 'os.spawnve path = %r, args=%r' % (path, args) if cwd: print 'chdir', cwd os.chdir(cwd) status = -1 try: status = os.spawnve(os.P_WAIT, path, args, env or {}) finally: print 'chdir', oldcwd os.chdir(oldcwd) return status, '', '' p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, env=env, cwd=cwd) stdout, stderr = p.communicate() stdout = stdout.replace('\r\n', '\n') stderr = stderr.replace('\r\n', '\n') status = p.returncode if verbose and status: print 'Command %r failed with returncode=%r' % (cmd, status) print '=' * 20 print 'STDOUT: %s' % (stdout) print '-' * 20 print 'STDERR: %s' % (stderr) print '=' * 20 elif verbose: print 'Command %r succesfully completed' % (cmd) print '=' * 20 print 'STDOUT: %s' % (stdout) print '-' * 20 print 'STDERR: %s' % (stderr) print '=' * 20 return status, stdout, stderr
def start_zeo_server(storage_conf, zeo_conf, port, keep=0): """Start a ZEO server in a separate process. Takes two positional arguments a string containing the storage conf and a ZEOConfig object. Returns the ZEO port, the test server port, the pid, and the path to the config file. """ # Store the config info in a temp file. tmpfile = tempfile.mktemp(".conf") fp = open(tmpfile, 'w') zeo_conf.dump(fp) fp.write(storage_conf) fp.close() # Find the zeoserver script import ZEO.tests.zeoserver script = ZEO.tests.zeoserver.__file__ if script.endswith('.pyc'): script = script[:-1] # Create a list of arguments, which we'll tuplify below qa = _quote_arg args = [qa(sys.executable), qa(script), '-C', qa(tmpfile)] if keep: args.append("-k") d = os.environ.copy() d['PYTHONPATH'] = os.pathsep.join(sys.path) pid = os.spawnve(os.P_NOWAIT, sys.executable, tuple(args), d) adminaddr = ('localhost', port + 1) # We need to wait until the server starts, but not forever. # 30 seconds is a somewhat arbitrary upper bound. A BDBStorage # takes a long time to open -- more than 10 seconds on occasion. for i in range(120): time.sleep(0.25) try: logger.debug('connect %s', i) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(adminaddr) ack = s.recv(1024) s.close() logging.debug('acked: %s' % ack) break except socket.error, e: if e[0] not in (errno.ECONNREFUSED, errno.ECONNRESET): raise s.close()
def cmd(self, cmd, soutfile=None, allowed_exit=[0], capture_stderr=False, timeout=None, mention_outputfile_on_errors=True): "Execute an OS command and captures the stderr and stdout which are returned in a file" if not soutfile: soutfile = tempfile.mktemp('.out') logger.debug('Running shell command: %s' % cmd) try: t0 = time.time() already_killed = False timeout0 = timeout pid = os.spawnve( os.P_NOWAIT, '/bin/sh', ['/bin/sh', '-c', '%s > %s 2>&1' % (cmd, soutfile)], self.env) while 1: wpid, sts = os.waitpid(pid, os.WNOHANG) if wpid != 0: if os.WIFSIGNALED(sts): rc = -os.WTERMSIG(sts) break elif os.WIFEXITED(sts): rc = os.WEXITSTATUS(sts) break if timeout and time.time() - t0 > timeout: logger.warning( 'Command interrupted - timeout %ss reached: %s', timeout0, cmd) if already_killed: sig = signal.SIGKILL else: sig = signal.SIGTERM logger.debug('killing process %d with signal %d', pid, sig) os.kill(pid, sig) t0 = time.time() timeout = 5 # wait just 5 seconds before killing with SIGKILL already_killed = True time.sleep(0.1) except OSError, (num, text): logger.warning('Problem with shell command: %s, %s', num, text) rc = 255
def exec_spawn(l, env): try: result = os.spawnve(os.P_WAIT, l[0], l, env) except OSError, e: try: result = exitvalmap[e[0]] sys.stderr.write("scons: %s: %s\n" % (l[0], e[1])) except KeyError: result = 127 if len(l) > 2: if len(l[2]) < 1000: command = ' '.join(l[0:3]) else: command = l[0] else: command = l[0] sys.stderr.write("scons: unknown OSError exception code %d - '%s': %s\n" % (e[0], command, e[1]))
def spawnwaitv(prog, args): """Spawn a program and wait for it to complete. The program is spawned in a modified environment.""" env = dict(os.environ) env.update(globals.program_env) # Print a status message if running in verbose mode if globals.verbose: print "Running '" + " ".join(args) + "' in " + os.getcwd() # Check that the program is runnable if not os.access(prog, os.X_OK): raise OSError, "Cannot execute '" + prog + "'" # Run the program return os.spawnve(os.P_WAIT, prog, args, env)
def system(self,cmd,allowed_exit=[0], stderr_file=None): """Execute on OS command. Useful for interactive commands. Stdout and Stderr are not caputured and are passed on the caller. stderr_capture may specify a name of a file to which stderr is redirected. """ logger.debug('Running shell command: %s' % cmd) if stderr_file: cmd += " 2> %s"%stderr_file try: rc = os.spawnve(os.P_WAIT,'/bin/sh',['/bin/sh','-c',cmd],self.env) except OSError, (num,text): logger.warning( 'Problem with shell command: %s, %s', num,text) rc = 255
def startup(self): """ Start a headless instance of OpenOffice. From here: http://www.linuxjournal.com/content/starting-stopping-and-connecting-openoffice-python """ args = ["soffice", '-accept=socket,host=localhost,port=%d;urp;StarOffice.ServiceManager' % self.port, '-norestore', '-nofirststartwizard', '-nologo', '-headless', ] try: pid = os.spawnve(os.P_NOWAIT, args[0], args, {}) except Exception, e: raise Exception, "Failed to start OpenOffice on port %d: %s" % (self.port, e.message)
def restart_with_reloader(script_dir=None): import __main__ while True: if not script_dir: script = os.path.abspath(__main__.__file__) else: script = os.path.abspath( os.path.join(script_dir, __main__.__file__)) args = [sys.executable, script] sys_argv = sys.argv[:] if len(sys_argv) > 1: sys_argv.pop(0) args = args + sys_argv new_environ = os.environ.copy() new_environ['RUN_MAIN'] = 'true' exit_code = os.spawnve(os.P_WAIT, sys.executable, args, new_environ) if exit_code != 3: return exit_code