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