Sanad_Package_2/vendor/sanad_pkg/license_check.py

52 lines
1.4 KiB
Python

"""License gate run by every package entrypoint.
python -m sanad_pkg.license_check P1
Exit codes:
0 -> ENTITLED (proceed to launch the package)
1 -> NOT entitled (entrypoint should exit the container cleanly, code 0)
2 -> license error / unreadable (treated as not entitled)
The entrypoint pattern (see Sanad_Package_*/entrypoint.sh):
if ! python -m sanad_pkg.license_check "$PKG"; then
echo "[$PKG] not licensed — container will idle/exit"; exit 0
fi
"""
from __future__ import annotations
import sys
from sanad_pkg import license as _lic
def main(argv=None) -> int:
argv = list(sys.argv[1:] if argv is None else argv)
if not argv:
sys.stderr.write("usage: python -m sanad_pkg.license_check <P1|P2|P3|P4>\n")
return 2
pkg = argv[0].strip().upper()
lic = _lic.load()
summary = lic.summary()
if not lic.valid:
sys.stderr.write("[license] INVALID: %s\n" % summary["reason"])
return 2
if lic.package(pkg):
sys.stdout.write(
"[license] %s ENTITLED (robot=%s, expires=%s)\n"
% (pkg, summary["robot_id"] or "?", summary["expires"] or "never")
)
return 0
sys.stderr.write(
"[license] %s NOT entitled (entitled: %s)\n"
% (pkg, ", ".join(k for k, v in summary["packages"].items() if v) or "none")
)
return 1
if __name__ == "__main__":
raise SystemExit(main())