2023-11-16 04:20:00 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import json
|
2024-01-02 11:29:13 +00:00
|
|
|
import os
|
2023-11-16 04:20:00 +00:00
|
|
|
import sys
|
|
|
|
|
2024-01-02 11:29:13 +00:00
|
|
|
import importlib_metadata
|
2024-07-27 06:49:29 +00:00
|
|
|
from packaging.requirements import InvalidRequirement, Requirement
|
2023-11-16 04:20:00 +00:00
|
|
|
|
|
|
|
|
2024-07-27 06:49:29 +00:00
|
|
|
def error(msg: str, ret: bool = False) -> None:
|
2024-01-02 11:29:13 +00:00
|
|
|
print(f" - {msg}", file=sys.stderr)
|
2024-07-27 06:49:29 +00:00
|
|
|
return ret
|
2024-01-02 11:29:13 +00:00
|
|
|
|
|
|
|
|
2023-11-16 04:20:00 +00:00
|
|
|
def check_requirement(req: str):
|
|
|
|
# https://packaging.pypa.io/en/stable/requirements.html
|
2024-07-27 06:49:29 +00:00
|
|
|
try:
|
|
|
|
requirement = Requirement(req)
|
|
|
|
except InvalidRequirement:
|
|
|
|
return error(f"{req} could not be parsed", ret=True)
|
|
|
|
|
2023-11-16 04:20:00 +00:00
|
|
|
try:
|
|
|
|
version = importlib_metadata.distribution(requirement.name).version
|
|
|
|
except importlib_metadata.PackageNotFoundError:
|
2024-01-02 11:29:13 +00:00
|
|
|
return error(f"{requirement.name}{requirement.specifier} not present")
|
2023-11-16 04:20:00 +00:00
|
|
|
|
|
|
|
# https://packaging.pypa.io/en/stable/specifiers.html
|
2024-01-02 11:29:13 +00:00
|
|
|
if version not in requirement.specifier:
|
|
|
|
return error(
|
|
|
|
f"{requirement.name}{requirement.specifier} expected, but got {version}"
|
2023-11-16 04:20:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def check_manifest(manifest_file: str):
|
|
|
|
with open(manifest_file) as fd:
|
|
|
|
manifest = json.load(fd)
|
2024-01-02 11:29:13 +00:00
|
|
|
|
|
|
|
ok = True
|
|
|
|
|
|
|
|
derivation_domain = os.environ.get("domain")
|
|
|
|
manifest_domain = manifest["domain"]
|
|
|
|
if derivation_domain != manifest_domain:
|
|
|
|
ok = False
|
|
|
|
error(
|
|
|
|
f"Derivation attribute domain ({derivation_domain}) must match manifest domain ({manifest_domain})"
|
|
|
|
)
|
|
|
|
|
2023-11-16 04:20:00 +00:00
|
|
|
if "requirements" in manifest:
|
|
|
|
for requirement in manifest["requirements"]:
|
|
|
|
ok &= check_requirement(requirement)
|
2024-01-02 11:29:13 +00:00
|
|
|
|
|
|
|
if not ok:
|
|
|
|
error("Manifest check failed.")
|
|
|
|
sys.exit(1)
|
2023-11-16 04:20:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if len(sys.argv) < 2:
|
|
|
|
raise RuntimeError(f"Usage {sys.argv[0]} <manifest>")
|
|
|
|
manifest_file = sys.argv[1]
|
|
|
|
check_manifest(manifest_file)
|