summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--scripts/lib/compatlayer/__init__.py163
-rw-r--r--scripts/lib/compatlayer/case.py7
-rw-r--r--scripts/lib/compatlayer/cases/__init__.py0
-rw-r--r--scripts/lib/compatlayer/cases/bsp.py26
-rw-r--r--scripts/lib/compatlayer/cases/common.py66
-rw-r--r--scripts/lib/compatlayer/cases/distro.py26
-rw-r--r--scripts/lib/compatlayer/context.py14
-rwxr-xr-xscripts/yocto-compat-layer.py153
8 files changed, 455 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
6import os
7from enum import Enum
8
9class LayerType(Enum):
10 BSP = 0
11 DISTRO = 1
12 SOFTWARE = 2
13 ERROR_NO_LAYER_CONF = 98
14 ERROR_BSP_DISTRO = 99
15
16def _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
25def _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
61def _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
111def 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
128def add_layer(bblayersconf, layer):
129 with open(bblayersconf, 'a+') as f:
130 f.write("\nBBLAYERS += \"%s\"\n" % layer['path'])
131
132def 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
diff --git a/scripts/lib/compatlayer/case.py b/scripts/lib/compatlayer/case.py
new file mode 100644
index 0000000000..54ce78aa60
--- /dev/null
+++ b/scripts/lib/compatlayer/case.py
@@ -0,0 +1,7 @@
1# Copyright (C) 2017 Intel Corporation
2# Released under the MIT license (see COPYING.MIT)
3
4from oeqa.core.case import OETestCase
5
6class OECompatLayerTestCase(OETestCase):
7 pass
diff --git a/scripts/lib/compatlayer/cases/__init__.py b/scripts/lib/compatlayer/cases/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/scripts/lib/compatlayer/cases/__init__.py
diff --git a/scripts/lib/compatlayer/cases/bsp.py b/scripts/lib/compatlayer/cases/bsp.py
new file mode 100644
index 0000000000..5d9bf93e4a
--- /dev/null
+++ b/scripts/lib/compatlayer/cases/bsp.py
@@ -0,0 +1,26 @@
1# Copyright (C) 2017 Intel Corporation
2# Released under the MIT license (see COPYING.MIT)
3
4import unittest
5
6from compatlayer import LayerType
7from compatlayer.case import OECompatLayerTestCase
8
9class BSPCompatLayer(OECompatLayerTestCase):
10 @classmethod
11 def setUpClass(self):
12 if self.tc.layer['type'] != LayerType.BSP:
13 raise unittest.SkipTest("BSPCompatLayer: Layer %s isn't BSP one." %\
14 self.tc.layer['name'])
15
16 def test_bsp_defines_machines(self):
17 self.assertTrue(self.tc.layer['conf']['machines'],
18 "Layer is BSP but doesn't defines machines.")
19
20 def test_bsp_no_set_machine(self):
21 from oeqa.utils.commands import get_bb_var
22
23 machine = get_bb_var('MACHINE')
24 self.assertEqual(self.td['bbvars']['MACHINE'], machine,
25 msg="Layer %s modified machine %s -> %s" % \
26 (self.tc.layer['name'], self.td['bbvars']['MACHINE'], machine))
diff --git a/scripts/lib/compatlayer/cases/common.py b/scripts/lib/compatlayer/cases/common.py
new file mode 100644
index 0000000000..4d328ec1f1
--- /dev/null
+++ b/scripts/lib/compatlayer/cases/common.py
@@ -0,0 +1,66 @@
1# Copyright (C) 2017 Intel Corporation
2# Released under the MIT license (see COPYING.MIT)
3
4import os
5import subprocess
6import unittest
7from compatlayer import get_signatures, LayerType
8from compatlayer.case import OECompatLayerTestCase
9
10class CommonCompatLayer(OECompatLayerTestCase):
11 def test_readme(self):
12 readme_file = os.path.join(self.tc.layer['path'], 'README')
13 self.assertTrue(os.path.isfile(readme_file),
14 msg="Layer doesn't contains README file.")
15
16 data = ''
17 with open(readme_file, 'r') as f:
18 data = f.read()
19 self.assertTrue(data,
20 msg="Layer contains README file but is empty.")
21
22 def test_parse(self):
23 try:
24 output = subprocess.check_output('bitbake -p', shell=True,
25 stderr=subprocess.PIPE)
26 except subprocess.CalledProcessError as e:
27 import traceback
28 exc = traceback.format_exc()
29 msg = 'Layer %s failed to parse.\n%s\n%s\n' % (self.tc.layer['name'],
30 exc, e.output.decode('utf-8'))
31 raise RuntimeError(msg)
32
33 def test_show_environment(self):
34 try:
35 output = subprocess.check_output('bitbake -e', shell=True,
36 stderr=subprocess.PIPE)
37 except subprocess.CalledProcessError as e:
38 import traceback
39 exc = traceback.format_exc()
40 msg = 'Layer %s failed to show environment.\n%s\n%s\n' % \
41 (self.tc.layer['name'], exc, e.output.decode('utf-8'))
42 raise RuntimeError(msg)
43
44 def test_signatures(self):
45 if self.tc.layer['type'] == LayerType.SOFTWARE:
46 raise unittest.SkipTest("Layer %s isn't BSP or DISTRO one." \
47 % self.tc.layer['name'])
48
49 sig_diff = {}
50
51 curr_sigs = get_signatures(self.td['builddir'], failsafe=True)
52 for task in self.td['sigs']:
53 if task not in curr_sigs:
54 continue
55
56 if self.td['sigs'][task] != curr_sigs[task]:
57 sig_diff[task] = '%s -> %s' % \
58 (self.td['sigs'][task], curr_sigs[task])
59
60 detail = ''
61 if sig_diff:
62 for task in sig_diff:
63 detail += "%s changed %s\n" % (task, sig_diff[task])
64 self.assertFalse(bool(sig_diff), "Layer %s changed signatures.\n%s" % \
65 (self.tc.layer['name'], detail))
66
diff --git a/scripts/lib/compatlayer/cases/distro.py b/scripts/lib/compatlayer/cases/distro.py
new file mode 100644
index 0000000000..523acc1e78
--- /dev/null
+++ b/scripts/lib/compatlayer/cases/distro.py
@@ -0,0 +1,26 @@
1# Copyright (C) 2017 Intel Corporation
2# Released under the MIT license (see COPYING.MIT)
3
4import unittest
5
6from compatlayer import LayerType
7from compatlayer.case import OECompatLayerTestCase
8
9class DistroCompatLayer(OECompatLayerTestCase):
10 @classmethod
11 def setUpClass(self):
12 if self.tc.layer['type'] != LayerType.DISTRO:
13 raise unittest.SkipTest("DistroCompatLayer: Layer %s isn't Distro one." %\
14 self.tc.layer['name'])
15
16 def test_distro_defines_distros(self):
17 self.assertTrue(self.tc.layer['conf']['distros'],
18 "Layer is BSP but doesn't defines machines.")
19
20 def test_distro_no_set_distros(self):
21 from oeqa.utils.commands import get_bb_var
22
23 distro = get_bb_var('DISTRO')
24 self.assertEqual(self.td['bbvars']['DISTRO'], distro,
25 msg="Layer %s modified distro %s -> %s" % \
26 (self.tc.layer['name'], self.td['bbvars']['DISTRO'], distro))
diff --git a/scripts/lib/compatlayer/context.py b/scripts/lib/compatlayer/context.py
new file mode 100644
index 0000000000..4932238798
--- /dev/null
+++ b/scripts/lib/compatlayer/context.py
@@ -0,0 +1,14 @@
1# Copyright (C) 2017 Intel Corporation
2# Released under the MIT license (see COPYING.MIT)
3
4import os
5import sys
6import glob
7import re
8
9from oeqa.core.context import OETestContext
10
11class CompatLayerTestContext(OETestContext):
12 def __init__(self, td=None, logger=None, layer=None):
13 super(CompatLayerTestContext, self).__init__(td, logger)
14 self.layer = layer
diff --git a/scripts/yocto-compat-layer.py b/scripts/yocto-compat-layer.py
new file mode 100755
index 0000000000..09dc5bf450
--- /dev/null
+++ b/scripts/yocto-compat-layer.py
@@ -0,0 +1,153 @@
1#!/usr/bin/env python3
2
3# Yocto Project compatibility layer tool
4#
5# Copyright (C) 2017 Intel Corporation
6# Released under the MIT license (see COPYING.MIT)
7
8import os
9import sys
10import argparse
11import logging
12import time
13import signal
14import shutil
15import collections
16
17scripts_path = os.path.dirname(os.path.realpath(__file__))
18lib_path = scripts_path + '/lib'
19sys.path = sys.path + [lib_path]
20import scriptutils
21import scriptpath
22scriptpath.add_oe_lib_path()
23scriptpath.add_bitbake_lib_path()
24
25from compatlayer import LayerType, detect_layers, add_layer, get_signatures
26from oeqa.utils.commands import get_bb_vars
27
28PROGNAME = 'yocto-compat-layer'
29DEFAULT_OUTPUT_LOG = '%s-%s.log' % (PROGNAME,
30 time.strftime("%Y%m%d%H%M%S"))
31OUTPUT_LOG_LINK = "%s.log" % PROGNAME
32CASES_PATHS = [os.path.join(os.path.abspath(os.path.dirname(__file__)),
33 'lib', 'compatlayer', 'cases')]
34logger = scriptutils.logger_create(PROGNAME)
35
36def test_layer_compatibility(td, layer):
37 from compatlayer.context import CompatLayerTestContext
38 logger.info("Starting to analyze: %s" % layer['name'])
39 logger.info("----------------------------------------------------------------------")
40
41 tc = CompatLayerTestContext(td=td, logger=logger, layer=layer)
42 tc.loadTests(CASES_PATHS)
43 return tc.runTests()
44
45def main():
46 parser = argparse.ArgumentParser(
47 description="Yocto Project compatibility layer tool",
48 add_help=False)
49 parser.add_argument('layers', metavar='LAYER_DIR', nargs='+',
50 help='Layer to test compatibility with Yocto Project')
51 parser.add_argument('-o', '--output-log',
52 help='Output log default: %s' % DEFAULT_OUTPUT_LOG,
53 action='store', default=DEFAULT_OUTPUT_LOG)
54
55 parser.add_argument('-d', '--debug', help='Enable debug output',
56 action='store_true')
57 parser.add_argument('-q', '--quiet', help='Print only errors',
58 action='store_true')
59
60 parser.add_argument('-h', '--help', action='help',
61 default=argparse.SUPPRESS,
62 help='show this help message and exit')
63
64 args = parser.parse_args()
65
66 fh = logging.FileHandler(args.output_log)
67 fh.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
68 logger.addHandler(fh)
69 if args.debug:
70 logger.setLevel(logging.DEBUG)
71 elif args.quiet:
72 logger.setLevel(logging.ERROR)
73 if os.path.exists(OUTPUT_LOG_LINK):
74 os.unlink(OUTPUT_LOG_LINK)
75 os.symlink(args.output_log, OUTPUT_LOG_LINK)
76
77 if not 'BUILDDIR' in os.environ:
78 logger.error("You must source the environment before run this script.")
79 logger.error("$ source oe-init-build-env")
80 return 1
81 builddir = os.environ['BUILDDIR']
82 bblayersconf = os.path.join(builddir, 'conf', 'bblayers.conf')
83
84 layers = detect_layers(args.layers)
85 if not layers:
86 logger.error("Fail to detect layers")
87 return 1
88
89 logger.info("Detected layers:")
90 for layer in layers:
91 if layer['type'] == LayerType.ERROR_BSP_DISTRO:
92 logger.error("%s: Can't be DISTRO and BSP type at the same time."\
93 " The conf/distro and conf/machine folders was found."\
94 % layer['name'])
95 layers.remove(layer)
96 elif layer['type'] == LayerType.ERROR_NO_LAYER_CONF:
97 logger.error("%s: Don't have conf/layer.conf file."\
98 % layer['name'])
99 layers.remove(layer)
100 else:
101 logger.info("%s: %s, %s" % (layer['name'], layer['type'],
102 layer['path']))
103 if not layers:
104 return 1
105
106 shutil.copyfile(bblayersconf, bblayersconf + '.backup')
107 def cleanup_bblayers(signum, frame):
108 shutil.copyfile(bblayersconf + '.backup', bblayersconf)
109 os.unlink(bblayersconf + '.backup')
110 signal.signal(signal.SIGTERM, cleanup_bblayers)
111 signal.signal(signal.SIGINT, cleanup_bblayers)
112
113 td = {}
114 results = collections.OrderedDict()
115
116 logger.info('')
117 logger.info('Getting initial bitbake variables ...')
118 td['bbvars'] = get_bb_vars()
119 logger.info('Getting initial signatures ...')
120 td['builddir'] = builddir
121 td['sigs'] = get_signatures(td['builddir'])
122 logger.info('')
123
124 for layer in layers:
125 if layer['type'] == LayerType.ERROR_NO_LAYER_CONF or \
126 layer['type'] == LayerType.ERROR_BSP_DISTRO:
127 continue
128
129 shutil.copyfile(bblayersconf + '.backup', bblayersconf)
130
131 add_layer(bblayersconf, layer)
132 result = test_layer_compatibility(td, layer)
133 results[layer['name']] = result
134
135 logger.info('')
136 logger.info('Summary of results:')
137 logger.info('')
138 for layer_name in results:
139 logger.info('%s ... %s' % (layer_name, 'PASS' if \
140 results[layer_name].wasSuccessful() else 'FAIL'))
141
142 cleanup_bblayers(None, None)
143
144 return 0
145
146if __name__ == '__main__':
147 try:
148 ret = main()
149 except Exception:
150 ret = 1
151 import traceback
152 traceback.print_exc()
153 sys.exit(ret)