|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +"""Check if license fields are valid in all records.""" |
| 4 | + |
| 5 | +import asyncio |
| 6 | +import json |
| 7 | +import logging |
| 8 | +import os |
| 9 | +import pathlib |
| 10 | +import time |
| 11 | + |
| 12 | +VALID_LICENSE_IDENTIFIERS = [ |
| 13 | + "CC0-1.0", |
| 14 | + "GPL-3.0-only", |
| 15 | + "MIT", |
| 16 | + "Apache-2.0", |
| 17 | + "BSD-3-Clause", |
| 18 | +] |
| 19 | + |
| 20 | +logging.basicConfig(level=logging.INFO, format="[%(levelname)s] %(message)s") |
| 21 | + |
| 22 | + |
| 23 | +async def validate_file(path: pathlib.Path) -> int: |
| 24 | + """Validate a single file.""" |
| 25 | + checks = 0 |
| 26 | + errors = 0 |
| 27 | + records = await asyncio.get_event_loop().run_in_executor( |
| 28 | + None, lambda p: json.loads(open(p, "rb").read()), path |
| 29 | + ) |
| 30 | + |
| 31 | + for record in records: |
| 32 | + if rec_licenses := record.get("license"): |
| 33 | + try: |
| 34 | + attr = rec_licenses["attribution"] |
| 35 | + except KeyError: |
| 36 | + recid = record.get("recid", "UNSET") |
| 37 | + message = f"License field set but without attribution in file {path.name} with recid {recid}!" |
| 38 | + |
| 39 | + logging.error(message) |
| 40 | + errors += 1 |
| 41 | + continue |
| 42 | + |
| 43 | + if attr not in VALID_LICENSE_IDENTIFIERS: |
| 44 | + recid = record.get("recid", "UNSET") |
| 45 | + message = f"Invalid license identifier `{attr}` in file {path.name} for recid {recid}! " |
| 46 | + |
| 47 | + logging.error(message) |
| 48 | + errors += 1 |
| 49 | + else: |
| 50 | + checks += 1 |
| 51 | + |
| 52 | + if errors: |
| 53 | + raise ValueError(errors) |
| 54 | + |
| 55 | + logging.info(f"Successfully validated file {path.name}") |
| 56 | + return checks |
| 57 | + |
| 58 | + |
| 59 | +async def check_all_paths(): |
| 60 | + """Execute checks on all found files.""" |
| 61 | + start_time = time.perf_counter() |
| 62 | + |
| 63 | + loop = asyncio.get_event_loop() |
| 64 | + |
| 65 | + root_path = pathlib.Path(os.getcwd()) / "data" / "records" |
| 66 | + all_paths = list(root_path.glob("*.json")) |
| 67 | + |
| 68 | + tasks = [loop.create_task(validate_file(file_path)) for file_path in all_paths] |
| 69 | + results = await asyncio.gather(*tasks, return_exceptions=True) |
| 70 | + |
| 71 | + finish_time = time.perf_counter() - start_time |
| 72 | + logging.info(f"Processed {len(all_paths)} files within {finish_time:.2f} seconds.") |
| 73 | + |
| 74 | + if any(isinstance(result, Exception) for result in results): |
| 75 | + errors = sum( |
| 76 | + [ |
| 77 | + int(str(result)) if str(result).isdigit() else 1 |
| 78 | + for result in results |
| 79 | + if isinstance(result, Exception) |
| 80 | + ] |
| 81 | + ) |
| 82 | + logging.error( |
| 83 | + f"Validation completed with {errors} errors!\n" |
| 84 | + f"\tPlease ensure the licenses are one of the following: {VALID_LICENSE_IDENTIFIERS}.\n" |
| 85 | + f"\tIf you are using a valid SPDX license string that is not in the above list, " |
| 86 | + f"please contact `[email protected]`." |
| 87 | + ) |
| 88 | + exit(1) |
| 89 | + |
| 90 | + else: |
| 91 | + logging.info(f"Successfully validated {sum(results)} records. No errors found.") |
| 92 | + |
| 93 | + |
| 94 | +def main(): |
| 95 | + """Test to validate all license fields.""" |
| 96 | + loop = asyncio.new_event_loop() |
| 97 | + try: |
| 98 | + loop.run_until_complete(check_all_paths()) |
| 99 | + finally: |
| 100 | + loop.close() |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + main() |
0 commit comments