]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
python: expose prefix configuration as a module docs-develop-mana-huiulj/deployments/3460
authorOto Šťáva <oto.stava@nic.cz>
Fri, 1 Mar 2024 09:13:08 +0000 (10:13 +0100)
committerOto Šťáva <oto.stava@nic.cz>
Wed, 13 Mar 2024 08:23:20 +0000 (09:23 +0100)
distro/pkg/arch/PKGBUILD
distro/pkg/deb/rules
distro/pkg/rpm/knot-resolver.spec
manager/knot_resolver_manager/constants.py
manager/scripts/_env.sh
meson.build
python/knot_resolver.py.in [new file with mode: 0644]
python/meson.build [new file with mode: 0644]
python/setup.py.in [new file with mode: 0644]

index 9d31b59faa00714cb99703144b6f4e99b6dc083e..5ecc0df51f8e2aa1e015cc81a7beb8d5d08626f5 100644 (file)
@@ -60,6 +60,9 @@ build() {
         -D malloc=jemalloc \
         -D unit_tests=enabled
     ninja -C build
+    pushd build/python
+    python -Pm build --wheel --no-isolation
+    popd
     pushd manager
     python -Pm build --wheel --no-isolation
     popd
@@ -81,6 +84,11 @@ package() {
     # remove modules with missing dependencies
     rm "${pkgdir}/usr/lib/knot-resolver/kres_modules/etcd.lua"
 
+    # install knot-resolver metadata Python module
+    pushd build/python
+    python -Pm installer --destdir="$pkgdir" dist/*.whl
+    popd
+
     # install knot-resolver-manager
     pushd manager
     python -Pm installer --destdir="$pkgdir" dist/*.whl
index b707c4a35b68f216f2fb46b6f391e75a7db90d03..cc925bf015b89abac5344a80e34fddfad409d199 100755 (executable)
@@ -10,8 +10,10 @@ export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic -fno-omit-frame-pointer
 # package maintainers to append LDFLAGS
 export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed
 
-export PYBUILD_NAME=knot_resolver_manager
-export PYBUILD_DESTDIR=debian/knot-resolver-manager/
+export PYKRES_NAME=knot_resolver
+export PYKRES_DEST=debian/knot-resolver-core/
+export KRES_MANAGER_NAME=knot_resolver_manager
+export KRES_MANAGER_DEST=debian/knot-resolver-manager/
 
 # see EXAMPLES in dpkg-buildflags(1) and read /usr/share/dpkg/*
 DPKG_EXPORT_BUILDFLAGS = 1
@@ -37,11 +39,17 @@ override_dh_auto_build:
                -Dc_args="$${CFLAGS}" \
                -Dc_link_args="$${LDFLAGS}"
        ninja -v -C build_deb
-       dh_auto_build --buildsystem=pybuild --sourcedirectory manager
+       PYBUILD_NAME="$${PYKRES_NAME}" PYBUILD_DESTDIR="$${PYKRES_DEST}" \
+               dh_auto_build --buildsystem=pybuild --sourcedirectory build_deb/python
+       PYBUILD_NAME="$${KRES_MANAGER_NAME}" PYBUILD_DESTDIR="$${KRES_MANAGER_DEST}" \
+               dh_auto_build --buildsystem=pybuild --sourcedirectory manager
 
 override_dh_auto_install:
        DESTDIR="${PWD}/debian/tmp" ninja -v -C build_deb install
-       dh_auto_install --buildsystem=pybuild --sourcedirectory manager
+       PYBUILD_NAME="$${PYKRES_NAME}" PYBUILD_DESTDIR="$${PYKRES_DEST}" \
+               dh_auto_install --buildsystem=pybuild --sourcedirectory build_deb/python
+       PYBUILD_NAME="$${KRES_MANAGER_NAME}" PYBUILD_DESTDIR="$${KRES_MANAGER_DEST}" \
+               dh_auto_install --buildsystem=pybuild --sourcedirectory manager
        install -m 644 -D manager/etc/knot-resolver/config.yaml debian/tmp/etc/knot-resolver/config.yaml
        install -m 644 -D manager/shell-completion/client.bash debian/tmp/usr/share/bash-completion/completions/kresctl
        install -m 644 -D manager/shell-completion/client.fish debian/tmp/usr/share/fish/completions/kresctl.fish
index 548ae65b8686820e747901aa8d8033ea71ad3865..2ba5383ad6666c5654db9d1ee84baed5b368b54b 100644 (file)
@@ -195,6 +195,10 @@ CFLAGS="%{optflags}" LDFLAGS="%{?__global_ldflags}" meson build_rpm \
 
 %{NINJA} -v -C build_rpm
 
+pushd build_rpm/python
+%py3_build
+popd
+
 pushd manager
 %py3_build
 popd
@@ -225,6 +229,10 @@ install -m 755 -d %{buildroot}/%{_pkgdocdir}
 mv %{buildroot}/%{_datadir}/doc/%{name}/* %{buildroot}/%{_pkgdocdir}/
 %endif
 
+pushd build_rpm/python
+%py3_install
+popd
+
 # install knot-resolver-manager
 pushd manager
 %py3_install
@@ -307,6 +315,13 @@ getent passwd knot-resolver >/dev/null || useradd -r -g knot-resolver -d %{_sysc
 %{_libdir}/knot-resolver/kres_modules/view.lua
 %{_libdir}/knot-resolver/kres_modules/watchdog.lua
 %{_libdir}/knot-resolver/kres_modules/workarounds.lua
+%{python3_sitelib}/knot_resolver.py
+%{python3_sitelib}/knot_resolver-*
+%if 0%{?suse_version}
+%pycache_only %{python3_sitelib}/__pycache__/knot_resolver.*
+%else
+%{python3_sitelib}/__pycache__/knot_resolver.*
+%endif
 %{_mandir}/man8/kresd.8.gz
 %{_mandir}/man8/kresctl.8.gz
 
index b80774a9337e5af2eda0449104b61f645ebc02e2..2d9ebd23d68b89340bc5212047dad9eee4e9a332 100644 (file)
@@ -1,8 +1,14 @@
+import importlib.util
 import logging
 from pathlib import Path
 from typing import TYPE_CHECKING, Optional
 
-from knot_resolver_manager.utils import which
+# Install config is semi-optional - only needed to actually run Manager, but not
+# for its unit tests.
+if importlib.util.find_spec("knot_resolver"):
+    import knot_resolver  # type: ignore[import-not-found]
+else:
+    knot_resolver = None
 
 if TYPE_CHECKING:
     from knot_resolver_manager.config_store import ConfigStore
@@ -20,11 +26,13 @@ MAX_WORKERS = 256
 
 
 def kresd_executable() -> Path:
-    return which.which("kresd")
+    assert knot_resolver is not None
+    return knot_resolver.sbin_dir / "kresd"
 
 
 def kres_gc_executable() -> Path:
-    return which.which("kres-cache-gc")
+    assert knot_resolver is not None
+    return knot_resolver.sbin_dir / "kres-cache-gc"
 
 
 def kresd_cache_dir(config: "KresConfig") -> Path:
index b1941edfed5beee1ab04f04a3f848e8aaaeb71c2..ddbbcf552e40a35d876bf3d572067815a1f0b15d 100644 (file)
@@ -47,6 +47,6 @@ function build_kresd {
        meson manager/.build_kresd --prefix=$(realpath manager/.install_kresd) --default-library=static --buildtype=debug
        ninja -C manager/.build_kresd
        ninja install -C manager/.build_kresd
-       export PATH="$(realpath manager/.install_kresd)/sbin:$PATH"
+       export PYTHONPATH="$(realpath manager/.build_kresd/python):${PYTHONPATH:-}"
        popd
 }
index 015d59f918f6133f700d76fe760369524bfa62e8..9ee683d77624a70ca9b2cb6c9418ddb60d2e7301 100644 (file)
@@ -231,6 +231,7 @@ subdir('lib')
 subdir('daemon')
 subdir('modules')
 subdir('manager')
+subdir('python')
 subdir('utils')
 if get_option('bench') == 'enabled'
   subdir('bench')
diff --git a/python/knot_resolver.py.in b/python/knot_resolver.py.in
new file mode 100644 (file)
index 0000000..262f7a8
--- /dev/null
@@ -0,0 +1,10 @@
+from pathlib import Path
+
+__version__ = "@kres_version@"
+
+sbin_dir = Path("@sbin_dir@")
+bin_dir = Path("@bin_dir@")
+etc_dir = Path("@etc_dir@")
+run_dir = Path("@run_dir@")
+lib_dir = Path("@lib_dir@")
+modules_dir = Path("@modules_dir@")
diff --git a/python/meson.build b/python/meson.build
new file mode 100644 (file)
index 0000000..e209df5
--- /dev/null
@@ -0,0 +1,23 @@
+# python
+# SPDX-License-Identifier: GPL-3.0-or-later
+
+python_config = configuration_data()
+python_config.set('kres_version', meson.project_version())
+python_config.set('sbin_dir', sbin_dir)
+python_config.set('bin_dir', bin_dir)
+python_config.set('etc_dir', etc_dir)
+python_config.set('run_dir', run_dir)
+python_config.set('lib_dir', lib_dir)
+python_config.set('modules_dir', modules_dir)
+
+configure_file(
+  input: 'knot_resolver.py.in',
+  output: 'knot_resolver.py',
+  configuration: python_config,
+)
+
+configure_file(
+  input: 'setup.py.in',
+  output: 'setup.py',
+  configuration: python_config,
+)
diff --git a/python/setup.py.in b/python/setup.py.in
new file mode 100644 (file)
index 0000000..07b7145
--- /dev/null
@@ -0,0 +1,12 @@
+from setuptools import setup
+
+# TODO: Migrate this to a pyproject.toml once Debian 11 support is dropped.
+setup(
+    name="knot_resolver",
+    version="@kres_version@",
+    description="Knot Resolver helper data for Python",
+    author="Oto Šťáva",
+    author_email="oto.stava@nic.cz",
+    python_requires=">=3.8,<4.0",
+    py_modules=["knot_resolver"],
+)