_common.py 4.1 KB

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