Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | # Yocto Project layer checking tool |
| 4 | # |
| 5 | # Copyright (C) 2017 Intel Corporation |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 6 | # |
| 7 | # SPDX-License-Identifier: MIT |
| 8 | # |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 9 | |
| 10 | import os |
| 11 | import sys |
| 12 | import argparse |
| 13 | import logging |
| 14 | import time |
| 15 | import signal |
| 16 | import shutil |
| 17 | import collections |
| 18 | |
| 19 | scripts_path = os.path.dirname(os.path.realpath(__file__)) |
| 20 | lib_path = scripts_path + '/lib' |
| 21 | sys.path = sys.path + [lib_path] |
| 22 | import scriptutils |
| 23 | import scriptpath |
| 24 | scriptpath.add_oe_lib_path() |
| 25 | scriptpath.add_bitbake_lib_path() |
| 26 | |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame^] | 27 | from checklayer import LayerType, detect_layers, add_layers, add_layer_dependencies, get_signatures, check_bblayers |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 28 | from oeqa.utils.commands import get_bb_vars |
| 29 | |
| 30 | PROGNAME = 'yocto-check-layer' |
| 31 | CASES_PATHS = [os.path.join(os.path.abspath(os.path.dirname(__file__)), |
| 32 | 'lib', 'checklayer', 'cases')] |
| 33 | logger = scriptutils.logger_create(PROGNAME, stream=sys.stdout) |
| 34 | |
| 35 | def 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 | |
| 44 | def 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 Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 87 | logger.error("You must source the environment before running this script.") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 88 | 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 Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 95 | logger.error("Failed to detect layers") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 96 | 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 Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 111 | " Both conf/distro and conf/machine folders were found."\ |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 112 | % 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 | |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame^] | 141 | if check_bblayers(bblayersconf, layer['path'], logger): |
| 142 | logger.info("%s already in %s. To capture initial signatures, layer under test should not present " |
| 143 | "in BBLAYERS. Please remove %s from BBLAYERS." % (layer['name'], bblayersconf, layer['name'])) |
| 144 | results[layer['name']] = None |
| 145 | results_status[layer['name']] = 'SKIPPED (Layer under test should not present in BBLAYERS)' |
| 146 | continue |
| 147 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 148 | logger.info('') |
| 149 | logger.info("Setting up for %s(%s), %s" % (layer['name'], layer['type'], |
| 150 | layer['path'])) |
| 151 | |
| 152 | shutil.copyfile(bblayersconf + '.backup', bblayersconf) |
| 153 | |
| 154 | missing_dependencies = not add_layer_dependencies(bblayersconf, layer, dep_layers, logger) |
| 155 | if not missing_dependencies: |
| 156 | for additional_layer in additional_layers: |
| 157 | if not add_layer_dependencies(bblayersconf, additional_layer, dep_layers, logger): |
| 158 | missing_dependencies = True |
| 159 | break |
| 160 | if not add_layer_dependencies(bblayersconf, layer, dep_layers, logger) or \ |
| 161 | any(map(lambda additional_layer: not add_layer_dependencies(bblayersconf, additional_layer, dep_layers, logger), |
| 162 | additional_layers)): |
| 163 | logger.info('Skipping %s due to missing dependencies.' % layer['name']) |
| 164 | results[layer['name']] = None |
| 165 | results_status[layer['name']] = 'SKIPPED (Missing dependencies)' |
| 166 | layers_tested = layers_tested + 1 |
| 167 | continue |
| 168 | |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 169 | if any(map(lambda additional_layer: not add_layers(bblayersconf, [additional_layer], logger), |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 170 | additional_layers)): |
| 171 | logger.info('Skipping %s due to missing additional layers.' % layer['name']) |
| 172 | results[layer['name']] = None |
| 173 | results_status[layer['name']] = 'SKIPPED (Missing additional layers)' |
| 174 | layers_tested = layers_tested + 1 |
| 175 | continue |
| 176 | |
| 177 | logger.info('Getting initial bitbake variables ...') |
| 178 | td['bbvars'] = get_bb_vars() |
| 179 | logger.info('Getting initial signatures ...') |
| 180 | td['builddir'] = builddir |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 181 | try: |
| 182 | td['sigs'], td['tunetasks'] = get_signatures(td['builddir']) |
| 183 | except RuntimeError as e: |
| 184 | logger.info(str(e)) |
| 185 | results[layer['name']] = None |
| 186 | results_status[layer['name']] = 'FAIL (Generating world signatures)' |
| 187 | layers_tested = layers_tested + 1 |
| 188 | continue |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 189 | td['machines'] = args.machines |
| 190 | |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 191 | if not add_layers(bblayersconf, [layer], logger): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 192 | logger.info('Skipping %s ???.' % layer['name']) |
| 193 | results[layer['name']] = None |
| 194 | results_status[layer['name']] = 'SKIPPED (Unknown)' |
| 195 | layers_tested = layers_tested + 1 |
| 196 | continue |
| 197 | |
| 198 | result = test_layer(td, layer, args.test_software_layer_signatures) |
| 199 | results[layer['name']] = result |
| 200 | results_status[layer['name']] = 'PASS' if results[layer['name']].wasSuccessful() else 'FAIL' |
| 201 | layers_tested = layers_tested + 1 |
| 202 | |
| 203 | ret = 0 |
| 204 | if layers_tested: |
| 205 | logger.info('') |
| 206 | logger.info('Summary of results:') |
| 207 | logger.info('') |
| 208 | for layer_name in results_status: |
| 209 | logger.info('%s ... %s' % (layer_name, results_status[layer_name])) |
| 210 | if not results[layer_name] or not results[layer_name].wasSuccessful(): |
| 211 | ret = 2 # ret = 1 used for initialization errors |
| 212 | |
| 213 | cleanup_bblayers(None, None) |
| 214 | |
| 215 | return ret |
| 216 | |
| 217 | if __name__ == '__main__': |
| 218 | try: |
| 219 | ret = main() |
| 220 | except Exception: |
| 221 | ret = 1 |
| 222 | import traceback |
| 223 | traceback.print_exc() |
| 224 | sys.exit(ret) |