parse_deploy_output.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/usr/bin/env python3
  2. """Parse `wrangler deploy --temporary` output into structured JSON.
  3. Reads wrangler's stdout/stderr from STDIN and extracts the live workers.dev
  4. URL, the claim URL, the temporary account name/state, the claim window, and
  5. whether a deploy actually happened. Stdlib only — no dependencies.
  6. Usage:
  7. npx wrangler@latest deploy --temporary 2>&1 | python3 parse_deploy_output.py
  8. python3 parse_deploy_output.py --selftest
  9. """
  10. from __future__ import annotations
  11. import json
  12. import re
  13. import sys
  14. # Match the live workers.dev URL (subdomain.subdomain.workers.dev).
  15. _LIVE_URL = re.compile(r"https://[A-Za-z0-9._-]+\.workers\.dev\S*")
  16. # Match the claim URL. Cloudflare uses dash.cloudflare.com/claim-preview?claimToken=...
  17. # Keep it broad enough to survive minor path changes while still requiring a claim token.
  18. _CLAIM_URL = re.compile(r"https://\S*claim\S*claimToken=\S+", re.IGNORECASE)
  19. # "Account: Serene Temple (created)" / "Account: example-name (reused)"
  20. # Account names can contain spaces (e.g. "Serene Temple"), so capture everything
  21. # up to the trailing "(state)" marker rather than a single token.
  22. _ACCOUNT = re.compile(
  23. r"Account:\s*(?P<name>.+?)\s*\((?P<state>created|reused)\)", re.IGNORECASE
  24. )
  25. # "Claim within: 60 minutes"
  26. _CLAIM_WITHIN = re.compile(r"Claim within:\s*(?P<minutes>\d+)\s*minutes?", re.IGNORECASE)
  27. # A successful deploy prints a "Deployed" / "Uploaded" line.
  28. _DEPLOYED = re.compile(r"^\s*(Deployed|Uploaded)\b", re.IGNORECASE | re.MULTILINE)
  29. def _first(pattern: re.Pattern, text: str) -> str | None:
  30. m = pattern.search(text)
  31. if not m:
  32. return None
  33. # Strip trailing punctuation that often clings to a URL in log lines.
  34. return m.group(0).rstrip(".,);]")
  35. def parse(text: str) -> dict:
  36. """Extract deploy facts from wrangler output text."""
  37. account = _ACCOUNT.search(text)
  38. claim_within = _CLAIM_WITHIN.search(text)
  39. return {
  40. "live_url": _first(_LIVE_URL, text),
  41. "claim_url": _first(_CLAIM_URL, text),
  42. "account": account.group("name") if account else None,
  43. "account_state": account.group("state").lower() if account else None,
  44. "expires_minutes": int(claim_within.group("minutes")) if claim_within else None,
  45. "deployed": bool(_DEPLOYED.search(text)),
  46. }
  47. _SAMPLE = """\
  48. Continuing means you accept Cloudflare's Terms of Service and Privacy Policy.
  49. Temporary account ready:
  50. Account: example-name (created)
  51. Claim within: 60 minutes
  52. Claim URL: https://dash.cloudflare.com/claim-preview?claimToken=abc123XYZ
  53. Uploaded example-worker
  54. Deployed example-worker triggers
  55. https://example-worker.example-name.workers.dev
  56. """
  57. _SAMPLE_REUSED = """\
  58. Temporary account ready:
  59. Account: example-name (reused)
  60. Claim within: 42 minutes
  61. Claim URL: https://dash.cloudflare.com/claim-preview?claimToken=def456
  62. Deployed example-worker triggers
  63. https://example-worker.example-name.workers.dev
  64. """
  65. _SAMPLE_NO_TEMP = """\
  66. ✘ [ERROR] You are not logged in.
  67. To continue without logging in, rerun this command with `--temporary`.
  68. """
  69. def _selftest() -> int:
  70. r = parse(_SAMPLE)
  71. assert r["live_url"] == "https://example-worker.example-name.workers.dev", r
  72. assert r["claim_url"] == "https://dash.cloudflare.com/claim-preview?claimToken=abc123XYZ", r
  73. assert r["account"] == "example-name", r
  74. assert r["account_state"] == "created", r
  75. assert r["expires_minutes"] == 60, r
  76. assert r["deployed"] is True, r
  77. r2 = parse(_SAMPLE_REUSED)
  78. assert r2["account_state"] == "reused", r2
  79. assert r2["expires_minutes"] == 42, r2
  80. assert r2["deployed"] is True, r2
  81. r3 = parse(_SAMPLE_NO_TEMP)
  82. assert r3["live_url"] is None, r3
  83. assert r3["claim_url"] is None, r3
  84. assert r3["account"] is None, r3
  85. assert r3["deployed"] is False, r3
  86. print("selftest: OK")
  87. return 0
  88. def main(argv: list[str]) -> int:
  89. if "--selftest" in argv:
  90. return _selftest()
  91. text = sys.stdin.read()
  92. result = parse(text)
  93. print(json.dumps(result, indent=2))
  94. # Non-zero exit if no live URL was found, so callers can branch on it.
  95. return 0 if result["live_url"] else 1
  96. if __name__ == "__main__":
  97. raise SystemExit(main(sys.argv[1:]))