Skip to content

Commit 50a4f6e

Browse files
committed
Feature: python: Add a python module wrapping libpacemaker.
This is a public, native python API that wraps libpacemaker. It aims to provide a python-based way of interacting with pacemaker, which means we need to be careful to use exceptions where appropriate, return python types, and in general write things in a pythonic style.
1 parent 805fb09 commit 50a4f6e

File tree

6 files changed

+41
-6
lines changed

6 files changed

+41
-6
lines changed

mk/python.mk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
.PHONY: pylint
1111
pylint: $(PYCHECKFILES)
12-
PYTHONPATH=$(abs_top_builddir)/python \
12+
PYTHONPATH=$(abs_top_builddir)/python:$(abs_top_builddir)/python/pacemaker/.libs \
1313
pylint --rcfile $(top_srcdir)/python/pylintrc $(PYCHECKFILES)
1414

1515
# Disabled warnings:
@@ -27,5 +27,5 @@ pylint: $(PYCHECKFILES)
2727
# Disable docstrings warnings on unit tests.
2828
.PHONY: pyflake
2929
pyflake: $(PYCHECKFILES)
30-
PYTHONPATH=$(abs_top_builddir)/python \
30+
PYTHONPATH=$(abs_top_builddir)/python:$(abs_top_builddir)/python/pacemaker/.libs \
3131
flake8 --ignore=W503,E402,E501,F401 --per-file-ignores="tests/*:D100,D101,D102,D104" $(PYCHECKFILES)

python/pacemaker/Makefile.am

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ _pcmksupport_la_LIBADD = $(top_builddir)/lib/pacemaker/libpacemaker.la
1818

1919
pkgpython_PYTHON = __init__.py \
2020
_library.py \
21-
exitstatus.py
21+
exceptions.py \
22+
exitstatus.py \
23+
resource.py
2224

2325
nodist_pkgpython_PYTHON = buildoptions.py
2426

python/pacemaker/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
__copyright__ = "Copyright 2023-2024 the Pacemaker project contributors"
44
__license__ = "GNU Lesser General Public License version 2.1 or later (LGPLv2.1+)"
55

6-
from pacemaker.buildoptions import BuildOptions
7-
from pacemaker.exitstatus import ExitStatus
6+
from .buildoptions import BuildOptions
7+
from .exitstatus import ExitStatus
8+
from . import exceptions
9+
from . import resource

python/pacemaker/exceptions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""A module providing exceptions that can be raised by the Pacemaker module."""
2+
3+
__all__ = ["PacemakerError"]
4+
__copyright__ = "Copyright 2025 the Pacemaker project contributors"
5+
__license__ = "GNU Lesser General Public License version 2.1 or later (LGPLv2.1+)"
6+
7+
8+
class PacemakerError(Exception):
9+
"""Base exception class for all Pacemaker errors."""

python/pacemaker/resource.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""A module for managing cluster resources."""
2+
3+
__all__ = ["list_standards"]
4+
__copyright__ = "Copyright 2025 the Pacemaker project contributors"
5+
__license__ = "GNU Lesser General Public License version 2.1 or later (LGPLv2.1+)"
6+
7+
import libxml2
8+
9+
import _pcmksupport
10+
from pacemaker.exceptions import PacemakerError
11+
12+
13+
def list_standards():
14+
"""Return a list of supported resource standards."""
15+
try:
16+
xml = _pcmksupport.list_standards()
17+
except _pcmksupport.PacemakerError as e:
18+
raise PacemakerError(*e.args) from None
19+
20+
doc = libxml2.xmlDoc(xml)
21+
22+
return [item.getContent() for item in doc.xpathEval("/pacemaker-result/standards/item")]

python/pylintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ unsafe-load-any-extension=no
4141
# A comma-separated list of package or module names from where C extensions may
4242
# be loaded. Extensions are loading into the active Python interpreter and may
4343
# run arbitrary code
44-
extension-pkg-allow-list=
44+
extension-pkg-allow-list=_pcmksupport
4545

4646
# Minimum supported python version
4747
# CHANGED

0 commit comments

Comments
 (0)