publish_multi_request.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env python3
  2. import argparse
  3. import requests
  4. import os
  5. import subprocess
  6. from typing import Iterable
  7. PUBLISH_TOKEN = os.environ["PUBLISH_TOKEN"]
  8. VERSION = os.environ["GITHUB_SHA"]
  9. RELEASE_DIR = "release"
  10. #
  11. # CONFIGURATION PARAMETERS
  12. # Forks should change these to publish to their own infrastructure.
  13. #
  14. ROBUST_CDN_URL = "https://cdn.civ13.com/"
  15. FORK_ID = "master"
  16. def main():
  17. parser = argparse.ArgumentParser()
  18. parser.add_argument("--fork-id", default=FORK_ID)
  19. args = parser.parse_args()
  20. fork_id = args.fork_id
  21. session = requests.Session()
  22. session.headers = {
  23. "Authorization": f"Bearer {PUBLISH_TOKEN}",
  24. }
  25. print(f"Starting publish on Robust.Cdn for version {VERSION}")
  26. data = {
  27. "version": VERSION,
  28. "engineVersion": get_engine_version(),
  29. }
  30. headers = {"Content-Type": "application/json"}
  31. resp = session.post(
  32. f"{ROBUST_CDN_URL}fork/{fork_id}/publish/start", json=data, headers=headers
  33. )
  34. resp.raise_for_status()
  35. print("Publish successfully started, adding files...")
  36. for file in get_files_to_publish():
  37. print(f"Publishing {file}")
  38. with open(file, "rb") as f:
  39. headers = {
  40. "Content-Type": "application/octet-stream",
  41. "Robust-Cdn-Publish-File": os.path.basename(file),
  42. "Robust-Cdn-Publish-Version": VERSION,
  43. }
  44. resp = session.post(
  45. f"{ROBUST_CDN_URL}fork/{fork_id}/publish/file", data=f, headers=headers
  46. )
  47. resp.raise_for_status()
  48. print("Successfully pushed files, finishing publish...")
  49. data = {"version": VERSION}
  50. headers = {"Content-Type": "application/json"}
  51. resp = session.post(
  52. f"{ROBUST_CDN_URL}fork/{fork_id}/publish/finish", json=data, headers=headers
  53. )
  54. resp.raise_for_status()
  55. print("SUCCESS!")
  56. def get_files_to_publish() -> Iterable[str]:
  57. for file in os.listdir(RELEASE_DIR):
  58. yield os.path.join(RELEASE_DIR, file)
  59. def get_engine_version() -> str:
  60. proc = subprocess.run(
  61. ["git", "describe", "--tags", "--abbrev=0"],
  62. stdout=subprocess.PIPE,
  63. cwd="RobustToolbox",
  64. check=True,
  65. encoding="UTF-8",
  66. )
  67. tag = proc.stdout.strip()
  68. assert tag.startswith("v")
  69. return tag[1:] # Cut off v prefix.
  70. if __name__ == "__main__":
  71. main()