Skip to content

Commit aabaacc

Browse files
dalcinlrdbisme
authored andcommitted
python: Install data files as package data
1 parent 41c79b1 commit aabaacc

File tree

5 files changed

+28
-15
lines changed

5 files changed

+28
-15
lines changed

.github/workflows/test_python.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,4 @@ jobs:
2727
run: python -m pip install ".[test]"
2828

2929
- name: Run tests
30-
run: >
31-
export MPP_DIRECTORY=$(pwd) &&
32-
export MPP_DATA_DIRECTORY=$MPP_DIRECTORY/data &&
33-
pytest
30+
run: pytest

.github/workflows/wheels.yml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ jobs:
2020
run: python -m pip install ".[test]"
2121

2222
- name: Run tests
23-
run: >
24-
export MPP_DIRECTORY=$(pwd) &&
25-
export MPP_DATA_DIRECTORY=$MPP_DIRECTORY/data &&
26-
pytest
23+
run: pytest
2724

2825
test_mac:
2926
runs-on: macos-latest
@@ -40,10 +37,7 @@ jobs:
4037
run: python -m pip install ".[test]"
4138

4239
- name: Run tests
43-
run: >
44-
export MPP_DIRECTORY=$(pwd) &&
45-
export MPP_DATA_DIRECTORY=$MPP_DIRECTORY/data &&
46-
pytest
40+
run: pytest
4741

4842
linux_wheels:
4943
strategy:

interface/python/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ target_link_libraries(_mutationpp PRIVATE mutation++)
1414
install(DIRECTORY mutationpp DESTINATION .)
1515

1616
install(TARGETS _mutationpp DESTINATION mutationpp)
17+
18+
install(DIRECTORY "${CMAKE_SOURCE_DIR}/data" DESTINATION mutationpp)

interface/python/src/pyGlobalOptions.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,26 @@ namespace py = pybind11;
77
* Python wrapper definition for the GlobalOptions class.
88
*/
99

10+
namespace {
11+
12+
inline void setDefaultDataDirectory(const std::string& datadir) {
13+
if (std::getenv("MPP_DATA_DIRECTORY") == nullptr) {
14+
Mutation::GlobalOptions::dataDirectory(datadir);
15+
}
16+
};
17+
18+
}
19+
1020
void py_export_GlobalOptions(py::module &m) {
1121

22+
const py::object resources = py::module_::import("importlib.resources");
23+
const py::object pkgname = m.attr("__spec__").attr("parent");
24+
const py::object rootdir = resources.attr("files")(pkgname);
25+
const py::object datadir = rootdir / py::str("data");
26+
const std::string data_directory = py::str(datadir).cast<std::string>();
27+
28+
setDefaultDataDirectory(data_directory);
29+
1230
/**
1331
* Overloaded member functions wrappers
1432
*/
@@ -29,8 +47,9 @@ void py_export_GlobalOptions(py::module &m) {
2947
.def_static("workingDirectory", []() {
3048
return Mutation::GlobalOptions::workingDirectory();
3149
})
32-
.def_static("reset", []() {
50+
.def_static("reset", [data_directory]() {
3351
Mutation::GlobalOptions::reset();
52+
setDefaultDataDirectory(data_directory);
3453
})
3554
;
3655
}

tests/python/test_options.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import unittest.mock as mock
55
import mutationpp as mpp
66

7-
INITIAL_DATADIR = os.environ.get("MPP_DATA_DIRECTORY", "")
7+
PACKAGE_DATADIR = os.fspath(importlib.resources.files(mpp) / "data")
8+
INITIAL_DATADIR = os.environ.get("MPP_DATA_DIRECTORY", PACKAGE_DATADIR)
89

910

1011
def test_options_datadir_initial():
@@ -62,6 +63,6 @@ def test_options_reset(datadir):
6263
with mock.patch.dict(os.environ, clear=True):
6364
mpp.GlobalOptions.reset()
6465
set_datadir = mpp.GlobalOptions.dataDirectory()
65-
assert set_datadir == ""
66+
assert set_datadir == PACKAGE_DATADIR
6667
set_workdir = mpp.GlobalOptions.workingDirectory()
6768
assert set_workdir == ""

0 commit comments

Comments
 (0)