c-two 0.1.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.
- c_two/__init__.py +116 -0
- c_two/client.py +45 -0
- c_two/service.py +43 -0
- c_two-0.1.0.dist-info/LICENSE +21 -0
- c_two-0.1.0.dist-info/METADATA +22 -0
- c_two-0.1.0.dist-info/RECORD +7 -0
- c_two-0.1.0.dist-info/WHEEL +4 -0
c_two/__init__.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import sys
|
|
3
|
+
import subprocess
|
|
4
|
+
|
|
5
|
+
class CRMServer:
|
|
6
|
+
def __init__(self, crm_module: str, crm_class: str, poetry_env_path: str):
|
|
7
|
+
self.crm_module = crm_module
|
|
8
|
+
self.crm_class = crm_class
|
|
9
|
+
self.crm_proc = self._start_crm(crm_module, crm_class, poetry_env_path)
|
|
10
|
+
self.component_proc_map: dict[str, subprocess.Popen] = {}
|
|
11
|
+
|
|
12
|
+
def _start_crm(self, crm_module: str, crm_class: str, poetry_env_path: str) -> subprocess.Popen:
|
|
13
|
+
"""Start the CRM service in a separate process using Poetry environment."""
|
|
14
|
+
python_path = os.path.join(poetry_env_path, "bin", "python" if sys.platform != "win32" else "Scripts", "python.exe")
|
|
15
|
+
if not os.path.exists(python_path):
|
|
16
|
+
raise FileNotFoundError(f"Poetry Python executable not found at {python_path}")
|
|
17
|
+
|
|
18
|
+
cmd = [python_path, "-m", "c_two.service", crm_module, crm_class]
|
|
19
|
+
proc = subprocess.Popen(
|
|
20
|
+
cmd,
|
|
21
|
+
stdin=subprocess.PIPE,
|
|
22
|
+
stdout=subprocess.PIPE,
|
|
23
|
+
stderr=subprocess.PIPE,
|
|
24
|
+
bufsize=0
|
|
25
|
+
)
|
|
26
|
+
# Check if process started successfully
|
|
27
|
+
if proc.poll() is not None:
|
|
28
|
+
error = proc.stderr.read().decode().strip()
|
|
29
|
+
raise RuntimeError(f"Failed to start CRM process: {error}")
|
|
30
|
+
return proc
|
|
31
|
+
|
|
32
|
+
def _start_component(self, component_path: str, component_args: list[str] = None) -> subprocess.Popen:
|
|
33
|
+
"""Start the component script in a separate process with optional arguments."""
|
|
34
|
+
cmd = [sys.executable, component_path]
|
|
35
|
+
if component_args:
|
|
36
|
+
cmd.extend(component_args)
|
|
37
|
+
env = os.environ.copy()
|
|
38
|
+
proc = subprocess.Popen(
|
|
39
|
+
cmd,
|
|
40
|
+
env=env,
|
|
41
|
+
stdin=subprocess.PIPE,
|
|
42
|
+
stdout=subprocess.PIPE,
|
|
43
|
+
stderr=subprocess.PIPE,
|
|
44
|
+
bufsize=0
|
|
45
|
+
)
|
|
46
|
+
if proc.poll() is not None:
|
|
47
|
+
error = proc.stderr.read().decode().strip()
|
|
48
|
+
raise RuntimeError(f"Failed to start Component process: {error}")
|
|
49
|
+
return proc
|
|
50
|
+
|
|
51
|
+
def invoke_component(self, uuid: str, component_path: str, component_args: list[str] = None) -> tuple[bool, str, str]:
|
|
52
|
+
"""Start a Component process, wait for it to finish, and return its result."""
|
|
53
|
+
if uuid in self.component_proc_map:
|
|
54
|
+
raise ValueError(f"Component with UUID {uuid} already running")
|
|
55
|
+
|
|
56
|
+
component_proc = self._start_component(component_path, component_args)
|
|
57
|
+
component_proc.__crm_process__ = self.crm_proc
|
|
58
|
+
self.component_proc_map[uuid] = component_proc
|
|
59
|
+
|
|
60
|
+
# Wait for Component to finish and capture output
|
|
61
|
+
component_proc.wait()
|
|
62
|
+
stdout_output = component_proc.stdout.read().decode().strip()
|
|
63
|
+
stderr_output = component_proc.stderr.read().decode().strip()
|
|
64
|
+
|
|
65
|
+
# Clean up Component resources
|
|
66
|
+
component_proc.stdin.close()
|
|
67
|
+
component_proc.stdout.close()
|
|
68
|
+
component_proc.stderr.close()
|
|
69
|
+
del self.component_proc_map[uuid]
|
|
70
|
+
|
|
71
|
+
return component_proc.returncode == 0, stderr_output, stdout_output
|
|
72
|
+
|
|
73
|
+
def stop(self):
|
|
74
|
+
"""Stop the CRM and any running Component processes gracefully."""
|
|
75
|
+
if self.crm_proc and self.crm_proc.poll() is None:
|
|
76
|
+
self.crm_proc.terminate()
|
|
77
|
+
try:
|
|
78
|
+
self.crm_proc.wait(timeout=5)
|
|
79
|
+
print("CRM process gracefully stopped.")
|
|
80
|
+
except subprocess.TimeoutExpired:
|
|
81
|
+
self.crm_proc.kill()
|
|
82
|
+
print("CRM process forcibly killed.")
|
|
83
|
+
finally:
|
|
84
|
+
self.crm_proc.stdin.close()
|
|
85
|
+
self.crm_proc.stdout.close()
|
|
86
|
+
self.crm_proc.stderr.close()
|
|
87
|
+
|
|
88
|
+
for uuid, comp_proc in list(self.component_proc_map.items()):
|
|
89
|
+
if comp_proc.poll() is None:
|
|
90
|
+
comp_proc.terminate()
|
|
91
|
+
try:
|
|
92
|
+
comp_proc.wait(timeout=5)
|
|
93
|
+
print(f"Component {uuid} gracefully stopped.")
|
|
94
|
+
except subprocess.TimeoutExpired:
|
|
95
|
+
comp_proc.kill()
|
|
96
|
+
print(f"Component {uuid} forcibly killed.")
|
|
97
|
+
finally:
|
|
98
|
+
comp_proc.stdin.close()
|
|
99
|
+
comp_proc.stdout.close()
|
|
100
|
+
comp_proc.stderr.close()
|
|
101
|
+
del self.component_proc_map[uuid]
|
|
102
|
+
|
|
103
|
+
# Helper ##################################################
|
|
104
|
+
def get_poetry_env_path(crm_project_path: str) -> str:
|
|
105
|
+
"""Get the Poetry virtual environment path for the CRM project."""
|
|
106
|
+
try:
|
|
107
|
+
result = subprocess.run(
|
|
108
|
+
["poetry", "env", "info", "--path"],
|
|
109
|
+
cwd=crm_project_path,
|
|
110
|
+
capture_output=True,
|
|
111
|
+
text=True,
|
|
112
|
+
check=True
|
|
113
|
+
)
|
|
114
|
+
return result.stdout.strip()
|
|
115
|
+
except subprocess.CalledProcessError:
|
|
116
|
+
raise RuntimeError("Poetry not installed or CRM project path invalid")
|
c_two/client.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import subprocess
|
|
2
|
+
import pickle
|
|
3
|
+
import sys
|
|
4
|
+
|
|
5
|
+
class CRMClient:
|
|
6
|
+
def __init__(self):
|
|
7
|
+
self.process: subprocess.Popen = sys.modules['__main__'].__crm_process__
|
|
8
|
+
if self.process.poll() is not None:
|
|
9
|
+
raise RuntimeError("CRM process is not running")
|
|
10
|
+
|
|
11
|
+
def _send_request(self, request_type: str, data):
|
|
12
|
+
"""Send a request to the CRM service and get the response synchronously."""
|
|
13
|
+
if self.process.poll() is not None:
|
|
14
|
+
raise RuntimeError("CRM process has terminated unexpectedly")
|
|
15
|
+
|
|
16
|
+
# Send request
|
|
17
|
+
request = pickle.dumps((request_type, data))
|
|
18
|
+
self.process.stdin.write(request + b'\n')
|
|
19
|
+
self.process.stdin.flush()
|
|
20
|
+
|
|
21
|
+
# Wait for response synchronously
|
|
22
|
+
response = self.process.stdout.readline()
|
|
23
|
+
if not response: # EOF or process closed stdout
|
|
24
|
+
raise RuntimeError("CRM process closed communication unexpectedly")
|
|
25
|
+
|
|
26
|
+
status, result = pickle.loads(response)
|
|
27
|
+
if status == "ERROR":
|
|
28
|
+
raise RuntimeError(f"CRM error: {result}")
|
|
29
|
+
return result
|
|
30
|
+
|
|
31
|
+
def create(self, *args, **kwargs):
|
|
32
|
+
"""Create a CRM instance with arbitrary arguments."""
|
|
33
|
+
self._send_request("CREATE", (args, kwargs))
|
|
34
|
+
|
|
35
|
+
def call(self, method_name: str, *args, **kwargs):
|
|
36
|
+
"""Call a method on the CRM instance."""
|
|
37
|
+
return self._send_request("CALL", (method_name, (args, kwargs)))
|
|
38
|
+
|
|
39
|
+
def stop(self):
|
|
40
|
+
"""Stop the CRM process."""
|
|
41
|
+
self._send_request("STOP", None)
|
|
42
|
+
self.process.wait()
|
|
43
|
+
self.process.stdin.close()
|
|
44
|
+
self.process.stdout.close()
|
|
45
|
+
self.process.stderr.close()
|
c_two/service.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
import pickle
|
|
3
|
+
import importlib
|
|
4
|
+
|
|
5
|
+
def run_crm_service(impl_module: str, impl_class: str):
|
|
6
|
+
"""CRM service running in a separate process."""
|
|
7
|
+
module = importlib.import_module(impl_module)
|
|
8
|
+
crm_class = getattr(module, impl_class)
|
|
9
|
+
crm = None
|
|
10
|
+
|
|
11
|
+
while True:
|
|
12
|
+
request = sys.stdin.buffer.readline()
|
|
13
|
+
if not request:
|
|
14
|
+
break
|
|
15
|
+
|
|
16
|
+
try:
|
|
17
|
+
request_type, data = pickle.loads(request.strip())
|
|
18
|
+
|
|
19
|
+
if request_type == 'STOP':
|
|
20
|
+
break
|
|
21
|
+
|
|
22
|
+
if request_type == "CREATE":
|
|
23
|
+
args, kwargs = data
|
|
24
|
+
crm = crm_class(*args, **kwargs)
|
|
25
|
+
response = ("SUCCESS", None)
|
|
26
|
+
|
|
27
|
+
elif request_type == "CALL":
|
|
28
|
+
if crm is None:
|
|
29
|
+
response = ("ERROR", "CRM not initialized")
|
|
30
|
+
else:
|
|
31
|
+
method_name, args, kwargs = data
|
|
32
|
+
method = getattr(crm, method_name)
|
|
33
|
+
result = method(*args, **kwargs)
|
|
34
|
+
response = ("SUCCESS", result)
|
|
35
|
+
|
|
36
|
+
else:
|
|
37
|
+
response = ("ERROR", "Unknown request type")
|
|
38
|
+
|
|
39
|
+
except Exception as e:
|
|
40
|
+
response = ("ERROR", str(e))
|
|
41
|
+
|
|
42
|
+
sys.stdout.buffer.write(pickle.dumps(response))
|
|
43
|
+
sys.stdout.buffer.flush()
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 world-in-progress
|
|
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,22 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: c-two
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A python server to support IPC between CRM and Component processes.
|
|
5
|
+
License: MIT
|
|
6
|
+
Author: YcSoku
|
|
7
|
+
Author-email: dsssyc02@gmail.com
|
|
8
|
+
Requires-Python: >=3.12
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
|
|
15
|
+
# C-Two
|
|
16
|
+
|
|
17
|
+
<p align="center">
|
|
18
|
+
<img align="center" width="150px" src="https://raw.githubusercontent.com/world-in-progress/c-two/main/doc/images/logo.png">
|
|
19
|
+
</p>
|
|
20
|
+
|
|
21
|
+
C-Two (C.C.) is a python server to support IPC between CRM and Component processes.
|
|
22
|
+
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
c_two/__init__.py,sha256=hqsNv-4LH8AsmokvvK0ZMG44TFh_1CUmAvJaeziKHok,4903
|
|
2
|
+
c_two/client.py,sha256=kDnPP8oooe10BD3pWTHK3G2vqVYFcAvRGG4LYvReZmc,1665
|
|
3
|
+
c_two/service.py,sha256=FEgcjWFkU8DlKL3yy1bb53JaedRr43Iq-idOUgby6fg,1373
|
|
4
|
+
c_two-0.1.0.dist-info/LICENSE,sha256=Z3ztcWEfJfFAc1ewKPtCsBX90cCc27d5ezqszUPAnCQ,1074
|
|
5
|
+
c_two-0.1.0.dist-info/METADATA,sha256=aRLrtI9uhb41zr5K0pHIzLvdTa2X_dFFG59VQB56GMo,696
|
|
6
|
+
c_two-0.1.0.dist-info/WHEEL,sha256=IYZQI976HJqqOpQU6PHkJ8fb3tMNBFjg-Cn-pwAbaFM,88
|
|
7
|
+
c_two-0.1.0.dist-info/RECORD,,
|