-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial version of sdnotify-wrapper
- Loading branch information
Showing
7 changed files
with
306 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
name: on-push | ||
|
||
on: [push] | ||
|
||
jobs: | ||
static_checks: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Python | ||
uses: actions/setup-python@v3 | ||
with: | ||
python-version: 3.11 | ||
- name: Install dependencies | ||
run: | | ||
pip install .[test] | ||
- name: Lint with Pylint | ||
run: pylint --score=n *.py | ||
- name: Check format with Black | ||
run: black --check . | ||
- name: Check import statement order with isort | ||
run: isort --check . | ||
- name: Check spelling | ||
run: git ls-files -z | xargs -0 -- codespell | ||
release: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Python | ||
uses: actions/setup-python@v3 | ||
with: | ||
python-version: 3.11 | ||
- name: Install dependencies | ||
run: | | ||
# python3-build is broken on 22.04: | ||
# https://bugs.launchpad.net/ubuntu/+source/python-build/+bug/1992108 | ||
# install everything from pip instead | ||
pip install build twine | ||
- name: Build dist packages | ||
run: | | ||
export SOURCE_DATE_EPOCH=$(git log -1 --pretty=%ct) | ||
python3 -m build | ||
- name: Upload to PyPi | ||
env: | ||
TWINE_USERNAME: __token__ | ||
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }} | ||
if: ${{ github.ref_type == 'tag' && startsWith(github.ref_name, 'v') && env.TWINE_PASSWORD != '' }} | ||
run: twine upload --non-interactive dist/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024-present Simon Holesch <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# sdnotify-wrapper-py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/bin/sh -e | ||
# called by meson to get project version | ||
|
||
main() { | ||
if [ "$1" = "--save" ]; then | ||
get_git_version > "${MESON_DIST_ROOT:?}/VERSION" | ||
elif [ -e VERSION ]; then | ||
cat VERSION | ||
else | ||
get_git_version | ||
fi | ||
} | ||
|
||
get_git_version() { | ||
version="$(git_describe)" | ||
version="${version#v}" # remove "v" prefix | ||
|
||
case "$version" in | ||
*-*) | ||
# not an exact match: make version PEP 440 compliant | ||
# e.g. 1.2.3-5-gd63c80c is turned into 1.2.3.dev5+gd63c80c | ||
latest_release="${version%%-*}" | ||
suffix="${version#*-}" | ||
dev_num="${suffix%%-*}" | ||
commit="${suffix#*-}" | ||
echo "$latest_release.dev$dev_num+$commit" | ||
;; | ||
*) | ||
echo "$version" | ||
esac | ||
} | ||
|
||
git_describe() { | ||
if ! git -C "${MESON_SOURCE_ROOT:-$PWD}" describe --match "v*" --tags 2>/dev/null; then | ||
echo "v0.1.0-0-g$(git rev-parse --short HEAD)" | ||
fi | ||
} | ||
|
||
main "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
project( | ||
'sdnotify-wrapper-py', | ||
version: run_command('./get_version', check: true).stdout().strip() | ||
) | ||
|
||
meson.add_dist_script('./get_version', '--save') | ||
|
||
py = import('python').find_installation() | ||
|
||
py.install_sources('sdnotify_wrapper.py') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
[project] | ||
# For more information on how to specify metadata, see | ||
# https://packaging.python.org/en/latest/specifications/pyproject-toml/ | ||
name = "sdnotify-wrapper-py" | ||
description = "Notify readiness to systemd by writing to stdout in a service" | ||
readme = "README.md" | ||
requires-python = ">=3.7" | ||
license = {file = "LICENSE.txt"} | ||
authors = [ | ||
{name = "Simon Holesch", email = "[email protected]"}, | ||
] | ||
keywords = ["systemd", "readiness", "sd_notify"] | ||
classifiers = [ | ||
"Development Status :: 5 - Production/Stable", | ||
"Environment :: Console", | ||
"Intended Audience :: Developers", | ||
"License :: OSI Approved :: MIT License", | ||
"Operating System :: POSIX :: Linux", | ||
"Programming Language :: Python", | ||
"Programming Language :: Python :: 3.7", | ||
"Programming Language :: Python :: 3.8", | ||
"Programming Language :: Python :: 3.9", | ||
"Programming Language :: Python :: 3.10", | ||
"Programming Language :: Python :: 3.11", | ||
"Programming Language :: Python :: 3.12", | ||
"Programming Language :: Python :: 3.13", | ||
"Topic :: System :: Boot :: Init", | ||
"Topic :: System :: Systems Administration", | ||
] | ||
dependencies = [] | ||
dynamic = ["version"] | ||
|
||
[project.urls] | ||
Issues = "https://github.com/holesch/sdnotify-wrapper-py/issues" | ||
Source = "https://github.com/holesch/sdnotify-wrapper-py" | ||
|
||
[project.scripts] | ||
sdnotify-wrapper = "sdnotify_wrapper:main" | ||
|
||
[project.optional-dependencies] | ||
test = [ | ||
"black", | ||
"codespell", | ||
"isort", | ||
"pylint", | ||
] | ||
|
||
[build-system] | ||
build-backend = "mesonpy" | ||
requires = ["meson-python"] | ||
|
||
[tool.pylint."MESSAGES CONTROL"] | ||
disable = [ | ||
"missing-class-docstring", | ||
"missing-function-docstring", | ||
"missing-module-docstring", | ||
] | ||
|
||
[tool.isort] | ||
profile = "black" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import getopt | ||
import os | ||
import struct | ||
import sys | ||
import typing | ||
|
||
|
||
def usage(): | ||
print( | ||
"usage: sdnotify-wrapper [ -d fd ] [ -f ] [ -t timeout ] [ -k ] prog...", | ||
file=sys.stderr, | ||
) | ||
|
||
|
||
def main(): | ||
args = Arguments.parse() | ||
|
||
if "NOTIFY_SOCKET" not in os.environ: | ||
os.execvp(args.argv[0], args.argv) | ||
|
||
main_pid = os.getpid() | ||
read_pipe, write_pipe = os.pipe() | ||
|
||
pid = os.fork() if args.fork_once else double_fork() | ||
if is_forked_child(pid): | ||
os.close(write_pipe) | ||
return run_child(read_pipe, args.timeout, main_pid) | ||
|
||
os.close(read_pipe) | ||
os.dup2(write_pipe, args.fd) | ||
os.close(write_pipe) | ||
|
||
if not args.keep: | ||
del os.environ["NOTIFY_SOCKET"] | ||
|
||
return os.execvp(args.argv[0], args.argv) | ||
|
||
|
||
class Arguments(typing.NamedTuple): | ||
argv: typing.List[str] | ||
fd: int = 1 # default is stdout | ||
fork_once: bool = False | ||
timeout: typing.Optional[int] = None | ||
keep: bool = False | ||
|
||
@classmethod | ||
def parse(cls): | ||
try: | ||
opts, args = getopt.getopt(sys.argv[1:], "d:ft:k") | ||
except getopt.GetoptError as err: | ||
print(err, file=sys.stderr) | ||
usage() | ||
sys.exit(2) | ||
|
||
init_args = {} | ||
for opt, arg in opts: | ||
if opt == "-d": | ||
init_args["fd"] = arg | ||
elif opt == "-f": | ||
init_args["fork_once"] = True | ||
elif opt == "-t": | ||
init_args["timeout"] = arg | ||
elif opt == "-k": | ||
init_args["keep"] = True | ||
else: | ||
assert False, "unhandled option" | ||
|
||
if not args: | ||
usage() | ||
sys.exit(2) | ||
|
||
return cls(argv=args, **init_args) | ||
|
||
|
||
# pylint: disable=R1732 | ||
# - consider-using-with: can't use a context manager when using fork() | ||
def double_fork(): | ||
read_pipe, write_pipe = os.pipe() | ||
read_pipe = open(read_pipe, "rb") | ||
write_pipe = open(write_pipe, "wb") | ||
|
||
pid = os.fork() | ||
if is_forked_child(pid): | ||
read_pipe.close() | ||
pid = os.fork() | ||
if is_forked_child(pid): | ||
# grandchild | ||
write_pipe.close() | ||
return 0 | ||
|
||
msg = struct.pack("!Q", pid) | ||
write_pipe.write(msg) | ||
write_pipe.flush() | ||
os._exit(0) | ||
|
||
write_pipe.close() | ||
msg = read_pipe.read(8) | ||
read_pipe.close() | ||
os.waitpid(pid, 0) # wait for child #1 to exit | ||
grandchild = struct.unpack("!Q", msg)[0] | ||
return grandchild | ||
|
||
|
||
def is_forked_child(pid): | ||
return pid == 0 | ||
|
||
|
||
# pylint: disable=W0613 | ||
# - unused-argument: timeout is not implemented, yet | ||
def run_child(read_pipe, timeout, main_pid): | ||
data = b"" | ||
while b"\n" not in data: | ||
data = os.read(read_pipe, 4096) | ||
if not data: | ||
return 1 | ||
return notify_systemd(main_pid) | ||
|
||
|
||
def notify_systemd(main_pid): | ||
argv = ["systemd-notify", "--ready", "--pid", str(main_pid)] | ||
os.execvp(argv[0], argv) | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(main()) |