fetch_from_npm.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import os
  2. import sys
  3. import time
  4. import logging
  5. import argparse
  6. import hashlib
  7. import sky
  8. import fetch_from
  9. NPM_BASEURL = "http://npm.yandex-team.ru/"
  10. def parse_args():
  11. parser = argparse.ArgumentParser()
  12. fetch_from.add_common_arguments(parser)
  13. parser.add_argument("--name", required=True)
  14. parser.add_argument("--version", required=True)
  15. parser.add_argument("--sky-id", required=True)
  16. parser.add_argument("--integrity", required=True)
  17. parser.add_argument("--integrity-algorithm", required=True)
  18. return parser.parse_args()
  19. def fetch(name, version, sky_id, integrity, integrity_algorithm, file_name, tries=5):
  20. """
  21. :param name: package name
  22. :type name: str
  23. :param version: package version
  24. :type version: str
  25. :param sky_id: sky id of tarball
  26. :type sky_id: str
  27. :param integrity: tarball integrity (hex)
  28. :type integrity: str
  29. :param integrity_algorithm: integrity algorithm (known for openssl)
  30. :type integrity_algorithm: str
  31. :param tries: tries count
  32. :type tries: int
  33. :return: path to fetched file
  34. :rtype: str
  35. """
  36. if sky.is_avaliable():
  37. fetcher = lambda: sky.fetch(sky_id, file_name)
  38. else:
  39. fetcher = lambda: _fetch_via_http(name, version, integrity, integrity_algorithm, file_name)
  40. fetched_file = None
  41. exc_info = None
  42. for i in range(0, tries):
  43. try:
  44. fetched_file = fetcher()
  45. exc_info = None
  46. break
  47. except Exception as e:
  48. logging.exception(e)
  49. exc_info = exc_info or sys.exc_info()
  50. time.sleep(i)
  51. if exc_info:
  52. raise exc_info[0], exc_info[1], exc_info[2]
  53. return fetched_file
  54. def _fetch_via_http(name, version, integrity, integrity_algorithm, file_name):
  55. # Example: "http://npm.yandex-team.ru/@scope/name/-/name-0.0.1.tgz" for @scope/name v0.0.1.
  56. url = NPM_BASEURL + "/".join([name, "-", "{}-{}.tgz".format(name.split("/").pop(), version)])
  57. hashobj = hashlib.new(integrity_algorithm)
  58. fetched_file = fetch_from.fetch_url(url, False, file_name, tries=1, writers=[hashobj.update])
  59. if hashobj.hexdigest() != integrity:
  60. raise fetch_from.BadChecksumFetchError("Expected {}, but got {} for {}".format(
  61. integrity,
  62. hashobj.hexdigest(),
  63. file_name,
  64. ))
  65. return fetched_file
  66. def main(args):
  67. file_name = os.path.basename(args.copy_to)
  68. fetched_file = fetch(args.name, args.version, args.sky_id, args.integrity, args.integrity_algorithm, file_name)
  69. fetch_from.process(fetched_file, file_name, args)
  70. if __name__ == "__main__":
  71. args = parse_args()
  72. fetch_from.setup_logging(args, os.path.basename(__file__))
  73. try:
  74. main(args)
  75. except Exception as e:
  76. logging.exception(e)
  77. print >>sys.stderr, open(args.abs_log_path).read()
  78. sys.stderr.flush()
  79. import error
  80. sys.exit(error.ExitCodes.INFRASTRUCTURE_ERROR if fetch_from.is_temporary(e) else 1)