utils.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. def read_file(filename):
  2. try:
  3. f = file( filename, "r" )
  4. except IOError, reason:
  5. return "" # WARNING: can't raise an error now because of the new RDEPENDS handling. This is a bit ugly. :M:
  6. else:
  7. return f.read().strip()
  8. return None
  9. def ifelse(condition, iftrue = True, iffalse = False):
  10. if condition:
  11. return iftrue
  12. else:
  13. return iffalse
  14. def conditional(variable, checkvalue, truevalue, falsevalue, d):
  15. if bb.data.getVar(variable,d,1) == checkvalue:
  16. return truevalue
  17. else:
  18. return falsevalue
  19. def less_or_equal(variable, checkvalue, truevalue, falsevalue, d):
  20. if float(bb.data.getVar(variable,d,1)) <= float(checkvalue):
  21. return truevalue
  22. else:
  23. return falsevalue
  24. def version_less_or_equal(variable, checkvalue, truevalue, falsevalue, d):
  25. result = bb.vercmp(bb.data.getVar(variable,d,True), checkvalue)
  26. if result <= 0:
  27. return truevalue
  28. else:
  29. return falsevalue
  30. def contains(variable, checkvalues, truevalue, falsevalue, d):
  31. val = bb.data.getVar(variable,d,1)
  32. if not val:
  33. return falsevalue
  34. matches = 0
  35. if type(checkvalues).__name__ == "str":
  36. checkvalues = [checkvalues]
  37. for value in checkvalues:
  38. if val.find(value) != -1:
  39. matches = matches + 1
  40. if matches == len(checkvalues):
  41. return truevalue
  42. return falsevalue
  43. def both_contain(variable1, variable2, checkvalue, d):
  44. if bb.data.getVar(variable1,d,1).find(checkvalue) != -1 and bb.data.getVar(variable2,d,1).find(checkvalue) != -1:
  45. return checkvalue
  46. else:
  47. return ""
  48. def prune_suffix(var, suffixes, d):
  49. # See if var ends with any of the suffixes listed and
  50. # remove it if found
  51. for suffix in suffixes:
  52. if var.endswith(suffix):
  53. return var.replace(suffix, "")
  54. return var
  55. def str_filter(f, str, d):
  56. from re import match
  57. return " ".join(filter(lambda x: match(f, x, 0), str.split()))
  58. def str_filter_out(f, str, d):
  59. from re import match
  60. return " ".join(filter(lambda x: not match(f, x, 0), str.split()))
  61. def param_bool(cfg, field, dflt = None):
  62. """Lookup <field> in <cfg> map and convert it to a boolean; take
  63. <dflt> when this <field> does not exist"""
  64. value = cfg.get(field, dflt)
  65. strvalue = str(value).lower()
  66. if strvalue in ('yes', 'y', 'true', 't', '1'):
  67. return True
  68. elif strvalue in ('no', 'n', 'false', 'f', '0'):
  69. return False
  70. raise ValueError("invalid value for boolean parameter '%s': '%s'" % (field, value))