Skip to content

Commit 96f5e65

Browse files
committed
store unit_name
1 parent dff50f7 commit 96f5e65

9 files changed

Lines changed: 69 additions & 30 deletions

File tree

.github/workflows/ci.yml

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,47 @@ jobs:
88
strategy:
99
fail-fast: false
1010
matrix:
11-
python: ['3.10', '3.11', '3.12', '3.13']
11+
python: ['3.10', '3.11', '3.12', '3.13', '3.14']
1212
runs-on: ubuntu-latest
1313
steps:
1414
- name: Checkout repository
1515
uses: actions/checkout@v2
16-
- name: Setup Python
17-
uses: actions/setup-python@v2
16+
- name: Install uv
17+
uses: astral-sh/setup-uv@v7
1818
with:
1919
python-version: ${{ matrix.python }}
20+
21+
# - name: Setup Python
22+
# uses: actions/setup-python@v2
23+
# with:
24+
# python-version: ${{ matrix.python }}
25+
2026
- name: update APT
2127
run: sudo apt-get update
2228
- name: Install dependencies
2329
run: sudo apt-get install -y libsystemd-dev
24-
- name: Install pystemd
25-
run: pip install -e '.[t]'
30+
31+
- name: Install environment
32+
run: uv sync --all-extras --dev
33+
34+
- name: Assert Python Version
35+
run: uv run python -V
36+
2637
- name: Run unit tests
27-
run: pytest --cov=pystemd tests
38+
run: uv run pytest --cov=pystemd tests
2839

2940
formatting:
3041
runs-on: ubuntu-latest
3142
steps:
3243
- name: Checkout repository
3344
uses: actions/checkout@v2
34-
- name: Run black
35-
uses: psf/black@stable
45+
- name: Run ruff
46+
uses: astral-sh/ruff-action@v3
47+
with:
48+
src: >-
49+
pystemd
50+
examples
51+
tests
3652
- name: Run isort
3753
uses: isort/isort-action@v1
3854

@@ -42,7 +58,15 @@ jobs:
4258
steps:
4359
- name: Checkout repository
4460
uses: actions/checkout@v2
45-
- name: install mypy
46-
run: pip install mypy types-psutil
47-
- name: run mypy
48-
run: mypy
61+
- name: Install uv
62+
uses: astral-sh/setup-uv@v7
63+
64+
- name: update APT
65+
run: sudo apt-get update
66+
- name: Install dependencies
67+
run: sudo apt-get install -y libsystemd-dev
68+
69+
- name: install environment
70+
run: uv sync --extra lint
71+
- name: run pyrefly
72+
run: uv run pyrefly check pystemd examples tests

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,7 @@ pystemd/RELEASE
9595
/pystemd-*.tar.gz
9696
/python-pystemd-*.src.rpm
9797
/python-pystemd.spec
98+
99+
100+
# uv files
101+
uv.lock

