forked from flux-framework/flux-core
-
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.
Merge pull request flux-framework#6486 from grondo/python-conf-builtin
add `flux.conf_builtin.conf_builtin_get()` to give Python access to compiled-in config values
- Loading branch information
Showing
5 changed files
with
105 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
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,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}'") |
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
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
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,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()) |