Skip to content

Commit

Permalink
Fix erroneous implementation in validate_bag_profile and allow for …
Browse files Browse the repository at this point in the history
…passing a local profile per PR: #54
  • Loading branch information
mikedarcy committed Jul 21, 2023
1 parent 95e8a63 commit 3961d9e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
3 changes: 1 addition & 2 deletions bdbag/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@
import mimetypes
import shutil
from datetime import datetime
from requests.utils import requote_uri
from distutils.util import strtobool
from importlib_metadata import distribution, PackageNotFoundError

logger = logging.getLogger(__name__)

__version__ = "1.7.0dev3"
__version__ = "1.7.0dev4"
__bagit_version__ = "1.8.1"
__bagit_profile_version__ = "1.3.1"

Expand Down
22 changes: 14 additions & 8 deletions bdbag/bdbag_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,18 +439,24 @@ def validate_bag_structure(bag_path, skip_remote=True):


def validate_bag_profile(bag_path, profile_path=None):

logger.info("Validating bag profile: %s" % bag_path)
logger.info("Validating bag profile: %s", bag_path)
bag = bdbagit.BDBag(bag_path)

# Instantiate a profile, supplying its URI.
if not profile_path:
profile_path = bag.info.get(BAG_PROFILE_TAG, None)
if not profile_path:
raise bdbp.ProfileValidationError("Bag does not contain a BagIt-Profile-Identifier")
profile_url = bag.info.get(BAG_PROFILE_TAG, None)
if not profile_url:
raise bdbp.ProfileValidationError("Bag does not contain a BagIt-Profile-Identifier")
logger.info("Loading profile: %s" % profile_path if profile_path else profile_url)

profile = None
if profile_path:
try:
with open(profile_path, encoding="UTF-8") as profile_file:
profile = json.loads(profile_file.read())
except (OSError, IOError, json.JSONDecodeError) as exc:
raise bdbp.ProfileValidationError("Profile %s could not be read: %s" % (profile_path, exc))

logger.info("Retrieving profile: %s" % profile_path)
profile = bdbp.BDBProfile(profile_path)
profile = bdbp.BDBProfile(profile_url, profile)

# Validate the profile.
if profile.validate(bag):
Expand Down
22 changes: 20 additions & 2 deletions test/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
from os.path import join as ospj
from os.path import exists as ospe
from os.path import isfile as ospif
from bdbag import bdbag_api as bdb, bdbag_config as bdbcfg, bdbag_ro as bdbro, bdbagit as bdbagit, filter_dict, \
get_typed_exception, DEFAULT_CONFIG_PATH
from bdbag import bdbag_api as bdb, bdbag_config as bdbcfg, bdbag_ro as bdbro, bdbagit as bdbagit, bdbagit_profile, \
filter_dict, get_typed_exception, DEFAULT_CONFIG_PATH
from bdbag.fetch.auth import keychain
from test.test_common import BaseTest

Expand Down Expand Up @@ -893,6 +893,24 @@ def test_validate_invalid_bag_state_fetch_filesize(self):
except Exception as e:
self.fail(get_typed_exception(e))

def test_validate_profile_with_local_profile(self):
logger.info(self.getTestHeader('validate local profile'))
try:
profile = bdb.validate_bag_profile(self.test_bag_profile_dir, "./profiles/bdbag-profile.json")
self.assertIsInstance(profile, bdbagit_profile.Profile)
except Exception as e:
self.fail(get_typed_exception(e))

def test_validate_profile_with_local_bad_path_profile(self):
logger.info(self.getTestHeader('validate bad path local profile'))
try:
self.assertRaises(bdbagit_profile.ProfileValidationError,
bdb.validate_bag_profile,
self.test_bag_profile_dir,
"./profiles/missing-bdbag-profile.json")
except Exception as e:
self.fail(get_typed_exception(e))

def test_filter_dict(self):
logger.info(self.getTestHeader('test filter function'))

Expand Down

0 comments on commit 3961d9e

Please sign in to comment.