Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing a local profile path for profile validation #54

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions bdbag/bdbag_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,19 +438,29 @@ def validate_bag_structure(bag_path, skip_remote=True):
raise e


def validate_bag_profile(bag_path, profile_path=None):

logger.info("Validating bag profile: %s" % bag_path)
def validate_bag_profile(bag_path, profile_url=None, profile_path=None):
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:
if not profile_url:
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("Retrieving profile: %s" % profile_path)
profile = bdbp.BDBProfile(profile_path)
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 path could not be read: %s" % exc)


if not profile_path:
logger.info("Retrieving profile: %s", profile_path)

profile = bdbp.BDBProfile(profile_url, profile)

# Validate the profile.
if profile.validate(bag):
Expand Down
34 changes: 21 additions & 13 deletions bdbag/bdbag_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ def parse_cli():
help="Validate a bag against the profile specified by the bag's \"BagIt-Profile-Identifier\" metadata field, "
"if present. If \"bag-only\" is specified, the bag's serialization method will not be validated.")

profile_path_arg = "--profile-path"
standard_args.add_argument(
profile_path_arg, metavar='<file>',
help="Optional path to local profile JSON to use for"
"profile validation. If not specified the profile"
"referenced in the bag info file will be fetched and used."
)

config_file_arg = "--config-file"
standard_args.add_argument(
config_file_arg, metavar='<file>',
Expand Down Expand Up @@ -362,28 +370,36 @@ def main():
return result

if args.ro_manifest_generate:
bdb.generate_ro_manifest(path, True if args.ro_manifest_generate == "overwrite" else False,
config_file=args.config_file)
bdb.generate_ro_manifest(path, args.ro_manifest_generate == "overwrite", config_file=args.config_file)

if args.resolve_fetch:
if args.validate == 'full':
sys.stderr.write(ASYNC_TRANSFER_VALIDATION_WARNING)
bdb.resolve_fetch(path,
force=True if args.resolve_fetch == 'all' else False,
force=args.resolve_fetch == 'all',
keychain_file=args.keychain_file,
config_file=args.config_file,
filter_expr=args.fetch_filter)

if args.validate_profile:
if is_file:
temp_path = bdb.extract_bag(path, args.output_path, temp=args.output_path is None)
profile = bdb.validate_bag_profile(temp_path if temp_path else path, profile_path=args.profile_path)
if not args.validate_profile == "bag-only":
bdb.validate_bag_serialization(archive if archive else path, profile)


if args.validate:
if is_file:
temp_path = bdb.extract_bag(path, args.output_path, temp=True if not args.output_path else False)
if not temp_path:
temp_path = bdb.extract_bag(path, args.output_path, temp=args.output_path is None)
if args.validate == 'structure':
bdb.validate_bag_structure(temp_path if temp_path else path)
elif args.validate == 'completeness':
bdb.validate_bag_structure(temp_path if temp_path else path, skip_remote=False)
else:
bdb.validate_bag(temp_path if temp_path else path,
fast=True if args.validate == 'fast' else False,
fast=args.validate == 'fast',
config_file=args.config_file)

if args.archiver:
Expand All @@ -392,14 +408,6 @@ def main():
if archive is None and is_file:
archive = path

if args.validate_profile:
if is_file:
if not temp_path:
temp_path = bdb.extract_bag(path, args.output_path, temp=True if not args.output_path else False)
profile = bdb.validate_bag_profile(temp_path if temp_path else path)
if not args.validate_profile == "bag-only":
bdb.validate_bag_serialization(archive if archive else path, profile)

if args.revert:
bdb.revert_bag(path)

Expand Down