Skip to content

Commit 3788f80

Browse files
committed
6. add --StandardOutput and --StandardError to morebuiltins.cmd.systemd.service, and update --Environment to list type.
7. add `is_port_free` to `morebuiltins.ipc`.
1 parent 34ac56d commit 3788f80

File tree

5 files changed

+74
-3
lines changed

5 files changed

+74
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
3. add `utils.cut_file` to cut file with `a+b` mode to limit the file size
55
4. add recheck for `utils.set_pid_file`
66
5. add `shared_memory.PLock` for singleton process with `multiprocessing.shared_memory`, support linux and windows.
7+
6. add `--StandardOutput` and `--StandardError` to `morebuiltins.cmd.systemd.service`, and update `--Environment` to list type.
8+
7. add `is_port_free` to `morebuiltins.ipc`.
79

810

911
### 1.2.3 (2025-03-07)

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ print(morebuiltins.__file__)
180180

181181
4.7 `find_free_port` - Finds and returns an available port number.
182182

183+
4.8 `is_port_free` - Checks if a port is free.
184+
183185

184186
## 5. morebuiltins.request
185187

doc.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1994,6 +1994,23 @@ True
19941994
---
19951995

19961996

1997+
1998+
4.8 `is_port_free` - Checks if a port is free.
1999+
2000+
2001+
```python
2002+
2003+
Demo:
2004+
2005+
>>> is_port_free(12345)
2006+
True
2007+
2008+
```
2009+
2010+
2011+
---
2012+
2013+
19972014
## 5. morebuiltins.request
19982015

19992016

morebuiltins/cmd/systemd/service.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,17 @@ def create_service_file(args: Dict[str, Any]) -> str:
7272
"RestartSec",
7373
"Environment",
7474
"EnvironmentFile",
75+
"StandardOutput",
76+
"StandardError",
7577
]
7678
for param in service_params:
77-
if args.get(param):
78-
content.append(f"{param}={args[param]}")
79+
value = args.get(param)
80+
if value:
81+
if isinstance(value, list):
82+
for v in value:
83+
content.append(f"Environment={v}")
84+
else:
85+
content.append(f"{param}={args[param]}")
7986

8087
content.append("\n[Install]")
8188
content.append(f"WantedBy={args.get('WantedBy', 'multi-user.target')}")
@@ -157,10 +164,24 @@ def service_handler():
157164
parser.add_argument(
158165
"-RestartSec", "--RestartSec", help="Restart interval (seconds)"
159166
)
160-
parser.add_argument("-Environment", "--Environment", help="Environment variables")
167+
parser.add_argument(
168+
"-Environment", "--Environment", help="Environment variables", action="append"
169+
)
161170
parser.add_argument(
162171
"-EnvironmentFile", "--EnvironmentFile", help="Environment file"
163172
)
173+
parser.add_argument(
174+
"-StandardOutput",
175+
"--StandardOutput",
176+
help="Standard output, e.g. syslog, journal, append:/tmp/app.log, file:/tmp/app.log",
177+
default="",
178+
)
179+
parser.add_argument(
180+
"-StandardError",
181+
"--StandardError",
182+
help="Standard error, e.g. syslog, journal, append:/tmp/app.log, file:/tmp/app.log",
183+
default="",
184+
)
164185

165186
# Install section arguments
166187
parser.add_argument("-WantedBy", "--WantedBy", default="multi-user.target")

morebuiltins/ipc.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"SocketServer",
1717
"SocketClient",
1818
"find_free_port",
19+
"is_port_free",
1920
]
2021

2122

@@ -367,6 +368,30 @@ def find_free_port(host="127.0.0.1", port=0):
367368
pass
368369

369370

371+
def is_port_free(
372+
port: int,
373+
host="127.0.0.1",
374+
family=socket.AF_INET,
375+
type=socket.SOCK_STREAM,
376+
timeout=0.1,
377+
):
378+
"""Checks if a port is free.
379+
380+
Demo:
381+
382+
>>> is_port_free(12345)
383+
True
384+
"""
385+
with socket.socket(family=family, type=type) as s:
386+
if timeout:
387+
s.settimeout(timeout)
388+
connected = s.connect_ex((host, port))
389+
if connected == 0:
390+
return False
391+
else:
392+
return True
393+
394+
370395
async def test_client(host="127.0.0.1", port=8090, encoder=None, cases=None):
371396
async with SocketClient(host=host, port=port, encoder=encoder) as c:
372397
for case in cases:
@@ -427,6 +452,10 @@ async def _test_ipc_logging():
427452

428453

429454
def test():
455+
import doctest
456+
457+
doctest.testmod()
458+
430459
globals().setdefault("print_log", True) # local test show logs
431460
for function in [_test_ipc, _test_ipc_logging]:
432461
asyncio.run(function())

0 commit comments

Comments
 (0)