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

Give validate command an --allow-any-path option #783

Merged
merged 2 commits into from
Oct 4, 2021
Merged
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
13 changes: 11 additions & 2 deletions dandi/cli/cmd_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@

@click.command()
@devel_option("--schema", help="Validate against new schema version", metavar="VERSION")
@devel_option(
"--allow-any-path",
help="For development: allow DANDI 'unsupported' file types/paths",
default=False,
is_flag=True,
)
@click.argument("paths", nargs=-1, type=click.Path(exists=True, dir_okay=True))
@devel_debug_option()
@map_to_click_exceptions
def validate(paths, schema=None, devel_debug=False):
def validate(paths, schema=None, devel_debug=False, allow_any_path=False):
"""Validate files for NWB (and DANDI) compliance.

Exits with non-0 exit code if any file is not compliant.
Expand All @@ -37,7 +43,10 @@ def validate(paths, schema=None, devel_debug=False):
all_files_errors = {}
nfiles = 0
for path, errors in validate_(
paths, schema_version=schema, devel_debug=devel_debug
paths,
schema_version=schema,
devel_debug=devel_debug,
allow_any_path=allow_any_path,
):
nfiles += 1
if view == "one-at-a-time":
Expand Down
23 changes: 14 additions & 9 deletions dandi/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .metadata import get_metadata
from .pynwb_utils import validate as pynwb_validate
from .pynwb_utils import validate_cache
from .utils import find_dandi_files, yaml_load
from .utils import find_dandi_files, find_files, yaml_load

lgr = get_logger()

Expand All @@ -15,7 +15,7 @@


# TODO: provide our own "errors" records, which would also include warnings etc
def validate(paths, schema_version=None, devel_debug=False):
def validate(paths, schema_version=None, devel_debug=False, allow_any_path=False):
"""Validate content

Parameters
Expand All @@ -28,7 +28,8 @@ def validate(paths, schema_version=None, devel_debug=False):
path, errors
errors for a path
"""
for path in find_dandi_files(paths):
filepaths = find_files(".*", paths) if allow_any_path else find_dandi_files(paths)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I thought to suggest adding exclude_datalad=True but since we already default to exclude_dotdirs = True I guess that doesn't matter.

for path in filepaths:
errors = validate_file(
path, schema_version=schema_version, devel_debug=devel_debug
)
Expand All @@ -41,7 +42,7 @@ def validate_file(filepath, schema_version=None, devel_debug=False):
filepath, schema_version=None, devel_debug=devel_debug
)
else:
return pynwb_validate(filepath, devel_debug=devel_debug) + validate_dandi_nwb(
return pynwb_validate(filepath, devel_debug=devel_debug) + validate_asset_file(
filepath, schema_version=schema_version, devel_debug=devel_debug
)

Expand Down Expand Up @@ -88,22 +89,26 @@ def validate_dandiset_yaml(filepath, schema_version=None, devel_debug=False):


@validate_cache.memoize_path
def validate_dandi_nwb(filepath, schema_version=None, devel_debug=False):
"""Provide validation of .nwb file regarding requirements we impose"""
def validate_asset_file(filepath, schema_version=None, devel_debug=False):
"""Provide validation of asset file regarding requirements we impose"""
if schema_version is not None:
from dandischema.models import BareAsset, get_schema_version
from pydantic import ValidationError

from .metadata import nwb2asset
from .metadata import get_asset_metadata

current_version = get_schema_version()
if schema_version != current_version:
raise ValueError(
f"Unsupported schema version: {schema_version}; expected {current_version}"
)
try:
asset = nwb2asset(
filepath, digest=32 * "d" + "-1", digest_type="dandi_etag"
asset = get_asset_metadata(
filepath,
relpath="dummy",
digest=32 * "d" + "-1",
digest_type="dandi_etag",
allow_any_path=True,
)
BareAsset(**asset.dict())
except ValidationError as e:
Expand Down