def cargo(ctx, cmd, *args, **kwargs): """Run Cargo as if inside the specified crate directory. :param ctx: Invoke's Context object :param cmd: Cargo command to run The following are optional keyword arguments: :param wait: Whether to wait for the Cargo process to finish (True), or to replace the whole Invoke process with it (False) :return: If wait=True, the Invoke's Result object of the run. """ cargo_args = [cmd] cargo_args.extend(args) env = {'RUST_BACKTRACE': 'full'} wait = kwargs.pop('wait', True) if wait: kwargs.setdefault('env', {}).update(env) return ctx.run('cargo ' + ' '.join(map(quote, cargo_args)), **kwargs) else: argv = ['cargo'] + cargo_args # execvpe() needs explicit argv[0] env.update(os.environ) os.execvpe(argv[0], argv, env)
def Spawn(self, command, ExitCode = 0): if os.fork() == 0: if os.fork(): sys.exit(0) spawnopts = shlex.split(command) os.execvpe(spawnopts[0], spawnopts, os.environ) os.wait()
def handle(self, *args, **kwargs): env = os.environ.copy() args = [ "java", ] jar = spark.conf.LIVY_ASSEMBLY_JAR.get() classpath = jar + os.path.pathsep + env.get('CLASSPATH', '') args.extend(("-cp", classpath)) server_host = spark.conf.LIVY_SERVER_HOST.get() args.append("-Dlivy.server.host=" + server_host) server_port = spark.conf.LIVY_SERVER_PORT.get() args.append("-Dlivy.server.port=" + server_port) session_factory = spark.conf.LIVY_SERVER_SESSION_KIND.get() args.append("-Dlivy.server.session.factory=" + session_factory) livy_yarn_jar = spark.conf.LIVY_YARN_JAR.get() if livy_yarn_jar: args.append("-Dlivy.yarn.jar=" + livy_yarn_jar) args.append("com.cloudera.hue.livy.server.Main") LOG.info("Executing %r (%r) (%r)" % (args[0], args, env)) # Use exec, so that this takes only one process. os.execvpe(args[0], args, env)
def reexec(self): """\ Relaunch the master and workers. """ if self.reexec_pid != 0: self.log.warning("USR2 signal ignored. Child exists.") return if self.master_pid != 0: self.log.warning("USR2 signal ignored. Parent exists") return master_pid = os.getpid() self.reexec_pid = os.fork() if self.reexec_pid != 0: return self.cfg.pre_exec(self) environ = self.cfg.env_orig.copy() fds = [l.fileno() for l in self.LISTENERS] environ['GUNICORN_FD'] = ",".join([str(fd) for fd in fds]) environ['GUNICORN_PID'] = str(master_pid) os.chdir(self.START_CTX['cwd']) # exec the process using the original environnement os.execvpe(self.START_CTX[0], self.START_CTX['args'], environ)
def xexec(args, stdin=None): """exec [-h|--help] command [args...] exec (also aliased as xexec) uses the os.execvpe() function to replace the xonsh process with the specified program. This provides the functionality of the bash 'exec' builtin:: >>> exec bash -l -i bash $ The '-h' and '--help' options print this message and exit. Notes ----- This command **is not** the same as the Python builtin function exec(). That function is for running Python code. This command, which shares the same name as the sh-lang statement, is for launching a command directly in the same process. In the event of a name conflict, please use the xexec command directly or dive into subprocess mode explicitly with ![exec command]. For more details, please see http://xon.sh/faq.html#exec. """ if len(args) == 0: return (None, 'xonsh: exec: no args specified\n', 1) elif args[0] == '-h' or args[0] == '--help': return inspect.getdoc(xexec) else: denv = builtins.__xonsh_env__.detype() try: os.execvpe(args[0], args, denv) except FileNotFoundError as e: return (None, 'xonsh: exec: file not found: {}: {}' '\n'.format(e.args[1], args[0]), 1)
def run(self, extra_args = None, parent_win = None): "run([parent window][,embedding args]), runs Hatari" # if parent_win given, embed Hatari to it pid = os.fork() if pid < 0: print("ERROR: fork()ing Hatari failed!") return if pid: # in parent self.pid = pid if self.server: print("WAIT hatari to connect to control socket...") (self.control, addr) = self.server.accept() print("connected!") else: # child runs Hatari env = os.environ if parent_win: self._set_embed_env(env, parent_win) # callers need to take care of confirming quitting args = [self.hataribin, "--confirm-quit", "off"] if self.server: args += ["--control-socket", self.controlpath] if extra_args: args += extra_args print("RUN:", args) os.execvpe(self.hataribin, args, env)
def shell(self): self.callee = utils.User(name='nobody') assert self.callee is not None try: os.chdir(self.path or self.callee.dir) except: pass env = os.environ # User has been auth with ssl or is the same user as server # or login is explicitly turned off if ( not tornado.options.options.unsecure and tornado.options.options.login and not ( self.socket.local and self.caller == self.callee and server == self.callee )): # User is authed by ssl, setting groups try: os.initgroups(self.callee.name, self.callee.gid) os.setgid(self.callee.gid) os.setuid(self.callee.uid) except: print('The server must be run as root ' 'if you want to log as different user\n') sys.exit(1) args = [tornado.options.options.shell or self.callee.shell] args.append('-q') args.append('--lf') args.append('/dev/pts/2') os.execvpe(args[0], args, env)
def run(self, name, *args): self.parser.set_usage(self.envdir_usage) self.parser.prog = "envdir" options, args = self.parser.parse_args(list(args)) if len(args) < 2: raise Response( "%s\nError: incorrect number of arguments\n" % (self.parser.get_usage()), 2, ) self.open(args[0], 2) # the args to call later args = args[1:] # in case someone passes in -- for any reason to separate the commands if args[0] == "--": args = args[1:] try: os.execvpe(args[0], args, os.environ) except OSError as err: raise Response( "Unable to run command %s: %s" % (args[0], err), status=err.errno ) raise Response()
def log_and_exec(self, cmd, args=[], env=None): """Log cmd and execve.""" logging.info([cmd, args]) if (self.dryrunFlag): print "os.execvpe (%s, %s, env)" % (cmd, [cmd] + args) return True os.execvpe(cmd, [cmd] + args, env)
def exec_command(self, args): env = dict(os.environ) virtualenv = self.get_virtualenv() env['PATH'] = virtualenv + "/bin:" + env['PATH'] env['LD_LIBRARY_PATH'] = "usr/local/lib" env['DYLD_LIBRARY_PATH'] = "usr/local/lib" os.execvpe(args[0], args, env)
def execvpe(self): """Convenience method exec's the command replacing the current process. Returns: Does not return. May raise an OSError though. """ args = copy(self._args) if self._shell: assert len(args) == 1 if _is_windows(): # The issue here is that in Lib/subprocess.py in # the Python distribution, if shell=True the code # jumps through some funky hoops setting flags on # the Windows API calls. We need to do that, rather # than calling os.execvpe which doesn't let us set those # flags. So we spawn the child and then exit. self.popen() sys.exit(0) else: # this is all shell=True does on unix args = ['/bin/sh', '-c'] + args try: old_dir = os.getcwd() os.chdir(self._cwd) sys.stderr.flush() sys.stdout.flush() _verbose_logger().info("$ %s", " ".join(args)) os.execvpe(args[0], args, self._env) finally: # avoid side effect if exec fails (or is mocked in tests) os.chdir(old_dir)
def contain(command, env): # TODO: exec command, note the difference between the exec flavours # https://docs.python.org/2/library/os.html#os.execv # NOTE: command is an array (the first element is path/file, and the entire # array is exec's args) os.execvpe(command[0], command, env) os._exit(0) # TODO: remove this after adding exec
def run(self, args): cmd = self.get_command(args) env = os.environ.copy() env.update(self.get_env(args)) _print_command(cmd) os.execvpe(cmd[0], cmd, env)
def _sqlite_shell(): if app.config['SQLALCHEMY_DATABASE_URI'] == 'sqlite://': print("Cannot start shell for in-memery database.") return 1 else: args = ['sqlite3', app.config['SQLALCHEMY_DATABASE_URI'][10:],] os.execvpe('sqlite3', args, os.environ)
def start(process, options): if process.alive(): print 'Already running as %s' % process.read_pid() return create_app_symlinks(options) args, env = build_java_execution(options, True) makedirs(dirname(options.launcher_log)) log = open_append(options.launcher_log) makedirs(options.data_dir) os.chdir(options.data_dir) pid = os.fork() if pid > 0: process.write_pid(pid) print 'Started as %s' % pid return os.setsid() redirect_stdin_to_devnull() redirect_output(log) os.close(log) os.execvpe(args[0], args, env)
def exec_piped_fork(l, env, stdout, stderr): # spawn using fork / exec and providing a pipe for the command's # stdout / stderr stream if stdout != stderr: (rFdOut, wFdOut) = os.pipe() (rFdErr, wFdErr) = os.pipe() else: (rFdOut, wFdOut) = os.pipe() rFdErr = rFdOut wFdErr = wFdOut # do the fork pid = os.fork() if not pid: # Child process os.close( rFdOut ) if rFdOut != rFdErr: os.close( rFdErr ) os.dup2( wFdOut, 1 ) # is there some symbolic way to do that ? os.dup2( wFdErr, 2 ) os.close( wFdOut ) if stdout != stderr: os.close( wFdErr ) exitval = 127 try: os.execvpe(l[0], l, env) except OSError, e: exitval = exitvalmap.get(e[0], e[0]) stderr.write("scons: %s: %s\n" % (l[0], e[1])) os._exit(exitval)
def _execChild(self, path, settingUID, uid, gid, command, args, environment): if path: os.chdir(path) if settingUID: switchUID(uid, gid) os.execvpe(command, args, environment)
def new_master(self): server = self.server os.environ[server.environ_fd_name] = str(server.socket.fileno()) if not os.fork(): self.log.info("in forked child, execing new master") os.execvpe(sys.executable, [sys.executable] + sys.argv, os.environ)
def reexec(self): """\ Relaunch the master and workers. """ if self.pidfile is not None: self.pidfile.rename("%s.oldbin" % self.pidfile.fname) self.reexec_pid = os.fork() if self.reexec_pid != 0: self.master_name = "Old Master" return environ = self.cfg.env_orig.copy() fds = [l.fileno() for l in self.LISTENERS] environ['GUNICORN_FD'] = ",".join([str(fd) for fd in fds]) os.chdir(self.START_CTX['cwd']) self.cfg.pre_exec(self) # exec the process using the original environnement os.execvpe(self.START_CTX[0], self.START_CTX['args'], environ)
def run(self): assert self.command LOG.info("Creating private logs directory '%s/'", self.log_dir) if self.log_dir.is_dir(): self.log_dir.chmod(0o777) else: self.log_dir.mkdir(mode=0o777) LOG.info("Redirecting stdout/stderr to %s/live.log", self.log_dir) sys.stdout.flush() sys.stderr.flush() # redirect stdout/stderr to a log file # not sure if the assertions would print with (self.log_dir / "live.log").open("w") as log: result = os.dup2(log.fileno(), 1) assert result != -1, "dup2 failed: " + os.strerror(ctypes.get_errno()) result = os.dup2(log.fileno(), 2) assert result != -1, "dup2 failed: " + os.strerror(ctypes.get_errno()) os.execvpe(self.command[0], self.command, self.environment)
def hlp_no_x(p, extra_args): node_args = [] if p.selector != None: node_args += ["-l", p.selector] if p.nodes != None: node_args += p.nodes nodes = get_nodes_with_external_host(node_args) for node_name in nodes: print("Name:", node_name, file=sys.stderr) node_host = nodes[node_name] if node_host == None: print("(no external access)", file=sys.stderr) continue cmd = [] if p.no_sudo != True: cmd = ["sudo"] if p.command != None and len(p.command) > 0 and p.command != "sh": cmd += [p.command] else: cmd += [SHELL_FINDER] if len(nodes) > 1: subprocess.run([ "ssh", "-t", "-o", "UserKnownHostsFile=/dev/null", "-o", "StrictHostKeyChecking=off", "-o", "LogLevel=error", p.user + "@" + node_host ] + cmd) else: os.execvpe("ssh", [ "ssh", "-t", "-o", "UserKnownHostsFile=/dev/null", "-o", "StrictHostKeyChecking=off", "-o", "LogLevel=error", p.user + "@" + node_host ] + cmd, env=os.environ)
def mc(ctx, args): r = ctx["session"].post( "token/search", json={ "search_terms": args, }, ) if r.ok: response = r.json() assembled = " ".join(args) mc_env = "" for arg, match in response["workspaces"].items(): workspace = schemas.WorkspaceDB(**match["workspace"]) scope = workspace.root.root_type.lower() key = s3utils.getWorkspaceKey(workspace) path = "/".join([ "myalias", workspace.root.bucket, key, match["path"].lstrip("/"), ]) assembled = assembled.replace(arg, path) if len(response["tokens"]) == 1: token = response["tokens"][0]["token"] access_key = token["access_key_id"] secret = token["secret_access_key"] session_token = token["session_token"] api_url = response["tokens"][0]["node"]["api_url"] url = urllib.parse.urlparse(api_url) mc_env = ( f"{url.scheme}://{access_key}:{secret}:{session_token}@{url.netloc}" ) command = ( "mc", *assembled.split(" "), ) os.execvpe(command[0], command, dict(os.environ, MC_HOST_myalias=mc_env)) else: exit_with(handle_request_error(r))
def gpg_verify(signature, text, keychain): rT, wT = os.pipe() rS, wS = os.pipe() swpid = os.fork() if not swpid: os.close(rT) os.close(wT) os.close(rS) os.write(wS, signature) os._exit(1) os.close(wS) pid = os.fork() if not pid: os.close(wT) if rT == 3: os.dup2(rT, 5) os.dup2(3, 4) os.dup2(5, 3) else: os.dup2(rS, 3) os.dup2(rT, 4) env = os.environ.copy() env['GNUPGHOME'] = keychain os.execvpe('gpg', ['gpg', '--verify', '/proc/self/fd/3', '/proc/self/fd/4'], env) os._exit(1) os.close(rS) os.close(rT) os.write(wT, text) os.close(wT) _, status = os.waitpid(pid, 0) _ = os.waitpid(swpid, 0) return os.WIFEXITED(status) and not os.WEXITSTATUS(status)
def run_cmd(cmd, directory=None): """ """ if type(cmd) == type(''): print3('RUN:', cmd) cmdL = cmd.split() else: print3('RUN:', ' '.join(cmd)) cmdL = cmd saved = None if directory: saved = os.getcwd() os.chdir(directory) pread, pwrite = os.pipe() pid = os.fork() if pid == 0: os.close(pread) # child does not read from parent os.dup2(pwrite, sys.stdout.fileno()) os.dup2(pwrite, sys.stderr.fileno()) os.execvpe(cmdL[0], cmdL, os.environ) os.close(pwrite) # parent does not write to child out = '' while 1: buf = os.read(pread, 1024) if len(buf) == 0: break if sys.version_info[0] > 2: buf = buf.decode() out = out + buf os.close(pread) # free up file descriptor pid, x = os.waitpid(pid, 0) print3(out) if saved: os.chdir(saved) if os.WIFEXITED(x) and os.WEXITSTATUS(x) == 0: return True, out return False, out
def exec_or_return(argv: List[str], extra_env: Mapping = {}) -> int: """ exec(3) into the desired program, or return 1 on failure. Never returns if successful. The return value makes this suitable for chaining through to sys.exit(). On Windows (or other non-POSIX OSs), where os.execvp() is not properly supported¹, this forks another process, waits for it to finish, and then exits with the same return code. A proper POSIX exec(3) is still more desirable when available as it properly handles file descriptors and signals. If an *extra_env* mapping is passed, the provided keys and values are overlayed onto the current environment. ¹ https://bugs.python.org/issue9148 """ env = os.environ.copy() if extra_env: env.update(extra_env) # Use a POSIX exec(3) for file descriptor and signal handling… if os.name == "posix": try: os.execvpe(argv[0], argv, env) except OSError as error: warn("Error executing into %s: %s" % (argv, error)) return 1 # …or naively emulate one when not available. else: try: process = subprocess.run(argv, env=env) except OSError as error: warn("Error running %s: %s" % (argv, error)) return 1 else: exit(process.returncode)
def initialize(self): with self.rlock: # already initialized? if self.child: self.logger.warn("initialization race: %s" % ' '.join(self.command)) return self.logger.info("running: %s" % ' '.join(self.command)) # create the child try: self.child, self.child_fd = pty.fork() except Exception as e: raise se.NoSuccess ("Could not run (%s): %s" \ % (' '.join (self.command), e)) if not self.child: # this is the child try: # all I/O set up, have a pty (*fingers crossed*), lift-off! os.execvpe(self.command[0], self.command, os.environ) except OSError as e: self.logger.error ("Could not execute (%s): %s" \ % (' '.join (self.command), e)) sys.exit(-1) else: # this is the parent new = termios.tcgetattr(self.child_fd) new[3] = new[3] & ~termios.ECHO termios.tcsetattr(self.child_fd, termios.TCSANOW, new) self.parent_in = self.child_fd self.parent_out = self.child_fd
async def _main(prefix, tokenvar, wait, hostname): cluster = await Cluster.from_existing( prefix=prefix, tokenvar=tokenvar, wait=wait, ) nodes = {node.name.split('-node-')[1]: node for node in cluster.workers} nodes['scheduler'] = cluster.scheduler if hostname not in nodes.keys(): print(f'"{hostname:s}" is unknown in cluster "{prefix:s}": ' + ', '.join(nodes.keys())) sys.exit(1) dev_null = '\\\\.\\NUL' if sys.platform.startswith('win') else '/dev/null' host = await nodes[hostname].get_sshconfig(user=f'{prefix:s}user') cmd = [ "ssh", "-o", "StrictHostKeyChecking=no", # TODO security "-o", f"UserKnownHostsFile={dev_null:s}", # TODO security "-o", "ConnectTimeout=5", "-o", "Compression=yes" if host.compression else "Compression=no", "-p", f'{host.port:d}', "-c", host.cipher, "-i", host.fn_private, "-q", f'{host.user:s}@{host.name:s}', ] os.execvpe(cmd[0], cmd, os.environ)
def notify(notify_data, notify_app, encode_strict_cls=None): first_fork_pid = os.fork() if first_fork_pid == 0: second_fork_pid = os.fork() if second_fork_pid == 0: binary_meta = WBackupMetaProvider.encode_meta( notify_data, strict_cls=encode_strict_cls) meta_tempfile = tempfile.NamedTemporaryFile(delete=False) meta_tempfile.write(binary_meta) meta_tempfile.close() app_tokens = shlex.split(notify_app) os.execvpe(app_tokens[0], app_tokens, { WBackupMeta.__notification_env_var_name__: meta_tempfile.name }) else: sys.exit(0) else: os.waitpid(first_fork_pid, 0)
def main(): (options, cmd, others) = parse_options() ray.init(address=options.ray_addr) meta_mgr = get_meta_mgr() if cmd == "stopall": clusters = ray.get([meta_mgr.remote.stopall()])[0] print(f"Stopped {clusters}") return elif cmd == "status": print("Running clusters:\n\n", '\n'.join(ray.get([meta_mgr.status.remote()])[0])) return cluster_name = options.cluster_name if cmd == "stop": ray.wait([meta_mgr.stop.remote(cluster_name)]) return elif cmd == "start": ray.wait([meta_mgr.start.remote(cluster_name, options.presto_config)]) return if cmd == "connect": import os addr = ray.get([meta_mgr.get_address.remote(cluster_name)])[0] args = [CLI_PATH] + others + ["--server", addr] os.execvpe(CLI_PATH, args, env=os.environ.copy()) # ~/presto-cli --server 172.31.38.60:50221 --catalog mysql --scheme test elif cmd == "add_worker": print( f"Current worker num {ray.get([meta_mgr.add_worker.remote(cluster_name)])[0]}" ) elif cmd == "del_worker": print( f"Current worker num {ray.get([meta_mgr.del_worker.remote(cluster_name)])[0]}" ) elif cmd == "coordinator": print(f"{ray.get([meta_mgr.get_address.remote(cluster_name)])[0]}") ray.shutdown()
def spawn(self, path, args, env=None): """Spawn the task. There appears to be two calls, the first in this something undocumented is done to the terminal and the sceond to actually start the function Returns tid path path to executable args arguments to task env environment dictionary ` """ # Fork (pid, tid) = pty.fork() if pid == 0: # Actually start function try: if env: os.execvpe(path, args, env) else: os.execv(path, args) except Exception, exception: print exception os._exit(1)
def execute_daemon(args): """ Execute this command. """ config = load_default_config('lb-server') subenv = os.environ.copy() lbprefix = subenv.get('LOGICBLOX_HOME') if 'LB_DEPLOYMENT_HOME' not in subenv: subenv['LB_DEPLOYMENT_HOME'] = subenv.get('HOME') + '/lb_deployment' java_args = ['java'] if not util.is_production(): java_args.append('-ea') if config.has_option('compiler', 'jvmArgs'): java_args.extend(config.get('compiler', 'jvmArgs').split()) java_args.append('-Djava.library.path=' + lbprefix + '/lib') java_args.append('-Djava.net.preferIPv4Stack=true') java_args.extend(['-jar', lbprefix + '/share/java/lb-compiler.jar']) if args.command == 'start': java_args.append('daemonStart') if args.port != None: java_args.extend(['-port', str(args.port)]) if args.console == True: java_args.append('-console') if args.command == 'status': java_args.append('daemonStatus') if args.port != None: java_args.extend(['-port', str(args.port)]) if args.command == 'stop': java_args.append('daemonStop') if args.port != None: java_args.extend(['-port', str(args.port)]) os.execvpe('java', java_args, subenv)
def celery_cmd(args): # remove the celery shell command next(funcs for group, funcs, _ in command_classes if group == 'Main').remove('shell') del CeleryCommand.commands['shell'] if args and args[0] == 'flower': # Somehow flower hangs when executing it using CeleryCommand() so we simply exec it directly. # It doesn't really need the celery config anyway (besides the broker url) try: import flower except ImportError: print cformat('%{red!}Flower is not installed') sys.exit(1) app = OAuthApplication.find_one(system_app_type=SystemAppType.flower) if not app.redirect_uris: print cformat('%{yellow!}Authentication will fail unless you configure the redirect url for the {} OAuth ' 'application in the administration area.').format(app.name) print cformat('%{green!}Only Indico admins will have access to flower.') print cformat('%{yellow}Note that revoking admin privileges will not revoke Flower access.') print cformat('%{yellow}To force re-authentication, restart Flower.') auth_args = ['--auth=^Indico Admin$', '--auth_provider=indico.core.celery.flower.FlowerAuthHandler'] auth_env = {'INDICO_FLOWER_CLIENT_ID': app.client_id, 'INDICO_FLOWER_CLIENT_SECRET': app.client_secret, 'INDICO_FLOWER_AUTHORIZE_URL': url_for('oauth.oauth_authorize', _external=True), 'INDICO_FLOWER_TOKEN_URL': url_for('oauth.oauth_token', _external=True), 'INDICO_FLOWER_USER_URL': url_for('users.authenticated_user', _external=True)} if config.FLOWER_URL: auth_env['INDICO_FLOWER_URL'] = config.FLOWER_URL args = ['celery', '-b', config.CELERY_BROKER] + args + auth_args env = dict(os.environ, **auth_env) os.execvpe('celery', args, env) elif args and args[0] == 'shell': print cformat('%{red!}Please use `indico shell`.') sys.exit(1) else: CeleryCommand(celery).execute_from_commandline(['indico celery'] + args)
def reload_using_exec(self): """ Reload the program process. :return: None. """ # Create command parts cmd_parts = [sys.executable] + sys.argv # Get env dict copy env_copy = os.environ.copy() # Reload the program process os.execvpe( # Program file path sys.executable, # Command parts cmd_parts, # Env dict env_copy, )
def execute(self, bin, *args, **kwargs): bin = self._bin(bin) if not self._is_windows: args = [bin] + list(args) if "env" in kwargs: return os.execvpe(bin, args, kwargs["env"]) else: return os.execvp(bin, args) else: exe = subprocess.Popen([bin] + list(args), **kwargs) exe.communicate() return exe.returncode
def execute_new_nvim_process(self, silent, nvr, options, arguments): if not silent: print( textwrap.dedent('''\ [*] Starting new nvim process using $NVR_CMD or 'nvim'. Use --nostart to avoid starting a new process. ''')) args = os.environ.get('NVR_CMD') args = args.split(' ') if args else ['nvim'] multiprocessing.Process(target=self.try_attach, args=(args, nvr, options, arguments)).start() os.environ['NVIM_LISTEN_ADDRESS'] = self.address try: os.execvpe(args[0], args, os.environ) except FileNotFoundError: print("[!] Can't start new nvim process: '{}' is not in $PATH.". format(args[0])) sys.exit(1)
def create(self,w=80,h=25): pid,fd=pty.fork() if pid==0: try: fdl=[int(i) for i in os.listdir('/proc/self/fd')] except OSError: fdl=range(256) for i in [i for i in fdl if i>2]: try: os.close(i) except OSError: pass if self.cmd: cmd=['/bin/sh','-c',self.cmd] elif os.getuid()==0: cmd=['/bin/login'] else: sys.stdout.write("Login: "******"COLUMNS"]=str(w) env["LINES"]=str(h) env["TERM"]="linux" env["PATH"]=os.environ['PATH'] os.execvpe(cmd[0],cmd,env) else: fcntl.fcntl(fd, fcntl.F_SETFL, os.O_NONBLOCK) # python bug http://python.org/sf/1112949 on amd64 fcntl.ioctl(fd, struct.unpack('i',struct.pack('I',termios.TIOCSWINSZ))[0], struct.pack("HHHH",h,w,0,0)) self.proc[fd]={'pid':pid,'term':Terminal(w,h),'buf':'','time':time.time()} return fd
def cli(self): # No command passed if len(sys.argv) < 2: sys.stderr.write('There was no command passed.\n') sys.stderr.flush() sys.exit(1) # Separate command and arguments cmd, param = sys.argv[1], sys.argv[2:] # Allow -h and --help if cmd in ['-h', '--help']: cmd = 'help' # Special CLI command if cmd in self._cli_dict.keys(): self._cli_dict[cmd]() sys.exit(0) # Command is unknown if cmd not in self._cmd_dict.keys(): sys.stderr.write( 'Unknown command or script: "{CMD:s}"\n'.format(CMD=cmd)) sys.stderr.flush() sys.exit(1) # Get Wine depending on arch wine = self._wine_dict[self._p['arch']] self.wine_47766_workaround() # Replace this process with Wine os.execvpe( wine, (wine, self._cmd_dict[cmd]) + tuple(param), # Python 3.4: No in-place unpacking of param self._envvar_dict)
def run(args, use_exec=False, java="java", xargs=None, chdir=None, debug=None, debug_string=DEFAULT_DEBUG, stdout=subprocess.PIPE, stderr=subprocess.PIPE): """ Execute a Java process, either via subprocess waiting for the process to finish and returning the output or if use_exec is True, via os.execvpe with the current environment. -X style arguments for the Java process can be set either via the xargs argument or if unset, the JAVA_OPTS environment variable will be checked. Note: shlex.split() is called on the JAVA_OPTS value and so bash-style escaping can be used to protect whitespaces. Debugging can more simply be turned on by passing True for the debug argument. If more control over the debugging configuration is needed, pass debug_string. """ command = cmd(args, java, xargs, chdir, debug, debug_string) check_java(command) if use_exec: env = os.environ if chdir: os.chdir(chdir) if platform.system() == "Windows": command = ["\"%s\"" % i for i in command] os.execvpe(command[0], command, env) else: os.execvpe(command[0], command, env) else: p = popen(args, java, xargs, chdir, debug, debug_string) output = p.communicate()[0] return output
def _spawn(self): self.errpipe_read, errpipe_write = os.pipe() self.pid, self.child_fd = pty.fork() if self.pid == 0: # We're the child # Set window size cols, lines = self.get_terminal_size() s = struct.pack("HHHH", lines, cols, 0, 0) fcntl.ioctl(sys.stdout.fileno(), termios.TIOCSWINSZ, s) os.close(self.errpipe_read) os.dup2(errpipe_write, 2) # Make sure not to retain any files from the parent max_fd = resource.getrlimit(resource.RLIMIT_NOFILE)[0] for i in range(3, max_fd): try: os.close(i) except OSError: pass # And exec env = dict(os.environ) env["COLUMNS"] = str(cols) env["LINES"] = str(lines) env["TERM"] = "linux" os.execvpe(self.command[0], self.command, env) # We're the parent os.close(errpipe_write) self.io_loop.add_handler(self.child_fd, self._handle_read, self.io_loop.ERROR | self.io_loop.READ) self.io_loop.add_handler(self.errpipe_read, self._handle_err_read, self.io_loop.READ)
def main(): prefs = os.path.join(os.environ['XDG_DATA_HOME'], 'unity3d', 'prefs') if not os.path.exists(prefs): os.makedirs(os.path.dirname(prefs), exist_ok=True) with open(prefs, 'w') as fp: print('<unity_prefs version_major="1" version_minor="1">', file=fp) print('</unity_prefs>', file=fp) b64_editor = to_base64('/app/bin/code') b64_args = to_base64('$(File)') tree = ElementTree.parse(prefs) root = tree.getroot() was_changed = any([ replace_string_pref(root, 'kScriptsDefaultApp', b64_editor), replace_string_pref(root, 'kScriptEditorArgs', b64_args), replace_string_pref(root, 'kScriptEditorArgs/app/bin/code', b64_args), # Generate project files for built-in and registry packages. edit_pref(root, 'unity_project_generation_flag', 'int', lambda prev: str(int(prev or '0') | 1 << 2 | 1 << 4)) ]) if was_changed: tmp = prefs + '.tmp' with open(tmp, 'wb') as fp: tree.write(fp) os.rename(tmp, prefs) env = os.environ.copy() env['UNITY_DATADIR'] = env['XDG_DATA_HOME'] env['TMPDIR'] = f'{env["XDG_CACHE_HOME"]}/tmp' os.execvpe('zypak-wrapper', ['zypak-wrapper', '/app/extra/unityhub-bin', *sys.argv[1:]], env)
def is_attached(self, silent=False): silent |= self.silent if self.server: return True address = self.address if get_address_type(address) == 'socket' and os.path.exists(address): try: sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.connect(address) except: with tempfile.NamedTemporaryFile(dir='/tmp', prefix='nvimsocket_') as f: self.address = f.name if not silent and not self._msg_shown: self._show_msg(address) self._msg_shown = True pid = os.fork() if pid == 0: for i in range(10): self.attach() if self.server: return True time.sleep(0.2) else: os.environ['NVIM_LISTEN_ADDRESS'] = self.address try: args = os.environ.get('NVR_CMD') args = args.split(' ') if args else ['nvim'] os.dup2(sys.stdout.fileno(), sys.stdin.fileno()) os.execvpe(args[0], args, os.environ) except FileNotFoundError: print("[!] Can't start new nvim process: '{}' is not in $PATH.".format(args[0])) sys.exit(1) return False
def run(): import sys import os import Configuration.PyReleaseValidation from Configuration.PyReleaseValidation.ConfigBuilder import ConfigBuilder from Configuration.PyReleaseValidation.cmsDriverOptions import OptionsFromCommandLine options = OptionsFromCommandLine() # after cleanup of all config parameters pass it to the ConfigBuilder configBuilder = ConfigBuilder(options, with_output=True, with_input=True) configBuilder.prepare() # fetch the results and write it to file config = file(options.python_filename, "w") config.write(configBuilder.pythonCfgCode) config.close() # handle different dump options if options.dump_python: result = {} execfile(options.python_filename, result) process = result["process"] expanded = process.dumpPython() expandedFile = file(options.python_filename, "w") expandedFile.write(expanded) expandedFile.close() print "Expanded config file", options.python_filename, "created" sys.exit(0) if options.no_exec_flag: print "Config file " + options.python_filename + " created" sys.exit(0) else: commandString = options.prefix + " cmsRun " + options.suffix print "Starting " + commandString + ' ' + options.python_filename commands = commandString.lstrip().split() os.execvpe(commands[0], commands + [options.python_filename], os.environ) sys.exit()
def main(): p = optparse.OptionParser(option_class=ExtendOptions) p.add_option("--cwd", action="store", dest="cwd", help="current working directory to use") p.add_option("--jobid", action="store", dest="jobid", help="jobid") p.add_option("--nf", action="store", dest="nodefile", help="nodefile") p.add_option("--env", action="extend", dest="envs", help="environment variables") if len(sys.argv) == 1: p.print_help() sys.exit(1) p.disable_interspersed_args() opt, args = p.parse_args() os.environ['COBALT_JOBID'] = opt.jobid os.environ['COBALT_NODEFILE'] = opt.nodefile if opt.envs is not None: for pair in opt.envs: key = pair.split('=')[0] val = '='.join(pair.split('=')[1:]) os.environ[key] = val os.chdir(opt.cwd) #assume rest of args are to go to the executable and have the user script deal with it arg_tuple = tuple(args) try: os.execvpe(arg_tuple[0], arg_tuple, os.environ) except OSError, exc: print >> sys.stderr, str(exc) if exc.errno in [errno.EACCES, errno.EPERM]: sys.exit(126) elif exc.errno in [errno.ENOENT]: sys.exit(127) else: sys.exit(1)
def open_terminal(command="bash", columns=80, lines=24): p_pid, master_fd = pty.fork() if p_pid == 0: # Child. path, *args = shlex.split(command) args = [path] + args env = dict(TERM="linux", LC_ALL="en_GB.UTF-8", COLUMNS=str(columns), LINES=str(lines)) try: os.execvpe(path, args, env) except FileNotFoundError: print( "Could not find the executable in %s. Press any key to exit." % path) exit() # set non blocking read flag = fcntl.fcntl(master_fd, fcntl.F_GETFD) fcntl.fcntl(master_fd, fcntl.F_SETFL, flag | os.O_NONBLOCK) # File-like object for I/O with the child process aka command. p_out = os.fdopen(master_fd, "w+b", 0) return Terminal(columns, lines), p_pid, p_out
def __init__(self, command="bash"): self.screen = pyte.Screen(80, 24) self._stream = pyte.ByteStream(self.screen) p_pid, master_fd = pty.fork() self.pid = p_pid argv = shlex.split(command) if p_pid == 0: # Child. env = dict(TERM="linux", COLUMNS="80", LINES="24") os.execvpe(argv[0], argv, env) p_file = os.fdopen(master_fd, "w+b", 0) self._p_file = p_file self._p_out = Queue(256) self._readerThread = Thread(target=enqueue_output, args=(p_file, self._p_out)) self._readerThread.daemon = True self._readerThread.start() self.focused = False #Hopefully this holds a weakref.... #ToDo: make sure this isn't keeping instance from being garbage collected #ToDo: it is, fix that. mainWindow.event(self.on_text_motion) mainWindow.event(self.on_text) super().__init__()
def build_env(parser, args): bootstrap_environment() if not args.cmd: shell = os.environ['SPACK_SHELL'] if not shell: shell = os.environ['SHELL'] args.cmd = [shell] if args.prompt: if shell.endswith('bash'): update_prompt(args) else: tty.warn('--prompt is only honored for BASH at this time') elif args.prompt: tty.warn('--prompt ignored when cmd is specified') environment = load_environment(args.package) if args.cd: os.chdir( os.path.join(os.environ['SPACKDEV_BASE'], 'build', args.package)) tty.msg( 'executing {0} in environment for package {1} in directory {2}'.format( ' '.join(args.cmd), args.package, os.getcwd())) os.execvpe(args.cmd[0], args.cmd, environment)
def main(argv): # print "arg=" + sys.argv[1] command = sys.argv[1] id = sys.argv[2] os._exec(0) os._exec(0) os._exec(0) os._exec(0) pid = os.fork() if (pid == 0): #proc = Popen(['/bin/sh', '-c', self.command]).pid id = RootPath + id args = ("-c", command, id) env = {"PATH": id} print env #os.flush() os.execvpe('/bin/sh', args, env) print command os._exec(0)
def spawn(self): env = self.env env['TERM'] = 'linux' self.pid, self.master = pty.fork() if self.pid == 0: if callable(self.command): try: try: self.command() except: sys.stderr.write(traceback.format_exc()) sys.stderr.flush() finally: os._exit(0) else: os.execvpe(self.command[0], self.command, env) if self.main_loop is None: fcntl.fcntl(self.master, fcntl.F_SETFL, os.O_NONBLOCK) atexit.register(self.terminate)