pmflow 1.0.0__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.
pm/__init__.py ADDED
File without changes
pm/main.py ADDED
@@ -0,0 +1,150 @@
1
+ #!/usr/bin/env python3
2
+ import os
3
+ import signal
4
+ import sys
5
+
6
+ import typer
7
+ import psutil
8
+ import subprocess
9
+ from rich.console import Console
10
+ from rich.table import Table
11
+ from pm.utils import StateManager
12
+
13
+ app = typer.Typer()
14
+
15
+ STATE_FILE = os.path.join(os.path.dirname(__file__), 'processes_state.json')
16
+ state = StateManager(STATE_FILE)
17
+
18
+ @app.command()
19
+ def greet(name: str):
20
+ print(f"Hello, {name}!")
21
+
22
+
23
+ @app.command()
24
+ def create(command: str, name: str = None) -> int:
25
+ """Create a new subprocess and optionally assign a name."""
26
+ proc = subprocess.Popen(command, shell=True)
27
+ pid = proc.pid
28
+ data = {
29
+ "command": command,
30
+ "name": name,
31
+ }
32
+ state.add_process(pid, data)
33
+ typer.echo(f"{pid}")
34
+
35
+
36
+ @app.command()
37
+ def pause(pid: int):
38
+ """Pause a subprocess and all its children by PID."""
39
+ pid_str = str(pid)
40
+ if pid_str in state.processes:
41
+ try:
42
+ process = psutil.Process(int(pid))
43
+ all_processes = [process] + process.children(recursive=True)
44
+ for proc in all_processes:
45
+ proc.send_signal(signal.SIGSTOP)
46
+ typer.echo(f"Process {pid} and its child processes have been paused.")
47
+ except psutil.NoSuchProcess:
48
+ typer.echo("Process not found.")
49
+ else:
50
+ typer.echo("Process not managed by this tool.")
51
+
52
+
53
+ @app.command()
54
+ def ls():
55
+ """List all managed subprocesses."""
56
+ table = Table(title="Processes")
57
+ table.add_column("PID", justify="right", style="cyan")
58
+ table.add_column("Name", justify="right", style="magenta")
59
+ table.add_column("Status", justify="right", style="green")
60
+ table.add_column("Command", justify="right", style="yellow")
61
+
62
+ for pid, properties in state.processes.items():
63
+ try:
64
+ process = psutil.Process(int(pid))
65
+ if process.status() == psutil.STATUS_STOPPED:
66
+ status = 'paused'
67
+ else:
68
+ status = 'running'
69
+ except psutil.NoSuchProcess:
70
+ status = "doesn't exist"
71
+
72
+ table.add_row(pid, properties["name"], status, properties["command"])
73
+
74
+ console = Console()
75
+ console.print(table)
76
+
77
+ @app.command()
78
+ def recreate():
79
+ """Recreate all managed subprocesses."""
80
+ new_processes = {}
81
+ for pid, data in state.processes.items():
82
+ proc = subprocess.Popen(data["command"], shell=True)
83
+ new_processes[str(proc.pid)] = data
84
+ typer.echo(f"Process {proc.pid} recreated with command: {data['command']}")
85
+
86
+ state.bulk_update(new_processes)
87
+
88
+
89
+ @app.command()
90
+ def respawn_all():
91
+ """Respawn processes that are in the JSON file but not running."""
92
+
93
+ for pid, data in state.processes.items():
94
+ if psutil.pid_exists(int(pid)):
95
+ process = psutil.Process(int(pid))
96
+ if not process.is_running():
97
+ typer.echo(f"Process {pid} not running. Respawning...")
98
+ process.resume()
99
+ typer.echo(f"Process {pid} respawed.")
100
+
101
+ typer.echo("Respawn complete.")
102
+
103
+
104
+ @app.command()
105
+ def kill(pid: int):
106
+ """Kill a subprocess by PID."""
107
+ pid_str = str(pid)
108
+ if pid_str in state.processes:
109
+ try:
110
+ process = psutil.Process(int(pid))
111
+ for child in process.children(recursive=True):
112
+ child.terminate()
113
+ process.terminate()
114
+ state.remove_process(pid_str)
115
+ typer.echo(f"Process {pid} killed.")
116
+ except psutil.NoSuchProcess:
117
+ state.remove_process(pid_str)
118
+ typer.echo("Process not found. Removed from the state file.")
119
+ else:
120
+ typer.echo("Process not managed by this tool.")
121
+
122
+
123
+ @app.command()
124
+ def kill_all():
125
+ """Kill all managed processes and clear the state."""
126
+
127
+ for pid in state.processes.keys():
128
+ try:
129
+ process = psutil.Process(int(pid))
130
+ for child in process.children(recursive=True):
131
+ child.terminate()
132
+ process.terminate()
133
+ typer.echo(f"Process {pid} terminated.")
134
+ except psutil.NoSuchProcess:
135
+ typer.echo(f"Process {pid} not found.")
136
+ except Exception as e:
137
+ typer.echo(f"Error terminating process {pid}: {str(e)}")
138
+
139
+ state.remove_all_processes()
140
+ typer.echo("All processes have been terminated and removed from the state.")
141
+
142
+
143
+
144
+ def signal_handler(sig, frame):
145
+ typer.echo("Ctrl+C pressed. Terminating all managed processes...")
146
+ sys.exit(0)
147
+
148
+ if __name__ == "__main__":
149
+ signal.signal(signal.SIGINT, signal_handler)
150
+ app()
pm/utils.py ADDED
@@ -0,0 +1,59 @@
1
+ import json
2
+ import os
3
+ import sys
4
+
5
+ import typer
6
+
7
+
8
+ class StateManager:
9
+ def __init__(self, STATE_FILE):
10
+ self.processes = {}
11
+ self.STATE_FILE = STATE_FILE
12
+ self.load_state()
13
+
14
+ def load_state(self):
15
+ print(f"Loading state from {self.STATE_FILE}")
16
+
17
+ if not os.path.exists(self.STATE_FILE):
18
+ with open(self.STATE_FILE, "w") as file:
19
+ json.dump({}, file)
20
+ print("Created new state file")
21
+
22
+ with open(self.STATE_FILE, "r") as file:
23
+ self.processes = json.load(file)
24
+
25
+ def save(self):
26
+ with open(self.STATE_FILE, "w") as file:
27
+ json.dump(self.processes, file)
28
+
29
+ def get_processes(self):
30
+ return self.processes
31
+
32
+ def add_process(self, pid, data):
33
+ self.processes[str(pid)] = data
34
+ self.save()
35
+
36
+ def remove_process(self, pid):
37
+ self.processes.pop(str(pid), None)
38
+ self.save()
39
+
40
+ def remove_all_processes(self):
41
+ self.processes = {}
42
+ self.save()
43
+
44
+ def update_process(self, pid, key, value):
45
+ self.processes[str(pid)][key] = value
46
+ self.save()
47
+
48
+ def bulk_update(self, bulk_data):
49
+ self.processes = bulk_data
50
+ self.save()
51
+
52
+
53
+
54
+ def load_state(path):
55
+ """Load processes state from a file."""
56
+ global processes
57
+ if os.path.exists(path):
58
+ with open(path, "r") as file:
59
+ processes = json.load(file)
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Md. Mahmudul Hasan Riyad
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,39 @@
1
+ Metadata-Version: 2.1
2
+ Name: pmflow
3
+ Version: 1.0.0
4
+ Summary: Manages processes
5
+ Author-email: "Md. Mahmudul Hasan" <mhriyad98@gmail.com>
6
+ License: MIT License
7
+
8
+ Copyright (c) 2024 Md. Mahmudul Hasan Riyad
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+ Project-URL: Homepage, https://github.com/mhriyad99/process_manager
28
+ Keywords: process manager,process,pm
29
+ Classifier: License :: OSI Approved :: MIT License
30
+ Classifier: Programming Language :: Python
31
+ Classifier: Programming Language :: Python :: 3
32
+ Requires-Python: >=3.10
33
+ Description-Content-Type: text/markdown
34
+ License-File: LICENSE
35
+ Requires-Dist: typer==0.12.5
36
+ Requires-Dist: psutil==6.0.0
37
+ Provides-Extra: dev
38
+
39
+ # process_manager
@@ -0,0 +1,9 @@
1
+ pm/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ pm/main.py,sha256=eazhzDlMO7vF2bIfElQaw2cxTVbuQY6quzir3Wzb0n4,4508
3
+ pm/utils.py,sha256=_1sqLtujb8F40i3mT-Ca09kOcqaonuvVGjtbpWJfmK8,1420
4
+ pmflow-1.0.0.dist-info/LICENSE,sha256=HpmlPSgC0Aaxf2PwgtpD6xPlsb9j66ZipQmC8jdeKqE,1080
5
+ pmflow-1.0.0.dist-info/METADATA,sha256=lf-GeRzapHlRciSdmep8cRE-7Ya5QGd9xhn96pTDEhc,1814
6
+ pmflow-1.0.0.dist-info/WHEEL,sha256=OVMc5UfuAQiSplgO0_WdW7vXVGAt9Hdd6qtN4HotdyA,91
7
+ pmflow-1.0.0.dist-info/entry_points.txt,sha256=nP0oRbAZsbgglMxaXniGwXOew_4qYamJbstYx0FoalE,35
8
+ pmflow-1.0.0.dist-info/top_level.txt,sha256=EyGjaLlndAMIL4TKnkAuosD9vW0v2Ci2tenil9yD3MU,3
9
+ pmflow-1.0.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (75.2.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ pm = pm.main:app
@@ -0,0 +1 @@
1
+ pm