Skip to content

Commit

Permalink
Merge pull request flux-framework#6486 from grondo/python-conf-builtin
Browse files Browse the repository at this point in the history
add `flux.conf_builtin.conf_builtin_get()` to give Python access to compiled-in config values
  • Loading branch information
mergify[bot] authored Dec 11, 2024
2 parents 7d4d63b + 024e5cb commit cb2b6f3
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/bindings/python/flux/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ nobase_fluxpy_PYTHON = \
memoized_property.py \
debugged.py \
importer.py \
conf_builtin.py \
cli/__init__.py \
cli/base.py \
cli/alloc.py \
Expand Down
64 changes: 64 additions & 0 deletions src/bindings/python/flux/conf_builtin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
###############################################################
# Copyright 2024 Lawrence Livermore National Security, LLC
# (c.f. AUTHORS, NOTICE.LLNS, COPYING)
#
# This file is part of the Flux resource manager framework.
# For details, see https://github.com/flux-framework.
#
# SPDX-License-Identifier: LGPL-3.0
###############################################################

import threading
from pathlib import Path

from flux.core.inner import raw

tls = threading.local()
tls.FLUX_CONF_AUTO_FLAG = None


def _conf_builtin_get_flag():
"""Simulate the use of FLUX_CONF_AUTO for Python
FLUX_CONF_AUTO will not work from Python since the executable will
be python and not something part of the Flux build tree or installed
by Flux. This function simulates FLUX_CONF_AUTO by returning the
correct FLUX_CONF_FLAG based on whether this module is under the
in tree PYTHONPATH or not.
"""
if tls.FLUX_CONF_AUTO_FLAG is None:
# Resolve builtin installed python path:
pythonpath = conf_builtin_get("python_path", which="intree").split(":")
for path in pythonpath:
if Path(path).resolve() in Path(__file__).resolve().parents:
# If path is one of this module's parents,
# then this module is in tree:
tls.FLUX_CONF_AUTO_FLAG = raw.FLUX_CONF_INTREE
return tls.FLUX_CONF_AUTO_FLAG
# O/w, assume we're installed
tls.FLUX_CONF_AUTO_FLAG = raw.FLUX_CONF_INSTALLED
return tls.FLUX_CONF_AUTO_FLAG


def conf_builtin_get(name, which="auto"):
"""Get builtin (compiled-in) configuration values from libflux
Args:
name (str): name of config value
which (str): one of "installed", "intree", or "auto" to return
the installed path, in tree path, or automatically determine
which to use. default=auto.
"""
if which == "auto":
flag = _conf_builtin_get_flag()
elif which == "installed":
flag = raw.FLUX_CONF_INSTALLED
elif which == "intree":
flag = raw.FLUX_CONF_INTREE
else:
raise ValueError("which must be one of auto, installed, or intree")

try:
return raw.flux_conf_builtin_get(name, flag).decode("utf-8")
except OSError:
raise ValueError(f"No builtin config value for '{name}'")
5 changes: 5 additions & 0 deletions src/common/libflux/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ struct flux_conf {
static const char *conf_auxkey = "flux::conf_object";

static struct builtin builtin_tab[] = {
{
.key = "confdir",
.val_installed = FLUXCONFDIR,
.val_intree = ABS_TOP_SRCDIR "/etc",
},
{
.key = "lua_cpath_add",
.val_installed = LUAEXECDIR "/?.so",
Expand Down
1 change: 1 addition & 0 deletions t/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ TESTSCRIPTS = \
python/t0028-compat36.py \
python/t0029-fileref.py \
python/t0030-journal.py \
python/t0031-conf-builtin.py \
python/t1000-service-add-remove.py

if HAVE_FLUX_SECURITY
Expand Down
34 changes: 34 additions & 0 deletions t/python/t0031-conf-builtin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python3
###############################################################
# Copyright 2024 Lawrence Livermore National Security, LLC
# (c.f. AUTHORS, NOTICE.LLNS, COPYING)
#
# This file is part of the Flux resource manager framework.
# For details, see https://github.com/flux-framework.
#
# SPDX-License-Identifier: LGPL-3.0
###############################################################

import unittest

import subflux # noqa: F401
from flux.conf_builtin import conf_builtin_get
from pycotap import TAPTestRunner


class TestConfBuiltin(unittest.TestCase):

def test_conf_builtin_get(self):
with self.assertRaises(ValueError):
conf_builtin_get("foo")

with self.assertRaises(ValueError):
conf_builtin_get("confdir", which="badarg")

self.assertIsNotNone(conf_builtin_get("confdir"))
self.assertIsNotNone(conf_builtin_get("confdir", which="intree"))
self.assertIsNotNone(conf_builtin_get("confdir", which="installed"))


if __name__ == "__main__":
unittest.main(testRunner=TAPTestRunner())

0 comments on commit cb2b6f3

Please sign in to comment.