python3-cyberfusion-systemd-support 1.1.1.1.1__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.
@@ -0,0 +1,70 @@
1
+ Metadata-Version: 2.1
2
+ Name: python3-cyberfusion-systemd-support
3
+ Version: 1.1.1.1.1
4
+ Summary: Library for systemd.
5
+ Home-page: https://github.com/CyberfusionIO/python3-cyberfusion-systemd-support
6
+ Author: Cyberfusion
7
+ Author-email: support@cyberfusion.io
8
+ Platform: linux
9
+ Description-Content-Type: text/markdown
10
+
11
+ # python3-cyberfusion-systemd-support
12
+
13
+ Library for [systemd](https://systemd.io/).
14
+
15
+ # Install
16
+
17
+ ## PyPI
18
+
19
+ Run the following command to install the package from PyPI:
20
+
21
+ pip3 install python3-cyberfusion-systemd-support
22
+
23
+ ## Generic
24
+
25
+ Run the following command to create a source distribution:
26
+
27
+ python3 setup.py sdist
28
+
29
+ Next, ensure you are working on a system that ships systemd.
30
+
31
+ ## Debian
32
+
33
+ Run the following commands to build a Debian package:
34
+
35
+ mk-build-deps -i -t 'apt -o Debug::pkgProblemResolver=yes --no-install-recommends -y'
36
+ dpkg-buildpackage -us -uc
37
+
38
+ # Configure
39
+
40
+ No configuration is supported.
41
+
42
+ # Usage
43
+
44
+ ## Units
45
+
46
+ ```python
47
+ from cyberfusion.SystemdSupport.units import Unit
48
+
49
+ unit = Unit(f"example.{Unit.SUFFIX_SERVICE}")
50
+
51
+ unit.disable()
52
+ unit.stop()
53
+ unit.enable()
54
+ unit.restart()
55
+ unit.reload()
56
+
57
+ print(unit.is_active)
58
+ print(unit.is_enabled)
59
+ ```
60
+
61
+ ## Tmp files
62
+
63
+ ```python
64
+ from cyberfusion.SystemdSupport.tmp_files import TmpFileConfigurationLine, TmpFileConfigurationFile
65
+
66
+ tmp_file_configuration_line = str(TmpFileConfigurationLine(type_="d", path="/tmp/example", mode="0755", user="example", group="example"))
67
+ tmp_file_configuration_file = TmpFileConfigurationFile(path="/etc/tmpfiles.d/example.conf")
68
+
69
+ tmp_file_configuration_file.create()
70
+ ```
@@ -0,0 +1,60 @@
1
+ # python3-cyberfusion-systemd-support
2
+
3
+ Library for [systemd](https://systemd.io/).
4
+
5
+ # Install
6
+
7
+ ## PyPI
8
+
9
+ Run the following command to install the package from PyPI:
10
+
11
+ pip3 install python3-cyberfusion-systemd-support
12
+
13
+ ## Generic
14
+
15
+ Run the following command to create a source distribution:
16
+
17
+ python3 setup.py sdist
18
+
19
+ Next, ensure you are working on a system that ships systemd.
20
+
21
+ ## Debian
22
+
23
+ Run the following commands to build a Debian package:
24
+
25
+ mk-build-deps -i -t 'apt -o Debug::pkgProblemResolver=yes --no-install-recommends -y'
26
+ dpkg-buildpackage -us -uc
27
+
28
+ # Configure
29
+
30
+ No configuration is supported.
31
+
32
+ # Usage
33
+
34
+ ## Units
35
+
36
+ ```python
37
+ from cyberfusion.SystemdSupport.units import Unit
38
+
39
+ unit = Unit(f"example.{Unit.SUFFIX_SERVICE}")
40
+
41
+ unit.disable()
42
+ unit.stop()
43
+ unit.enable()
44
+ unit.restart()
45
+ unit.reload()
46
+
47
+ print(unit.is_active)
48
+ print(unit.is_enabled)
49
+ ```
50
+
51
+ ## Tmp files
52
+
53
+ ```python
54
+ from cyberfusion.SystemdSupport.tmp_files import TmpFileConfigurationLine, TmpFileConfigurationFile
55
+
56
+ tmp_file_configuration_line = str(TmpFileConfigurationLine(type_="d", path="/tmp/example", mode="0755", user="example", group="example"))
57
+ tmp_file_configuration_file = TmpFileConfigurationFile(path="/etc/tmpfiles.d/example.conf")
58
+
59
+ tmp_file_configuration_file.create()
60
+ ```
@@ -0,0 +1,21 @@
1
+ [tool.isort]
2
+ profile = "black"
3
+ line_length = 79
4
+ known_first_party = ["cyberfusion"]
5
+ default_section = "THIRDPARTY"
6
+
7
+ [tool.black]
8
+ line-length = 79
9
+ exclude = '''
10
+ (
11
+ /(
12
+ \.eggs # exclude a few common directories in the
13
+ | \.git # root of the project
14
+ | \.hg
15
+ | \.mypy_cache
16
+ | \.tox
17
+ | \.venv
18
+ | venv
19
+ )/
20
+ )
21
+ '''
@@ -0,0 +1,12 @@
1
+ [aliases]
2
+ test = pytest
3
+
4
+ [tool:pytest]
5
+ norecursedirs = .git build dist *.egg __pycache__ .cache
6
+ testpaths = tests
7
+ junit_suite_name = python3-cyberfusion-systemd-support
8
+
9
+ [egg_info]
10
+ tag_build =
11
+ tag_date = 0
12
+
@@ -0,0 +1,23 @@
1
+ """A setuptools based setup module."""
2
+
3
+ from setuptools import setup
4
+
5
+ with open("README.md", "r", encoding="utf-8") as fh:
6
+ long_description = fh.read()
7
+
8
+ setup(
9
+ name="python3-cyberfusion-systemd-support",
10
+ version="1.1.1.1.1",
11
+ description="Library for systemd.",
12
+ long_description=long_description,
13
+ long_description_content_type="text/markdown",
14
+ author="Cyberfusion",
15
+ author_email="support@cyberfusion.io",
16
+ url="https://github.com/CyberfusionIO/python3-cyberfusion-systemd-support",
17
+ platforms=["linux"],
18
+ packages=[
19
+ "cyberfusion.SystemdSupport",
20
+ ],
21
+ package_dir={"": "src"},
22
+ data_files=[],
23
+ )
@@ -0,0 +1,43 @@
1
+ """Classes for systemd."""
2
+
3
+ import subprocess
4
+ from typing import List
5
+
6
+ from cyberfusion.SystemdSupport._constants import SYSTEMCTL_BIN
7
+ from cyberfusion.SystemdSupport.units import Unit
8
+
9
+
10
+ class Systemd:
11
+ """Represents systemd."""
12
+
13
+ def __init__(self) -> None:
14
+ """Do nothing."""
15
+ pass
16
+
17
+ def search_units(self, string: str) -> List[Unit]:
18
+ """Search units."""
19
+ output = []
20
+
21
+ units = subprocess.run(
22
+ [
23
+ SYSTEMCTL_BIN,
24
+ "list-units",
25
+ "--plain",
26
+ "--type",
27
+ "service",
28
+ "--all",
29
+ string,
30
+ "--no-pager",
31
+ "--no-legend",
32
+ ],
33
+ check=True,
34
+ stdout=subprocess.PIPE,
35
+ text=True,
36
+ ).stdout.splitlines()
37
+
38
+ for unit in units:
39
+ name = unit.split()[0]
40
+
41
+ output.append(Unit(name))
42
+
43
+ return output
@@ -0,0 +1,5 @@
1
+ """Constants."""
2
+
3
+ import os
4
+
5
+ SYSTEMCTL_BIN = os.path.join(os.path.sep, "bin", "systemctl")
@@ -0,0 +1,14 @@
1
+ """Classes for systemd manager."""
2
+
3
+ import subprocess
4
+
5
+ from cyberfusion.SystemdSupport._constants import SYSTEMCTL_BIN
6
+
7
+
8
+ class SystemdManager:
9
+ """Represents systemd manager."""
10
+
11
+ @staticmethod
12
+ def daemon_reload() -> None:
13
+ """Reload manager configuration."""
14
+ subprocess.run([SYSTEMCTL_BIN, "daemon-reload"], check=True)
@@ -0,0 +1,61 @@
1
+ """Classes for systemd tmp files."""
2
+
3
+ import os
4
+ import subprocess
5
+ from typing import Optional
6
+
7
+ SYSTEMCTL_TMPFILES_BIN = os.path.join(os.path.sep, "bin", "systemd-tmpfiles")
8
+
9
+
10
+ class TmpFileConfigurationFile:
11
+ """Represents tmp file configuration file."""
12
+
13
+ def __init__(self, path: str) -> None:
14
+ """Set attributes."""
15
+ self.path = path
16
+
17
+ def create(self) -> None:
18
+ """Create tmp files."""
19
+ subprocess.run(
20
+ [SYSTEMCTL_TMPFILES_BIN, "--create", self.path], check=True
21
+ )
22
+
23
+
24
+ class TmpFileConfigurationLine:
25
+ """Represents tmp file configuration line."""
26
+
27
+ TYPE_DIRECTORY_CREATE = "d"
28
+
29
+ def __init__(
30
+ self,
31
+ type_: str,
32
+ path: str,
33
+ *,
34
+ mode: str,
35
+ user: str,
36
+ group: str,
37
+ age: Optional[str] = None,
38
+ ) -> None:
39
+ """Set attributes."""
40
+ self.type = type_
41
+ self.path = path
42
+ self.mode = mode
43
+ self.user = user
44
+ self.group = group
45
+ self.age = age
46
+
47
+ def __str__(self) -> str:
48
+ """Get line for in configuration."""
49
+ return (
50
+ self.type
51
+ + " "
52
+ + self.path
53
+ + " "
54
+ + self.mode
55
+ + " "
56
+ + self.user
57
+ + " "
58
+ + self.group
59
+ + " "
60
+ + (self.age if self.age else "-")
61
+ )
@@ -0,0 +1,141 @@
1
+ """Classes for systemd units."""
2
+
3
+ import glob
4
+ import os
5
+ import subprocess
6
+ from functools import wraps
7
+ from typing import Callable, Optional, TypeVar, cast
8
+
9
+ from cyberfusion.SystemdSupport._constants import SYSTEMCTL_BIN
10
+ from cyberfusion.SystemdSupport.manager import SystemdManager
11
+
12
+ F = TypeVar("F", bound=Callable[..., None])
13
+
14
+
15
+ def reload_manager(f: F) -> F:
16
+ """Reload manager configuration if needed.."""
17
+
18
+ @wraps(f)
19
+ def wrapper(
20
+ self: "Unit",
21
+ *args: tuple,
22
+ **kwargs: dict,
23
+ ) -> None:
24
+ if self.needs_reload:
25
+ SystemdManager.daemon_reload()
26
+
27
+ f(self, *args, **kwargs)
28
+
29
+ return cast(F, wrapper)
30
+
31
+
32
+ class Unit:
33
+ """Represents unit."""
34
+
35
+ SUFFIX_SERVICE = "service"
36
+
37
+ DIRECTORY_SYSTEMD_OVERRIDE = os.path.join(
38
+ os.path.sep, "etc", "systemd", "system"
39
+ )
40
+
41
+ def __init__(self, name: str) -> None:
42
+ """Set attributes."""
43
+ self.name = name
44
+
45
+ def get_property(self, name: str) -> Optional[str]:
46
+ """Get unit property from systemd."""
47
+ output = subprocess.run(
48
+ [SYSTEMCTL_BIN, "-P", name, "show", self.name],
49
+ check=True,
50
+ stdout=subprocess.PIPE,
51
+ text=True,
52
+ ).stdout.rstrip()
53
+
54
+ if output == "":
55
+ return None
56
+
57
+ return output
58
+
59
+ @property
60
+ def drop_in_directory(self) -> str:
61
+ """Get path to drop-in directory."""
62
+ return os.path.join(
63
+ os.path.sep, "etc", "systemd", "system", self.name + ".d"
64
+ )
65
+
66
+ @property
67
+ def needs_reload(self) -> bool:
68
+ """Get if manager needs to be reloaded for unit."""
69
+
70
+ # systemd itself decides that daemon-reload is needed
71
+
72
+ if self.get_property("NeedDaemonReload") == "yes":
73
+ return True
74
+
75
+ # Drop-in directory was added after the latest daemon-reload (systemd
76
+ # does not detect it itself in that case, see: https://github.com/systemd/systemd/issues/31752)
77
+
78
+ if (
79
+ os.path.isdir(self.drop_in_directory)
80
+ and glob.glob(os.path.join(self.drop_in_directory, "*.conf"))
81
+ and self.get_property("DropInPaths") is None
82
+ ):
83
+ return True
84
+
85
+ return False
86
+
87
+ def disable(self) -> None:
88
+ """Disable unit."""
89
+ if not self.is_enabled:
90
+ return
91
+
92
+ subprocess.run([SYSTEMCTL_BIN, "disable", self.name], check=True)
93
+
94
+ def enable(self) -> None:
95
+ """Enable unit."""
96
+ if self.is_enabled:
97
+ return
98
+
99
+ subprocess.run([SYSTEMCTL_BIN, "enable", self.name], check=True)
100
+
101
+ @reload_manager
102
+ def stop(self) -> None:
103
+ """Stop unit."""
104
+ if not self.is_active:
105
+ return
106
+
107
+ subprocess.run([SYSTEMCTL_BIN, "stop", self.name], check=True)
108
+
109
+ @reload_manager
110
+ def restart(self) -> None:
111
+ """Restart unit."""
112
+ subprocess.run([SYSTEMCTL_BIN, "restart", self.name], check=True)
113
+
114
+ @reload_manager
115
+ def reload(self) -> None:
116
+ """Reload unit."""
117
+ subprocess.run([SYSTEMCTL_BIN, "reload", self.name], check=True)
118
+
119
+ @property
120
+ def is_active(self) -> bool:
121
+ """Get if unit is active."""
122
+ try:
123
+ subprocess.run(
124
+ [SYSTEMCTL_BIN, "is-active", "--quiet", self.name], check=True
125
+ )
126
+ except subprocess.CalledProcessError:
127
+ return False
128
+
129
+ return True
130
+
131
+ @property
132
+ def is_enabled(self) -> bool:
133
+ """Get if unit is enabled."""
134
+ try:
135
+ subprocess.run(
136
+ [SYSTEMCTL_BIN, "is-enabled", "--quiet", self.name], check=True
137
+ )
138
+ except subprocess.CalledProcessError:
139
+ return False
140
+
141
+ return True
@@ -0,0 +1,70 @@
1
+ Metadata-Version: 2.1
2
+ Name: python3-cyberfusion-systemd-support
3
+ Version: 1.1.1.1.1
4
+ Summary: Library for systemd.
5
+ Home-page: https://github.com/CyberfusionIO/python3-cyberfusion-systemd-support
6
+ Author: Cyberfusion
7
+ Author-email: support@cyberfusion.io
8
+ Platform: linux
9
+ Description-Content-Type: text/markdown
10
+
11
+ # python3-cyberfusion-systemd-support
12
+
13
+ Library for [systemd](https://systemd.io/).
14
+
15
+ # Install
16
+
17
+ ## PyPI
18
+
19
+ Run the following command to install the package from PyPI:
20
+
21
+ pip3 install python3-cyberfusion-systemd-support
22
+
23
+ ## Generic
24
+
25
+ Run the following command to create a source distribution:
26
+
27
+ python3 setup.py sdist
28
+
29
+ Next, ensure you are working on a system that ships systemd.
30
+
31
+ ## Debian
32
+
33
+ Run the following commands to build a Debian package:
34
+
35
+ mk-build-deps -i -t 'apt -o Debug::pkgProblemResolver=yes --no-install-recommends -y'
36
+ dpkg-buildpackage -us -uc
37
+
38
+ # Configure
39
+
40
+ No configuration is supported.
41
+
42
+ # Usage
43
+
44
+ ## Units
45
+
46
+ ```python
47
+ from cyberfusion.SystemdSupport.units import Unit
48
+
49
+ unit = Unit(f"example.{Unit.SUFFIX_SERVICE}")
50
+
51
+ unit.disable()
52
+ unit.stop()
53
+ unit.enable()
54
+ unit.restart()
55
+ unit.reload()
56
+
57
+ print(unit.is_active)
58
+ print(unit.is_enabled)
59
+ ```
60
+
61
+ ## Tmp files
62
+
63
+ ```python
64
+ from cyberfusion.SystemdSupport.tmp_files import TmpFileConfigurationLine, TmpFileConfigurationFile
65
+
66
+ tmp_file_configuration_line = str(TmpFileConfigurationLine(type_="d", path="/tmp/example", mode="0755", user="example", group="example"))
67
+ tmp_file_configuration_file = TmpFileConfigurationFile(path="/etc/tmpfiles.d/example.conf")
68
+
69
+ tmp_file_configuration_file.create()
70
+ ```
@@ -0,0 +1,13 @@
1
+ README.md
2
+ pyproject.toml
3
+ setup.cfg
4
+ setup.py
5
+ src/cyberfusion/SystemdSupport/__init__.py
6
+ src/cyberfusion/SystemdSupport/_constants.py
7
+ src/cyberfusion/SystemdSupport/manager.py
8
+ src/cyberfusion/SystemdSupport/tmp_files.py
9
+ src/cyberfusion/SystemdSupport/units.py
10
+ src/python3_cyberfusion_systemd_support.egg-info/PKG-INFO
11
+ src/python3_cyberfusion_systemd_support.egg-info/SOURCES.txt
12
+ src/python3_cyberfusion_systemd_support.egg-info/dependency_links.txt
13
+ src/python3_cyberfusion_systemd_support.egg-info/top_level.txt