Skip to content

Commit

Permalink
Merge branch 'master' of github.com:recipy/recipy
Browse files Browse the repository at this point in the history
  • Loading branch information
robintw committed Sep 24, 2016
2 parents 243efae + 0c1a526 commit 70aa35d
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 8 deletions.
39 changes: 38 additions & 1 deletion recipy/log.py
Expand Up @@ -67,7 +67,8 @@ def log_init():
"date": datetime.datetime.utcnow(),
"command_args": " ".join(cmd_args),
"warnings": [],
"libraries": [get_version('recipy')]
"libraries": [get_version('recipy')],
"custom_values": {}
}

if not option_set('ignored metadata', 'git'):
Expand Down Expand Up @@ -99,6 +100,30 @@ def log_init():
sys.excepthook = log_exception


def log_values(custom_values=None, **kwargs):
""" Log a custom value-key pairs into the database
e.g,
>>> log_values(a=1, b=2)
>>> log_values({'c': 3, 'd': 4})
>>> log_values({'e': 5, 'f': 6}, g=7, h=8)
"""

# create dictionary of custom values from arguments
custom_values = {} if custom_values is None else custom_values
assert isinstance(custom_values, dict), \
"custom_values must be a dict. type(custom_values) = %s" % type(custom_values)
custom_values.update(kwargs)

# debugging
if option_set('general', 'debug'):
print('Logging custom values: %s' % str(custom_values))

# Update object in DB
db = open_or_create_db()
db.update(add_dict("custom_values", custom_values), eids=[RUN_ID])
db.close()


def log_input(filename, source):
"""Log input to the database.
Expand Down Expand Up @@ -223,6 +248,18 @@ def transform(element):
return transform


def add_dict(field, dict_of_values):
"""
Add a given dict of values to a given array field.
"""
def transform(element):
assert isinstance(element[field], dict), \
"add_dict called on a non-dict object. type(element[%s]) = %s" % (field, type(element[field]))
element[field].update(dict_of_values)

return transform


# atexit functions will run on script exit (even on exception)
@atexit.register
def log_exit():
Expand Down
61 changes: 61 additions & 0 deletions recipy/test/test_log.py
@@ -0,0 +1,61 @@
import os
import unittest
import mock

from recipy.log import open_or_create_db, log_values, log_init


TEST_DB_PATH = os.path.expanduser('~/.recipy/test_recipyDB.json')


def open_or_create_test_db():
return open_or_create_db(TEST_DB_PATH)


class TestLog(unittest.TestCase):
def setUp(self):
""" Invoke log_init with the test database, so as not to interfere with the regular database """
with mock.patch('recipy.log.open_or_create_db', open_or_create_test_db):
log_init()

def test_log_values_single_dictionary(self):
with mock.patch('recipy.log.open_or_create_db', open_or_create_test_db):
log_values({'a': 1, 'b': 2})

db = open_or_create_test_db()
last_entry = db.all()[-1]

self.assertIn('custom_values', last_entry)
self.assertEquals(last_entry['custom_values'], {'a': 1, 'b': 2})

def test_log_values_multiple_dictionaries(self):
with mock.patch('recipy.log.open_or_create_db', open_or_create_test_db):
log_values({'a': 1, 'b': 2})
log_values({'a': 3, 'b': 4})
log_values({'c': 5, 'd': 6})

db = open_or_create_test_db()
last_entry = db.all()[-1]

self.assertIn('custom_values', last_entry)
self.assertEquals(last_entry['custom_values'], {'a': 3, 'b': 4, 'c': 5, 'd': 6})

def test_log_values_keyword_arguments(self):
with mock.patch('recipy.log.open_or_create_db', open_or_create_test_db):
log_values(cat=1, dog=2)

db = open_or_create_test_db()
last_entry = db.all()[-1]

self.assertIn('custom_values', last_entry)
self.assertEquals(last_entry['custom_values'], {'cat': 1, 'dog': 2})

def test_log_values_dict_and_keyword_arguments(self):
with mock.patch('recipy.log.open_or_create_db', open_or_create_test_db):
log_values({'bananas': 3, 'pears': 4}, apples=1, oranges=2)

db = open_or_create_test_db()
last_entry = db.all()[-1]

self.assertIn('custom_values', last_entry)
self.assertEquals(last_entry['custom_values'], {'bananas': 3, 'pears': 4, 'apples':1, 'oranges':2})
13 changes: 6 additions & 7 deletions recipyCommon/version_control.py
Expand Up @@ -46,28 +46,28 @@ def add_git_info(run, scriptpath):
# We can't store git info for some reason, so just skip it
pass


# The released version of PySvn doesn't do local diffs yet, so we have to do
# it the hard way...

class SvnException(Exception):
pass


def svn_diff(path):
"""
Run svn diff for the given path and return the result. Raise SvnException
if the command doesn't return 0.
"""
cmd = ["svn", "diff", path]
p = subprocess.Popen(cmd,
stdout = subprocess.PIPE,
stderr = subprocess.STDOUT,
env={"LANG" : "en_US.UTF-8"})
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
env={"LANG": "en_US.UTF-8"})
stdout = p.stdout.read()
r = p.wait()
if r != 0:
raise SvnException("SVN Command exited with status code {0}".format(r))
return stdout.decode()


def add_svn_info(run, scriptpath):
"""
Add information about the svn repository holding the source file to the
Expand All @@ -80,7 +80,6 @@ def add_svn_info(run, scriptpath):
run["svncommit"] = svn_info["commit_revision"]
if not option_set('ignored metadata', 'diff'):
run['diff'] = svn_diff(svn_info["wc-info/wcroot-abspath"])
except (SvnException, ValueError):
except (SvnException, ValueError, OSError):
# We can't access svn info for some reason, so just skip it
pass

0 comments on commit 70aa35d

Please sign in to comment.