1# Copyright 2021 The Chromium Authors 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4"""Common argument parsing-related code for unexpected pass finders.""" 5 6import argparse 7import logging 8import os 9 10from unexpected_passes_common import constants 11 12 13def AddCommonArguments(parser: argparse.ArgumentParser) -> None: 14 """Adds arguments that are common to all unexpected pass finders. 15 16 Args: 17 parser: An argparse.ArgumentParser instance to add arguments to. 18 """ 19 parser.add_argument('--project', 20 required=True, 21 help='The billing project to use for BigQuery queries. ' 22 'Must have access to the ResultDB BQ tables, e.g. ' 23 '"chrome-luci-data.chromium.gpu_ci_test_results".') 24 parser.add_argument('--num-samples', 25 type=int, 26 default=100, 27 help='The number of recent builds to query.') 28 parser.add_argument('--output-format', 29 choices=[ 30 'html', 31 'print', 32 ], 33 default='html', 34 help='How to output script results.') 35 parser.add_argument('--remove-stale-expectations', 36 action='store_true', 37 default=False, 38 help='Automatically remove any expectations that are ' 39 'determined to be stale from the expectation file.') 40 parser.add_argument('--narrow-semi-stale-expectation-scope', 41 action='store_true', 42 default=False, 43 help='Automatically modify or split semi-stale ' 44 'expectations so they only apply to configurations that ' 45 'actually need them.') 46 parser.add_argument('--no-auto-close-bugs', 47 dest='auto_close_bugs', 48 action='store_false', 49 default=True, 50 help='Disables automatic closing of bugs that no longer ' 51 'have active expectations once the generated CL lands. ' 52 'If set, a comment will be posted to the bug when all ' 53 'active expectations are gone instead.') 54 parser.add_argument('-v', 55 '--verbose', 56 action='count', 57 default=0, 58 help='Increase logging verbosity, can be passed multiple ' 59 'times.') 60 parser.add_argument('-q', 61 '--quiet', 62 action='store_true', 63 default=False, 64 help='Disable logging for non-errors.') 65 parser.add_argument('--large-query-mode', 66 action='store_true', 67 default=False, 68 help='Run the script in large query mode. This incurs ' 69 'a significant performance hit, but allows the use of ' 70 'larger sample sizes on large test suites by partially ' 71 'working around a hard memory limit in BigQuery.') 72 parser.add_argument('--expectation-grace-period', 73 type=int, 74 default=7, 75 help=('How many days old an expectation needs to be in ' 76 'order to be a candidate for being removed or ' 77 'modified. This prevents newly added expectations ' 78 'from being removed before a sufficient amount of ' 79 'data has been generated with the expectation ' 80 'active. Set to a negative value to disable.')) 81 parser.add_argument('--result-output-file', 82 help=('Output file to store the generated results. If ' 83 'not specified, will use a temporary file.')) 84 parser.add_argument('--bug-output-file', 85 help=('Output file to store "Bug:"/"Fixed:" text ' 86 'intended for use in CL descriptions. If not ' 87 'specified, will be printed to the terminal ' 88 'instead.')) 89 parser.add_argument('--jobs', 90 '-j', 91 type=int, 92 help=('How many parallel jobs to run. By default, runs ' 93 'all work in parallel.')) 94 parser.add_argument('--disable-batching', 95 dest='use_batching', 96 action='store_false', 97 default=True, 98 help=('Disables the use of batching when running ' 99 'queries. Batching allows for more queries to be ' 100 'run in parallel, but increases query overhead by ' 101 'a variable amount.')) 102 internal_group = parser.add_mutually_exclusive_group() 103 internal_group.add_argument('--include-internal-builders', 104 action='store_true', 105 dest='include_internal_builders', 106 default=None, 107 help=('Includes builders that are defined in ' 108 'src-internal in addition to the public ' 109 'ones. If left unset, will be ' 110 'automatically determined by the presence ' 111 'of src-internal.')) 112 internal_group.add_argument('--no-include-internal-builders', 113 action='store_false', 114 dest='include_internal_builders', 115 default=None, 116 help=('Does not include builders that are ' 117 'defined in src-internal. If left unset, ' 118 'will be automatically determined by the ' 119 'presence of src-internal.')) 120 121 122def PerformCommonPostParseSetup(args: argparse.Namespace) -> None: 123 """Helper function to perform all common post-parse setup. 124 125 Args: 126 args: Parsed arguments from an argparse.ArgumentParser. 127 """ 128 SetLoggingVerbosity(args) 129 SetInternalBuilderInclusion(args) 130 131 132def SetLoggingVerbosity(args: argparse.Namespace) -> None: 133 """Sets logging verbosity based on parsed arguments. 134 135 Args: 136 args: Parsed arguments from an argparse.ArgumentParser. 137 """ 138 if args.quiet: 139 args.verbose = -1 140 verbosity_level = args.verbose 141 if verbosity_level == -1: 142 level = logging.ERROR 143 elif verbosity_level == 0: 144 level = logging.WARNING 145 elif verbosity_level == 1: 146 level = logging.INFO 147 else: 148 level = logging.DEBUG 149 logging.getLogger().setLevel(level) 150 151 152def SetInternalBuilderInclusion(args: argparse.Namespace) -> None: 153 """Sets internal builder inclusion based on parsed arguments. 154 155 Args: 156 args: Parsed arguments from an argparse.ArgumentParser. 157 """ 158 if args.include_internal_builders is not None: 159 return 160 161 if os.path.isdir(constants.SRC_INTERNAL_DIR): 162 args.include_internal_builders = True 163 else: 164 args.include_internal_builders = False 165