def autostart(): subprocess.Popen(["sfx", "startup"]) os.system("autorandr") os.system("wal -R") os.system("~/.fehbg") os.system("numlockx on") os.system("xmodmap ~/.Xmodmap") os.system("xset r rate 360 42") # Run setup-specific cmds if setup.is_laptop: os.system("touchpad.sh") os.system("trackpoint.sh") subprocess.Popen("xcape") for cmd in setup.cmds: os.system(cmd) # Launch some background processes subprocess.Popen(["picom", "--daemon", "--dbus", "--experimental-backends"]) subprocess.Popen( [ "xidlehook", "--not-when-fullscreen", "--not-when-audio", "--timer", "600", "lockmeup", ] ) # Avoid opening two Redshift instances since that causes flicker if os.waitstatus_to_exitcode(os.system("pgrep redshift-gtk")) > 0: subprocess.Popen("redshift-gtk") subprocess.Popen("nm-applet") subprocess.Popen("dropbox")
def test_return_code(self): self.assertEqual(os.popen("exit 0").close(), None) status = os.popen("exit 42").close() if os.name == 'nt': self.assertEqual(status, 42) else: self.assertEqual(os.waitstatus_to_exitcode(status), 42)
def check_output(cmd, **kwargs): if kwargs: raise NotImplementedError(repr(kwargs)) if not _check_cmd(cmd): raise ValueError(f"unsupported command: {cmd!r}") tmp_filename = "check_output.tmp" if not isinstance(cmd, str): cmd = " ".join(cmd) cmd = f"{cmd} >{tmp_filename}" try: # system() spawns a shell status = os.system(cmd) exitcode = os.waitstatus_to_exitcode(status) if exitcode: raise ValueError(f"Command {cmd!r} returned non-zero " f"exit status {exitcode!r}") try: with open(tmp_filename, "rb") as fp: stdout = fp.read() except FileNotFoundError: stdout = b'' finally: try: os.unlink(tmp_filename) except OSError: pass return stdout
def waitstatus_to_exitcode(wait_status): # pragma: no cover """Convert a wait status to an exit code.""" if hasattr(os, 'waitstatus_to_exitcode'): # This was added in Python 3.9 return os.waitstatus_to_exitcode(wait_status) # The code below was ported from here: # https://github.com/python/cpython/blob/v3.9.1/Modules/posixmodule.c#L14363-L14402 if os.WIFEXITED(wait_status): exitcode = os.WEXITSTATUS(wait_status) if exitcode < 0: raise ValueError('invalid WEXITSTATUS: {}'.format(exitcode)) elif os.WIFSIGNALED(wait_status): signum = os.WTERMSIG(wait_status) if signum <= 0: raise ValueError('invalid WTERMSIG: {}'.format(signum)) exitcode = -signum elif os.WIFSTOPPED(wait_status): signum = os.WSTOPSIG(wait_status) raise ValueError( 'process stopped by delivery of signal {}'.format(signum)) else: raise ValueError('invalid wait status: {}'.format(wait_status)) return exitcode
def poll(self, flag=os.WNOHANG): if self.returncode is None: try: pid, sts = os.waitpid(self.pid, flag) except OSError: # Child process not yet created. See #1731717 # e.errno == errno.ECHILD == 10 return None if pid == self.pid: self.returncode = os.waitstatus_to_exitcode(sts) return self.returncode
def wait_impl(self, cpid, *, exitcode): # This many iterations can be required, since some previously run # tests (e.g. test_ctypes) could have spawned a lot of children # very quickly. for _ in support.sleeping_retry(support.SHORT_TIMEOUT): # wait3() shouldn't hang, but some of the buildbots seem to hang # in the forking tests. This is an attempt to fix the problem. spid, status, rusage = os.wait3(os.WNOHANG) if spid == cpid: break self.assertEqual(spid, cpid) self.assertEqual(os.waitstatus_to_exitcode(status), exitcode) self.assertTrue(rusage)
def wait_impl(self, cpid, *, exitcode): option = os.WNOHANG if sys.platform.startswith('aix'): # Issue #11185: wait4 is broken on AIX and will always return 0 # with WNOHANG. option = 0 for _ in support.sleeping_retry(support.SHORT_TIMEOUT): # wait4() shouldn't hang, but some of the buildbots seem to hang # in the forking tests. This is an attempt to fix the problem. spid, status, rusage = os.wait4(cpid, option) if spid == cpid: break self.assertEqual(spid, cpid) self.assertEqual(os.waitstatus_to_exitcode(status), exitcode) self.assertTrue(rusage)
def wait(self): pid = os.fork() if pid == 0: # Child process try: if self._env is not None: os.execve(self._cmd[0], self._cmd, self._env) else: os.execv(self._cmd[0], self._cmd) finally: os._exit(1) else: # Parent process _, status = os.waitpid(pid, 0) self.returncode = os.waitstatus_to_exitcode(status) return self.returncode
def wrapper(environment, output_path): dump_command = template.format( host=environment.get('DATABASE_HOST'), user=environment.get('DATABASE_USER'), password=environment.get('DATABASE_PASSWORD'), database=environment.get('DATABASE_NAME'), port=environment.get('DATABASE_PORT'), dump_path=output_path, ) wait_exit_status = os.system(dump_command) exit_status = os.waitstatus_to_exitcode(wait_exit_status) if wait_exit_status != 0 or exit_status != 0: logger.error("RETURN CODE OF DUMP PROCESS != 0. CHECK OUTPUT ABOVE FOR ERRORS!") send_slack_message(environment, "Failed to create DB dump. Please check the error in the container logs.", 'FAIL') raise Exception('Failed to create dump of database')
def main(): pathlist = os.environ['PATH'].split(os.pathsep) sts = 0 longlist = '' if sys.argv[1:] and sys.argv[1][:2] == '-l': longlist = sys.argv[1] del sys.argv[1] for prog in sys.argv[1:]: ident = () for dir in pathlist: filename = os.path.join(dir, prog) try: st = os.stat(filename) except OSError: continue if not S_ISREG(st[ST_MODE]): msg(filename + ': not a disk file') else: mode = S_IMODE(st[ST_MODE]) if mode & 0o111: if not ident: print(filename) ident = st[:3] else: if st[:3] == ident: s = 'same as: ' else: s = 'also: ' msg(s + filename) else: msg(filename + ': not executable') if longlist: sts = os.system('ls ' + longlist + ' ' + filename) sts = os.waitstatus_to_exitcode(sts) if sts: msg('"ls -l" exit status: ' + repr(sts)) if not ident: msg(prog + ': not found') sts = 1 sys.exit(sts)
def set_wallpapers(path): """Sets the desktop wallpaper using the file at the given path. If using feh, a space-separated list of n paths will set the wallpaper for n monitors. Parameters ---------- path : string The path to the wallpaper file. """ # TODO let user provide options to pass to feh, or user another program for setting wallpaper print("Setting wallpaper...") command = f"feh --no-fehbg --bg-scale {path} > /dev/null 2>&1" status = os.system(command) if os.waitstatus_to_exitcode(status) != 0: print( f"Unable to set wallpaper. Please check the path and verify the file exists.", file=sys.stderr, )
def test(): import sys caps = getcaps() if not sys.argv[1:]: show(caps) return for i in range(1, len(sys.argv), 2): args = sys.argv[i:i + 2] if len(args) < 2: print("usage: mailcap [MIMEtype file] ...") return MIMEtype = args[0] file = args[1] command, e = findmatch(caps, MIMEtype, 'view', file) if not command: print("No viewer found for", type) else: print("Executing:", command) sts = os.system(command) sts = os.waitstatus_to_exitcode(sts) if sts: print("Exit status:", sts)
def wait(self): if self.pid > 0: _, status = os.waitpid(self.pid, 0) self.pid = -1 return os.waitstatus_to_exitcode(status) return None
def main(listener_fd, alive_r, preload, main_path=None, sys_path=None): '''Run forkserver.''' if preload: if '__main__' in preload and main_path is not None: process.current_process()._inheriting = True try: spawn.import_main_path(main_path) finally: del process.current_process()._inheriting for modname in preload: try: __import__(modname) except ImportError: pass util._close_stdin() sig_r, sig_w = os.pipe() os.set_blocking(sig_r, False) os.set_blocking(sig_w, False) def sigchld_handler(*_unused): # Dummy signal handler, doesn't do anything pass handlers = { # unblocking SIGCHLD allows the wakeup fd to notify our event loop signal.SIGCHLD: sigchld_handler, # protect the process from ^C signal.SIGINT: signal.SIG_IGN, } old_handlers = { sig: signal.signal(sig, val) for (sig, val) in handlers.items() } # calling os.write() in the Python signal handler is racy signal.set_wakeup_fd(sig_w) # map child pids to client fds pid_to_fd = {} with socket.socket(socket.AF_UNIX, fileno=listener_fd) as listener, \ selectors.DefaultSelector() as selector: _forkserver._forkserver_address = listener.getsockname() selector.register(listener, selectors.EVENT_READ) selector.register(alive_r, selectors.EVENT_READ) selector.register(sig_r, selectors.EVENT_READ) while True: try: while True: rfds = [key.fileobj for (key, events) in selector.select()] if rfds: break if alive_r in rfds: # EOF because no more client processes left assert os.read(alive_r, 1) == b'', "Not at EOF?" raise SystemExit if sig_r in rfds: # Got SIGCHLD os.read(sig_r, 65536) # exhaust while True: # Scan for child processes try: pid, sts = os.waitpid(-1, os.WNOHANG) except ChildProcessError: break if pid == 0: break child_w = pid_to_fd.pop(pid, None) if child_w is not None: returncode = os.waitstatus_to_exitcode(sts) # Send exit code to client process try: write_signed(child_w, returncode) except BrokenPipeError: # client vanished pass os.close(child_w) else: # This shouldn't happen really warnings.warn('forkserver: waitpid returned ' 'unexpected pid %d' % pid) if listener in rfds: # Incoming fork request with listener.accept()[0] as s: # Receive fds from client fds = reduction.recvfds(s, MAXFDS_TO_SEND + 1) if len(fds) > MAXFDS_TO_SEND: raise RuntimeError( "Too many ({0:n}) fds to send".format( len(fds))) child_r, child_w, *fds = fds s.close() pid = os.fork() if pid == 0: # Child code = 1 try: listener.close() selector.close() unused_fds = [alive_r, child_w, sig_r, sig_w] unused_fds.extend(pid_to_fd.values()) code = _serve_one(child_r, fds, unused_fds, old_handlers) except Exception: sys.excepthook(*sys.exc_info()) sys.stderr.flush() finally: os._exit(code) else: # Send pid to client process try: write_signed(child_w, pid) except BrokenPipeError: # client vanished pass pid_to_fd[pid] = child_w os.close(child_r) for fd in fds: os.close(fd) except OSError as e: if e.errno != errno.ECONNABORTED: raise
def run_cgi(self): """Execute a CGI script.""" dir, rest = self.cgi_info path = dir + '/' + rest i = path.find('/', len(dir)+1) while i >= 0: nextdir = path[:i] nextrest = path[i+1:] scriptdir = self.translate_path(nextdir) if os.path.isdir(scriptdir): dir, rest = nextdir, nextrest i = path.find('/', len(dir)+1) else: break # find an explicit query string, if present. rest, _, query = rest.partition('?') # dissect the part after the directory name into a script name & # a possible additional path, to be stored in PATH_INFO. i = rest.find('/') if i >= 0: script, rest = rest[:i], rest[i:] else: script, rest = rest, '' scriptname = dir + '/' + script scriptfile = self.translate_path(scriptname) if not os.path.exists(scriptfile): self.send_error( HTTPStatus.NOT_FOUND, "No such CGI script (%r)" % scriptname) return if not os.path.isfile(scriptfile): self.send_error( HTTPStatus.FORBIDDEN, "CGI script is not a plain file (%r)" % scriptname) return ispy = self.is_python(scriptname) if self.have_fork or not ispy: if not self.is_executable(scriptfile): self.send_error( HTTPStatus.FORBIDDEN, "CGI script is not executable (%r)" % scriptname) return # Reference: http://hoohoo.ncsa.uiuc.edu/cgi/env.html # XXX Much of the following could be prepared ahead of time! env = copy.deepcopy(os.environ) env['SERVER_SOFTWARE'] = self.version_string() env['SERVER_NAME'] = self.server.server_name env['GATEWAY_INTERFACE'] = 'CGI/1.1' env['SERVER_PROTOCOL'] = self.protocol_version env['SERVER_PORT'] = str(self.server.server_port) env['REQUEST_METHOD'] = self.command uqrest = urllib.parse.unquote(rest) env['PATH_INFO'] = uqrest env['PATH_TRANSLATED'] = self.translate_path(uqrest) env['SCRIPT_NAME'] = scriptname env['QUERY_STRING'] = query env['REMOTE_ADDR'] = self.client_address[0] authorization = self.headers.get("authorization") if authorization: authorization = authorization.split() if len(authorization) == 2: import base64, binascii env['AUTH_TYPE'] = authorization[0] if authorization[0].lower() == "basic": try: authorization = authorization[1].encode('ascii') authorization = base64.decodebytes(authorization).\ decode('ascii') except (binascii.Error, UnicodeError): pass else: authorization = authorization.split(':') if len(authorization) == 2: env['REMOTE_USER'] = authorization[0] # XXX REMOTE_IDENT if self.headers.get('content-type') is None: env['CONTENT_TYPE'] = self.headers.get_content_type() else: env['CONTENT_TYPE'] = self.headers['content-type'] length = self.headers.get('content-length') if length: env['CONTENT_LENGTH'] = length referer = self.headers.get('referer') if referer: env['HTTP_REFERER'] = referer accept = self.headers.get_all('accept', ()) env['HTTP_ACCEPT'] = ','.join(accept) ua = self.headers.get('user-agent') if ua: env['HTTP_USER_AGENT'] = ua co = filter(None, self.headers.get_all('cookie', [])) cookie_str = ', '.join(co) if cookie_str: env['HTTP_COOKIE'] = cookie_str # XXX Other HTTP_* headers # Since we're setting the env in the parent, provide empty # values to override previously set values for k in ('QUERY_STRING', 'REMOTE_HOST', 'CONTENT_LENGTH', 'HTTP_USER_AGENT', 'HTTP_COOKIE', 'HTTP_REFERER'): env.setdefault(k, "") self.send_response(HTTPStatus.OK, "Script output follows") self.flush_headers() decoded_query = query.replace('+', ' ') if self.have_fork: # Unix -- fork as we should args = [script] if '=' not in decoded_query: args.append(decoded_query) nobody = nobody_uid() self.wfile.flush() # Always flush before forking pid = os.fork() if pid != 0: # Parent pid, sts = os.waitpid(pid, 0) # throw away additional data [see bug #427345] while select.select([self.rfile], [], [], 0)[0]: if not self.rfile.read(1): break exitcode = os.waitstatus_to_exitcode(sts) if exitcode: self.log_error(f"CGI script exit code {exitcode}") return # Child try: try: os.setuid(nobody) except OSError: pass os.dup2(self.rfile.fileno(), 0) os.dup2(self.wfile.fileno(), 1) os.execve(scriptfile, args, env) except: self.server.handle_error(self.request, self.client_address) os._exit(127) else: # Non-Unix -- use subprocess import subprocess cmdline = [scriptfile] if self.is_python(scriptfile): interp = sys.executable if interp.lower().endswith("w.exe"): # On Windows, use python.exe, not pythonw.exe interp = interp[:-5] + interp[-4:] cmdline = [interp, '-u'] + cmdline if '=' not in query: cmdline.append(query) self.log_message("command: %s", subprocess.list2cmdline(cmdline)) try: nbytes = int(length) except (TypeError, ValueError): nbytes = 0 p = subprocess.Popen(cmdline, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env = env ) if self.command.lower() == "post" and nbytes > 0: data = self.rfile.read(nbytes) else: data = None # throw away additional data [see bug #427345] while select.select([self.rfile._sock], [], [], 0)[0]: if not self.rfile._sock.recv(1): break stdout, stderr = p.communicate(data) self.wfile.write(stdout) if stderr: self.log_error('%s', stderr) p.stderr.close() p.stdout.close() status = p.returncode if status: self.log_error("CGI script exit status %#x", status) else: self.log_message("CGI script exited OK")
gamma = valueMap.get('gamma') if 'gamma' in valueMap else (.7 * 127) if 'brightness' in valueMap: bright = valueMap['brightness'] / 127.0 params.append("-b {}".format(bright)) if 'redshift' in valueMap: redshift = int(12000 * valueMap['redshift'] / 127.0 + 1000) params.append("-r {}".format(redshift)) todoMap['screen'] = "night {}".format(' '.join(params)) elif todo in ['prev', 'next', 'pauseplay'] and value == 0: cmdList.append("{}/ipc-do.js {}".format(base_path, todo)) elif todo in cmdMap: if value == 0: cmdList.append("tmux send-keys -t mpv-once '{}'".format( cmdMap[todo])) else: pass if cmdList: for cmd in cmdList: try: logging.info("{} {}".format( os.waitstatus_to_exitcode(os.system(cmd)), cmd)) except: pass
def run_command(cmd): status = os.system(cmd) return os.waitstatus_to_exitcode(status)
def test_fork(self): debug("calling pty.fork()") pid, master_fd = pty.fork() self.addCleanup(os.close, master_fd) if pid == pty.CHILD: # stdout should be connected to a tty. if not os.isatty(1): debug("Child's fd 1 is not a tty?!") os._exit(3) # After pty.fork(), the child should already be a session leader. # (on those systems that have that concept.) debug("In child, calling os.setsid()") try: os.setsid() except OSError: # Good, we already were session leader debug("Good: OSError was raised.") pass except AttributeError: # Have pty, but not setsid()? debug("No setsid() available?") pass except: # We don't want this error to propagate, escaping the call to # os._exit() and causing very peculiar behavior in the calling # regrtest.py ! # Note: could add traceback printing here. debug("An unexpected error was raised.") os._exit(1) else: debug("os.setsid() succeeded! (bad!)") os._exit(2) os._exit(4) else: debug("Waiting for child (%d) to finish." % pid) # In verbose mode, we have to consume the debug output from the # child or the child will block, causing this test to hang in the # parent's waitpid() call. The child blocks after a # platform-dependent amount of data is written to its fd. On # Linux 2.6, it's 4000 bytes and the child won't block, but on OS # X even the small writes in the child above will block it. Also # on Linux, the read() will raise an OSError (input/output error) # when it tries to read past the end of the buffer but the child's # already exited, so catch and discard those exceptions. It's not # worth checking for EIO. while True: try: data = os.read(master_fd, 80) except OSError: break if not data: break sys.stdout.write( str(data.replace(b'\r\n', b'\n'), encoding='ascii')) ##line = os.read(master_fd, 80) ##lines = line.replace('\r\n', '\n').split('\n') ##if False and lines != ['In child, calling os.setsid()', ## 'Good: OSError was raised.', '']: ## raise TestFailed("Unexpected output from child: %r" % line) (pid, status) = os.waitpid(pid, 0) res = os.waitstatus_to_exitcode(status) debug("Child (%d) exited with code %d (status %d)." % (pid, res, status)) if res == 1: self.fail( "Child raised an unexpected exception in os.setsid()") elif res == 2: self.fail("pty.fork() failed to make child a session leader.") elif res == 3: self.fail( "Child spawned by pty.fork() did not have a tty as stdout") elif res != 4: self.fail("pty.fork() failed for unknown reasons.")
print("./content: exists, giving up", file=sys.stderr) exit(1) content_path.mkdir() generate_default_config('./content/config.yaml') posts_path = Path(f'./content/{publish_directory}/posts') posts_path.mkdir(parents=True) atoms_path = Path('./content/atoms') atoms_path.mkdir() markdowns_path = Path('./content/markdowns') markdowns_path.mkdir() build.create(f"./content/{publish_directory}") shutil.copytree("gitatom/templates/", "./content/templates") # Set up the content repo. wstatus = os.system("""sh -c ' cd content && git init && git add . && git commit -m "my blog, by GitAtom"' """) code = os.waitstatus_to_exitcode(wstatus) if code != 0: print("content: repo creation failed, giving up.", file=sys.stderr) exit(1)
# main process children.add(pid) continuous_num_spawns += 1 else: # child process break else: # main process - redirect SIGTERM to SystemExit and wait for children signal.signal(signal.SIGTERM, lambda *_: exit(os.EX_OK)) last_spawn_timestamp = time.monotonic() try: while children: pid, status = os.wait() children.remove(pid) ec = os.waitstatus_to_exitcode(status) if ec > 0 or -ec not in {0, signal.SIGINT}: # restart the child process if killed or ending abnormally, # unless we tried too many times continuously now = time.monotonic() if now - last_spawn_timestamp > NUM_SPAWNS_RESET_INTERVAL: continuous_num_spawns = 0 last_spawn_timestamp = now continuous_num_spawns += 1 if continuous_num_spawns > max_worker_spawns: # GOTCHA: we shouldn't return here because we need the # exception handler below to clean up the workers exit(os.EX_UNAVAILABLE) if pid := os.fork(): # main process
def run_command_basic(command): exitcode = os.system(command) if hasattr("os", "waitstatus_to_exitcode"): exitcode = os.waitstatus_to_exitcode(exitcode) return exitcode == 0