From 0d7b44a3f5733dd0d3a37e85fbc3228abb436b9d Mon Sep 17 00:00:00 2001 From: Olivier Mangin Date: Fri, 30 Mar 2018 18:46:53 -0400 Subject: [PATCH 1/2] Have version defined in only one place (fixes #86). --- pubs/__init__.py | 2 +- pubs/version.py | 1 + setup.py | 5 +++-- 3 files changed, 5 insertions(+), 3 deletions(-) create mode 100644 pubs/version.py diff --git a/pubs/__init__.py b/pubs/__init__.py index a71c5c7..58f3ace 100644 --- a/pubs/__init__.py +++ b/pubs/__init__.py @@ -1 +1 @@ -__version__ = '0.7.0' +from .version import __version__ diff --git a/pubs/version.py b/pubs/version.py new file mode 100644 index 0000000..a71c5c7 --- /dev/null +++ b/pubs/version.py @@ -0,0 +1 @@ +__version__ = '0.7.0' diff --git a/setup.py b/setup.py index 62a9d3f..d9ff787 100644 --- a/setup.py +++ b/setup.py @@ -2,11 +2,12 @@ from setuptools import setup -VERSION = '0.7.0' +with open('pubs/version.py') as f: + exec(f.read()) # defines __version__ setup( name='pubs', - version=VERSION, + version=__version__, author='Fabien Benureau, Olivier Mangin, Jonathan Grizou', author_email='fabien.benureau@gmail.com', maintainer='Olivier Mangin', From 801e0c035fca28785610302153a9c6590d8b82ec Mon Sep 17 00:00:00 2001 From: Olivier Mangin Date: Fri, 30 Mar 2018 19:16:23 -0400 Subject: [PATCH 2/2] Adds code version to cache. (Fixes #129) Current code version is added to the cache on writting. If the cache version does not match the code version on read, the cache is ignored which means it resets. --- pubs/databroker.py | 11 ++++++++--- pubs/datacache.py | 2 -- pubs/version.py | 2 +- tests/test_databroker.py | 20 +++++++++++++++++++- tests/test_datacache.py | 1 - 5 files changed, 28 insertions(+), 8 deletions(-) diff --git a/pubs/databroker.py b/pubs/databroker.py index d4a5274..5bf02ef 100644 --- a/pubs/databroker.py +++ b/pubs/databroker.py @@ -1,6 +1,7 @@ from . import filebroker from . import endecoder from .p3 import pickle +from . import __version__ class DataBroker(object): @@ -22,12 +23,16 @@ class DataBroker(object): pass def pull_cache(self, name): - """Load cache data from distk. Exceptions are handled by the caller.""" + """Load cache data from disk. Exceptions are handled by the caller.""" data_raw = self.filebroker.pull_cachefile(name) - return pickle.loads(data_raw) + cache = pickle.loads(data_raw) + if cache['version'] != __version__: + raise ValueError('Cache not matching code version.') + return cache['data'] def push_cache(self, name, data): - data_raw = pickle.dumps(data) + cache_content = {'version': __version__, 'data': data} + data_raw = pickle.dumps(cache_content) self.filebroker.push_cachefile(name, data_raw) # filebroker+endecoder diff --git a/pubs/datacache.py b/pubs/datacache.py index f9b4960..2da4fe0 100644 --- a/pubs/datacache.py +++ b/pubs/datacache.py @@ -91,8 +91,6 @@ class DataCache(object): 2. Keeps an up-to-date, pickled version of the repository, to speed up things when they are a lot of files. Update are also done only when required. Changes are detected using data modification timestamps. - - For the moment, only (1) is implemented. """ def __init__(self, pubsdir, docsdir, create=False): self.pubsdir = pubsdir diff --git a/pubs/version.py b/pubs/version.py index a71c5c7..1ffac67 100644 --- a/pubs/version.py +++ b/pubs/version.py @@ -1 +1 @@ -__version__ = '0.7.0' +__version__ = '0.8.dev1' diff --git a/tests/test_databroker.py b/tests/test_databroker.py index 49c46a7..57e5c66 100644 --- a/tests/test_databroker.py +++ b/tests/test_databroker.py @@ -16,7 +16,6 @@ class TestDataBroker(fake_env.TestFakeFs): def test_databroker(self): - ende = endecoder.EnDecoder() page99_metadata = ende.decode_metadata(str_fixtures.metadata_raw0) page99_bibentry = ende.decode_bibdata(str_fixtures.bibtex_raw0) @@ -71,6 +70,25 @@ class TestDataBroker(fake_env.TestFakeFs): db.remove_doc('docsdir://Page99.pdf') + def test_push_pull_cache(self): + db = databroker.DataBroker('tmp', 'tmp/doc', create=True) + data_in = {'a': 1} + db.push_cache('meta', data_in) + data_out = db.pull_cache('meta') + self.assertEqual(data_in, data_out) + + def test_pull_cache_fails_on_version_mismatch(self): + db = databroker.DataBroker('tmp', 'tmp/doc', create=True) + data_in = {'a': 1} + db.push_cache('meta', data_in) + ver = databroker.__version__ + databroker.__version__ = '0.0.0' + try: + with self.assertRaises(ValueError): + db.pull_cache('meta') + finally: + databroker.__version__ = ver + if __name__ == '__main__': unittest.main() diff --git a/tests/test_datacache.py b/tests/test_datacache.py index 7e95f81..4709aca 100644 --- a/tests/test_datacache.py +++ b/tests/test_datacache.py @@ -126,7 +126,6 @@ class TestCacheEntrySet(unittest.TestCase): value = self.bibcache.pull('a') self.assertEqual(value, 'b') - def test_is_outdated_when_unknown_citekey(self): self.assertTrue(self.metacache._is_outdated('a'))