_common.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import six
  2. import sys
  3. import hashlib
  4. import base64
  5. class Result(object):
  6. pass
  7. def lazy(func):
  8. result = Result()
  9. def wrapper():
  10. try:
  11. return result._result
  12. except AttributeError:
  13. result._result = func()
  14. return result._result
  15. return wrapper
  16. def pathid(path):
  17. return six.ensure_str(base64.b32encode(hashlib.md5(six.ensure_binary(path)).digest()).lower().strip(b'='))
  18. def listid(items):
  19. return pathid(str(sorted(items)))
  20. def unpair(lst):
  21. for x, y in lst:
  22. yield x
  23. yield y
  24. def iterpair(lst):
  25. y = None
  26. for x in lst:
  27. if y:
  28. yield (y, x)
  29. y = None
  30. else:
  31. y = x
  32. def stripext(fname):
  33. return fname[: fname.rfind('.')]
  34. def tobuilddir(fname):
  35. if not fname:
  36. return '$B'
  37. if fname.startswith('$S'):
  38. return fname.replace('$S', '$B', 1)
  39. else:
  40. return fname
  41. def before(s, ss):
  42. p = s.find(ss)
  43. if p == -1:
  44. return s
  45. return s[:p]
  46. def sort_by_keywords(keywords, args):
  47. flat = []
  48. res = {}
  49. cur_key = None
  50. limit = -1
  51. for arg in args:
  52. if arg in keywords:
  53. limit = keywords[arg]
  54. if limit == 0:
  55. res[arg] = True
  56. cur_key = None
  57. limit = -1
  58. else:
  59. cur_key = arg
  60. continue
  61. if limit == 0:
  62. cur_key = None
  63. limit = -1
  64. if cur_key:
  65. if cur_key in res:
  66. res[cur_key].append(arg)
  67. else:
  68. res[cur_key] = [arg]
  69. limit -= 1
  70. else:
  71. flat.append(arg)
  72. return (flat, res)
  73. def get_norm_unit_path(unit, extra=None):
  74. path = strip_roots(unit.path())
  75. if extra:
  76. return '{}/{}'.format(path, extra)
  77. return path
  78. def resolve_common_const(path):
  79. if path.startswith('${ARCADIA_ROOT}'):
  80. return path.replace('${ARCADIA_ROOT}', '$S', 1)
  81. if path.startswith('${ARCADIA_BUILD_ROOT}'):
  82. return path.replace('${ARCADIA_BUILD_ROOT}', '$B', 1)
  83. return path
  84. def resolve_to_abs_path(path, source_root, build_root):
  85. if path.startswith('$S') and source_root is not None:
  86. return path.replace('$S', source_root, 1)
  87. if path.startswith('$B') and build_root is not None:
  88. return path.replace('$B', build_root, 1)
  89. return path
  90. def resolve_to_ymake_path(path):
  91. return resolve_to_abs_path(path, '${ARCADIA_ROOT}', '${ARCADIA_BUILD_ROOT}')
  92. def join_intl_paths(*args):
  93. return '/'.join(args)
  94. def get(fun, num):
  95. return fun()[num][0]
  96. def make_tuples(arg_list):
  97. def tpl():
  98. for x in arg_list:
  99. yield (x, [])
  100. return list(tpl())
  101. def resolve_includes(unit, src, paths):
  102. return unit.resolve_include([src] + paths) if paths else []
  103. def rootrel_arc_src(src, unit):
  104. if src.startswith('${ARCADIA_ROOT}/'):
  105. return src[16:]
  106. if src.startswith('${ARCADIA_BUILD_ROOT}/'):
  107. return src[22:]
  108. elif src.startswith('${CURDIR}/'):
  109. return unit.path()[3:] + '/' + src[10:]
  110. else:
  111. resolved = unit.resolve_arc_path(src)
  112. if resolved.startswith('$S/'):
  113. return resolved[3:]
  114. return src # leave as is
  115. def skip_build_root(x):
  116. if x.startswith('${ARCADIA_BUILD_ROOT}'):
  117. return x[len('${ARCADIA_BUILD_ROOT}') :].lstrip('/')
  118. return x
  119. def get_interpreter_path():
  120. interpreter_path = [sys.executable]
  121. if 'ymake' in interpreter_path[0]:
  122. interpreter_path.append('--python')
  123. return interpreter_path
  124. def filter_out_by_keyword(test_data, keyword):
  125. def _iterate():
  126. i = 0
  127. while i < len(test_data):
  128. if test_data[i] == keyword:
  129. i += 2
  130. else:
  131. yield test_data[i]
  132. i += 1
  133. return list(_iterate())
  134. def generate_chunks(lst, chunk_size):
  135. for i in xrange(0, len(lst), chunk_size):
  136. yield lst[i : (i + chunk_size)]
  137. def strip_roots(path):
  138. for prefix in ["$B/", "$S/"]:
  139. if path.startswith(prefix):
  140. return path[len(prefix) :]
  141. return path
  142. def to_yesno(x):
  143. return "yes" if x else "no"
  144. def get_no_lint_value(unit):
  145. import ymake
  146. supported_no_lint_values = ('none', 'none_internal', 'ktlint')
  147. no_lint_value = unit.get('_NO_LINT_VALUE')
  148. if no_lint_value and no_lint_value not in supported_no_lint_values:
  149. ymake.report_configure_error('Unsupported value for NO_LINT macro: {}'.format(no_lint_value))
  150. return no_lint_value