upload-index-json.py 1.5 KB

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