fetch_from_mds.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import os
  2. import sys
  3. import logging
  4. import argparse
  5. import fetch_from
  6. def parse_args():
  7. parser = argparse.ArgumentParser()
  8. fetch_from.add_common_arguments(parser)
  9. parser.add_argument('--key', required=True)
  10. parser.add_argument('--entrypoint', required=False, default="storage.yandex-team.ru/get-devtools")
  11. parser.add_argument('--scheme', required=False, default="https")
  12. return parser.parse_args()
  13. def fetch(args):
  14. parts = args.key.split("/")
  15. if len(parts) != 3:
  16. raise ValueError("Invalid MDS key '{}'".format(args.key))
  17. _, sha1, file_name = parts
  18. url = "{}://{}/{}".format(args.scheme, args.entrypoint, args.key)
  19. fetched_file = fetch_from.fetch_url(url, False, file_name, expected_sha1=sha1)
  20. return fetched_file, file_name
  21. def main(args):
  22. fetched_file, resource_file_name = fetch(args)
  23. fetch_from.process(fetched_file, resource_file_name, args)
  24. if __name__ == '__main__':
  25. args = parse_args()
  26. fetch_from.setup_logging(args, os.path.basename(__file__))
  27. try:
  28. main(args)
  29. except Exception as e:
  30. logging.exception(e)
  31. print >> sys.stderr, open(args.abs_log_path).read()
  32. sys.stderr.flush()
  33. import error
  34. sys.exit(error.ExitCodes.INFRASTRUCTURE_ERROR if fetch_from.is_temporary(e) else 1)