-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidate_manifest.py
More file actions
48 lines (37 loc) · 1.36 KB
/
Copy pathvalidate_manifest.py
File metadata and controls
48 lines (37 loc) · 1.36 KB
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
#!/usr/bin/env python3
"""Validate a compliance bundle zip contains manifest.json with required keys."""
from __future__ import annotations
import argparse
import json
import sys
import zipfile
from pathlib import Path
REQUIRED_TOP_LEVEL = ("version", "timestamp", "artifacts", "summary")
def validate_zip(path: Path) -> int:
if not path.is_file():
print(f"error: not a file: {path}", file=sys.stderr)
return 2
try:
with zipfile.ZipFile(path, "r") as zf:
names = set(zf.namelist())
if "manifest.json" not in names:
print("error: manifest.json missing from archive", file=sys.stderr)
return 1
raw = zf.read("manifest.json").decode("utf-8")
data = json.loads(raw)
except (zipfile.BadZipFile, json.JSONDecodeError, UnicodeDecodeError) as e:
print(f"error: {e}", file=sys.stderr)
return 1
missing = [k for k in REQUIRED_TOP_LEVEL if k not in data]
if missing:
print(f"error: manifest missing keys: {missing}", file=sys.stderr)
return 1
print("manifest OK")
return 0
def main() -> None:
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("zip_path", type=Path, help="Path to compliance-bundle.zip")
args = p.parse_args()
sys.exit(validate_zip(args.zip_path))
if __name__ == "__main__":
main()