blob: 2445ad5e43fd45eb8fdbbf5cd23b9891d04e8b42 [file] [log] [blame]
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001#!/usr/bin/env python3
2
3# Yocto Project layer checking tool
4#
5# Copyright (C) 2017 Intel Corporation
Brad Bishopc342db32019-05-15 21:57:59 -04006#
7# SPDX-License-Identifier: MIT
8#
Brad Bishopd7bf8c12018-02-25 22:55:05 -05009
10import os
11import sys
12import argparse
13import logging
14import time
15import signal
16import shutil
17import collections
18
19scripts_path = os.path.dirname(os.path.realpath(__file__))
20lib_path = scripts_path + '/lib'
21sys.path = sys.path + [lib_path]
22import scriptutils
23import scriptpath
24scriptpath.add_oe_lib_path()
25scriptpath.add_bitbake_lib_path()
26
Patrick Williams213cb262021-08-07 19:21:33 -050027from checklayer import LayerType, detect_layers, add_layers, add_layer_dependencies, get_layer_dependencies, get_signatures, check_bblayers
Brad Bishopd7bf8c12018-02-25 22:55:05 -050028from oeqa.utils.commands import get_bb_vars
29
30PROGNAME = 'yocto-check-layer'
31CASES_PATHS = [os.path.join(os.path.abspath(os.path.dirname(__file__)),
32 'lib', 'checklayer', 'cases')]
33logger = scriptutils.logger_create(PROGNAME, stream=sys.stdout)
34
35def test_layer(td, layer, test_software_layer_signatures):
36 from checklayer.context import CheckLayerTestContext
37 logger.info("Starting to analyze: %s" % layer['name'])
38 logger.info("----------------------------------------------------------------------")
39
40 tc = CheckLayerTestContext(td=td, logger=logger, layer=layer, test_software_layer_signatures=test_software_layer_signatures)
41 tc.loadTests(CASES_PATHS)
42 return tc.runTests()
43
44def main():
45 parser = argparse.ArgumentParser(
46 description="Yocto Project layer checking tool",
47 add_help=False)
48 parser.add_argument('layers', metavar='LAYER_DIR', nargs='+',
49 help='Layer to check')
50 parser.add_argument('-o', '--output-log',
51 help='File to output log (optional)', action='store')
52 parser.add_argument('--dependency', nargs="+",
53 help='Layers to process for dependencies', action='store')
Patrick Williams213cb262021-08-07 19:21:33 -050054 parser.add_argument('--no-auto-dependency', help='Disable automatic testing of dependencies',
55 action='store_true')
Brad Bishopd7bf8c12018-02-25 22:55:05 -050056 parser.add_argument('--machines', nargs="+",
57 help='List of MACHINEs to be used during testing', action='store')
58 parser.add_argument('--additional-layers', nargs="+",
59 help='List of additional layers to add during testing', action='store')
60 group = parser.add_mutually_exclusive_group()
61 group.add_argument('--with-software-layer-signature-check', action='store_true', dest='test_software_layer_signatures',
62 default=True,
63 help='check that software layers do not change signatures (on by default)')
64 group.add_argument('--without-software-layer-signature-check', action='store_false', dest='test_software_layer_signatures',
65 help='disable signature checking for software layers')
66 parser.add_argument('-n', '--no-auto', help='Disable auto layer discovery',
67 action='store_true')
68 parser.add_argument('-d', '--debug', help='Enable debug output',
69 action='store_true')
70 parser.add_argument('-q', '--quiet', help='Print only errors',
71 action='store_true')
72
73 parser.add_argument('-h', '--help', action='help',
74 default=argparse.SUPPRESS,
75 help='show this help message and exit')
76
77 args = parser.parse_args()
78
79 if args.output_log:
80 fh = logging.FileHandler(args.output_log)
81 fh.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
82 logger.addHandler(fh)
83 if args.debug:
84 logger.setLevel(logging.DEBUG)
85 elif args.quiet:
86 logger.setLevel(logging.ERROR)
87
88 if not 'BUILDDIR' in os.environ:
Andrew Geissler82c905d2020-04-13 13:39:40 -050089 logger.error("You must source the environment before running this script.")
Brad Bishopd7bf8c12018-02-25 22:55:05 -050090 logger.error("$ source oe-init-build-env")
91 return 1
92 builddir = os.environ['BUILDDIR']
93 bblayersconf = os.path.join(builddir, 'conf', 'bblayers.conf')
94
95 layers = detect_layers(args.layers, args.no_auto)
96 if not layers:
Andrew Geissler82c905d2020-04-13 13:39:40 -050097 logger.error("Failed to detect layers")
Brad Bishopd7bf8c12018-02-25 22:55:05 -050098 return 1
99 if args.additional_layers:
100 additional_layers = detect_layers(args.additional_layers, args.no_auto)
101 else:
102 additional_layers = []
103 if args.dependency:
104 dep_layers = detect_layers(args.dependency, args.no_auto)
105 dep_layers = dep_layers + layers
106 else:
107 dep_layers = layers
108
109 logger.info("Detected layers:")
110 for layer in layers:
111 if layer['type'] == LayerType.ERROR_BSP_DISTRO:
112 logger.error("%s: Can't be DISTRO and BSP type at the same time."\
Andrew Geissler82c905d2020-04-13 13:39:40 -0500113 " Both conf/distro and conf/machine folders were found."\
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500114 % layer['name'])
115 layers.remove(layer)
116 elif layer['type'] == LayerType.ERROR_NO_LAYER_CONF:
Andrew Geisslerc926e172021-05-07 16:11:35 -0500117 logger.info("%s: Doesn't have conf/layer.conf file, so ignoring"\
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500118 % layer['name'])
119 layers.remove(layer)
120 else:
121 logger.info("%s: %s, %s" % (layer['name'], layer['type'],
122 layer['path']))
123 if not layers:
124 return 1
125
Patrick Williams213cb262021-08-07 19:21:33 -0500126 # Find all dependencies, and get them checked too
127 if not args.no_auto_dependency:
128 depends = []
129 for layer in layers:
130 layer_depends = get_layer_dependencies(layer, dep_layers, logger)
131 if layer_depends:
132 for d in layer_depends:
133 if d not in depends:
134 depends.append(d)
135
136 for d in depends:
137 if d not in layers:
138 logger.info("Adding %s to the list of layers to test, as a dependency", d['name'])
139 layers.append(d)
140
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500141 shutil.copyfile(bblayersconf, bblayersconf + '.backup')
142 def cleanup_bblayers(signum, frame):
143 shutil.copyfile(bblayersconf + '.backup', bblayersconf)
144 os.unlink(bblayersconf + '.backup')
145 signal.signal(signal.SIGTERM, cleanup_bblayers)
146 signal.signal(signal.SIGINT, cleanup_bblayers)
147
148 td = {}
149 results = collections.OrderedDict()
150 results_status = collections.OrderedDict()
151
152 layers_tested = 0
153 for layer in layers:
154 if layer['type'] == LayerType.ERROR_NO_LAYER_CONF or \
155 layer['type'] == LayerType.ERROR_BSP_DISTRO:
156 continue
157
Andrew Geisslerc926e172021-05-07 16:11:35 -0500158 # Reset to a clean backup copy for each run
159 shutil.copyfile(bblayersconf + '.backup', bblayersconf)
160
Andrew Geissler635e0e42020-08-21 15:58:33 -0500161 if check_bblayers(bblayersconf, layer['path'], logger):
162 logger.info("%s already in %s. To capture initial signatures, layer under test should not present "
163 "in BBLAYERS. Please remove %s from BBLAYERS." % (layer['name'], bblayersconf, layer['name']))
164 results[layer['name']] = None
165 results_status[layer['name']] = 'SKIPPED (Layer under test should not present in BBLAYERS)'
166 continue
167
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500168 logger.info('')
169 logger.info("Setting up for %s(%s), %s" % (layer['name'], layer['type'],
170 layer['path']))
171
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500172 missing_dependencies = not add_layer_dependencies(bblayersconf, layer, dep_layers, logger)
173 if not missing_dependencies:
174 for additional_layer in additional_layers:
175 if not add_layer_dependencies(bblayersconf, additional_layer, dep_layers, logger):
176 missing_dependencies = True
177 break
Patrick Williams213cb262021-08-07 19:21:33 -0500178 if missing_dependencies:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500179 logger.info('Skipping %s due to missing dependencies.' % layer['name'])
180 results[layer['name']] = None
181 results_status[layer['name']] = 'SKIPPED (Missing dependencies)'
182 layers_tested = layers_tested + 1
183 continue
184
Andrew Geissler99467da2019-02-25 18:54:23 -0600185 if any(map(lambda additional_layer: not add_layers(bblayersconf, [additional_layer], logger),
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500186 additional_layers)):
187 logger.info('Skipping %s due to missing additional layers.' % layer['name'])
188 results[layer['name']] = None
189 results_status[layer['name']] = 'SKIPPED (Missing additional layers)'
190 layers_tested = layers_tested + 1
191 continue
192
193 logger.info('Getting initial bitbake variables ...')
194 td['bbvars'] = get_bb_vars()
195 logger.info('Getting initial signatures ...')
196 td['builddir'] = builddir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800197 try:
198 td['sigs'], td['tunetasks'] = get_signatures(td['builddir'])
199 except RuntimeError as e:
200 logger.info(str(e))
201 results[layer['name']] = None
202 results_status[layer['name']] = 'FAIL (Generating world signatures)'
203 layers_tested = layers_tested + 1
204 continue
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500205 td['machines'] = args.machines
206
Andrew Geissler99467da2019-02-25 18:54:23 -0600207 if not add_layers(bblayersconf, [layer], logger):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500208 logger.info('Skipping %s ???.' % layer['name'])
209 results[layer['name']] = None
210 results_status[layer['name']] = 'SKIPPED (Unknown)'
211 layers_tested = layers_tested + 1
212 continue
213
214 result = test_layer(td, layer, args.test_software_layer_signatures)
215 results[layer['name']] = result
216 results_status[layer['name']] = 'PASS' if results[layer['name']].wasSuccessful() else 'FAIL'
217 layers_tested = layers_tested + 1
218
219 ret = 0
220 if layers_tested:
221 logger.info('')
222 logger.info('Summary of results:')
223 logger.info('')
224 for layer_name in results_status:
225 logger.info('%s ... %s' % (layer_name, results_status[layer_name]))
226 if not results[layer_name] or not results[layer_name].wasSuccessful():
227 ret = 2 # ret = 1 used for initialization errors
228
229 cleanup_bblayers(None, None)
230
231 return ret
232
233if __name__ == '__main__':
234 try:
235 ret = main()
236 except Exception:
237 ret = 1
238 import traceback
239 traceback.print_exc()
240 sys.exit(ret)