_common.py 3.9 KB

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