-
Notifications
You must be signed in to change notification settings - Fork 0
/
recording.py
58 lines (51 loc) · 2.12 KB
/
recording.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from niprov.externals import Externals
from niprov.logging import log
from niprov.commandline import Commandline
def record(command, new=None, parents=None, transient=False, args=[], kwargs={},
externals=Externals(), listener=Commandline()):
"""Execute a command and log it as provenance for the newly created file.
Args:
command (list or str or callable): Command to be executed. Either a
string of executable system code, a list of components thereof, or
a python function object.
new (str, optional): Override path to the new file, i.e. if it
cannot be parsed from the command.
parents (list, optional): Override paths to parent files, i.e. if they
cannot be parsed from the command.
transient (bool, optional): Set this to True to indicate that the file
is only temporary and future checks should not expect it to be
physically present. Defaults to False, assuming that the file
remains.
args (list, optional): Positional arguments to be passed to command.
kwargs (dict, optional): Keyword arguments to be passed to command.
Returns:
dict: New provenance
"""
if isinstance(command, basestring):
command = command.split()
if isinstance(command, (list, tuple)):
transformation = command[0]
code = ' '.join(command)
for c in range(len(command)):
if command[c] in ['-out']:
_new = command[c+1]
if command[c] in ['-in']:
_parents = [command[c+1]]
else:
transformation = command.func_name
code = None
if parents is not None:
_parents = parents
if new:
_new = new
listener.interpretedRecording(_new, transformation, _parents)
if isinstance(command, (list, tuple)):
result = externals.run(command)
output = result.output
else:
command(*args, **kwargs)
output = None
return log(_new, transformation, _parents, code=code, transient=transient,
logtext=output)