• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env vpython3
2# Copyright 2020 The ChromiumOS Authors
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
9import pathlib
10
11from checker import constraint_suite_discovery
12from checker import io_utils
13
14COMMON_CHECKS_PATH = os.path.join(
15    os.path.dirname(__file__), 'checker', 'common_checks')
16
17
18def argument_parser():
19  """Returns an ArgumentParser for the script."""
20  parser = argparse.ArgumentParser(description=__doc__)
21  parser.add_argument(
22      '--program',
23      required=True,
24      help=('Path to the program config json proto e.g. '
25            '.../chromiumos/src/program/program1/generated/config.jsonproto.'),
26      metavar='PATH')
27  parser.add_argument(
28      '--project',
29      required=True,
30      help=('Path to the project config binary proto e.g. '
31            '.../chromiumos/src/project/project1/generated/config.jsonproto.'),
32      metavar='PATH')
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  )
42  return parser
43
44
45def main():
46  """Runs the script."""
47  parser = argument_parser()
48  args = parser.parse_args()
49
50  project_config = io_utils.read_config(args.project)
51  program_config = io_utils.read_config(args.program)
52
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
58  constraint_suite_directories = [
59      program_checks_dir,
60      COMMON_CHECKS_PATH,
61  ]
62
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 = {}
66  for directory in constraint_suite_directories:
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
71
72  for suite in constraint_suites.values():
73    suite.run_checks(
74        program_config=program_config,
75        project_config=project_config,
76        factory_dir=args.factory_dir,
77        verbose=1,
78    )
79
80
81if __name__ == '__main__':
82  main()
83