cs-psutils 20241122__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
cs/psutils.py ADDED
@@ -0,0 +1,467 @@
1
+ #!/usr/bin/python
2
+ #
3
+
4
+ r'''
5
+ Assorted process and subprocess management functions.
6
+
7
+ Not to be confused with the excellent
8
+ (psutil)[https://pypi.org/project/psutil/] package.
9
+ '''
10
+
11
+ import builtins
12
+ from contextlib import contextmanager
13
+ import errno
14
+ import io
15
+ from itertools import chain
16
+ import logging
17
+ import os
18
+ import shlex
19
+ from signal import SIGTERM, SIGKILL, signal
20
+ from subprocess import DEVNULL as subprocess_DEVNULL, PIPE, Popen, run as subprocess_run
21
+ import sys
22
+ import time
23
+
24
+ from cs.deco import fmtdoc, uses_doit, uses_quiet
25
+ from cs.gimmicks import trace, warning, DEVNULL
26
+ from cs.pfx import pfx_call
27
+
28
+ __version__ = '20241122'
29
+
30
+ DISTINFO = {
31
+ 'keywords': ["python2", "python3"],
32
+ 'classifiers': [
33
+ "Programming Language :: Python",
34
+ "Programming Language :: Python :: 3",
35
+ ],
36
+ 'install_requires': [
37
+ 'cs.deco',
38
+ 'cs.gimmicks>=devnull',
39
+ 'cs.pfx',
40
+ ],
41
+ }
42
+
43
+ # maximum number of bytes usable in the argv list for the exec*() functions
44
+ # 262144 below is from MacOS El Capitan "sysctl kern.argmax", then
45
+ # halved because even allowing for the size of the environment this
46
+ # can be too big. Unsure why.
47
+ MAX_ARGV = 262144 // 2
48
+
49
+ def stop(pid, signum=SIGTERM, wait=None, do_SIGKILL=False):
50
+ ''' Stop the process specified by `pid`, optionally await its demise.
51
+
52
+ Parameters:
53
+ * `pid`: process id.
54
+ If `pid` is a string, treat as a process id file and read the
55
+ process id from it.
56
+ * `signum`: the signal to send, default `signal.SIGTERM`.
57
+ * `wait`: whether to wait for the process, default `None`.
58
+ If `None`, return `True` (signal delivered).
59
+ If `0`, wait indefinitely until the process exits as tested by
60
+ `os.kill(pid, 0)`.
61
+ If greater than 0, wait up to `wait` seconds for the process to die;
62
+ if it exits, return `True`, otherwise `False`;
63
+ * `do_SIGKILL`: if true (default `False`),
64
+ send the process `signal.SIGKILL` as a final measure before return.
65
+ '''
66
+ if isinstance(pid, str):
67
+ return stop(int(open(pid, encoding='ascii').read().strip()))
68
+ os.kill(pid, signum)
69
+ if wait is None:
70
+ return True
71
+ assert wait >= 0, "wait (%s) should be >= 0" % (wait,)
72
+ now = time.time()
73
+ then = now + wait
74
+ while True:
75
+ time.sleep(0.1)
76
+ if wait == 0 or time.time() < then:
77
+ try:
78
+ os.kill(pid, 0)
79
+ except OSError as e:
80
+ if e.errno != errno.ESRCH:
81
+ raise
82
+ # process no longer present
83
+ return True
84
+ else:
85
+ if do_SIGKILL:
86
+ try:
87
+ os.kill(pid, SIGKILL)
88
+ except OSError as e:
89
+ if e.errno != errno.ESRCH:
90
+ raise
91
+ return False
92
+
93
+ @contextmanager
94
+ def signal_handler(sig, handler, call_previous=False):
95
+ ''' Context manager to push a new signal handler,
96
+ yielding the old handler,
97
+ restoring the old handler on exit.
98
+ If `call_previous` is true (default `False`)
99
+ also call the old handler after the new handler on receipt of the signal.
100
+
101
+ Parameters:
102
+ * `sig`: the `int` signal number to catch
103
+ * `handler`: the handler function to call with `(sig,frame)`
104
+ * `call_previous`: optional flag (default `False`);
105
+ if true, also call the old handler (if any) after `handler`
106
+ '''
107
+ if call_previous:
108
+ # replace handler() with a wrapper to call both it and the old handler
109
+ handler0 = handler
110
+
111
+ def handler(sig, frame): # pylint:disable=function-redefined
112
+ ''' Call the handler and then the previous handler if requested.
113
+ '''
114
+ handler0(sig, frame)
115
+ if callable(old_handler):
116
+ old_handler(sig, frame)
117
+
118
+ old_handler = signal(sig, handler)
119
+ try:
120
+ yield old_handler
121
+ finally:
122
+ # restiore the previous handler
123
+ signal(sig, old_handler)
124
+
125
+ @contextmanager
126
+ def signal_handlers(sig_hnds, call_previous=False, _stacked=None):
127
+ ''' Context manager to stack multiple signal handlers,
128
+ yielding a mapping of `sig`=>`old_handler`.
129
+
130
+ Parameters:
131
+ * `sig_hnds`: a mapping of `sig`=>`new_handler`
132
+ or an iterable of `(sig,new_handler)` pairs
133
+ * `call_previous`: optional flag (default `False`), passed
134
+ to `signal_handler()`
135
+ '''
136
+ if _stacked is None:
137
+ _stacked = {}
138
+ try:
139
+ items = sig_hnds.items
140
+ except AttributeError:
141
+ # (sig,hnd),... from iterable
142
+ it = iter(sig_hnds)
143
+ else:
144
+ # (sig,hnd),... from mapping
145
+ it = iter(items())
146
+ try:
147
+ sig, handler = next(it)
148
+ except StopIteration:
149
+ pass
150
+ else:
151
+ with signal_handler(sig, handler,
152
+ call_previous=call_previous) as old_handler:
153
+ _stacked[sig] = old_handler
154
+ with signal_handlers(it, call_previous=call_previous,
155
+ _stacked=_stacked) as stacked:
156
+ yield stacked
157
+ return
158
+ yield _stacked
159
+
160
+ def write_pidfile(path, pid=None):
161
+ ''' Write a process id to a pid file.
162
+
163
+ Parameters:
164
+ * `path`: the path to the pid file.
165
+ * `pid`: the process id to write, defautl from `os.getpid`.
166
+ '''
167
+ if pid is None:
168
+ pid = os.getpid()
169
+ with open(path, 'w', encoding='ascii') as pidfp:
170
+ print(pid, file=pidfp)
171
+
172
+ def remove_pidfile(path):
173
+ ''' Truncate and remove a pidfile, permissions permitting.
174
+ '''
175
+ try:
176
+ with open(path, "wb"): # pylint: disable=unspecified-encoding
177
+ pass
178
+ os.remove(path)
179
+ except OSError as e:
180
+ if e.errno != errno.EPERM:
181
+ raise
182
+
183
+ @contextmanager
184
+ def PidFileManager(path, pid=None):
185
+ ''' Context manager for a pid file.
186
+
187
+ Parameters:
188
+ * `path`: the path to the process id file.
189
+ * `pid`: the process id to store in the pid file,
190
+ default from `os.etpid`.
191
+
192
+ Writes the process id file at the start
193
+ and removes the process id file at the end.
194
+ '''
195
+ write_pidfile(path, pid=pid)
196
+ try:
197
+ yield
198
+ finally:
199
+ remove_pidfile(path)
200
+
201
+ @uses_doit
202
+ @uses_quiet
203
+ def run(
204
+ argv,
205
+ *,
206
+ doit: bool,
207
+ logger=None,
208
+ quiet: bool,
209
+ input=None,
210
+ stdin=None,
211
+ print=None,
212
+ **subp_options,
213
+ ):
214
+ ''' Run a command via `subprocess.run`.
215
+ Return the `CompletedProcess` result or `None` if `doit` is false.
216
+
217
+ Parameters:
218
+ * `argv`: the command line to run
219
+ * `doit`: optional flag, default `True`;
220
+ if false do not run the command and return `None`
221
+ * `logger`: optional logger, default `None`;
222
+ if `True`, use `logging.getLogger()`;
223
+ if not `None` or `False` trace using `print_argv`
224
+ * `quiet`: default `True`; if false, print the command and its output
225
+ * `input`: default `None`: alternative to `stdin`;
226
+ passed to `subprocess.run`
227
+ * `stdin`: standard input for the subprocess, default `subprocess.DEVNULL`;
228
+ passed to `subprocess.run`
229
+ * `subp_options`: optional mapping of keyword arguments
230
+ to pass to `subprocess.run`
231
+
232
+ Note that `argv` is passed through `prep_argv` before use,
233
+ allowing direct invocation with conditional parts.
234
+ See the `prep_argv` function for details.
235
+ '''
236
+ argv = prep_argv(*argv)
237
+ if logger is True:
238
+ logger = logging.getLogger()
239
+ if not doit:
240
+ if not quiet:
241
+ if logger:
242
+ trace("skip: %s", shlex.join(argv))
243
+ else:
244
+ print_argv(*argv, fold=True)
245
+ return None
246
+ if not quiet:
247
+ if logger:
248
+ trace("+ %s", shlex.join(argv))
249
+ else:
250
+ print_argv(*argv, indent="+ ", file=sys.stderr, print=print)
251
+ if input is None:
252
+ if stdin is None:
253
+ stdin = subprocess_DEVNULL
254
+ elif stdin is not None:
255
+ raise ValueError("you may not specify both input and stdin")
256
+ cp = pfx_call(subprocess_run, argv, input=input, stdin=stdin, **subp_options)
257
+ if cp.stderr:
258
+ print(" stderr:")
259
+ print(" ", cp.stderr.rstrip().replace("\n", "\n "))
260
+ if cp.returncode != 0:
261
+ warning(
262
+ "run fails, exit code %s from %s",
263
+ cp.returncode,
264
+ shlex.join(cp.args),
265
+ )
266
+ return cp
267
+
268
+ @uses_quiet
269
+ def pipefrom(argv, *, quiet: bool, text=True, stdin=DEVNULL, **popen_kw):
270
+ ''' Pipe text (usually) from a command using `subprocess.Popen`.
271
+ Return the `Popen` object with `.stdout` as a pipe.
272
+
273
+ Parameters:
274
+ * `argv`: the command argument list
275
+ * `quiet`: optional flag, default `False`;
276
+ if true, print the command to `stderr`
277
+ * `text`: optional flag, default `True`; passed to `Popen`.
278
+ * `stdin`: optional value for `Popen`'s `stdin`, default `DEVNULL`
279
+ Other keyword arguments are passed to `Popen`.
280
+
281
+ Note that `argv` is passed through `prep_argv` before use,
282
+ allowing direct invocation with conditional parts.
283
+ See the `prep_argv` function for details.
284
+ '''
285
+ argv = prep_argv(*argv)
286
+ if not quiet:
287
+ print_argv(*argv, indent="+ ", end=" |\n", file=sys.stderr)
288
+ return Popen(argv, stdout=PIPE, text=text, stdin=stdin, **popen_kw)
289
+
290
+ @uses_quiet
291
+ def pipeto(argv, *, quiet: bool, **kw):
292
+ ''' Pipe text to a command.
293
+ Optionally trace invocation.
294
+ Return the Popen object with .stdin encoded as text.
295
+
296
+ Parameters:
297
+ * `argv`: the command argument list
298
+ * `trace`: if true (default `False`),
299
+ if `trace` is `True`, recite invocation to stderr
300
+ otherwise presume that `trace` is a stream
301
+ to which to recite the invocation.
302
+
303
+ Other keyword arguments are passed to the `io.TextIOWrapper`
304
+ which wraps the command's input.
305
+
306
+ Note that `argv` is passed through `prep_argv` before use,
307
+ allowing direct invocation with conditional parts.
308
+ See the `prep_argv` function for details.
309
+ '''
310
+ argv = prep_argv(*argv)
311
+ if not quiet:
312
+ print_argv(*argv, indent="| ", file=sys.stderr)
313
+ P = Popen(argv, stdin=PIPE) # pylint: disable=consider-using-with
314
+ P.stdin = io.TextIOWrapper(P.stdin, **kw)
315
+ return P
316
+
317
+ @fmtdoc
318
+ def groupargv(pre_argv, argv, post_argv=(), max_argv=None, encode=False):
319
+ ''' Distribute the array `argv` over multiple arrays
320
+ to fit within `MAX_ARGV`.
321
+ Return a list of argv lists.
322
+
323
+ Parameters:
324
+ * `pre_argv`: the sequence of leading arguments
325
+ * `argv`: the sequence of arguments to distribute; this may not be empty
326
+ * `post_argv`: optional, the sequence of trailing arguments
327
+ * `max_argv`: optional, the maximum length of each distributed
328
+ argument list, default from `MAX_ARGV`: `{MAX_ARGV}`
329
+ * `encode`: default `False`.
330
+ If true, encode the argv sequences into bytes for accurate tallying.
331
+ If `encode` is a Boolean,
332
+ encode the elements with their .encode() method.
333
+ If `encode` is a `str`, encode the elements with their `.encode()`
334
+ method with `encode` as the encoding name;
335
+ otherwise presume that `encode` is a callable
336
+ for encoding each element.
337
+
338
+ The returned argv arrays will contain the encoded element values.
339
+ '''
340
+ if not argv:
341
+ raise ValueError("argv may not be empty")
342
+ if max_argv is None:
343
+ max_argv = MAX_ARGV
344
+ if encode:
345
+ if isinstance(encode, bool):
346
+ pre_argv = [arg.encode() for arg in pre_argv]
347
+ argv = [arg.encode() for arg in argv]
348
+ post_argv = [arg.encode() for arg in post_argv]
349
+ elif isinstance(encode, str):
350
+ pre_argv = [arg.encode(encode) for arg in pre_argv]
351
+ argv = [arg.encode(encode) for arg in argv]
352
+ post_argv = [arg.encode(encode) for arg in post_argv]
353
+ else:
354
+ pre_argv = [encode(arg) for arg in pre_argv]
355
+ argv = [encode(arg) for arg in argv]
356
+ post_argv = [encode(arg) for arg in post_argv]
357
+ else:
358
+ pre_argv = list(pre_argv)
359
+ post_argv = list(post_argv)
360
+ pre_nbytes = sum(len(arg) + 1 for arg in pre_argv)
361
+ post_nbytes = sum(len(arg) + 1 for arg in post_argv)
362
+ argvs = []
363
+ available = max_argv - pre_nbytes - post_nbytes
364
+ per = []
365
+ for arg in argv:
366
+ nbytes = len(arg) + 1
367
+ if available - nbytes < 0:
368
+ if not per:
369
+ raise ValueError(
370
+ "cannot fit argument into argv: available=%d, len(arg)=%d: %r" %
371
+ (available, len(arg), arg)
372
+ )
373
+ argvs.append(pre_argv + per + post_argv)
374
+ available = max_argv - pre_nbytes - post_nbytes
375
+ per = []
376
+ per.append(arg)
377
+ available -= nbytes
378
+ if per:
379
+ argvs.append(pre_argv + per + post_argv)
380
+ return argvs
381
+
382
+ def prep_argv(*argv):
383
+ ''' A trite list comprehension to reduce an argument list `*argv`
384
+ to the entries which are not `None` or `False`
385
+ and to flatten other entries which are not strings.
386
+
387
+ This exists ease the construction of argument lists
388
+ with methods like this:
389
+
390
+ >>> command_exe = 'hashindex'
391
+ >>> hashname = 'sha1'
392
+ >>> quiet = False
393
+ >>> verbose = True
394
+ >>> prep_argv(
395
+ ... command_exe,
396
+ ... quiet and '-q',
397
+ ... verbose and '-v',
398
+ ... hashname and ('-h', hashname),
399
+ ... )
400
+ ['hashindex', '-v', '-h', 'sha1']
401
+
402
+ where `verbose` is a `bool` governing the `-v` option
403
+ and `hashname` is either `str` to be passed with `-h hashname`
404
+ or `None` to omit the option.
405
+ '''
406
+ return list(
407
+ chain(
408
+ *[
409
+ ((arg,) if isinstance(arg, str) else arg)
410
+ for arg in argv
411
+ if arg is not None and arg is not False
412
+ ]
413
+ )
414
+ )
415
+
416
+ def print_argv(
417
+ *argv,
418
+ indent="",
419
+ subindent=" ",
420
+ end="\n",
421
+ file=None,
422
+ fold=False,
423
+ print=None,
424
+ ):
425
+ ''' Print an indented possibly folded command line.
426
+ '''
427
+ if file is None:
428
+ file = sys.stdout
429
+ if print is None:
430
+ print = builtins.print
431
+ print_argv = []
432
+ was_opt = False
433
+ for i, arg in enumerate(argv):
434
+ if i == 0:
435
+ print_argv.append(indent)
436
+ was_opt = False
437
+ elif len(arg) >= 2 and arg.startswith('-'):
438
+ if fold:
439
+ # options get a new line
440
+ print_argv.append(" \\\n" + indent + subindent)
441
+ else:
442
+ print_argv.append(" ")
443
+ was_opt = True
444
+ else:
445
+ if was_opt:
446
+ print_argv.append(" ")
447
+ elif fold:
448
+ # nonoptions get a new line
449
+ print_argv.append(" \\\n" + indent + subindent)
450
+ else:
451
+ print_argv.append(" ")
452
+ was_opt = False
453
+ print_argv.append(shlex.quote(arg))
454
+ print(*print_argv, sep='', end=end, file=file)
455
+
456
+ if __name__ == '__main__':
457
+ for test_max_argv in 64, 20, 16, 8:
458
+ print(
459
+ test_max_argv,
460
+ repr(
461
+ groupargv(
462
+ ['cp', '-a'], ['a', 'bbbb', 'ddddddddddddd'], ['end'],
463
+ max_argv=test_max_argv,
464
+ encode=True
465
+ )
466
+ )
467
+ )
@@ -0,0 +1,292 @@
1
+ Metadata-Version: 2.1
2
+ Name: cs-psutils
3
+ Version: 20241122
4
+ Summary: Assorted process and subprocess management functions.
5
+ Author-email: Cameron Simpson <cs@cskk.id.au>
6
+ License: GNU General Public License v3 or later (GPLv3+)
7
+ Project-URL: Monorepo Hg/Mercurial Mirror, https://hg.sr.ht/~cameron-simpson/css
8
+ Project-URL: Monorepo Git Mirror, https://github.com/cameron-simpson/css
9
+ Project-URL: MonoRepo Commits, https://bitbucket.org/cameron_simpson/css/commits/branch/main
10
+ Project-URL: Source, https://github.com/cameron-simpson/css/blob/main/lib/python/cs/psutils.py
11
+ Keywords: python2,python3
12
+ Classifier: Programming Language :: Python
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Development Status :: 4 - Beta
15
+ Classifier: Intended Audience :: Developers
16
+ Classifier: Operating System :: OS Independent
17
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
18
+ Classifier: License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)
19
+ Description-Content-Type: text/markdown
20
+ Requires-Dist: cs.deco>=20241109
21
+ Requires-Dist: cs.gimmicks>=20220429
22
+ Requires-Dist: cs.pfx>=20240630
23
+
24
+ Assorted process and subprocess management functions.
25
+
26
+ *Latest release 20241122*:
27
+ * print_argv: new print= parameter to provide a print() function, refactor to use print instead of file.write.
28
+ * run: new optional print parameter, plumb to print_argv.
29
+ * Use @uses_doit and @uses_quiet to provide the default quiet and doit states.
30
+
31
+ Not to be confused with the excellent
32
+ (psutil)[https://pypi.org/project/psutil/] package.
33
+
34
+ ## <a name="groupargv"></a>`groupargv(pre_argv, argv, post_argv=(), max_argv=None, encode=False)`
35
+
36
+ Distribute the array `argv` over multiple arrays
37
+ to fit within `MAX_ARGV`.
38
+ Return a list of argv lists.
39
+
40
+ Parameters:
41
+ * `pre_argv`: the sequence of leading arguments
42
+ * `argv`: the sequence of arguments to distribute; this may not be empty
43
+ * `post_argv`: optional, the sequence of trailing arguments
44
+ * `max_argv`: optional, the maximum length of each distributed
45
+ argument list, default from `MAX_ARGV`: `131072`
46
+ * `encode`: default `False`.
47
+ If true, encode the argv sequences into bytes for accurate tallying.
48
+ If `encode` is a Boolean,
49
+ encode the elements with their .encode() method.
50
+ If `encode` is a `str`, encode the elements with their `.encode()`
51
+ method with `encode` as the encoding name;
52
+ otherwise presume that `encode` is a callable
53
+ for encoding each element.
54
+
55
+ The returned argv arrays will contain the encoded element values.
56
+
57
+ ## <a name="PidFileManager"></a>`PidFileManager(path, pid=None)`
58
+
59
+ Context manager for a pid file.
60
+
61
+ Parameters:
62
+ * `path`: the path to the process id file.
63
+ * `pid`: the process id to store in the pid file,
64
+ default from `os.etpid`.
65
+
66
+ Writes the process id file at the start
67
+ and removes the process id file at the end.
68
+
69
+ ## <a name="pipefrom"></a>`pipefrom(argv, *, quiet: bool, text=True, stdin=-3, **popen_kw)`
70
+
71
+ Pipe text (usually) from a command using `subprocess.Popen`.
72
+ Return the `Popen` object with `.stdout` as a pipe.
73
+
74
+ Parameters:
75
+ * `argv`: the command argument list
76
+ * `quiet`: optional flag, default `False`;
77
+ if true, print the command to `stderr`
78
+ * `text`: optional flag, default `True`; passed to `Popen`.
79
+ * `stdin`: optional value for `Popen`'s `stdin`, default `DEVNULL`
80
+ Other keyword arguments are passed to `Popen`.
81
+
82
+ Note that `argv` is passed through `prep_argv` before use,
83
+ allowing direct invocation with conditional parts.
84
+ See the `prep_argv` function for details.
85
+
86
+ ## <a name="pipeto"></a>`pipeto(argv, *, quiet: bool, **kw)`
87
+
88
+ Pipe text to a command.
89
+ Optionally trace invocation.
90
+ Return the Popen object with .stdin encoded as text.
91
+
92
+ Parameters:
93
+ * `argv`: the command argument list
94
+ * `trace`: if true (default `False`),
95
+ if `trace` is `True`, recite invocation to stderr
96
+ otherwise presume that `trace` is a stream
97
+ to which to recite the invocation.
98
+
99
+ Other keyword arguments are passed to the `io.TextIOWrapper`
100
+ which wraps the command's input.
101
+
102
+ Note that `argv` is passed through `prep_argv` before use,
103
+ allowing direct invocation with conditional parts.
104
+ See the `prep_argv` function for details.
105
+
106
+ ## <a name="prep_argv"></a>`prep_argv(*argv)`
107
+
108
+ A trite list comprehension to reduce an argument list `*argv`
109
+ to the entries which are not `None` or `False`
110
+ and to flatten other entries which are not strings.
111
+
112
+ This exists ease the construction of argument lists
113
+ with methods like this:
114
+
115
+ >>> command_exe = 'hashindex'
116
+ >>> hashname = 'sha1'
117
+ >>> quiet = False
118
+ >>> verbose = True
119
+ >>> prep_argv(
120
+ ... command_exe,
121
+ ... quiet and '-q',
122
+ ... verbose and '-v',
123
+ ... hashname and ('-h', hashname),
124
+ ... )
125
+ ['hashindex', '-v', '-h', 'sha1']
126
+
127
+ where `verbose` is a `bool` governing the `-v` option
128
+ and `hashname` is either `str` to be passed with `-h hashname`
129
+ or `None` to omit the option.
130
+
131
+ ## <a name="print_argv"></a>`print_argv(*argv, indent='', subindent=' ', end='\n', file=None, fold=False, print=None)`
132
+
133
+ Print an indented possibly folded command line.
134
+
135
+ ## <a name="remove_pidfile"></a>`remove_pidfile(path)`
136
+
137
+ Truncate and remove a pidfile, permissions permitting.
138
+
139
+ ## <a name="run"></a>`run(argv, *, doit: bool, logger=None, quiet: bool, input=None, stdin=None, print=None, **subp_options)`
140
+
141
+ Run a command via `subprocess.run`.
142
+ Return the `CompletedProcess` result or `None` if `doit` is false.
143
+
144
+ Parameters:
145
+ * `argv`: the command line to run
146
+ * `doit`: optional flag, default `True`;
147
+ if false do not run the command and return `None`
148
+ * `logger`: optional logger, default `None`;
149
+ if `True`, use `logging.getLogger()`;
150
+ if not `None` or `False` trace using `print_argv`
151
+ * `quiet`: default `True`; if false, print the command and its output
152
+ * `input`: default `None`: alternative to `stdin`;
153
+ passed to `subprocess.run`
154
+ * `stdin`: standard input for the subprocess, default `subprocess.DEVNULL`;
155
+ passed to `subprocess.run`
156
+ * `subp_options`: optional mapping of keyword arguments
157
+ to pass to `subprocess.run`
158
+
159
+ Note that `argv` is passed through `prep_argv` before use,
160
+ allowing direct invocation with conditional parts.
161
+ See the `prep_argv` function for details.
162
+
163
+ ## <a name="signal_handler"></a>`signal_handler(sig, handler, call_previous=False)`
164
+
165
+ Context manager to push a new signal handler,
166
+ yielding the old handler,
167
+ restoring the old handler on exit.
168
+ If `call_previous` is true (default `False`)
169
+ also call the old handler after the new handler on receipt of the signal.
170
+
171
+ Parameters:
172
+ * `sig`: the `int` signal number to catch
173
+ * `handler`: the handler function to call with `(sig,frame)`
174
+ * `call_previous`: optional flag (default `False`);
175
+ if true, also call the old handler (if any) after `handler`
176
+
177
+ ## <a name="signal_handlers"></a>`signal_handlers(sig_hnds, call_previous=False, _stacked=None)`
178
+
179
+ Context manager to stack multiple signal handlers,
180
+ yielding a mapping of `sig`=>`old_handler`.
181
+
182
+ Parameters:
183
+ * `sig_hnds`: a mapping of `sig`=>`new_handler`
184
+ or an iterable of `(sig,new_handler)` pairs
185
+ * `call_previous`: optional flag (default `False`), passed
186
+ to `signal_handler()`
187
+
188
+ ## <a name="stop"></a>`stop(pid, signum=<Signals.SIGTERM: 15>, wait=None, do_SIGKILL=False)`
189
+
190
+ Stop the process specified by `pid`, optionally await its demise.
191
+
192
+ Parameters:
193
+ * `pid`: process id.
194
+ If `pid` is a string, treat as a process id file and read the
195
+ process id from it.
196
+ * `signum`: the signal to send, default `signal.SIGTERM`.
197
+ * `wait`: whether to wait for the process, default `None`.
198
+ If `None`, return `True` (signal delivered).
199
+ If `0`, wait indefinitely until the process exits as tested by
200
+ `os.kill(pid, 0)`.
201
+ If greater than 0, wait up to `wait` seconds for the process to die;
202
+ if it exits, return `True`, otherwise `False`;
203
+ * `do_SIGKILL`: if true (default `False`),
204
+ send the process `signal.SIGKILL` as a final measure before return.
205
+
206
+ ## <a name="write_pidfile"></a>`write_pidfile(path, pid=None)`
207
+
208
+ Write a process id to a pid file.
209
+
210
+ Parameters:
211
+ * `path`: the path to the pid file.
212
+ * `pid`: the process id to write, defautl from `os.getpid`.
213
+
214
+ # Release Log
215
+
216
+
217
+
218
+ *Release 20241122*:
219
+ * print_argv: new print= parameter to provide a print() function, refactor to use print instead of file.write.
220
+ * run: new optional print parameter, plumb to print_argv.
221
+ * Use @uses_doit and @uses_quiet to provide the default quiet and doit states.
222
+
223
+ *Release 20240316*:
224
+ Fixed release upload artifacts.
225
+
226
+ *Release 20240211*:
227
+ * run: new optional input= parameter to presupply input data.
228
+ * New prep_argv(*argv) function to flatten and trim computes argv lists; use automatically in run(), pipeot(), pipefrom().
229
+
230
+ *Release 20231129*:
231
+ run(): default stdin=subprocess.DEVNULL.
232
+
233
+ *Release 20230612*:
234
+ * pipefrom: default stdin=DEVNULL.
235
+ * Make many parameters keyword only.
236
+
237
+ *Release 20221228*:
238
+ * signal_handlers: bugfix iteration of sig_hnds.
239
+ * Use cs.gimmicks instead of cs.logutils.
240
+ * Drop use of cs.upd, fixes circular import; users of run() may need to call "with Upd().above()" themselves.
241
+
242
+ *Release 20221118*:
243
+ run: do not print cp.stdout.
244
+
245
+ *Release 20220805*:
246
+ run: print trace to stderr.
247
+
248
+ *Release 20220626*:
249
+ run: default quiet=True.
250
+
251
+ *Release 20220606*:
252
+ * run: fold in the superior run() from cs.ebooks.
253
+ * pipefrom,pipeto: replace trace= with quiet= like run().
254
+ * print_argv: add an `end` parameter (used by pipefrom).
255
+
256
+ *Release 20220531*:
257
+ * New print_argv function for writing shell command lines out nicely.
258
+ * Bump requirements for cs.gimmicks.
259
+
260
+ *Release 20220504*:
261
+ signal_handlers: reshape try/except to avoid confusing traceback.
262
+
263
+ *Release 20220429*:
264
+ * New signal_handler(sig,handler,call_previous=False) context manager to push a signal handler.
265
+ * New signal_handlers() context manager to stack handlers for multiple signals.
266
+
267
+ *Release 20190101*:
268
+ Bugfix context manager cleanup. groupargv improvements.
269
+
270
+ *Release 20171112*:
271
+ Bugfix array length counting.
272
+
273
+ *Release 20171110*:
274
+ New function groupargv for breaking up argv lists to fit within the maximum argument limit; constant MAX_ARGV for the default limit.
275
+
276
+ *Release 20171031*:
277
+ run: accept optional pids parameter, a setlike collection of process ids.
278
+
279
+ *Release 20171018*:
280
+ run: replace `trace` parameter with `logger`, default None
281
+
282
+ *Release 20170908.1*:
283
+ remove dependency on cs.pfx
284
+
285
+ *Release 20170908*:
286
+ run: pass extra keyword arguments to subprocess.call
287
+
288
+ *Release 20170906.1*:
289
+ Add run, pipefrom and pipeto - were incorrectly in another module.
290
+
291
+ *Release 20170906*:
292
+ First PyPI release.
@@ -0,0 +1,5 @@
1
+ cs/psutils.py,sha256=_H7rDlZGbZWa82OGg_askFjh9WcTXTzgsnlkWatVBbQ,14425
2
+ cs_psutils-20241122.dist-info/METADATA,sha256=JQcpDVsw5_zP9P4uvmdXAId1adZ7MNB5g3ew99H-YpM,10404
3
+ cs_psutils-20241122.dist-info/WHEEL,sha256=P9jw-gEje8ByB7_hXoICnHtVCrEwMQh-630tKvQWehc,91
4
+ cs_psutils-20241122.dist-info/top_level.txt,sha256=MJn10B_hUOb4f5hIJP5wKj_mMYsOQ6NUWqo9vSLmwcQ,3
5
+ cs_psutils-20241122.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (75.3.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ cs