Skip to content

Commit c022768

Browse files
committed
Add get_petsc_dirs utility function
1 parent 7551fed commit c022768

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

petsctools/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
get_config,
44
get_petsc_dir,
55
get_petsc_arch,
6+
get_petsc_dirs,
67
get_petscvariables,
78
get_petscconf_h,
89
get_external_packages,

petsctools/config.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,47 @@ def get_petsc_arch():
3636
return get_config()["PETSC_ARCH"]
3737

3838

39+
def get_petsc_dirs(
40+
*,
41+
prefix: str = "",
42+
subdir: str | None = None,
43+
) -> tuple[str, str]:
44+
"""Return $PETSC_DIR and $PETSC_DIR/$PETSC_ARCH.
45+
46+
This function is useful for generating compiler arguments. For example
47+
48+
.. code-block:: python
49+
50+
get_petsc_dirs(subdir="include", prefix="-I")
51+
52+
will return
53+
54+
.. code-block:: python
55+
56+
("-I/path/to/petsc/include", "-I/path/to/petsc/mypetscarch/include")
57+
58+
Parameters
59+
----------
60+
prefix :
61+
Optional prefix to prepend to the paths.
62+
subdir :
63+
Optional subdirectory to include in the returned paths.
64+
65+
Returns
66+
-------
67+
tuple :
68+
``$PETSC_DIR`` and ``$PETSC_DIR/$PETSC_ARCH`` with appropriate
69+
sub-directories and prefixes.
70+
71+
"""
72+
dirs = []
73+
for path in [[get_petsc_dir()], [get_petsc_dir(), get_petsc_arch()]]:
74+
if subdir:
75+
path.append(subdir)
76+
dirs.append(f"{prefix}{os.path.join(*path)}")
77+
return tuple(dirs)
78+
79+
3980
@functools.lru_cache()
4081
def get_petscvariables():
4182
"""Return PETSc's configuration information."""

tests/test_config.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@ def test_get_config():
2020
)
2121

2222

23+
def test_get_petsc_dirs():
24+
petsc_dir = petsctools.get_petsc_dir()
25+
petsc_arch = petsctools.get_petsc_arch()
26+
27+
expected = (petsc_dir, f"{petsc_dir}/{petsc_arch}")
28+
assert petsctools.get_petsc_dirs() == expected
29+
30+
expected = (
31+
f"PREFIX{petsc_dir}/SUBDIR", f"PREFIX{petsc_dir}/{petsc_arch}/SUBDIR"
32+
)
33+
assert (
34+
petsctools.get_petsc_dirs(prefix="PREFIX", subdir="SUBDIR") == expected
35+
)
36+
37+
2338
def test_get_petscvariables():
2439
petsctools.get_petscvariables()
2540

0 commit comments

Comments
 (0)