pyproject.toml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ name = "pystemd"
77
version = "0.14.0"
88
readme = "README.md"
99
description="A systemd binding for python"
10+
requires-python = ">=3.10"
1011
dependencies = [
1112
"lxml",
1213
"psutil"
@@ -40,11 +41,12 @@ t = [
4041
"pytest",
4142
"pytest-cov",
4243
# cstq and toml are used for testing version
43-
"cstq", "toml"
44+
"cstq",
45+
"toml",
4446
]
4547
lint = [
46-
"black",
47-
"mypi",
48+
"ruff",
49+
"pyrefly",
4850
"isort",
4951
]
5052

@@ -53,10 +55,6 @@ changelog = "https://github.com/systemd/pystemd/blob/main/CHANGES.md"
5355
homepage = "https://github.com/systemd/pystemd"
5456
repository = "https://github.com/systemd/pystemd.git"
5557

56-
[tool.mypy]
57-
strict = false
58-
packages = ["pystemd"]
59-
6058
[tool.isort]
6159
atomic = true
6260
profile = "black"

pystemd/__init__.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from typing import Any
1010

11-
from pystemd import DBus, __version__, machine1, systemd1
11+
from pystemd import machine1, systemd1
1212

1313
SDUnit = systemd1.Unit
1414
SDManager = systemd1.Manager

pystemd/base.pyi

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
# the root directory of this source tree.
77
#
88

9-
import re
109
from contextlib import contextmanager
1110
from typing import Any, AnyStr, Iterator, Optional
1211
from xml.dom.minidom import Element
1312

14-
from pystemd.dbuslib import DBus, apply_signature
15-
from pystemd.utils import x2char_star
13+
from pystemd.dbuslib import DBus
1614

1715
class SDObject(object):
1816
def __init__(

pystemd/futures.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
from concurrent.futures import ProcessPoolExecutor
3+
from functools import cached_property
34
from multiprocessing import Process
45
from multiprocessing.context import BaseContext
56
from pathlib import Path
@@ -10,6 +11,7 @@
1011
import pystemd.cutils
1112
import pystemd.run
1213
import pystemd.utils
14+
from pystemd.dbuslib import DBus
1315

1416

1517
class TransientUnitContext(BaseContext):
@@ -24,10 +26,12 @@ def __init__(
2426
properties: Dict[str, Any],
2527
main_process: Sequence[str] = (),
2628
user_mode: bool = False,
29+
unit_name: Optional[str] = None,
2730
) -> None:
2831
self.unit: Optional[pystemd.systemd1.Unit] = None
2932
self.properties = properties
3033
self.user_mode = user_mode
34+
self.unit_name = unit_name
3135
self.main_process_cmd = main_process or [
3236
"/bin/bash",
3337
"-c",
@@ -39,6 +43,7 @@ def start_unit(self) -> pystemd.systemd1.Unit:
3943
assert self.unit is None, "Unit already started"
4044
self.unit = pystemd.run(
4145
self.main_process_cmd,
46+
name=self.unit_name,
4247
user_mode=self.user_mode,
4348
extra={
4449
**self.properties,
@@ -121,17 +126,25 @@ class TransientUnitProcess(_ProcessWithPreRun):
121126
the unit will finish.
122127
"""
123128

124-
def __init__(self, *, properties=None, user_mode=False, **kwargs) -> None:
129+
def __init__(self, *, properties=None, user_mode=False, unit_name=None, **kwargs) -> None:
125130
super().__init__(**kwargs)
126131
self.user_mode = user_mode
132+
self.unit_name = unit_name or pystemd.utils.random_unit_name(prefix="pystemd-future-")
127133
self.properties = {
128134
pystemd.utils.x2char_star(k): v for k, v in (properties or {}).items()
129135
}
130136

137+
@cached_property
138+
def unit(self):
139+
bus = DBus(user_mode=self.user_mode)
140+
bus.__enter__()
141+
return pystemd.systemd1.Unit(self.unit_name, bus, _autoload=True)
142+
131143
def pre_run(self):
132144
context = TransientUnitContext(
133145
properties=self.properties,
134146
user_mode=self.user_mode,
147+
unit_name=self.unit_name,
135148
main_process=[
136149
"/bin/bash",
137150
"-c",

pystemd/run.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import sys
1414
import termios
1515
import tty
16-
import uuid
1716
from contextlib import ExitStack
1817
from selectors import EVENT_READ, DefaultSelector
1918

@@ -22,7 +21,7 @@
2221
from pystemd.exceptions import PystemdRunError
2322
from pystemd.systemd1 import Manager as SDManager
2423
from pystemd.systemd1 import Unit
25-
from pystemd.utils import x2char_star, x2cmdlist
24+
from pystemd.utils import random_unit_name, x2char_star, x2cmdlist
2625

2726
EXIT_SUBSTATES = (b"exited", b"failed", b"dead")
2827
USER_MODE = os.getuid() != 0
@@ -145,7 +144,7 @@ def bus_factory():
145144
else:
146145
return DBus(user_mode=user_mode)
147146

148-
name = x2char_star(name or "pystemd{}.service".format(uuid.uuid4().hex))
147+
name = x2char_star(name or random_unit_name())
149148
runtime_max_usec = (runtime_max_sec or 0) * 10**6 or runtime_max_sec
150149

151150
stdin, stdout, stderr = get_fno(stdin), get_fno(stdout), get_fno(stderr)

pystemd/systemd1/unit.pyi

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
# the root directory of this source tree.
77
#
88

9-
from typing import Any, AnyStr, Iterator, List, Optional, Tuple
9+
from typing import Any, AnyStr, List, Optional, Tuple
1010

1111
from pystemd.base import SDInterface, SDObject
12-
from pystemd.dbuslib import DBus, path_encode
13-
from pystemd.utils import x2char_star
12+
from pystemd.dbuslib import DBus
1413

1514
class Unit_Unit(SDInterface):
1615
ActiveEnterTimestamp: int # t

pystemd/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#
88

99
import shlex
10+
import uuid
1011
from pathlib import Path
1112
from typing import Any, Iterable, List, Optional, Tuple, TypeVar, Union
1213

@@ -118,3 +119,6 @@ def unwrap(obj: Optional[T], msg="object was None") -> T:
118119
if obj is None:
119120
raise ValueError(msg)
120121
return obj
122+
123+
def random_unit_name(*, unit_type="service", prefix="pystemd"):
124+
return f"{prefix}{uuid.uuid4().hex}.{unit_type}"

0 commit comments

Comments
 (0)