diff options
Diffstat (limited to 'scripts/lib/compatlayer/__init__.py')
-rw-r--r-- | scripts/lib/compatlayer/__init__.py | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/scripts/lib/compatlayer/__init__.py b/scripts/lib/compatlayer/__init__.py new file mode 100644 index 0000000000..b3a166aa9a --- /dev/null +++ b/scripts/lib/compatlayer/__init__.py | |||
@@ -0,0 +1,163 @@ | |||
1 | # Yocto Project compatibility layer tool | ||
2 | # | ||
3 | # Copyright (C) 2017 Intel Corporation | ||
4 | # Released under the MIT license (see COPYING.MIT) | ||
5 | |||
6 | import os | ||
7 | from enum import Enum | ||
8 | |||
9 | class LayerType(Enum): | ||
10 | BSP = 0 | ||
11 | DISTRO = 1 | ||
12 | SOFTWARE = 2 | ||
13 | ERROR_NO_LAYER_CONF = 98 | ||
14 | ERROR_BSP_DISTRO = 99 | ||
15 | |||
16 | def _get_configurations(path): | ||
17 | configs = [] | ||
18 | |||
19 | for f in os.listdir(path): | ||
20 | file_path = os.path.join(path, f) | ||
21 | if os.path.isfile(file_path) and f.endswith('.conf'): | ||
22 | configs.append(f[:-5]) # strip .conf | ||
23 | return configs | ||
24 | |||
25 | def _get_layer_collections(layer_path, lconf=None, data=None): | ||
26 | import bb.parse | ||
27 | import bb.data | ||
28 | |||
29 | if lconf is None: | ||
30 | lconf = os.path.join(layer_path, 'conf', 'layer.conf') | ||
31 | |||
32 | if data is None: | ||
33 | ldata = bb.data.init() | ||
34 | bb.parse.init_parser(ldata) | ||
35 | else: | ||
36 | ldata = data.createCopy() | ||
37 | |||
38 | ldata.setVar('LAYERDIR', layer_path) | ||
39 | try: | ||
40 | ldata = bb.parse.handle(lconf, ldata, include=True) | ||
41 | except BaseException as exc: | ||
42 | raise LayerError(exc) | ||
43 | ldata.expandVarref('LAYERDIR') | ||
44 | |||
45 | collections = (ldata.getVar('BBFILE_COLLECTIONS', True) or '').split() | ||
46 | if not collections: | ||
47 | name = os.path.basename(layer_path) | ||
48 | collections = [name] | ||
49 | |||
50 | collections = {c: {} for c in collections} | ||
51 | for name in collections: | ||
52 | priority = ldata.getVar('BBFILE_PRIORITY_%s' % name, True) | ||
53 | pattern = ldata.getVar('BBFILE_PATTERN_%s' % name, True) | ||
54 | depends = ldata.getVar('LAYERDEPENDS_%s' % name, True) | ||
55 | collections[name]['priority'] = priority | ||
56 | collections[name]['pattern'] = pattern | ||
57 | collections[name]['depends'] = depends | ||
58 | |||
59 | return collections | ||
60 | |||
61 | def _detect_layer(layer_path): | ||
62 | """ | ||
63 | Scans layer directory to detect what type of layer | ||
64 | is BSP, Distro or Software. | ||
65 | |||
66 | Returns a dictionary with layer name, type and path. | ||
67 | """ | ||
68 | |||
69 | layer = {} | ||
70 | layer_name = os.path.basename(layer_path) | ||
71 | |||
72 | layer['name'] = layer_name | ||
73 | layer['path'] = layer_path | ||
74 | layer['conf'] = {} | ||
75 | |||
76 | if not os.path.isfile(os.path.join(layer_path, 'conf', 'layer.conf')): | ||
77 | layer['type'] = LayerType.ERROR_NO_LAYER_CONF | ||
78 | return layer | ||
79 | |||
80 | machine_conf = os.path.join(layer_path, 'conf', 'machine') | ||
81 | distro_conf = os.path.join(layer_path, 'conf', 'distro') | ||
82 | |||
83 | is_bsp = False | ||
84 | is_distro = False | ||
85 | |||
86 | if os.path.isdir(machine_conf): | ||
87 | machines = _get_configurations(machine_conf) | ||
88 | if machines: | ||
89 | is_bsp = True | ||
90 | |||
91 | if os.path.isdir(distro_conf): | ||
92 | distros = _get_configurations(distro_conf) | ||
93 | if distros: | ||
94 | is_distro = True | ||
95 | |||
96 | if is_bsp and is_distro: | ||
97 | layer['type'] = LayerType.ERROR_BSP_DISTRO | ||
98 | elif is_bsp: | ||
99 | layer['type'] = LayerType.BSP | ||
100 | layer['conf']['machines'] = machines | ||
101 | elif is_distro: | ||
102 | layer['type'] = LayerType.DISTRO | ||
103 | layer['conf']['distros'] = distros | ||
104 | else: | ||
105 | layer['type'] = LayerType.SOFTWARE | ||
106 | |||
107 | layer['collections'] = _get_layer_collections(layer['path']) | ||
108 | |||
109 | return layer | ||
110 | |||
111 | def detect_layers(layer_directories): | ||
112 | layers = [] | ||
113 | |||
114 | for directory in layer_directories: | ||
115 | if directory[-1] == '/': | ||
116 | directory = directory[0:-1] | ||
117 | |||
118 | for root, dirs, files in os.walk(directory): | ||
119 | dir_name = os.path.basename(root) | ||
120 | conf_dir = os.path.join(root, 'conf') | ||
121 | if dir_name.startswith('meta-') and os.path.isdir(conf_dir): | ||
122 | layer = _detect_layer(root) | ||
123 | if layer: | ||
124 | layers.append(layer) | ||
125 | |||
126 | return layers | ||
127 | |||
128 | def add_layer(bblayersconf, layer): | ||
129 | with open(bblayersconf, 'a+') as f: | ||
130 | f.write("\nBBLAYERS += \"%s\"\n" % layer['path']) | ||
131 | |||
132 | def get_signatures(builddir, failsafe=False): | ||
133 | import subprocess | ||
134 | import re | ||
135 | |||
136 | sigs = {} | ||
137 | |||
138 | try: | ||
139 | cmd = 'bitbake ' | ||
140 | if failsafe: | ||
141 | cmd += '-k ' | ||
142 | cmd += '-S none world' | ||
143 | output = subprocess.check_output(cmd, shell=True, | ||
144 | stderr=subprocess.PIPE) | ||
145 | except subprocess.CalledProcessError as e: | ||
146 | import traceback | ||
147 | exc = traceback.format_exc() | ||
148 | msg = '%s\n%s\n' % (exc, e.output.decode('utf-8')) | ||
149 | raise RuntimeError(msg) | ||
150 | sigs_file = os.path.join(builddir, 'locked-sigs.inc') | ||
151 | |||
152 | sig_regex = re.compile("^(?P<task>.*:.*):(?P<hash>.*) .$") | ||
153 | with open(sigs_file, 'r') as f: | ||
154 | for line in f.readlines(): | ||
155 | line = line.strip() | ||
156 | s = sig_regex.match(line) | ||
157 | if s: | ||
158 | sigs[s.group('task')] = s.group('hash') | ||
159 | |||
160 | if not sigs: | ||
161 | raise RuntimeError('Can\'t load signatures from %s' % sigs_file) | ||
162 | |||
163 | return sigs | ||