cs-psutils 20241122__tar.gz

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.
@@ -0,0 +1 @@
1
+ include README.md
@@ -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,269 @@
1
+ Assorted process and subprocess management functions.
2
+
3
+ *Latest release 20241122*:
4
+ * print_argv: new print= parameter to provide a print() function, refactor to use print instead of file.write.
5
+ * run: new optional print parameter, plumb to print_argv.
6
+ * Use @uses_doit and @uses_quiet to provide the default quiet and doit states.
7
+
8
+ Not to be confused with the excellent
9
+ (psutil)[https://pypi.org/project/psutil/] package.
10
+
11
+ ## <a name="groupargv"></a>`groupargv(pre_argv, argv, post_argv=(), max_argv=None, encode=False)`
12
+
13
+ Distribute the array `argv` over multiple arrays
14
+ to fit within `MAX_ARGV`.
15
+ Return a list of argv lists.
16
+
17
+ Parameters:
18
+ * `pre_argv`: the sequence of leading arguments
19
+ * `argv`: the sequence of arguments to distribute; this may not be empty
20
+ * `post_argv`: optional, the sequence of trailing arguments
21
+ * `max_argv`: optional, the maximum length of each distributed
22
+ argument list, default from `MAX_ARGV`: `131072`
23
+ * `encode`: default `False`.
24
+ If true, encode the argv sequences into bytes for accurate tallying.
25
+ If `encode` is a Boolean,
26
+ encode the elements with their .encode() method.
27
+ If `encode` is a `str`, encode the elements with their `.encode()`
28
+ method with `encode` as the encoding name;
29
+ otherwise presume that `encode` is a callable
30
+ for encoding each element.
31
+
32
+ The returned argv arrays will contain the encoded element values.
33
+
34
+ ## <a name="PidFileManager"></a>`PidFileManager(path, pid=None)`
35
+
36
+ Context manager for a pid file.
37
+
38
+ Parameters:
39
+ * `path`: the path to the process id file.
40
+ * `pid`: the process id to store in the pid file,
41
+ default from `os.etpid`.
42
+
43
+ Writes the process id file at the start
44
+ and removes the process id file at the end.
45
+
46
+ ## <a name="pipefrom"></a>`pipefrom(argv, *, quiet: bool, text=True, stdin=-3, **popen_kw)`
47
+
48
+ Pipe text (usually) from a command using `subprocess.Popen`.
49
+ Return the `Popen` object with `.stdout` as a pipe.
50
+
51
+ Parameters:
52
+ * `argv`: the command argument list
53
+ * `quiet`: optional flag, default `False`;
54
+ if true, print the command to `stderr`
55
+ * `text`: optional flag, default `True`; passed to `Popen`.
56
+ * `stdin`: optional value for `Popen`'s `stdin`, default `DEVNULL`
57
+ Other keyword arguments are passed to `Popen`.
58
+
59
+ Note that `argv` is passed through `prep_argv` before use,
60
+ allowing direct invocation with conditional parts.
61
+ See the `prep_argv` function for details.
62
+
63
+ ## <a name="pipeto"></a>`pipeto(argv, *, quiet: bool, **kw)`
64
+
65
+ Pipe text to a command.
66
+ Optionally trace invocation.
67
+ Return the Popen object with .stdin encoded as text.
68
+
69
+ Parameters:
70
+ * `argv`: the command argument list
71
+ * `trace`: if true (default `False`),
72
+ if `trace` is `True`, recite invocation to stderr
73
+ otherwise presume that `trace` is a stream
74
+ to which to recite the invocation.
75
+
76
+ Other keyword arguments are passed to the `io.TextIOWrapper`
77
+ which wraps the command's input.
78
+
79
+ Note that `argv` is passed through `prep_argv` before use,
80
+ allowing direct invocation with conditional parts.
81
+ See the `prep_argv` function for details.
82
+
83
+ ## <a name="prep_argv"></a>`prep_argv(*argv)`
84
+
85
+ A trite list comprehension to reduce an argument list `*argv`
86
+ to the entries which are not `None` or `False`
87
+ and to flatten other entries which are not strings.
88
+
89
+ This exists ease the construction of argument lists
90
+ with methods like this:
91
+
92
+ >>> command_exe = 'hashindex'
93
+ >>> hashname = 'sha1'
94
+ >>> quiet = False
95
+ >>> verbose = True
96
+ >>> prep_argv(
97
+ ... command_exe,
98
+ ... quiet and '-q',
99
+ ... verbose and '-v',
100
+ ... hashname and ('-h', hashname),
101
+ ... )
102
+ ['hashindex', '-v', '-h', 'sha1']
103
+
104
+ where `verbose` is a `bool` governing the `-v` option
105
+ and `hashname` is either `str` to be passed with `-h hashname`
106
+ or `None` to omit the option.
107
+
108
+ ## <a name="print_argv"></a>`print_argv(*argv, indent='', subindent=' ', end='\n', file=None, fold=False, print=None)`
109
+
110
+ Print an indented possibly folded command line.
111
+
112
+ ## <a name="remove_pidfile"></a>`remove_pidfile(path)`
113
+
114
+ Truncate and remove a pidfile, permissions permitting.
115
+
116
+ ## <a name="run"></a>`run(argv, *, doit: bool, logger=None, quiet: bool, input=None, stdin=None, print=None, **subp_options)`
117
+
118
+ Run a command via `subprocess.run`.
119
+ Return the `CompletedProcess` result or `None` if `doit` is false.
120
+
121
+ Parameters:
122
+ * `argv`: the command line to run
123
+ * `doit`: optional flag, default `True`;
124
+ if false do not run the command and return `None`
125
+ * `logger`: optional logger, default `None`;
126
+ if `True`, use `logging.getLogger()`;
127
+ if not `None` or `False` trace using `print_argv`
128
+ * `quiet`: default `True`; if false, print the command and its output
129
+ * `input`: default `None`: alternative to `stdin`;
130
+ passed to `subprocess.run`
131
+ * `stdin`: standard input for the subprocess, default `subprocess.DEVNULL`;
132
+ passed to `subprocess.run`
133
+ * `subp_options`: optional mapping of keyword arguments
134
+ to pass to `subprocess.run`
135
+
136
+ Note that `argv` is passed through `prep_argv` before use,
137
+ allowing direct invocation with conditional parts.
138
+ See the `prep_argv` function for details.
139
+
140
+ ## <a name="signal_handler"></a>`signal_handler(sig, handler, call_previous=False)`
141
+
142
+ Context manager to push a new signal handler,
143
+ yielding the old handler,
144
+ restoring the old handler on exit.
145
+ If `call_previous` is true (default `False`)
146
+ also call the old handler after the new handler on receipt of the signal.
147
+
148
+ Parameters:
149
+ * `sig`: the `int` signal number to catch
150
+ * `handler`: the handler function to call with `(sig,frame)`
151
+ * `call_previous`: optional flag (default `False`);
152
+ if true, also call the old handler (if any) after `handler`
153
+
154
+ ## <a name="signal_handlers"></a>`signal_handlers(sig_hnds, call_previous=False, _stacked=None)`
155
+
156
+ Context manager to stack multiple signal handlers,
157
+ yielding a mapping of `sig`=>`old_handler`.
158
+
159
+ Parameters:
160
+ * `sig_hnds`: a mapping of `sig`=>`new_handler`
161
+ or an iterable of `(sig,new_handler)` pairs
162
+ * `call_previous`: optional flag (default `False`), passed
163
+ to `signal_handler()`
164
+
165
+ ## <a name="stop"></a>`stop(pid, signum=<Signals.SIGTERM: 15>, wait=None, do_SIGKILL=False)`
166
+
167
+ Stop the process specified by `pid`, optionally await its demise.
168
+
169
+ Parameters:
170
+ * `pid`: process id.
171
+ If `pid` is a string, treat as a process id file and read the
172
+ process id from it.
173
+ * `signum`: the signal to send, default `signal.SIGTERM`.
174
+ * `wait`: whether to wait for the process, default `None`.
175
+ If `None`, return `True` (signal delivered).
176
+ If `0`, wait indefinitely until the process exits as tested by
177
+ `os.kill(pid, 0)`.
178
+ If greater than 0, wait up to `wait` seconds for the process to die;
179
+ if it exits, return `True`, otherwise `False`;
180
+ * `do_SIGKILL`: if true (default `False`),
181
+ send the process `signal.SIGKILL` as a final measure before return.
182
+
183
+ ## <a name="write_pidfile"></a>`write_pidfile(path, pid=None)`
184
+
185
+ Write a process id to a pid file.
186
+
187
+ Parameters:
188
+ * `path`: the path to the pid file.
189
+ * `pid`: the process id to write, defautl from `os.getpid`.
190
+
191
+ # Release Log
192
+
193
+
194
+
195
+ *Release 20241122*:
196
+ * print_argv: new print= parameter to provide a print() function, refactor to use print instead of file.write.
197
+ * run: new optional print parameter, plumb to print_argv.
198
+ * Use @uses_doit and @uses_quiet to provide the default quiet and doit states.
199
+
200
+ *Release 20240316*:
201
+ Fixed release upload artifacts.
202
+
203
+ *Release 20240211*:
204
+ * run: new optional input= parameter to presupply input data.
205
+ * New prep_argv(*argv) function to flatten and trim computes argv lists; use automatically in run(), pipeot(), pipefrom().
206
+
207
+ *Release 20231129*:
208
+ run(): default stdin=subprocess.DEVNULL.
209
+
210
+ *Release 20230612*:
211
+ * pipefrom: default stdin=DEVNULL.
212
+ * Make many parameters keyword only.
213
+
214
+ *Release 20221228*:
215
+ * signal_handlers: bugfix iteration of sig_hnds.
216
+ * Use cs.gimmicks instead of cs.logutils.
217
+ * Drop use of cs.upd, fixes circular import; users of run() may need to call "with Upd().above()" themselves.
218
+
219
+ *Release 20221118*:
220
+ run: do not print cp.stdout.
221
+
222
+ *Release 20220805*:
223
+ run: print trace to stderr.
224
+
225
+ *Release 20220626*:
226
+ run: default quiet=True.
227
+
228
+ *Release 20220606*:
229
+ * run: fold in the superior run() from cs.ebooks.
230
+ * pipefrom,pipeto: replace trace= with quiet= like run().
231
+ * print_argv: add an `end` parameter (used by pipefrom).
232
+
233
+ *Release 20220531*:
234
+ * New print_argv function for writing shell command lines out nicely.
235
+ * Bump requirements for cs.gimmicks.
236
+
237
+ *Release 20220504*:
238
+ signal_handlers: reshape try/except to avoid confusing traceback.
239
+
240
+ *Release 20220429*:
241
+ * New signal_handler(sig,handler,call_previous=False) context manager to push a signal handler.
242
+ * New signal_handlers() context manager to stack handlers for multiple signals.
243
+
244
+ *Release 20190101*:
245
+ Bugfix context manager cleanup. groupargv improvements.
246
+
247
+ *Release 20171112*:
248
+ Bugfix array length counting.
249
+
250
+ *Release 20171110*:
251
+ New function groupargv for breaking up argv lists to fit within the maximum argument limit; constant MAX_ARGV for the default limit.
252
+
253
+ *Release 20171031*:
254
+ run: accept optional pids parameter, a setlike collection of process ids.
255
+
256
+ *Release 20171018*:
257
+ run: replace `trace` parameter with `logger`, default None
258
+
259
+ *Release 20170908.1*:
260
+ remove dependency on cs.pfx
261
+
262
+ *Release 20170908*:
263
+ run: pass extra keyword arguments to subprocess.call
264
+
265
+ *Release 20170906.1*:
266
+ Add run, pipefrom and pipeto - were incorrectly in another module.
267
+
268
+ *Release 20170906*:
269
+ First PyPI release.