blob: 9c6e49f6e247c5594694a7b910282e5ddf55e61b [file] [log] [blame]
Andrew Lambeceaec02020-02-11 14:16:41 -07001#!/usr/bin/env python3
2# Copyright 2020 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""Run a program's constraint checks on a project."""
6
7import argparse
8import os
Andrew Lambf3491802020-05-29 11:41:47 -06009import pathlib
Andrew Lambeceaec02020-02-11 14:16:41 -070010
Andrew Lambc75a6392020-02-14 13:08:14 -070011from checker import constraint_suite_discovery
Andrew Lambeceaec02020-02-11 14:16:41 -070012from checker import io_utils
13
Andrew Lambc75a6392020-02-14 13:08:14 -070014COMMON_CHECKS_PATH = os.path.join(
15 os.path.dirname(__file__), 'checker', 'common_checks')
16
Andrew Lambeceaec02020-02-11 14:16:41 -070017
18def argument_parser():
Andrew Lamb0ed1b902020-06-08 17:03:47 -060019 """Returns an ArgumentParser for the script."""
Andrew Lambeceaec02020-02-11 14:16:41 -070020 parser = argparse.ArgumentParser(description=__doc__)
21 parser.add_argument(
22 '--program',
23 required=True,
Andrew Lamb2413c982020-05-29 12:15:36 -060024 help=('Path to the program config json proto e.g. '
25 '.../chromiumos/src/program/program1/generated/config.jsonproto.'),
Andrew Lambeceaec02020-02-11 14:16:41 -070026 metavar='PATH')
27 parser.add_argument(
28 '--project',
29 required=True,
Andrew Lamb2413c982020-05-29 12:15:36 -060030 help=('Path to the project config binary proto e.g. '
31 '.../chromiumos/src/project/project1/generated/config.jsonproto.'),
Andrew Lambeceaec02020-02-11 14:16:41 -070032 metavar='PATH')
Andrew Lambf3491802020-05-29 11:41:47 -060033 parser.add_argument(
34 '--factory_dir',
35 # TODO(crbug.com/1085429): Require this once passed by Recipes.
36 required=False,
37 type=pathlib.Path,
38 help=('Path to the project factory confir dir e.g.'
39 '.../chromiumos/src/project/project1/factory'),
40 metavar='PATH',
41 )
Andrew Lambeceaec02020-02-11 14:16:41 -070042 return parser
43
44
45def main():
Andrew Lamb0ed1b902020-06-08 17:03:47 -060046 """Runs the script."""
Andrew Lambeceaec02020-02-11 14:16:41 -070047 parser = argument_parser()
48 args = parser.parse_args()
49
Andrew Lamb9328c402020-03-05 12:57:59 -070050 project_config = io_utils.read_config(args.project)
51 program_config = io_utils.read_config(args.program)
Andrew Lambeceaec02020-02-11 14:16:41 -070052
Andrew Lambd43ddd72020-05-27 14:06:38 -060053 # Expect program checks in a 'checks' dir under the root of the program repo.
54 # Note that args.program points to the generated file, so use dirname + '..'
55 # to find the root of the program repo.
56 program_checks_dir = os.path.join(os.path.dirname(args.program), '../checks')
57
Andrew Lambc75a6392020-02-14 13:08:14 -070058 constraint_suite_directories = [
Andrew Lambd43ddd72020-05-27 14:06:38 -060059 program_checks_dir,
Andrew Lamb98153da2021-02-26 07:39:08 -070060 COMMON_CHECKS_PATH,
Andrew Lambc75a6392020-02-14 13:08:14 -070061 ]
62
Andrew Lamb98153da2021-02-26 07:39:08 -070063 # If two suites have the same name, only run the one in the program directory.
64 # This allows programs to override common suites.
65 constraint_suites = {}
Andrew Lambc75a6392020-02-14 13:08:14 -070066 for directory in constraint_suite_directories:
Andrew Lamb98153da2021-02-26 07:39:08 -070067 for suite in constraint_suite_discovery.discover_suites(directory):
68 suite_name = suite.__class__.__name__
69 if suite_name not in constraint_suites:
70 constraint_suites[suite_name] = suite
Andrew Lambc75a6392020-02-14 13:08:14 -070071
Andrew Lamb98153da2021-02-26 07:39:08 -070072 for suite in constraint_suites.values():
Andrew Lambc75a6392020-02-14 13:08:14 -070073 suite.run_checks(
Andrew Lambf3491802020-05-29 11:41:47 -060074 program_config=program_config,
75 project_config=project_config,
76 factory_dir=args.factory_dir,
77 verbose=1,
78 )
Andrew Lambeceaec02020-02-11 14:16:41 -070079
80
81if __name__ == '__main__':
82 main()