bhtool 0.2.2__tar.gz → 0.2.3__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: bhtool
3
- Version: 0.2.2
3
+ Version: 0.2.3
4
4
  Summary: Assorted CLI utilities for Bosco’s workflows (dev, media, macOS, LLM helpers).
5
5
  Author-email: Bosco Ho <apposite@gmail.com>
6
6
  Requires-Python: >=3.13
@@ -0,0 +1,53 @@
1
+ #!/usr/bin/env python3
2
+
3
+ """Find processes with name"""
4
+
5
+ import os
6
+ import re
7
+ import signal
8
+
9
+ from bhtool.utils import run_output
10
+
11
+
12
+ def own_pids() -> set[int]:
13
+ """Our process and its ancestors, which must never be killed."""
14
+ parents = {
15
+ int(pid): int(ppid)
16
+ for pid, ppid in re.findall(
17
+ r"(\d+)\s+(\d+)", run_output("ps -o pid=,ppid= -ax")
18
+ )
19
+ }
20
+ pids = {os.getpid()}
21
+ pid = os.getpid()
22
+ while pid in parents and parents[pid] > 1 and parents[pid] not in pids:
23
+ pid = parents[pid]
24
+ pids.add(pid)
25
+ return pids
26
+
27
+
28
+ def psword(words: tuple[str, ...], kill: bool = False):
29
+ killed: set[int] = set()
30
+ skip = own_pids()
31
+ for word in words:
32
+ txt = run_output(f"ps aux | grep {word}")
33
+ for line in txt.splitlines():
34
+ tokens = line.split(None, 10)
35
+ if len(tokens) < 11:
36
+ continue
37
+ cmd = tokens[10]
38
+ if cmd.startswith("grep ") or "ps aux | grep" in cmd:
39
+ continue
40
+ pid = int(tokens[1])
41
+ if pid in skip or pid in killed:
42
+ continue
43
+ if len(cmd) > 80:
44
+ cmd = cmd[:80] + "..."
45
+ if kill:
46
+ print(f"kill target ({cmd})")
47
+ try:
48
+ os.kill(pid, signal.SIGKILL)
49
+ killed.add(pid)
50
+ except (ProcessLookupError, PermissionError) as err:
51
+ print(f"could not kill {pid}: {err}")
52
+ else:
53
+ print(f"process {pid}: {cmd}")
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "bhtool"
3
- version = "0.2.2"
3
+ version = "0.2.3"
4
4
  description = "Assorted CLI utilities for Bosco’s workflows (dev, media, macOS, LLM helpers)."
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.13"
@@ -1,23 +0,0 @@
1
- #!/usr/bin/env python3
2
-
3
- """Find processes with name"""
4
-
5
- from bhtool.utils import run, run_output
6
-
7
-
8
- def psword(words: tuple[str, ...], kill: bool = False):
9
- for word in words:
10
- txt = run_output(f"ps aux | grep {word}")
11
- for line in txt.splitlines():
12
- tokens = line.split()
13
- i_process = tokens[1]
14
- cmd = " ".join(tokens[10:])
15
- if len(cmd) > 80:
16
- cmd = cmd[:80] + "..."
17
- if "psword" in line:
18
- continue
19
- if kill:
20
- print(f"kill target ({cmd})")
21
- run(f"kill -9 {i_process}")
22
- else:
23
- print(f"process {i_process}: {cmd}")
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes