Download tasks/amazon/tests/oracle-runtime/clone/harbor_entry.py from TIGER-Lab/WebsiteBench: direct link, hf CLI and curl.
- Browser
- Download file 5.8 kB
-
https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/amazon/tests/oracle-runtime/clone/harbor_entry.py
- Command line
-
hf download hf://datasets/TIGER-Lab/WebsiteBench/tasks/amazon/tests/oracle-runtime/clone/harbor_entry.py
-
curl -L -o harbor_entry.py https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/amazon/tests/oracle-runtime/clone/harbor_entry.py
5.8 kB
| """WebsiteBench Harbor foreground entrypoint for the Amazon clone.""" | |
| from __future__ import annotations | |
| import json | |
| import os | |
| import re | |
| import urllib.error | |
| import urllib.parse | |
| import urllib.request | |
| from pathlib import Path | |
| ROOT = Path(__file__).resolve().parent | |
| SITE_ROOT = ROOT.parent | |
| DATA_DIR = Path(os.environ["DATA_DIR"]) | |
| if not DATA_DIR.is_absolute(): | |
| raise RuntimeError("DATA_DIR must be absolute") | |
| DATA_DIR = DATA_DIR.resolve() | |
| DATA_DIR.mkdir(parents=True, exist_ok=True) | |
| os.environ["AMAZON_HOST"] = os.environ["HOST"] | |
| os.environ["AMAZON_DB_PATH"] = str(DATA_DIR / "amazon.sqlite3") | |
| os.environ["WEBSITEBENCH_DATA_DIR"] = str(DATA_DIR) | |
| os.environ["WEBSITEBENCH_SITE_BACKEND_DATABASE"] = str(DATA_DIR / "amazon.sqlite3") | |
| os.environ["WEBSITEBENCH_SITE_BACKEND_RUNTIME"] = str(SITE_ROOT / "backend" / "runtime.json") | |
| if os.environ.get("WEBSITEBENCH_MAILBOX_BOOTSTRAP_URL", "").strip(): | |
| os.environ["WEBSITEBENCH_DEPLOYMENT_PROFILE"] = "offline-harbor" | |
| os.environ["WEBSITEBENCH_PAYMENT_PROFILE"] = "local-sandbox" | |
| os.environ["AMAZON_PAYMENT_MODE"] = "local-sandbox" | |
| class _NoRedirect(urllib.request.HTTPRedirectHandler): | |
| def redirect_request(self, request, fp, code, msg, headers, newurl): | |
| del request, fp, code, msg, headers, newurl | |
| return None | |
| def _configure_harbor_mailbox() -> None: | |
| bootstrap_url = os.environ.get("WEBSITEBENCH_MAILBOX_BOOTSTRAP_URL", "").strip() | |
| bootstrap_token = os.environ.get( | |
| "WEBSITEBENCH_MAILBOX_BOOTSTRAP_TOKEN", "" | |
| ).strip() | |
| if not bootstrap_url and not bootstrap_token: | |
| return | |
| parsed = urllib.parse.urlsplit(bootstrap_url) | |
| if ( | |
| parsed.scheme != "http" | |
| or parsed.hostname not in { | |
| "127.0.0.1", | |
| "::1", | |
| "localhost", | |
| "mailbox-internal", | |
| } | |
| or parsed.username is not None | |
| or parsed.password is not None | |
| or len(bootstrap_token) < 32 | |
| ): | |
| raise RuntimeError("Harbor mailbox bootstrap configuration is invalid") | |
| request = urllib.request.Request( | |
| bootstrap_url, | |
| headers={ | |
| "Accept": "application/json", | |
| "Authorization": f"Bearer {bootstrap_token}", | |
| }, | |
| method="GET", | |
| ) | |
| try: | |
| with urllib.request.build_opener(_NoRedirect).open(request, timeout=10) as response: | |
| raw = response.read(64 * 1024 + 1) | |
| except (OSError, urllib.error.URLError) as exc: | |
| raise RuntimeError("Harbor mailbox bootstrap is unavailable") from exc | |
| if len(raw) > 64 * 1024: | |
| raise RuntimeError("Harbor mailbox bootstrap response is too large") | |
| try: | |
| identity = json.loads(raw.decode("utf-8")) | |
| namespace = str(identity["namespace"]) | |
| capability = str(identity["capability"]) | |
| smtp_host = str(identity["smtp_host"]) | |
| smtp_port = int(identity["smtp_port"]) | |
| except (KeyError, TypeError, ValueError, UnicodeError, json.JSONDecodeError) as exc: | |
| raise RuntimeError("Harbor mailbox bootstrap response is invalid") from exc | |
| if ( | |
| re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9._-]*", namespace) is None | |
| or len(namespace) > 128 | |
| or len(capability) < 32 | |
| or smtp_host not in {"127.0.0.1", "::1", "localhost", "mailbox-internal"} | |
| or not 1 <= smtp_port <= 65535 | |
| ): | |
| raise RuntimeError("Harbor mailbox identity is invalid") | |
| os.environ.update( | |
| { | |
| "AMAZON_CLONE_REQUIRE_SMTP": "1", | |
| "AMAZON_CLONE_SMTP_HOST": smtp_host, | |
| "AMAZON_CLONE_SMTP_PORT": str(smtp_port), | |
| "AMAZON_CLONE_SMTP_TLS": "none", | |
| # The trial mailbox account only accepts envelopes from the | |
| # sender the harness offers; a site-branded literal is rejected | |
| # with "mail sender is not allowed" and no code ever lands. | |
| "AMAZON_CLONE_SMTP_FROM": ( | |
| os.environ.get("WEBSITEBENCH_SMTP_FROM", "").strip() | |
| or os.environ.get("WEBSITEBENCH_MAIL_FROM", "").strip() | |
| or "Amazon WebsiteBench <no-reply@amazon.websitebench.local>" | |
| ), | |
| "AMAZON_CLONE_MAILBOX_NAMESPACE": namespace, | |
| "AMAZON_CLONE_MAILBOX_CAPABILITY": capability, | |
| } | |
| ) | |
| _configure_harbor_mailbox() | |
| import server # noqa: E402 | |
| _original_get = server.PublicHandler.do_GET | |
| _original_head = server.PublicHandler.do_HEAD | |
| # /healthz is the contract (ADR-0010); the private path stays mounted so | |
| # bundles frozen before the migration keep answering. | |
| HEALTH_PATHS = ("/healthz", "/__websitebench/health") | |
| def _health(handler, *, head: bool) -> bool: | |
| if urllib.parse.urlsplit(handler.path).path not in HEALTH_PATHS: | |
| return False | |
| body = b'{"status":"ok"}' | |
| handler.send_response(200) | |
| handler.send_header("Content-Type", "application/json") | |
| handler.send_header("Content-Length", str(len(body))) | |
| handler.send_header("Cache-Control", "no-store") | |
| handler.end_headers() | |
| if not head: | |
| handler.wfile.write(body) | |
| return True | |
| def do_get(handler): | |
| if not _health(handler, head=False): | |
| _original_get(handler) | |
| def do_head(handler): | |
| if not _health(handler, head=True): | |
| _original_head(handler) | |
| class _DisabledAdminServer: | |
| def serve_forever(self): | |
| return None | |
| def shutdown(self): | |
| return None | |
| def server_close(self): | |
| return None | |
| _real_server = server.ReusableThreadingHTTPServer | |
| def _server_factory(address, handler): | |
| if handler is server.AdminHandler: | |
| return _DisabledAdminServer() | |
| return _real_server(address, handler) | |
| server.PublicHandler.do_GET = do_get | |
| server.PublicHandler.do_HEAD = do_head | |
| server.ReusableThreadingHTTPServer = _server_factory | |
| if __name__ == "__main__": | |
| raise SystemExit(server.main()) | |