upload-index-json.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. proposed_content = urlopen(
  27. request
  28. ).read()
  29. if is_json(proposed_content):
  30. return proposed_content
  31. print("bad attempt")
  32. raise Exception("Failed to fetch valid JSON from the metadumper service")
  33. except Exception as e:
  34. if retry_count == 0:
  35. raise e
  36. return get_content(retry_count - 1)
  37. def main():
  38. if not authToken or authToken == "":
  39. raise Exception("Please set META_DUMPER_AUTH_HEADER")
  40. # Upload the index.json.
  41. with scoped_cwd(ELECTRON_DIR):
  42. safe_mkdir(OUT_DIR)
  43. index_json = os.path.relpath(os.path.join(OUT_DIR, 'index.json'))
  44. new_content = get_content()
  45. with open(index_json, "wb") as f:
  46. f.write(new_content)
  47. store_artifact(OUT_DIR, 'headers/dist', [index_json])
  48. if __name__ == '__main__':
  49. sys.exit(main())