fetch_from_npm.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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("--tarball-url", required=True)
  14. parser.add_argument("--sky-id", required=True)
  15. parser.add_argument("--integrity", required=True)
  16. parser.add_argument("--integrity-algorithm", required=True)
  17. return parser.parse_args()
  18. def fetch(tarball_url, sky_id, integrity, integrity_algorithm, file_name, tries=5):
  19. """
  20. :param name: package name
  21. :type name: str
  22. :param version: package version
  23. :type version: str
  24. :param sky_id: sky id of tarball
  25. :type sky_id: str
  26. :param integrity: tarball integrity (hex)
  27. :type integrity: str
  28. :param integrity_algorithm: integrity algorithm (known for openssl)
  29. :type integrity_algorithm: str
  30. :param tries: tries count
  31. :type tries: int
  32. :return: path to fetched file
  33. :rtype: str
  34. """
  35. # if sky.is_avaliable() and 'NOTS_FETCH_FROM_HTTP_ONLY' not in os.environ:
  36. # fetcher = lambda: sky.fetch(sky_id, file_name)
  37. # else:
  38. # Отключаем походы через скай
  39. # TODO: https://st.yandex-team.ru/FBP-351
  40. if 'NOTS_FETCH_FROM_SKY' in os.environ and sky.is_avaliable():
  41. fetcher = lambda: sky.fetch(sky_id, file_name)
  42. else:
  43. fetcher = lambda: _fetch_via_http(tarball_url, integrity, integrity_algorithm, file_name)
  44. fetched_file = None
  45. exc_info = None
  46. for i in range(0, tries):
  47. try:
  48. fetched_file = fetcher()
  49. exc_info = None
  50. break
  51. except Exception as e:
  52. logging.exception(e)
  53. exc_info = exc_info or sys.exc_info()
  54. time.sleep(i)
  55. if exc_info:
  56. raise exc_info[0], exc_info[1], exc_info[2]
  57. return fetched_file
  58. def _fetch_via_http(tarball_url, integrity, integrity_algorithm, file_name):
  59. is_abs_url = tarball_url.startswith("https://") or tarball_url.startswith("http://")
  60. url_delim = "" if tarball_url.startswith("/") else "/"
  61. url = tarball_url if is_abs_url else NPM_BASEURL + url_delim + tarball_url
  62. hashobj = hashlib.new(integrity_algorithm)
  63. fetched_file = fetch_from.fetch_url(url, False, file_name, tries=1, writers=[hashobj.update])
  64. if hashobj.hexdigest() != integrity:
  65. raise fetch_from.BadChecksumFetchError("Expected {}, but got {} for {}".format(
  66. integrity,
  67. hashobj.hexdigest(),
  68. file_name,
  69. ))
  70. return fetched_file
  71. def main(args):
  72. file_name = os.path.basename(args.copy_to)
  73. fetched_file = fetch(args.tarball_url, args.sky_id, args.integrity, args.integrity_algorithm, file_name)
  74. fetch_from.process(fetched_file, file_name, args)
  75. if __name__ == "__main__":
  76. args = parse_args()
  77. fetch_from.setup_logging(args, os.path.basename(__file__))
  78. try:
  79. main(args)
  80. except Exception as e:
  81. logging.exception(e)
  82. print >>sys.stderr, open(args.abs_log_path).read()
  83. sys.stderr.flush()
  84. import error
  85. sys.exit(error.ExitCodes.INFRASTRUCTURE_ERROR if fetch_from.is_temporary(e) else 1)