8000 Merge pull request #580 from rcali21/PROV_io · nipype/pydra@5bbb4a9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5bbb4a9

Browse files
authored
Merge pull request #580 from rcali21/PROV_io
WIP-Adding software version retrieval
2 parents f72193e + 4b9df08 commit 5bbb4a9

File tree

2 files changed

+73
-9
lines changed

2 files changed

+73
-9
lines changed

pydra/engine/audit.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,21 +170,48 @@ def audit_check(self, flag):
170170
return self.audit_flags & flag
171171

172172
def audit_task(self, task):
173+
import subprocess as sp
174+
173175
label = task.name
174176
if hasattr(task.inputs, "executable"):
175177
command = task.cmdline
176178
# assume function task
177179
else:
178-
# work on changing this to function name
179180
command = None
181+
# if hasattr(task.inputs, "in_file"):
182+
# input_file = task.inputs.in_file
183+
# else:
184+
# input_file = None
185+
186+
if command is not None:
187+
cmd_name = command.split()[0]
188+
software = f"{cmd_name} --version"
189+
# take the first word of command as the
190+
# name of the executable
191+
# (this may not always be the case)
192+
version_cmd = sp.run(software, shell=True, stdout=sp.PIPE).stdout.decode(
193+
"utf-8"
194+
)
195+
try:
196+
version_cmd = version_cmd.splitlines()[0]
197+
198+
except IndexError:
199+
version_cmd = f"{cmd_name} -- Version unknown"
200+
201+
else:
202+
version_cmd = None
180203

181204
start_message = {
182205
"@id": self.aid,
183206
"@type": "task",
184-
"label": label,
185-
"command": command,
186-
"startedAtTime": now(),
207+
"Label": label,
208+
"Command": command,
209+
"StartedAtTime": now(),
210+
"AssociatedWith": version_cmd,
187211
}
212+
213+
# new code to be added here for i/o tracking - WIP
214+
188215
self.audit_message(start_message, AuditFlag.PROV)
189216

190217
# add more fields according to BEP208 doc

pydra/engine/tests/test_task.py

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,9 +1003,12 @@ def testfunc(a: int, b: float = 0.1) -> ty.NamedTuple("Output", [("out", float)]
10031003
for file in glob(str(message_path) + "/*.jsonld"):
10041004
with open(file, "r") as f:
10051005
data = json.load(f)
1006-
if "label" in data:
1006+
print(data)
1007+
if "Label" in data:
10071008
json_content.append(True)
1008-
assert "testfunc" == data["label"]
1009+
assert "testfunc" == data["Label"]
1010+
if "AssociatedWith" in data:
1011+
assert None == data["AssociatedWith"]
10091012
assert any(json_content)
10101013

10111014

@@ -1031,16 +1034,50 @@ def test_audit_shellcommandtask(tmpdir):
10311034
for file in glob(str(message_path) + "/*.jsonld"):
10321035
with open(file, "r") as f:
10331036
data = json.load(f)
1034-
if "label" in data:
1037+
print(data)
1038+
if "Label" in data:
10351039
label_content.append(True)
1036-
if "command" in data:
1040+
if "Command" in data:
10371041
command_content.append(True)
1038-
assert "ls -l" == data["command"]
1042+
assert "ls -l" == data["Command"]
10391043

10401044
print(command_content)
10411045
assert any(label_content)
10421046

10431047

1048+
def test_audit_shellcommandtask_version(tmpdir):
1049+
import subprocess as sp
1050+
1051+
version_cmd = sp.run("less --version", shell=True, stdout=sp.PIPE).stdout.decode(
1052+
"utf-8"
1053+
)
1054+
version_cmd = version_cmd.splitlines()[0]
1055+
cmd = "less"
1056+
shelly = ShellCommandTask(
1057+
name="shelly",
1058+
executable=cmd,
1059+
args="test_task.py",
1060+
audit_flags=AuditFlag.PROV,
1061+
messengers=FileMessenger(),
1062+
)
1063+
1064+
import glob
1065+
1066+
shelly.cache_dir = tmpdir
1067+
shelly()
1068+
message_path = tmpdir / shelly.checksum / "messages"
1069+
# go through each jsonld file in message_path and check if the label field exists
1070+
version_content = []
1071+
for file in glob.glob(str(message_path) + "/*.jsonld"):
1072+
with open(file, "r") as f:
1073+
data = json.load(f)
1074+
if "AssociatedWith" in data:
1075+
if version_cmd in data["AssociatedWith"]:
1076+
version_content.append(True)
1077+
1078+
assert any(version_content)
1079+
1080+
10441081
def test_audit_prov_messdir_1(tmpdir, use_validator):
10451082
"""customized messenger dir"""
10461083

0 commit comments

Comments
 (0)
0