Andrew Lamb | eceaec0 | 2020-02-11 14:16:41 -0700 | [diff] [blame] | 1 | #!/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 | |
| 7 | import argparse |
| 8 | import os |
Andrew Lamb | f349180 | 2020-05-29 11:41:47 -0600 | [diff] [blame] | 9 | import pathlib |
Andrew Lamb | eceaec0 | 2020-02-11 14:16:41 -0700 | [diff] [blame] | 10 | |
Andrew Lamb | c75a639 | 2020-02-14 13:08:14 -0700 | [diff] [blame] | 11 | from checker import constraint_suite_discovery |
Andrew Lamb | eceaec0 | 2020-02-11 14:16:41 -0700 | [diff] [blame] | 12 | from checker import io_utils |
| 13 | |
Andrew Lamb | c75a639 | 2020-02-14 13:08:14 -0700 | [diff] [blame] | 14 | COMMON_CHECKS_PATH = os.path.join( |
| 15 | os.path.dirname(__file__), 'checker', 'common_checks') |
| 16 | |
Andrew Lamb | eceaec0 | 2020-02-11 14:16:41 -0700 | [diff] [blame] | 17 | |
| 18 | def argument_parser(): |
Andrew Lamb | 0ed1b90 | 2020-06-08 17:03:47 -0600 | [diff] [blame] | 19 | """Returns an ArgumentParser for the script.""" |
Andrew Lamb | eceaec0 | 2020-02-11 14:16:41 -0700 | [diff] [blame] | 20 | parser = argparse.ArgumentParser(description=__doc__) |
| 21 | parser.add_argument( |
| 22 | '--program', |
| 23 | required=True, |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 24 | help=('Path to the program config json proto e.g. ' |
| 25 | '.../chromiumos/src/program/program1/generated/config.jsonproto.'), |
Andrew Lamb | eceaec0 | 2020-02-11 14:16:41 -0700 | [diff] [blame] | 26 | metavar='PATH') |
| 27 | parser.add_argument( |
| 28 | '--project', |
| 29 | required=True, |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 30 | help=('Path to the project config binary proto e.g. ' |
| 31 | '.../chromiumos/src/project/project1/generated/config.jsonproto.'), |
Andrew Lamb | eceaec0 | 2020-02-11 14:16:41 -0700 | [diff] [blame] | 32 | metavar='PATH') |
Andrew Lamb | f349180 | 2020-05-29 11:41:47 -0600 | [diff] [blame] | 33 | 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 Lamb | eceaec0 | 2020-02-11 14:16:41 -0700 | [diff] [blame] | 42 | return parser |
| 43 | |
| 44 | |
| 45 | def main(): |
Andrew Lamb | 0ed1b90 | 2020-06-08 17:03:47 -0600 | [diff] [blame] | 46 | """Runs the script.""" |
Andrew Lamb | eceaec0 | 2020-02-11 14:16:41 -0700 | [diff] [blame] | 47 | parser = argument_parser() |
| 48 | args = parser.parse_args() |
| 49 | |
Andrew Lamb | 9328c40 | 2020-03-05 12:57:59 -0700 | [diff] [blame] | 50 | project_config = io_utils.read_config(args.project) |
| 51 | program_config = io_utils.read_config(args.program) |
Andrew Lamb | eceaec0 | 2020-02-11 14:16:41 -0700 | [diff] [blame] | 52 | |
Andrew Lamb | d43ddd7 | 2020-05-27 14:06:38 -0600 | [diff] [blame] | 53 | # 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 Lamb | c75a639 | 2020-02-14 13:08:14 -0700 | [diff] [blame] | 58 | constraint_suite_directories = [ |
Andrew Lamb | d43ddd7 | 2020-05-27 14:06:38 -0600 | [diff] [blame] | 59 | program_checks_dir, |
Andrew Lamb | 98153da | 2021-02-26 07:39:08 -0700 | [diff] [blame] | 60 | COMMON_CHECKS_PATH, |
Andrew Lamb | c75a639 | 2020-02-14 13:08:14 -0700 | [diff] [blame] | 61 | ] |
| 62 | |
Andrew Lamb | 98153da | 2021-02-26 07:39:08 -0700 | [diff] [blame] | 63 | # 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 Lamb | c75a639 | 2020-02-14 13:08:14 -0700 | [diff] [blame] | 66 | for directory in constraint_suite_directories: |
Andrew Lamb | 98153da | 2021-02-26 07:39:08 -0700 | [diff] [blame] | 67 | 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 Lamb | c75a639 | 2020-02-14 13:08:14 -0700 | [diff] [blame] | 71 | |
Andrew Lamb | 98153da | 2021-02-26 07:39:08 -0700 | [diff] [blame] | 72 | for suite in constraint_suites.values(): |
Andrew Lamb | c75a639 | 2020-02-14 13:08:14 -0700 | [diff] [blame] | 73 | suite.run_checks( |
Andrew Lamb | f349180 | 2020-05-29 11:41:47 -0600 | [diff] [blame] | 74 | program_config=program_config, |
| 75 | project_config=project_config, |
| 76 | factory_dir=args.factory_dir, |
| 77 | verbose=1, |
| 78 | ) |
Andrew Lamb | eceaec0 | 2020-02-11 14:16:41 -0700 | [diff] [blame] | 79 | |
| 80 | |
| 81 | if __name__ == '__main__': |
| 82 | main() |