fetch_from_external.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import sys
  2. import json
  3. import os.path
  4. import fetch_from
  5. import argparse
  6. import logging
  7. def parse_args():
  8. parser = argparse.ArgumentParser()
  9. fetch_from.add_common_arguments(parser)
  10. parser.add_argument('--external-file', required=True)
  11. parser.add_argument('--custom-fetcher')
  12. parser.add_argument('--resource-file')
  13. return parser.parse_args()
  14. def main(args):
  15. external_file = args.external_file.rstrip('.external')
  16. if os.path.isfile(args.resource_file):
  17. fetch_from.process(args.resource_file, os.path.basename(args.resource_file), args, False)
  18. return
  19. error = None
  20. try:
  21. with open(args.external_file) as f:
  22. js = json.load(f)
  23. if js['storage'] == 'SANDBOX':
  24. import fetch_from_sandbox as ffsb
  25. del args.external_file
  26. args.resource_id = js['resource_id']
  27. ffsb.main(args)
  28. elif js['storage'] == 'MDS':
  29. import fetch_from_mds as fmds
  30. del args.external_file
  31. args.key = js['resource_id']
  32. fmds.main(args)
  33. else:
  34. error = 'Unsupported storage in {}'.format(external_file)
  35. except:
  36. logging.error('Invalid external file: {}'.format(external_file))
  37. raise
  38. if error:
  39. raise Exception(error)
  40. if __name__ == '__main__':
  41. args = parse_args()
  42. fetch_from.setup_logging(args, os.path.basename(__file__))
  43. try:
  44. main(args)
  45. except Exception as e:
  46. logging.exception(e)
  47. print >>sys.stderr, open(args.abs_log_path).read()
  48. sys.stderr.flush()
  49. import error
  50. sys.exit(error.ExitCodes.INFRASTRUCTURE_ERROR if fetch_from.is_temporary(e) else 1)