_common.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import six
  2. import hashlib
  3. import base64
  4. class Result(object):
  5. pass
  6. def lazy(func):
  7. result = Result()
  8. def wrapper(*args, **kwargs):
  9. try:
  10. return result._result
  11. except AttributeError:
  12. result._result = func(*args, **kwargs)
  13. return result._result
  14. return wrapper
  15. def cache_by_second_arg(func):
  16. result = {}
  17. def wrapper(arg0, arg1, *args, **kwargs):
  18. try:
  19. return result[arg1]
  20. except KeyError:
  21. result[arg1] = func(arg0, arg1, *args, **kwargs)
  22. return result[arg1]
  23. return wrapper
  24. def pathid(path):
  25. return six.ensure_str(base64.b32encode(hashlib.md5(six.ensure_binary(path)).digest()).lower().strip(b'='))
  26. def listid(items):
  27. return pathid(str(sorted(items)))
  28. def sort_uniq(items):
  29. return sorted(set(items))
  30. def stripext(fname):
  31. return fname[: fname.rfind('.')]
  32. def tobuilddir(fname):
  33. if not fname:
  34. return '$B'
  35. if fname.startswith('$S'):
  36. return fname.replace('$S', '$B', 1)
  37. else:
  38. return fname
  39. def sort_by_keywords(keywords, args):
  40. flat = []
  41. res = {}
  42. cur_key = None
  43. limit = -1
  44. for arg in args:
  45. if arg in keywords:
  46. limit = keywords[arg]
  47. if limit == 0:
  48. res[arg] = True
  49. cur_key = None
  50. limit = -1
  51. else:
  52. cur_key = arg
  53. continue
  54. if limit == 0:
  55. cur_key = None
  56. limit = -1
  57. if cur_key:
  58. if cur_key in res:
  59. res[cur_key].append(arg)
  60. else:
  61. res[cur_key] = [arg]
  62. limit -= 1
  63. else:
  64. flat.append(arg)
  65. return (flat, res)
  66. def get_norm_unit_path(unit, extra=None):
  67. path = strip_roots(unit.path())
  68. if extra:
  69. return '{}/{}'.format(path, extra)
  70. return path
  71. def resolve_common_const(path):
  72. if path.startswith('${ARCADIA_ROOT}'):
  73. return path.replace('${ARCADIA_ROOT}', '$S', 1)
  74. if path.startswith('${ARCADIA_BUILD_ROOT}'):
  75. return path.replace('${ARCADIA_BUILD_ROOT}', '$B', 1)
  76. return path
  77. def get(fun, num):
  78. return fun()[num][0]
  79. def resolve_includes(unit, src, paths):
  80. return unit.resolve_include([src] + paths) if paths else []
  81. def rootrel_arc_src(src, unit):
  82. if src.startswith('${ARCADIA_ROOT}/'):
  83. return src[16:]
  84. if src.startswith('${ARCADIA_BUILD_ROOT}/'):
  85. return src[22:]
  86. elif src.startswith('${CURDIR}/'):
  87. return unit.path()[3:] + '/' + src[10:]
  88. else:
  89. resolved = unit.resolve_arc_path(src)
  90. if resolved.startswith('$S/'):
  91. return resolved[3:]
  92. return src # leave as is
  93. def skip_build_root(x):
  94. if x.startswith('${ARCADIA_BUILD_ROOT}'):
  95. return x[len('${ARCADIA_BUILD_ROOT}') :].lstrip('/')
  96. return x
  97. def filter_out_by_keyword(test_data, keyword):
  98. def _iterate():
  99. i = 0
  100. while i < len(test_data):
  101. if test_data[i] == keyword:
  102. i += 2
  103. else:
  104. yield test_data[i]
  105. i += 1
  106. return list(_iterate())
  107. def strip_roots(path):
  108. for prefix in ["$B/", "$S/"]:
  109. if path.startswith(prefix):
  110. return path[len(prefix) :]
  111. return path
  112. def to_yesno(x):
  113. return "yes" if x else "no"
  114. def get_no_lint_value(unit):
  115. import ymake
  116. supported_no_lint_values = ('none', 'none_internal', 'ktlint')
  117. no_lint_value = unit.get('_NO_LINT_VALUE')
  118. if no_lint_value and no_lint_value not in supported_no_lint_values:
  119. ymake.report_configure_error('Unsupported value for NO_LINT macro: {}'.format(no_lint_value))
  120. return no_lint_value
  121. def ugly_conftest_exception(path):
  122. """
  123. FIXME:
  124. TAXICOMMON-9288: Taxi abused bug with absolute paths and built conftest descovery upon it
  125. until the issue is filed let's limit impact only to existing files.
  126. Never let this list grow!!! Fix issue before adding any new violating conftests
  127. """
  128. exceptions = [
  129. 'taxi/uservices/userver-arc-utils/functional_tests/basic/conftest.py',
  130. 'taxi/uservices/userver-arc-utils/functional_tests/basic_chaos/conftest.py',
  131. 'taxi/uservices/userver-arc-utils/functional_tests/json2yaml/conftest.py',
  132. ]
  133. if not path.endswith('conftest.py'):
  134. return False
  135. for e in exceptions:
  136. if path.endswith(e):
  137. return True
  138. return False