summaryrefslogtreecommitdiffstats
path: root/meta/lib/oe/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'meta/lib/oe/utils.py')
-rw-r--r--meta/lib/oe/utils.py166
1 files changed, 166 insertions, 0 deletions
diff --git a/meta/lib/oe/utils.py b/meta/lib/oe/utils.py
new file mode 100644
index 0000000..defa536
--- /dev/null
+++ b/meta/lib/oe/utils.py
@@ -0,0 +1,166 @@
1try:
2 # Python 2
3 import commands as cmdstatus
4except ImportError:
5 # Python 3
6 import subprocess as cmdstatus
7
8def read_file(filename):
9 try:
10 f = open( filename, "r" )
11 except IOError as reason:
12 return "" # WARNING: can't raise an error now because of the new RDEPENDS handling. This is a bit ugly. :M:
13 else:
14 data = f.read().strip()
15 f.close()
16 return data
17 return None
18
19def ifelse(condition, iftrue = True, iffalse = False):
20 if condition:
21 return iftrue
22 else:
23 return iffalse
24
25def conditional(variable, checkvalue, truevalue, falsevalue, d):
26 if d.getVar(variable,1) == checkvalue:
27 return truevalue
28 else:
29 return falsevalue
30
31def less_or_equal(variable, checkvalue, truevalue, falsevalue, d):
32 if float(d.getVar(variable,1)) <= float(checkvalue):
33 return truevalue
34 else:
35 return falsevalue
36
37def version_less_or_equal(variable, checkvalue, truevalue, falsevalue, d):
38 result = bb.utils.vercmp_string(d.getVar(variable,True), checkvalue)
39 if result <= 0:
40 return truevalue
41 else:
42 return falsevalue
43
44def contains(variable, checkvalues, truevalue, falsevalue, d):
45 val = d.getVar(variable, True)
46 if not val:
47 return falsevalue
48 val = set(val.split())
49 if isinstance(checkvalues, basestring):
50 checkvalues = set(checkvalues.split())
51 else:
52 checkvalues = set(checkvalues)
53 if checkvalues.issubset(val):
54 return truevalue
55 return falsevalue
56
57def both_contain(variable1, variable2, checkvalue, d):
58 if d.getVar(variable1,1).find(checkvalue) != -1 and d.getVar(variable2,1).find(checkvalue) != -1:
59 return checkvalue
60 else:
61 return ""
62
63def prune_suffix(var, suffixes, d):
64 # See if var ends with any of the suffixes listed and
65 # remove it if found
66 for suffix in suffixes:
67 if var.endswith(suffix):
68 var = var.replace(suffix, "")
69
70 prefix = d.getVar("MLPREFIX", True)
71 if prefix and var.startswith(prefix):
72 var = var.replace(prefix, "")
73
74 return var
75
76def str_filter(f, str, d):
77 from re import match
78 return " ".join(filter(lambda x: match(f, x, 0), str.split()))
79
80def str_filter_out(f, str, d):
81 from re import match
82 return " ".join(filter(lambda x: not match(f, x, 0), str.split()))
83
84def param_bool(cfg, field, dflt = None):
85 """Lookup <field> in <cfg> map and convert it to a boolean; take
86 <dflt> when this <field> does not exist"""
87 value = cfg.get(field, dflt)
88 strvalue = str(value).lower()
89 if strvalue in ('yes', 'y', 'true', 't', '1'):
90 return True
91 elif strvalue in ('no', 'n', 'false', 'f', '0'):
92 return False
93 raise ValueError("invalid value for boolean parameter '%s': '%s'" % (field, value))
94
95def inherits(d, *classes):
96 """Return True if the metadata inherits any of the specified classes"""
97 return any(bb.data.inherits_class(cls, d) for cls in classes)
98
99def features_backfill(var,d):
100 # This construct allows the addition of new features to variable specified
101 # as var
102 # Example for var = "DISTRO_FEATURES"
103 # This construct allows the addition of new features to DISTRO_FEATURES
104 # that if not present would disable existing functionality, without
105 # disturbing distributions that have already set DISTRO_FEATURES.
106 # Distributions wanting to elide a value in DISTRO_FEATURES_BACKFILL should
107 # add the feature to DISTRO_FEATURES_BACKFILL_CONSIDERED
108 features = (d.getVar(var, True) or "").split()
109 backfill = (d.getVar(var+"_BACKFILL", True) or "").split()
110 considered = (d.getVar(var+"_BACKFILL_CONSIDERED", True) or "").split()
111
112 addfeatures = []
113 for feature in backfill:
114 if feature not in features and feature not in considered:
115 addfeatures.append(feature)
116
117 if addfeatures:
118 d.appendVar(var, " " + " ".join(addfeatures))
119
120
121def packages_filter_out_system(d):
122 """
123 Return a list of packages from PACKAGES with the "system" packages such as
124 PN-dbg PN-doc PN-locale-eb-gb removed.
125 """
126 pn = d.getVar('PN', True)
127 blacklist = map(lambda suffix: pn + suffix, ('', '-dbg', '-dev', '-doc', '-locale', '-staticdev'))
128 localepkg = pn + "-locale-"
129 pkgs = []
130
131 for pkg in d.getVar('PACKAGES', True).split():
132 if pkg not in blacklist and localepkg not in pkg:
133 pkgs.append(pkg)
134 return pkgs
135
136def getstatusoutput(cmd):
137 return cmdstatus.getstatusoutput(cmd)
138
139
140def trim_version(version, num_parts=2):
141 """
142 Return just the first <num_parts> of <version>, split by periods. For
143 example, trim_version("1.2.3", 2) will return "1.2".
144 """
145 if type(version) is not str:
146 raise TypeError("Version should be a string")
147 if num_parts < 1:
148 raise ValueError("Cannot split to parts < 1")
149
150 parts = version.split(".")
151 trimmed = ".".join(parts[:num_parts])
152 return trimmed
153
154def cpu_count():
155 import multiprocessing
156 return multiprocessing.cpu_count()
157
158def execute_pre_post_process(d, cmds):
159 if cmds is None:
160 return
161
162 for cmd in cmds.strip().split(';'):
163 cmd = cmd.strip()
164 if cmd != '':
165 bb.note("Executing %s ..." % cmd)
166 bb.build.exec_func(cmd, d)