Files
nvdriver/tools/timer-timeout-build-product.py

61 lines
2.2 KiB
Python

#!/usr/bin/env python3
"""Resolve matching580 conftest build roles; never inspect host virtualization."""
import argparse
import json
import os
from pathlib import Path
import tempfile
ROLES = (
("VGX_BUILD", "NV_DOM0_KERNEL_PRESENT", "nv_rs_vgx_dom0"),
("VGX_KVM_BUILD", "NV_VGPU_KVM_BUILD", "nv_rs_vgx_kvm"),
("VGX_DEVICE_VM_BUILD", "NV_DEVICE_VM_BUILD", "nv_rs_vgx_device_vm"),
)
def resolve(environment):
# conftest.sh uses [ -n "$NAME" ]; '0' and whitespace therefore enable it.
return {define: bool(environment.get(name, "")) for name, define, _ in ROLES}
def main(argv=None):
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--receipt", type=Path)
parser.add_argument("--json", action="store_true")
args = parser.parse_args(argv)
roles = resolve(os.environ)
receipt = {
"schema": 1,
"source": "matching580 nonempty build-product environment",
"roles": roles,
"is_vgx_hyper": any(roles.values()),
}
content = json.dumps(receipt, indent=2, sort_keys=True) + "\n"
if args.receipt:
args.receipt.parent.mkdir(parents=True, exist_ok=True)
if not args.receipt.exists() or args.receipt.read_text(encoding="utf-8") != content:
# A same-filesystem atomic replacement; parallel equal-config Make
# invocations cannot leave a partially written dependency receipt.
temporary_path = None
try:
with tempfile.NamedTemporaryFile(
mode="w", encoding="utf-8", dir=args.receipt.parent, delete=False
) as temporary:
temporary_path = Path(temporary.name)
temporary.write(content)
temporary_path.replace(args.receipt)
temporary_path = None
finally:
if temporary_path is not None:
temporary_path.unlink(missing_ok=True)
if args.json:
print(content, end="")
else:
flags = ["--cfg=nv_rs_build_product_resolved"]
flags += ["--cfg=" + cfg for _, define, cfg in ROLES if roles[define]]
print(" ".join(flags))
if __name__ == "__main__":
main()