blob: ca6c79bc8de07eecb46164804d30309ff13ee3bc [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
Andrew Geissler99467da2019-02-25 18:54:23 -060027from checklayer import LayerType, detect_layers, add_layers, add_layer_dependencies, get_signatures
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')
54 parser.add_argument('--machines', nargs="+",
55 help='List of MACHINEs to be used during testing', action='store')
56 parser.add_argument('--additional-layers', nargs="+",
57 help='List of additional layers to add during testing', action='store')
58 group = parser.add_mutually_exclusive_group()
59 group.add_argument('--with-software-layer-signature-check', action='store_true', dest='test_software_layer_signatures',
60 default=True,
61 help='check that software layers do not change signatures (on by default)')
62 group.add_argument('--without-software-layer-signature-check', action='store_false', dest='test_software_layer_signatures',
63 help='disable signature checking for software layers')
64 parser.add_argument('-n', '--no-auto', help='Disable auto layer discovery',
65 action='store_true')
66 parser.add_argument('-d', '--debug', help='Enable debug output',
67 action='store_true')
68 parser.add_argument('-q', '--quiet', help='Print only errors',
69 action='store_true')
70
71 parser.add_argument('-h', '--help', action='help',
72 default=argparse.SUPPRESS,
73 help='show this help message and exit')
74
75 args = parser.parse_args()
76
77 if args.output_log:
78 fh = logging.FileHandler(args.output_log)
79 fh.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
80 logger.addHandler(fh)
81 if args.debug:
82 logger.setLevel(logging.DEBUG)
83 elif args.quiet:
84 logger.setLevel(logging.ERROR)
85
86 if not 'BUILDDIR' in os.environ:
Andrew Geissler82c905d2020-04-13 13:39:40 -050087 logger.error("You must source the environment before running this script.")
Brad Bishopd7bf8c12018-02-25 22:55:05 -050088 logger.error("$ source oe-init-build-env")
89 return 1
90 builddir = os.environ['BUILDDIR']
91 bblayersconf = os.path.join(builddir, 'conf', 'bblayers.conf')
92
93 layers = detect_layers(args.layers, args.no_auto)
94 if not layers:
Andrew Geissler82c905d2020-04-13 13:39:40 -050095 logger.error("Failed to detect layers")
Brad Bishopd7bf8c12018-02-25 22:55:05 -050096 return 1
97 if args.additional_layers:
98 additional_layers = detect_layers(args.additional_layers, args.no_auto)
99 else:
100 additional_layers = []
101 if args.dependency:
102 dep_layers = detect_layers(args.dependency, args.no_auto)
103 dep_layers = dep_layers + layers
104 else:
105 dep_layers = layers
106
107 logger.info("Detected layers:")
108 for layer in layers:
109 if layer['type'] == LayerType.ERROR_BSP_DISTRO:
110 logger.error("%s: Can't be DISTRO and BSP type at the same time."\
Andrew Geissler82c905d2020-04-13 13:39:40 -0500111 " Both conf/distro and conf/machine folders were found."\
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500112 % layer['name'])
113 layers.remove(layer)
114 elif layer['type'] == LayerType.ERROR_NO_LAYER_CONF:
115 logger.error("%s: Don't have conf/layer.conf file."\
116 % layer['name'])
117 layers.remove(layer)
118 else:
119 logger.info("%s: %s, %s" % (layer['name'], layer['type'],
120 layer['path']))
121 if not layers:
122 return 1
123
124 shutil.copyfile(bblayersconf, bblayersconf + '.backup')
125 def cleanup_bblayers(signum, frame):
126 shutil.copyfile(bblayersconf + '.backup', bblayersconf)
127 os.unlink(bblayersconf + '.backup')
128 signal.signal(signal.SIGTERM, cleanup_bblayers)
129 signal.signal(signal.SIGINT, cleanup_bblayers)
130
131 td = {}
132 results = collections.OrderedDict()
133 results_status = collections.OrderedDict()
134
135 layers_tested = 0
136 for layer in layers:
137 if layer['type'] == LayerType.ERROR_NO_LAYER_CONF or \
138 layer['type'] == LayerType.ERROR_BSP_DISTRO:
139 continue
140
141 logger.info('')
142 logger.info("Setting up for %s(%s), %s" % (layer['name'], layer['type'],
143 layer['path']))
144
145 shutil.copyfile(bblayersconf + '.backup', bblayersconf)
146
147 missing_dependencies = not add_layer_dependencies(bblayersconf, layer, dep_layers, logger)
148 if not missing_dependencies:
149 for additional_layer in additional_layers:
150 if not add_layer_dependencies(bblayersconf, additional_layer, dep_layers, logger):
151 missing_dependencies = True
152 break
153 if not add_layer_dependencies(bblayersconf, layer, dep_layers, logger) or \
154 any(map(lambda additional_layer: not add_layer_dependencies(bblayersconf, additional_layer, dep_layers, logger),
155 additional_layers)):
156 logger.info('Skipping %s due to missing dependencies.' % layer['name'])
157 results[layer['name']] = None
158 results_status[layer['name']] = 'SKIPPED (Missing dependencies)'
159 layers_tested = layers_tested + 1
160 continue
161
Andrew Geissler99467da2019-02-25 18:54:23 -0600162 if any(map(lambda additional_layer: not add_layers(bblayersconf, [additional_layer], logger),
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500163 additional_layers)):
164 logger.info('Skipping %s due to missing additional layers.' % layer['name'])
165 results[layer['name']] = None
166 results_status[layer['name']] = 'SKIPPED (Missing additional layers)'
167 layers_tested = layers_tested + 1
168 continue
169
170 logger.info('Getting initial bitbake variables ...')
171 td['bbvars'] = get_bb_vars()
172 logger.info('Getting initial signatures ...')
173 td['builddir'] = builddir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800174 try:
175 td['sigs'], td['tunetasks'] = get_signatures(td['builddir'])
176 except RuntimeError as e:
177 logger.info(str(e))
178 results[layer['name']] = None
179 results_status[layer['name']] = 'FAIL (Generating world signatures)'
180 layers_tested = layers_tested + 1
181 continue
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500182 td['machines'] = args.machines
183
Andrew Geissler99467da2019-02-25 18:54:23 -0600184 if not add_layers(bblayersconf, [layer], logger):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500185 logger.info('Skipping %s ???.' % layer['name'])
186 results[layer['name']] = None
187 results_status[layer['name']] = 'SKIPPED (Unknown)'
188 layers_tested = layers_tested + 1
189 continue
190
191 result = test_layer(td, layer, args.test_software_layer_signatures)
192 results[layer['name']] = result
193 results_status[layer['name']] = 'PASS' if results[layer['name']].wasSuccessful() else 'FAIL'
194 layers_tested = layers_tested + 1
195
196 ret = 0
197 if layers_tested:
198 logger.info('')
199 logger.info('Summary of results:')
200 logger.info('')
201 for layer_name in results_status:
202 logger.info('%s ... %s' % (layer_name, results_status[layer_name]))
203 if not results[layer_name] or not results[layer_name].wasSuccessful():
204 ret = 2 # ret = 1 used for initialization errors
205
206 cleanup_bblayers(None, None)
207
208 return ret
209
210if __name__ == '__main__':
211 try:
212 ret = main()
213 except Exception:
214 ret = 1
215 import traceback
216 traceback.print_exc()
217 sys.exit(ret)