Skip to content

Commit

Permalink
Add CLI argument for --profile-path and add profile validation step…
Browse files Browse the repository at this point in the history
… before bag validation in CLI execution order. See #54.

Add unit test and doc entry for above.
Fix a typo in a related logging statement.
  • Loading branch information
mikedarcy committed Jul 24, 2023
1 parent 8638ca9 commit 98b716e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 11 deletions.
2 changes: 1 addition & 1 deletion bdbag/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

logger = logging.getLogger(__name__)

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

Expand Down
2 changes: 1 addition & 1 deletion bdbag/bdbag_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ def validate_bag_profile(bag_path, profile_path=None):
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)
logger.info("Loading profile: %s" % (profile_path if profile_path else profile_url))

profile = None
if profile_path:
Expand Down
22 changes: 15 additions & 7 deletions bdbag/bdbag_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ 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 @@ -374,6 +380,10 @@ def main():
config_file=args.config_file,
filter_expr=args.fetch_filter)

if args.validate_profile:
if not is_file:
profile = bdb.validate_bag_profile(temp_path if temp_path else path, profile_path=args.profile_path)

if args.validate:
if is_file:
temp_path = bdb.extract_bag(path, args.output_path, temp=True if not args.output_path else False)
Expand All @@ -392,13 +402,11 @@ 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.validate_profile == "full" and is_file:
if not temp_path:
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)
bdb.validate_bag_serialization(archive if archive else path, profile)

if args.revert:
bdb.revert_bag(path)
Expand Down
7 changes: 7 additions & 0 deletions doc/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ usage: bdbag
[--fetch-filter <column><operator><value>]
[--validate {fast,full,structure,completeness}]
[--validate-profile {profile-only,full}]
[--profile-path <file>]
[--config-file <file>]
[--keychain-file <file>]
[--metadata-file <file>]
Expand Down Expand Up @@ -185,6 +186,12 @@ Validate a bag against the profile specified by the bag's `BagIt-Profile-Identif
`bag-only` argument keyword can be used to bypass the otherwise automatic bag serialization validation
(implied by the default value, `full`), and therefore is suitable for use on bag directories.

----
#### `--profile-path <file>`
Optional path to a `Bagit-Profiles-Specification` JSON file. The file format is described
[here](https://bagit-profiles.github.io/bagit-profiles-specification/).
If this argument is not specified, the profile specified by the bag's `BagIt-Profile-Identifier` metadata field will be used, if present.

This comment has been minimized.

Copy link
@prettybits

prettybits Jul 25, 2023

I'm not sure it's sufficiently clear how this behaves yet. If profile validation is requested the profile path specified here is used, otherwise it is read from the bag. In either case, the BagIt-Profile-Identifier field must be present or an error will be raised, so I'd remove the "if present" clause at the end here. I'm thinking it would make sense to also add an additional constraint for the argument on the presence of --validate-profile and add that to the Argument compatibility table below?


----
#### `--config-file <file>`
Optional path to a *bdbag* configuration file. The configuration file format is described
Expand Down
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
'globus': ["globus_sdk>=1.6.0"],
'gcs': ["google_cloud_storage"]
},
python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, <4',
python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, !=3.5.*, <4',
license='Apache 2.0',
classifiers=[
'Intended Audience :: Science/Research',
Expand All @@ -85,7 +85,6 @@
"Operating System :: MacOS :: MacOS X",
"Operating System :: Microsoft :: Windows",
'Programming Language :: Python',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
Expand Down
7 changes: 7 additions & 0 deletions test/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ def test_validate_profile_skip_serialization(self):
self._test_successful_invocation(
args, ["Bag structure conforms to specified profile"])

def test_validate_local_profile(self):
args = ARGS + [self.test_bag_profile_dir, '--validate-profile', 'bag-only',
'--profile-path', './profiles/bdbag-profile.json']
logfile.writelines(self.getTestHeader('validate-local-profile', args))
self._test_successful_invocation(
args, ["Loading profile: ./profiles/bdbag-profile.json", "Bag structure conforms to specified profile"])


class TestCliArgParsing(BaseTest):

Expand Down

0 comments on commit 98b716e

Please sign in to comment.