diff --git a/bagit_profile.py b/bagit_profile.py index 5b419e1..bb44266 100755 --- a/bagit_profile.py +++ b/bagit_profile.py @@ -112,16 +112,19 @@ def get_profile(self): # are validated in validate_serialization(). def validate(self, bag): self.report = ProfileValidationReport() - for (fn, msg) in [ - (self.validate_bag_info, 'Error in bag-info.txt'), - (self.validate_manifests_required, 'Required manifests not found'), - (self.validate_tag_manifests_required, 'Required tag manifests not found'), - (self.validate_tag_files_required, 'Required tag files not found'), - (self.validate_tag_files_allowed, 'Tag files not allowed'), - (self.validate_allow_fetch, 'fetch.txt is present but is not allowed'), - (self.validate_accept_bagit_version, 'Required BagIt version not found'), + for (fn, msg, min_version) in [ + (self.validate_bag_info, 'Error in bag-info.txt', None), + (self.validate_manifests_required, 'Required manifests not found', None), + (self.validate_tag_manifests_required, 'Required tag manifests not found', None), + (self.validate_tag_files_required, 'Required tag files not found', None), + (self.validate_allow_fetch, 'fetch.txt is present but is not allowed', None), + (self.validate_accept_bagit_version, 'Required BagIt version not found', None), + (self.validate_tag_files_allowed, 'Tag files not allowed', (1, 2, 0)), ]: try: + if min_version and self.profile_version_info < min_version: + logging.info("Skipping %s introduced in version %s (version validated: %s)", fn, min_version, self.profile_version_info) + continue fn(bag) except ProfileValidationError as e: # self._warn("%s: %s" % (msg, e)) @@ -136,6 +139,11 @@ def validate_bagit_profile(self, profile): profile['Serialization'] = 'optional' if 'Allow-Fetch.txt' not in profile: profile['Allow-Fetch.txt'] = True + if 'BagIt-Profile-Info' in profile and 'BagIt-Profile-Version' in profile['BagIt-Profile-Info']: + profile_version = profile['BagIt-Profile-Info']['BagIt-Profile-Version'] + else: + profile_version = '1.1.0' + self.profile_version_info = tuple(int(i) for i in profile_version.split(".")) self.validate_bagit_profile_info(profile) self.validate_bagit_profile_accept_bagit_versions(profile) diff --git a/fixtures/bagProfileBar.json b/fixtures/bagProfileBar.json index f817bdd..ee5f5b2 100644 --- a/fixtures/bagProfileBar.json +++ b/fixtures/bagProfileBar.json @@ -1,6 +1,7 @@ { "BagIt-Profile-Info":{ "BagIt-Profile-Identifier":"http://canadiana.org/standards/bagit/tdr_ingest.json", + "BagIt-Profile-Version": "1.2.0", "Source-Organization":"Candiana.org", "Contact-Name":"William Wueppelmann", "Contact-Email":"tdr@canadiana.com", diff --git a/fixtures/test-tag-files-allowed/profile.json b/fixtures/test-tag-files-allowed/profile.json index f891299..30bd5e9 100644 --- a/fixtures/test-tag-files-allowed/profile.json +++ b/fixtures/test-tag-files-allowed/profile.json @@ -1,6 +1,7 @@ { "BagIt-Profile-Info": { "BagIt-Profile-Identifier": "TEST", + "BagIt-Profile-Version": "1.2.0", "Source-Organization": "bagit-profiles.py", "Contact-Name": "John Doe", "Contact-Email": "johndoe@example.org", diff --git a/test.py b/test.py index abf8521..b282421 100644 --- a/test.py +++ b/test.py @@ -5,7 +5,7 @@ from unittest import TestCase, main from shutil import rmtree, copytree -import bagit +from bagit import Bag from bagit_profile import Profile, find_tag_files PROFILE_URL = 'https://raw.github.com/bagit-profiles/bagit-profiles/master/bagProfileBar.json' @@ -28,7 +28,7 @@ def setUp(self): def test_not_given(self): profile = Profile('TEST', self.profile_dict) - bag = bagit.Bag(self.bagdir) + bag = Bag(self.bagdir) result = profile.validate(bag) self.assertTrue(result) @@ -37,7 +37,7 @@ def test_required_not_allowed(self): self.profile_dict["Tag-Files-Required"] = ['tag-foo'] with open(join(self.bagdir, 'tag-foo'), 'w'): pass profile = Profile('TEST', self.profile_dict) - result = profile.validate(bagit.Bag(self.bagdir)) + result = profile.validate(Bag(self.bagdir)) self.assertFalse(result) self.assertEqual(len(profile.report.errors), 1) self.assertTrue('Required tag files' in profile.report.errors[0].value) @@ -46,7 +46,7 @@ def test_existing_not_allowed(self): self.profile_dict["Tag-Files-Allowed"] = [] with open(join(self.bagdir, 'tag-foo'), 'w'): pass profile = Profile('TEST', self.profile_dict) - result = profile.validate(bagit.Bag(self.bagdir)) + result = profile.validate(Bag(self.bagdir)) self.assertFalse(result) self.assertEqual(len(profile.report.errors), 1) self.assertTrue("Existing tag file" in profile.report.errors[0].value) @@ -63,14 +63,20 @@ def test_profile_kwarg(self): profile_url = Profile(PROFILE_URL) profile_dict = Profile(PROFILE_URL, profile=self.profile_dict) profile_str = Profile(PROFILE_URL, profile=self.profile_str) - self.maxDiff = None self.assertEqual(json.dumps(profile_str.profile), json.dumps(profile_dict.profile), 'Loaded from string') self.assertEqual(json.dumps(profile_url.profile), json.dumps(profile_dict.profile), 'Loaded from URL') + def testVersionInfo(self): + profile = Profile(PROFILE_URL, profile=self.profile_dict) + self.assertEqual(profile.profile_version_info, (1, 2, 0), 'Bundled: 1.2.0') + del(self.profile_dict['BagIt-Profile-Info']['BagIt-Profile-Version']) + profile = Profile(PROFILE_URL, profile=self.profile_dict) + self.assertEqual(profile.profile_version_info, (1, 1, 0), 'Default profile version 1.1.0') + class Test_bag_profile(TestCase): def setUp(self): - self.bag = bagit.Bag('fixtures/test-bar') + self.bag = Bag('fixtures/test-bar') self.profile = Profile(PROFILE_URL) self.retrieved_profile = self.profile.get_profile()