_common.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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(*args, **kwargs):
  10. try:
  11. return result._result
  12. except AttributeError:
  13. result._result = func(*args, **kwargs)
  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 sort_uniq(items):
  21. return sorted(set(items))
  22. def stripext(fname):
  23. return fname[: fname.rfind('.')]
  24. def tobuilddir(fname):
  25. if not fname:
  26. return '$B'
  27. if fname.startswith('$S'):
  28. return fname.replace('$S', '$B', 1)
  29. else:
  30. return fname
  31. def before(s, ss):
  32. p = s.find(ss)
  33. if p == -1:
  34. return s
  35. return s[:p]
  36. def sort_by_keywords(keywords, args):
  37. flat = []
  38. res = {}
  39. cur_key = None
  40. limit = -1
  41. for arg in args:
  42. if arg in keywords:
  43. limit = keywords[arg]
  44. if limit == 0:
  45. res[arg] = True
  46. cur_key = None
  47. limit = -1
  48. else:
  49. cur_key = arg
  50. continue
  51. if limit == 0:
  52. cur_key = None
  53. limit = -1
  54. if cur_key:
  55. if cur_key in res:
  56. res[cur_key].append(arg)
  57. else:
  58. res[cur_key] = [arg]
  59. limit -= 1
  60. else:
  61. flat.append(arg)
  62. return (flat, res)
  63. def get_norm_unit_path(unit, extra=None):
  64. path = strip_roots(unit.path())
  65. if extra:
  66. return '{}/{}'.format(path, extra)
  67. return path
  68. def resolve_common_const(path):
  69. if path.startswith('${ARCADIA_ROOT}'):
  70. return path.replace('${ARCADIA_ROOT}', '$S', 1)
  71. if path.startswith('${ARCADIA_BUILD_ROOT}'):
  72. return path.replace('${ARCADIA_BUILD_ROOT}', '$B', 1)
  73. return path
  74. def resolve_to_abs_path(path, source_root, build_root):
  75. if path.startswith('$S') and source_root is not None:
  76. return path.replace('$S', source_root, 1)
  77. if path.startswith('$B') and build_root is not None:
  78. return path.replace('$B', build_root, 1)
  79. return path
  80. def resolve_to_ymake_path(path):
  81. return resolve_to_abs_path(path, '${ARCADIA_ROOT}', '${ARCADIA_BUILD_ROOT}')
  82. def get(fun, num):
  83. return fun()[num][0]
  84. def make_tuples(arg_list):
  85. def tpl():
  86. for x in arg_list:
  87. yield (x, [])
  88. return list(tpl())
  89. def resolve_includes(unit, src, paths):
  90. return unit.resolve_include([src] + paths) if paths else []
  91. def rootrel_arc_src(src, unit):
  92. if src.startswith('${ARCADIA_ROOT}/'):
  93. return src[16:]
  94. if src.startswith('${ARCADIA_BUILD_ROOT}/'):
  95. return src[22:]
  96. elif src.startswith('${CURDIR}/'):
  97. return unit.path()[3:] + '/' + src[10:]
  98. else:
  99. resolved = unit.resolve_arc_path(src)
  100. if resolved.startswith('$S/'):
  101. return resolved[3:]
  102. return src # leave as is
  103. def skip_build_root(x):
  104. if x.startswith('${ARCADIA_BUILD_ROOT}'):
  105. return x[len('${ARCADIA_BUILD_ROOT}') :].lstrip('/')
  106. return x
  107. def get_interpreter_path():
  108. interpreter_path = [sys.executable]
  109. if 'ymake' in interpreter_path[0]:
  110. interpreter_path.append('--python')
  111. return interpreter_path
  112. def filter_out_by_keyword(test_data, keyword):
  113. def _iterate():
  114. i = 0
  115. while i < len(test_data):
  116. if test_data[i] == keyword:
  117. i += 2
  118. else:
  119. yield test_data[i]
  120. i += 1
  121. return list(_iterate())
  122. def strip_roots(path):
  123. for prefix in ["$B/", "$S/"]:
  124. if path.startswith(prefix):
  125. return path[len(prefix) :]
  126. return path
  127. def to_yesno(x):
  128. return "yes" if x else "no"
  129. def get_no_lint_value(unit):
  130. import ymake
  131. supported_no_lint_values = ('none', 'none_internal', 'ktlint')
  132. no_lint_value = unit.get('_NO_LINT_VALUE')
  133. if no_lint_value and no_lint_value not in supported_no_lint_values:
  134. ymake.report_configure_error('Unsupported value for NO_LINT macro: {}'.format(no_lint_value))
  135. return no_lint_value