Skip to content

Commit eeb76e8

Browse files
committed
RF: Use importlib[._]resources to access resources, not __file__
1 parent 9f9b27a commit eeb76e8

File tree

6 files changed

+46
-27
lines changed

6 files changed

+46
-27
lines changed

pydra/engine/audit.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
"""Module to keep track of provenance information."""
22
import os
3-
from pathlib import Path
43
import json
54
import attr
65
from ..utils.messenger import send_message, make_message, gen_uuid, now, AuditFlag
76
from .helpers import ensure_list, gather_runtime_info, hash_file
87
from .specs import attr_fields, File, Directory
98

9+
try:
10+
import importlib_resources
11+
except ImportError:
12+
import importlib.resources as importlib_resources # type: ignore
13+
1014

1115
class Audit:
1216
"""Handle provenance tracking and resource utilization."""
@@ -133,9 +137,8 @@ def audit_message(self, message, flags=None):
133137
134138
"""
135139
if self.develop:
136-
with open(
137-
Path(os.path.dirname(__file__)) / ".." / "schema/context.jsonld"
138-
) as fp:
140+
context_file = importlib_resources.files("pydra") / "schema/context.jsonld"
141+
with context_file.open() as fp:
139142
context = json.load(fp)
140143
else:
141144
context = {

pydra/engine/tests/conftest.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pytest
2+
3+
try:
4+
import importlib_resources
5+
except ImportError:
6+
import importlib.resources as importlib_resources
7+
8+
9+
@pytest.fixture(scope="package")
10+
def data_tests_dir():
11+
test_nii = importlib_resources.files("pydra").joinpath(
12+
"engine", "tests", "data_tests"
13+
)
14+
with importlib_resources.as_file(test_nii) as path:
15+
yield path

pydra/engine/tests/test_boutiques.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
reason="requires docker and bosh",
1919
)
2020

21-
Infile = Path(__file__).resolve().parent / "data_tests" / "test.nii.gz"
22-
2321
pytestmark = pytest.mark.skip()
2422

2523

@@ -30,10 +28,10 @@
3028
"maskfile", ["test_brain.nii.gz", "test_brain", "test_brain.nii"]
3129
)
3230
@pytest.mark.parametrize("results_function", [result_no_submitter, result_submitter])
33-
def test_boutiques_1(maskfile, plugin, results_function, tmpdir):
31+
def test_boutiques_1(maskfile, plugin, results_function, tmpdir, data_tests_dir):
3432
"""simple task to run fsl.bet using BoshTask"""
3533
btask = BoshTask(name="NA", zenodo_id="1482743")
36-
btask.inputs.infile = Infile
34+
btask.inputs.infile = data_tests_dir / "test.nii.gz"
3735
btask.inputs.maskfile = maskfile
3836
btask.cache_dir = tmpdir
3937
res = results_function(btask, plugin)
@@ -50,12 +48,12 @@ def test_boutiques_1(maskfile, plugin, results_function, tmpdir):
5048
@no_win
5149
@need_bosh_docker
5250
@pytest.mark.flaky(reruns=3)
53-
def test_boutiques_spec_1():
51+
def test_boutiques_spec_1(data_tests_dir):
5452
"""testing spec: providing input/output fields names"""
5553
btask = BoshTask(
5654
name="NA",
5755
zenodo_id="1482743",
58-
infile=Infile,
56+
infile=data_tests_dir / "test.nii.gz",
5957
maskfile="test_brain.nii.gz",
6058
input_spec_names=["infile", "maskfile"],
6159
output_spec_names=["outfile", "out_outskin_off"],
@@ -75,12 +73,12 @@ def test_boutiques_spec_1():
7573
@no_win
7674
@need_bosh_docker
7775
@pytest.mark.flaky(reruns=3)
78-
def test_boutiques_spec_2():
76+
def test_boutiques_spec_2(data_tests_dir):
7977
"""testing spec: providing partial input/output fields names"""
8078
btask = BoshTask(
8179
name="NA",
8280
zenodo_id="1482743",
83-
infile=Infile,
81+
infile=data_tests_dir / "test.nii.gz",
8482
maskfile="test_brain.nii.gz",
8583
input_spec_names=["infile"],
8684
output_spec_names=[],
@@ -101,11 +99,11 @@ def test_boutiques_spec_2():
10199
@pytest.mark.parametrize(
102100
"maskfile", ["test_brain.nii.gz", "test_brain", "test_brain.nii"]
103101
)
104-
def test_boutiques_wf_1(maskfile, plugin, tmpdir):
102+
def test_boutiques_wf_1(maskfile, plugin, tmpdir, infile):
105103
"""wf with one task that runs fsl.bet using BoshTask"""
106104
wf = Workflow(name="wf", input_spec=["maskfile", "infile"])
107105
wf.inputs.maskfile = maskfile
108-
wf.inputs.infile = Infile
106+
wf.inputs.infile = infile
109107
wf.cache_dir = tmpdir
110108

111109
wf.add(
@@ -134,11 +132,11 @@ def test_boutiques_wf_1(maskfile, plugin, tmpdir):
134132
@pytest.mark.parametrize(
135133
"maskfile", ["test_brain.nii.gz", "test_brain", "test_brain.nii"]
136134
)
137-
def test_boutiques_wf_2(maskfile, plugin, tmpdir):
135+
def test_boutiques_wf_2(maskfile, plugin, tmpdir, infile):
138136
"""wf with two BoshTasks (fsl.bet and fsl.stats) and one ShellTask"""
139137
wf = Workflow(name="wf", input_spec=["maskfile", "infile"])
140138
wf.inputs.maskfile = maskfile
141-
wf.inputs.infile = Infile
139+
wf.inputs.infile = infile
142140
wf.cache_dir = tmpdir
143141

144142
wf.add(

pydra/engine/tests/test_dockertask.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -509,19 +509,17 @@ def test_wf_docker_1_dockerflag(plugin, tmpdir):
509509
@no_win
510510
@need_docker
511511
@pytest.mark.skip(reason="we probably don't want to support bindings as an input")
512-
def test_wf_docker_2pre(plugin, tmpdir):
512+
def test_wf_docker_2pre(plugin, tmpdir, data_tests_dir):
513513
"""a workflow with two connected task that run python scripts
514514
the first one creates a text file and the second one reads the file
515515
"""
516516

517-
scripts_dir = os.path.join(os.path.dirname(__file__), "data_tests")
518-
519517
cmd1 = ["python", "/scripts/saving.py", "-f", "/outputs/tmp.txt"]
520518
dt = DockerTask(
521519
name="save",
522520
image="python:3.7-alpine",
523521
executable=cmd1,
524-
bindings=[(str(tmpdir), "/outputs"), (scripts_dir, "/scripts", "ro")],
522+
bindings=[(str(tmpdir), "/outputs"), (str(data_tests_dir), "/scripts", "ro")],
525523
strip=True,
526524
)
527525
res = dt(plugin=plugin)
@@ -531,13 +529,11 @@ def test_wf_docker_2pre(plugin, tmpdir):
531529
@no_win
532530
@need_docker
533531
@pytest.mark.skip(reason="we probably don't want to support bindings as an input")
534-
def test_wf_docker_2(plugin, tmpdir):
532+
def test_wf_docker_2(plugin, tmpdir, data_tests_dir):
535533
"""a workflow with two connected task that run python scripts
536534
the first one creates a text file and the second one reads the file
537535
"""
538536

539-
scripts_dir = os.path.join(os.path.dirname(__file__), "data_tests")
540-
541537
wf = Workflow(name="wf", input_spec=["cmd1", "cmd2"])
542538
wf.inputs.cmd1 = ["python", "/scripts/saving.py", "-f", "/outputs/tmp.txt"]
543539
wf.inputs.cmd2 = ["python", "/scripts/loading.py", "-f"]
@@ -546,7 +542,10 @@ def test_wf_docker_2(plugin, tmpdir):
546542
name="save",
547543
image="python:3.7-alpine",
548544
executable=wf.lzin.cmd1,
549-
bindings=[(str(tmpdir), "/outputs"), (scripts_dir, "/scripts", "ro")],
545+
bindings=[
546+
(str(tmpdir), "/outputs"),
547+
(str(data_tests_dir), "/scripts", "ro"),
548+
],
550549
strip=True,
551550
)
552551
)
@@ -556,7 +555,10 @@ def test_wf_docker_2(plugin, tmpdir):
556555
image="python:3.7-alpine",
557556
executable=wf.lzin.cmd2,
558557
args=wf.save.lzout.stdout,
559-
bindings=[(str(tmpdir), "/outputs"), (scripts_dir, "/scripts", "ro")],
558+
bindings=[
559+
(str(tmpdir), "/outputs"),
560+
(str(data_tests_dir), "/scripts", "ro"),
561+
],
560562
strip=True,
561563
)
562564
)

pydra/engine/tests/test_shelltask.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4157,7 +4157,7 @@ def no_fsl():
41574157

41584158

41594159
@pytest.mark.skipif(no_fsl(), reason="fsl is not installed")
4160-
def test_fsl():
4160+
def test_fsl(data_tests_dir):
41614161
"""mandatory field added to fields, value provided"""
41624162

41634163
_xor_inputs = [
@@ -4385,7 +4385,7 @@ def change_name(file):
43854385
)
43864386

43874387
# TODO: not sure why this has to be string
4388-
in_file = Path(__file__).parent / "data_tests" / "test.nii.gz"
4388+
in_file = data_tests_dir / "test.nii.gz"
43894389

43904390
# separate command into exec + args
43914391
shelly = ShellCommandTask(

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ dependencies = [
1212
"cloudpickle >=2.0.0",
1313
"etelemetry >=0.2.2",
1414
"filelock >=3.0.0",
15+
"importlib_resources >=5.7; python_version < '3.11'",
1516
]
1617
license = {file = "LICENSE"}
1718
authors = [

0 commit comments

Comments
 (0)