upload-index-json.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env python
  2. import json
  3. import os
  4. import sys
  5. import urllib2
  6. from lib.config import s3_config
  7. from lib.util import s3put, scoped_cwd, safe_mkdir
  8. SOURCE_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
  9. OUT_DIR = os.path.join(SOURCE_ROOT, 'out', 'D')
  10. BASE_URL = 'https://electron-metadumper.herokuapp.com/?version='
  11. version = sys.argv[1]
  12. authToken = os.getenv('META_DUMPER_AUTH_HEADER')
  13. def is_json(myjson):
  14. try:
  15. json.loads(myjson)
  16. except ValueError:
  17. return False
  18. return True
  19. def get_content(retry_count = 5):
  20. try:
  21. request = urllib2.Request(
  22. BASE_URL + version,
  23. headers={"Authorization" : authToken}
  24. )
  25. proposed_content = urllib2.urlopen(
  26. request
  27. ).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(SOURCE_ROOT):
  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, "w") as f:
  45. f.write(new_content)
  46. bucket, access_key, secret_key = s3_config()
  47. s3put(bucket, access_key, secret_key, OUT_DIR, 'atom-shell/dist',
  48. [index_json])
  49. if __name__ == '__main__':
  50. sys.exit(main())