File: validate.py

package info (click to toggle)
python-asdf 5.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,940 kB
  • sloc: python: 23,812; makefile: 123
file content (58 lines) | stat: -rw-r--r-- 1,659 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
Commands for validating ASDF files
"""

import asdf

from .main import Command

__all__ = ["validate"]


class Validate(Command):
    @classmethod
    def setup_arguments(cls, subparsers):
        parser = subparsers.add_parser(
            "validate",
            help="Validates an ASDF file.",
            description=(
                "Validates tree against all tagged schemas (and optionally a custom schema) "
                "and blocks with stored checksums."
            ),
        )

        parser.add_argument("filename", help="path to ASDF file")
        parser.add_argument(
            "--custom-schema",
            type=str,
            help="path or URI of custom schema",
        )
        parser.add_argument(
            "--skip-block-validation",
            default=False,
            action="store_true",
            help="Do not compare blocks against stored checksums.",
        )
        parser.set_defaults(func=cls.run)

        return parser

    @classmethod
    def run(cls, args):
        validate(args.filename, args.custom_schema, args.skip_block_validation)


def validate(filename, custom_schema, skip_block_validation):
    # if we are skipping checksums we can lazy load, otherwise don't
    with asdf.open(
        filename,
        custom_schema=custom_schema,
        validate_checksums=not skip_block_validation,
        lazy_load=skip_block_validation,
    ) as af:  # noqa: F841
        msg = f"{filename} is valid"
        if custom_schema:
            msg += f", conforms to {custom_schema}"
        if not skip_block_validation:
            msg += ", and block checksums match contents"
        print(msg)