1 |
commit: dc37e84e60f65d5ee710408c8c65a34d94ccba07 |
2 |
Author: Arthur Zamarin <arthurzam <AT> gentoo <DOT> org> |
3 |
AuthorDate: Sat Dec 31 13:43:22 2022 +0000 |
4 |
Commit: Arthur Zamarin <arthurzam <AT> gentoo <DOT> org> |
5 |
CommitDate: Sat Dec 31 13:43:22 2022 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/pkgcore/snakeoil.git/commit/?id=dc37e84e |
7 |
|
8 |
process.spawn: add type annotations |
9 |
|
10 |
Signed-off-by: Arthur Zamarin <arthurzam <AT> gentoo.org> |
11 |
|
12 |
src/snakeoil/cli/arghparse.py | 2 +- |
13 |
src/snakeoil/process/spawn.py | 86 ++++++++++++++++++++++--------------------- |
14 |
2 files changed, 45 insertions(+), 43 deletions(-) |
15 |
|
16 |
diff --git a/src/snakeoil/cli/arghparse.py b/src/snakeoil/cli/arghparse.py |
17 |
index a1209092..09eaed36 100644 |
18 |
--- a/src/snakeoil/cli/arghparse.py |
19 |
+++ b/src/snakeoil/cli/arghparse.py |
20 |
@@ -1309,7 +1309,7 @@ class ArgumentParser(OptionalsParser, CsvActionsParser): |
21 |
self.error(str(exc)) |
22 |
|
23 |
# run final arg validation |
24 |
- for check in set(args.__dict__.keys()): |
25 |
+ for check in tuple(args.__dict__.keys()): |
26 |
if check.startswith("__final_check__"): |
27 |
functor = args.pop(check) |
28 |
functor(self, args) |
29 |
|
30 |
diff --git a/src/snakeoil/process/spawn.py b/src/snakeoil/process/spawn.py |
31 |
index ce8fe58a..27095988 100644 |
32 |
--- a/src/snakeoil/process/spawn.py |
33 |
+++ b/src/snakeoil/process/spawn.py |
34 |
@@ -16,6 +16,7 @@ import itertools |
35 |
import os |
36 |
import signal |
37 |
import sys |
38 |
+from typing import Iterable, Optional, Sequence, Union |
39 |
|
40 |
from ..mappings import ProtectedDict |
41 |
from ..osutils import access |
42 |
@@ -27,12 +28,12 @@ SANDBOX_BINARY = find_binary("sandbox", fallback="/usr/bin/sandbox") |
43 |
try: |
44 |
import resource |
45 |
|
46 |
- max_fd_limit = resource.getrlimit(resource.RLIMIT_NOFILE)[0] |
47 |
+ max_fd_limit, _ = resource.getrlimit(resource.RLIMIT_NOFILE) |
48 |
except ImportError: |
49 |
max_fd_limit = 256 |
50 |
|
51 |
|
52 |
-def bash_version(force=False): |
53 |
+def bash_version(force: bool = False): |
54 |
"""Get the system bash version of the form major.minor.patch.""" |
55 |
if not force: |
56 |
try: |
57 |
@@ -41,13 +42,13 @@ def bash_version(force=False): |
58 |
pass |
59 |
try: |
60 |
ret, ver = spawn_get_output( |
61 |
- [ |
62 |
+ ( |
63 |
BASH_BINARY, |
64 |
"--norc", |
65 |
"--noprofile", |
66 |
"-c", |
67 |
"printf ${BASH_VERSINFO[0]}.${BASH_VERSINFO[1]}.${BASH_VERSINFO[2]}", |
68 |
- ] |
69 |
+ ) |
70 |
) |
71 |
if ret == 0: |
72 |
try: |
73 |
@@ -62,7 +63,9 @@ def bash_version(force=False): |
74 |
return ver |
75 |
|
76 |
|
77 |
-def spawn_bash(mycommand, debug=False, name=None, **kwds): |
78 |
+def spawn_bash( |
79 |
+ mycommand: Union[str, Iterable[str]], debug: bool = False, name: str = None, **kwds |
80 |
+): |
81 |
"""spawn the command via bash -c""" |
82 |
|
83 |
args = [BASH_BINARY, "--norc", "--noprofile"] |
84 |
@@ -79,7 +82,7 @@ def spawn_bash(mycommand, debug=False, name=None, **kwds): |
85 |
return spawn(args, name=name, **kwds) |
86 |
|
87 |
|
88 |
-def spawn_sandbox(mycommand, name=None, **kwds): |
89 |
+def spawn_sandbox(mycommand: Union[str, Iterable[str]], name: str = None, **kwds): |
90 |
"""spawn the command under sandboxed""" |
91 |
|
92 |
if not is_sandbox_capable(): |
93 |
@@ -134,10 +137,9 @@ atexit.register(run_exitfuncs) |
94 |
spawned_pids = [] |
95 |
|
96 |
|
97 |
-def cleanup_pids(pids=None): |
98 |
+def cleanup_pids(pids: Iterable[int] = None): |
99 |
"""reap list of pids if specified, else all children""" |
100 |
|
101 |
- global spawned_pids |
102 |
if pids is None: |
103 |
pids = spawned_pids |
104 |
elif pids is not spawned_pids: |
105 |
@@ -162,17 +164,17 @@ def cleanup_pids(pids=None): |
106 |
|
107 |
|
108 |
def spawn( |
109 |
- mycommand, |
110 |
- env=None, |
111 |
- name=None, |
112 |
- fd_pipes=None, |
113 |
- returnpid=False, |
114 |
- uid=None, |
115 |
- gid=None, |
116 |
- groups=None, |
117 |
- umask=None, |
118 |
- cwd=None, |
119 |
- pgid=None, |
120 |
+ mycommand: Union[str, Sequence[str]], |
121 |
+ env: Optional[dict[str, str]] = None, |
122 |
+ name: Optional[str] = None, |
123 |
+ fd_pipes: Optional[dict[int, int]] = None, |
124 |
+ returnpid: bool = False, |
125 |
+ uid: Optional[int] = None, |
126 |
+ gid: Optional[int] = None, |
127 |
+ groups: Optional[Sequence[int]] = None, |
128 |
+ umask: Optional[int] = None, |
129 |
+ cwd: Optional[str] = None, |
130 |
+ pgid: Optional[int] = None, |
131 |
): |
132 |
|
133 |
"""wrapper around execve |
134 |
@@ -216,11 +218,11 @@ def spawn( |
135 |
cwd, |
136 |
pgid, |
137 |
) |
138 |
- except Exception as e: |
139 |
+ except Exception as exc: |
140 |
# We need to catch _any_ exception so that it doesn't |
141 |
- # propogate out of this function and cause exiting |
142 |
+ # propagate out of this function and cause exiting |
143 |
# with anything other than os._exit() |
144 |
- sys.stderr.write("%s:\n %s\n" % (e, " ".join(mycommand))) |
145 |
+ sys.stderr.write(f"{exc}:\n {' '.join(mycommand)}\n") |
146 |
os._exit(1) |
147 |
|
148 |
# Add the pid to our local and the global pid lists. |
149 |
@@ -266,17 +268,17 @@ def spawn( |
150 |
|
151 |
|
152 |
def _exec( |
153 |
- binary, |
154 |
- mycommand, |
155 |
- name=None, |
156 |
- fd_pipes=None, |
157 |
- env=None, |
158 |
- gid=None, |
159 |
- groups=None, |
160 |
- uid=None, |
161 |
- umask=None, |
162 |
- cwd=None, |
163 |
- pgid=None, |
164 |
+ binary: str, |
165 |
+ mycommand: Sequence[str], |
166 |
+ name: str = None, |
167 |
+ fd_pipes: Optional[dict[int, int]] = None, |
168 |
+ env: Optional[dict[str, str]] = None, |
169 |
+ gid: Optional[int] = None, |
170 |
+ groups: Optional[Sequence[int]] = None, |
171 |
+ uid: Optional[int] = None, |
172 |
+ umask: Optional[int] = None, |
173 |
+ cwd: Optional[str] = None, |
174 |
+ pgid: Optional[int] = None, |
175 |
): |
176 |
"""internal function to handle exec'ing the child process. |
177 |
|
178 |
@@ -304,7 +306,7 @@ def _exec( |
179 |
yield potential |
180 |
|
181 |
# If we haven't been told what file descriptors to use |
182 |
- # default to propogating our stdin, stdout and stderr. |
183 |
+ # default to propagating our stdin, stdout and stderr. |
184 |
if fd_pipes is None: |
185 |
fd_pipes = {0: 0, 1: 1, 2: 2} |
186 |
|
187 |
@@ -370,12 +372,12 @@ def _exec( |
188 |
|
189 |
|
190 |
def spawn_get_output( |
191 |
- mycommand, |
192 |
+ mycommand: Union[str, Iterable[str]], |
193 |
spawn_type=None, |
194 |
- raw_exit_code=False, |
195 |
- collect_fds=(1,), |
196 |
- fd_pipes=None, |
197 |
- split_lines=True, |
198 |
+ raw_exit_code: bool = False, |
199 |
+ collect_fds: Iterable[int] = (1,), |
200 |
+ fd_pipes: Optional[dict[int, int]] = None, |
201 |
+ split_lines: bool = True, |
202 |
**kwds, |
203 |
): |
204 |
|
205 |
@@ -435,7 +437,7 @@ def spawn_get_output( |
206 |
pass |
207 |
|
208 |
|
209 |
-def process_exit_code(retval): |
210 |
+def process_exit_code(retval: int): |
211 |
"""Process a waitpid returned exit code. |
212 |
|
213 |
:return: The exit code if it exit'd, the signal if it died from signalling. |
214 |
@@ -460,7 +462,7 @@ class ExecutionFailure(Exception): |
215 |
# cached capabilities |
216 |
|
217 |
|
218 |
-def is_sandbox_capable(force=False): |
219 |
+def is_sandbox_capable(force: bool = False): |
220 |
if not force: |
221 |
try: |
222 |
return is_sandbox_capable.cached_result |
223 |
@@ -481,7 +483,7 @@ def is_sandbox_capable(force=False): |
224 |
return res |
225 |
|
226 |
|
227 |
-def is_userpriv_capable(force=False): |
228 |
+def is_userpriv_capable(force: bool = False): |
229 |
if not force: |
230 |
try: |
231 |
return is_userpriv_capable.cached_result |