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('-v', 47 '--verbose', 48 action='count', 49 default=0, 50 help='Increase logging verbosity, can be passed multiple ' 51 'times.') 52 parser.add_argument('-q', 53 '--quiet', 54 action='store_true', 55 default=False, 56 help='Disable logging for non-errors.') 57 parser.add_argument('--large-query-mode', 58 action='store_true', 59 default=False, 60 help='Run the script in large query mode. This incurs ' 61 'a significant performance hit, but allows the use of ' 62 'larger sample sizes on large test suites by partially ' 63 'working around a hard memory limit in BigQuery.') 64 parser.add_argument('--expectation-grace-period', 65 type=int, 66 default=7, 67 help=('How many days old an expectation needs to be in ' 68 'order to be a candidate for being removed or ' 69 'modified. This prevents newly added expectations ' 70 'from being removed before a sufficient amount of ' 71 'data has been generated with the expectation ' 72 'active. Set to a negative value to disable.')) 73 parser.add_argument('--result-output-file', 74 help=('Output file to store the generated results. If ' 75 'not specified, will use a temporary file.')) 76 parser.add_argument('--bug-output-file', 77 help=('Output file to store "Bug:"/"Fixed:" text ' 78 'intended for use in CL descriptions. If not ' 79 'specified, will be printed to the terminal ' 80 'instead.')) 81 parser.add_argument('--jobs', 82 '-j', 83 type=int, 84 help=('How many parallel jobs to run. By default, runs ' 85 'all work in parallel.')) 86 parser.add_argument('--disable-batching', 87 dest='use_batching', 88 action='store_false', 89 default=True, 90 help=('Disables the use of batching when running ' 91 'queries. Batching allows for more queries to be ' 92 'run in parallel, but increases query overhead by ' 93 'a variable amount.')) 94 internal_group = parser.add_mutually_exclusive_group() 95 internal_group.add_argument('--include-internal-builders', 96 action='store_true', 97 dest='include_internal_builders', 98 default=None, 99 help=('Includes builders that are defined in ' 100 'src-internal in addition to the public ' 101 'ones. If left unset, will be ' 102 'automatically determined by the presence ' 103 'of src-internal.')) 104 internal_group.add_argument('--no-include-internal-builders', 105 action='store_false', 106 dest='include_internal_builders', 107 default=None, 108 help=('Does not include builders that are ' 109 'defined in src-internal. If left unset, ' 110 'will be automatically determined by the ' 111 'presence of src-internal.')) 112 113 114def PerformCommonPostParseSetup(args: argparse.Namespace) -> None: 115 """Helper function to perform all common post-parse setup. 116 117 Args: 118 args: Parsed arguments from an argparse.ArgumentParser. 119 """ 120 SetLoggingVerbosity(args) 121 SetInternalBuilderInclusion(args) 122 123 124def SetLoggingVerbosity(args: argparse.Namespace) -> None: 125 """Sets logging verbosity based on parsed arguments. 126 127 Args: 128 args: Parsed arguments from an argparse.ArgumentParser. 129 """ 130 if args.quiet: 131 args.verbose = -1 132 verbosity_level = args.verbose 133 if verbosity_level == -1: 134 level = logging.ERROR 135 elif verbosity_level == 0: 136 level = logging.WARNING 137 elif verbosity_level == 1: 138 level = logging.INFO 139 else: 140 level = logging.DEBUG 141 logging.getLogger().setLevel(level) 142 143 144def SetInternalBuilderInclusion(args: argparse.Namespace) -> None: 145 """Sets internal builder inclusion based on parsed arguments. 146 147 Args: 148 args: Parsed arguments from an argparse.ArgumentParser. 149 """ 150 if args.include_internal_builders is not None: 151 return 152 153 if os.path.isdir(constants.SRC_INTERNAL_DIR): 154 args.include_internal_builders = True 155 else: 156 args.include_internal_builders = False 157