"""Read-only check of archived PLY/STL download routes; sends no scanner commands.""" import argparse import hashlib import json from pathlib import Path import urllib.parse import urllib.request from verify_cmvr_native import Client, ROOT, require def verify(client, execution_id): require(execution_id.isdigit(), "Execution ID must be a positive integer") result = client.request("GET", "/admin-api/lab/execution/scanner-view/assets", query={"executionId": execution_id}, expected=(0,)) assets = [item for item in result["data"] if Path(item["name"]).suffix.lower() in (".ply", ".stl")] require({Path(item["name"]).suffix.lower() for item in assets} == {".ply", ".stl"}, "This execution must contain both PLY and STL results") checked = [] for asset in assets: file_id = asset["fileId"] require(file_id.isalnum(), "Unexpected file identifier") metadata = ROOT / ".local/artifacts/1" / execution_id / "scanner-files" / (file_id + ".json") archived = json.loads(metadata.read_text(encoding="utf-8")) path = "/admin-api/lab/execution/scanner-view/asset/" + urllib.parse.quote(file_id) path += "?" + urllib.parse.urlencode({"executionId": execution_id}) request = urllib.request.Request(client.base_url + path, headers={ "Authorization": "Bearer " + client._token, "tenant-id": "1"}) digest, length = hashlib.sha256(), 0 with urllib.request.urlopen(request, timeout=30) as response: require(response.status == 200, "File route did not return HTTP 200") require("json" not in response.headers.get("content-type", ""), "File route returned an error payload") while chunk := response.read(1024 * 1024): length += len(chunk) digest.update(chunk) require(length == archived["bytes"] == asset["bytes"], "Downloaded length differs from archived result") require(digest.hexdigest() == archived["sha256"], "Downloaded content differs from archived result") checked.append({"name": asset["name"], "bytes": length, "sha256Matches": True}) return {"executionId": execution_id, "files": checked} if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--execution-id", action="append", required=True) args = parser.parse_args() client = Client().login() print(json.dumps({"passed": True, "checks": [verify(client, value) for value in args.execution_id]}, ensure_ascii=False))