upload-index-json.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env python3
  2. import json
  3. import os
  4. import sys
  5. from urllib.request import Request, urlopen
  6. sys.path.append(
  7. os.path.abspath(os.path.dirname(os.path.abspath(__file__)) + "/../.."))
  8. from lib.util import store_artifact, scoped_cwd, safe_mkdir, get_out_dir, \
  9. ELECTRON_DIR
  10. OUT_DIR = get_out_dir()
  11. BASE_URL = 'https://electron-metadumper.herokuapp.com/?version='
  12. version = sys.argv[1]
  13. authToken = os.getenv('META_DUMPER_AUTH_HEADER')
  14. def is_json(myjson):
  15. try:
  16. json.loads(myjson)
  17. except ValueError:
  18. return False
  19. return True
  20. def get_content(retry_count = 5):
  21. try:
  22. request = Request(
  23. BASE_URL + version,
  24. headers={"Authorization" : authToken}
  25. )
  26. with urlopen(request) as resp:
  27. proposed_content = resp.read()
  28. if is_json(proposed_content):
  29. return proposed_content
  30. print("bad attempt")
  31. raise Exception("Failed to fetch valid JSON from the metadumper service")
  32. except Exception as e:
  33. if retry_count == 0:
  34. raise e
  35. return get_content(retry_count - 1)
  36. def main():
  37. if not authToken or authToken == "":
  38. raise Exception("Please set META_DUMPER_AUTH_HEADER")
  39. # Upload the index.json.
  40. with scoped_cwd(ELECTRON_DIR):
  41. safe_mkdir(OUT_DIR)
  42. index_json = os.path.relpath(os.path.join(OUT_DIR, 'index.json'))
  43. new_content = get_content()
  44. with open(index_json, "wb") as f:
  45. f.write(new_content)
  46. store_artifact(OUT_DIR, 'headers/dist', [index_json])
  47. if __name__ == '__main__':
  48. sys.exit(main())