fetch_from_mds.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from __future__ import print_function
  2. import os
  3. import sys
  4. import logging
  5. import argparse
  6. # Explicitly enable local imports
  7. # Don't forget to add imported scripts to inputs of the calling command!
  8. sys.path.append(os.path.dirname(os.path.abspath(__file__)))
  9. import fetch_from
  10. def parse_args():
  11. parser = argparse.ArgumentParser()
  12. fetch_from.add_common_arguments(parser)
  13. parser.add_argument('--key', required=True)
  14. parser.add_argument('--entrypoint', required=False, default="storage.yandex-team.ru/get-devtools")
  15. parser.add_argument('--scheme', required=False, default="https")
  16. return parser.parse_args()
  17. def fetch(args):
  18. parts = args.key.split("/")
  19. if len(parts) != 3:
  20. raise ValueError("Invalid MDS key '{}'".format(args.key))
  21. _, sha1, file_name = parts
  22. url = "{}://{}/{}".format(args.scheme, args.entrypoint, args.key)
  23. fetched_file = fetch_from.fetch_url(url, False, file_name, expected_sha1=sha1)
  24. return fetched_file, file_name
  25. def main(args):
  26. fetched_file, resource_file_name = fetch(args)
  27. fetch_from.process(fetched_file, resource_file_name, args)
  28. if __name__ == '__main__':
  29. args = parse_args()
  30. fetch_from.setup_logging(args, os.path.basename(__file__))
  31. try:
  32. main(args)
  33. except Exception as e:
  34. logging.exception(e)
  35. print(open(args.abs_log_path).read(), file=sys.stderr)
  36. sys.stderr.flush()
  37. import error
  38. sys.exit(error.ExitCodes.INFRASTRUCTURE_ERROR if fetch_from.is_temporary(e) else 1)