Skip to content

Commit

Permalink
Record() will inform about the way it interprets the code.
Browse files Browse the repository at this point in the history
  • Loading branch information
JasperVanDenBosch committed Feb 18, 2015
1 parent 508c8a8 commit 0638c63
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 34 deletions.
11 changes: 8 additions & 3 deletions niprov/commandline.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@
class Commandline(object):

def fileFound(self, fname, provenance):
template = '{0[path]}'
template = '[provenance] {0[path]}'
print(template.format(provenance))

def missingDependencyForImage(self, lib, fpath):
template = 'Missing python package "{0}" to read file: {1}'
template = '[provenance] Missing python package "{0}" to read file: {1}'
print(template.format(lib, fpath))

def fileError(self, fpath):
print('Error opening file: {0}'.format(fpath))
print('[provenance] Error opening file: {0}'.format(fpath))

def interpretedRecording(self, new, transform, parents):
template = ('[provenance] Recorded the command [{1}] to create [{0}] '+
'based on [{2}]')
print(template.format(new, transform, ', '.join(parents)))

7 changes: 5 additions & 2 deletions niprov/recording.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
# -*- 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, externals=Externals()):
def record(command, new=None, parents=None, transient=False,
externals=Externals(), listener=Commandline()):
"""Execute a command and log it as provenance for the newly created file.
Args:
Expand All @@ -21,7 +23,6 @@ def record(command, new=None, parents=None, transient=False, externals=Externals
Returns:
dict: New provenance
"""
result = externals.run(command)
transformation = command[0]
code = ' '.join(command)
for c in range(len(command)):
Expand All @@ -33,5 +34,7 @@ def record(command, new=None, parents=None, transient=False, externals=Externals
_parents = parents
if new:
_new = new
listener.interpretedRecording(_new, transformation, _parents)
result = externals.run(command)
return log(_new, transformation, _parents, code=code, transient=transient,
logtext=result.output)
57 changes: 28 additions & 29 deletions tests/recording.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,43 @@

class RecordingTests(unittest.TestCase):

def test_Executes_commands(self):
def setUp(self):
import niprov.recording as recording
log = Mock()
recording.log = log
self.log = Mock()
self.sub = Mock()
self.listener = Mock()
recording.log = self.log
self.recording = recording

def record(self, cmd, **kwargs):
self.recording.record(cmd, externals=self.sub, listener=self.listener, **kwargs)

def test_Executes_commands(self):
cmd = ['mytransform','-out','newfile.f','-in','oldfile.f']
sub = Mock()
recording.record(cmd, externals=sub)
sub.run.assert_any_call(cmd)
self.record(cmd)
self.sub.run.assert_any_call(cmd)

def test_Extracts_provenance_from_commands(self):
import niprov.recording as recording
log = Mock()
cmd = ['mytransform','-out','newfile.f','-in','oldfile.f']
sub = Mock()
recording.log = log
recording.record(cmd, externals=sub)
log.assert_called_with('newfile.f','mytransform',['oldfile.f'],
transient=False, code=' '.join(cmd), logtext=sub.run().output)
self.record(cmd)
self.log.assert_called_with('newfile.f','mytransform',['oldfile.f'],
transient=False, code=' '.join(cmd), logtext=self.sub.run().output)

def test_If_parent_or_new_provided_override_parsed(self):
import niprov.recording as recording
log = Mock()
cmd = ['mytransform','-out','newfile.f','-in','oldfile.f']
sub = Mock()
recording.log = log
recording.record(cmd, parents=['customParent'], new='customNew',
externals=sub)
log.assert_called_with('customNew','mytransform',['customParent'],
transient=False, code=' '.join(cmd), logtext=sub.run().output)
self.record(cmd, parents=['customParent'], new='customNew')
self.log.assert_called_with('customNew','mytransform',['customParent'],
transient=False, code=' '.join(cmd), logtext=self.sub.run().output)

def test_Passes_transient_flag(self):
import niprov.recording as recording
log = Mock()
cmd = ['mytransform','-out','newfile.f','-in','oldfile.f']
sub = Mock()
recording.log = log
recording.record(cmd, transient=True, externals=sub)
log.assert_called_with('newfile.f','mytransform',['oldfile.f'],
transient=True, code=' '.join(cmd), logtext=sub.run().output)

self.record(cmd, transient=True)
self.log.assert_called_with('newfile.f','mytransform',['oldfile.f'],
transient=True, code=' '.join(cmd), logtext=self.sub.run().output)

def test_Informs_listener_about_interpretation(self):
cmd = ['mytransform','-out','newfile.f','-in','oldfile.f']
self.record(cmd)
self.listener.interpretedRecording.assert_called_with(
'newfile.f','mytransform',['oldfile.f'])

0 comments on commit 0638c63

Please sign in to comment.