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('--expectation-grace-period', 66 type=int, 67 default=7, 68 help=('How many days old an expectation needs to be in ' 69 'order to be a candidate for being removed or ' 70 'modified. This prevents newly added expectations ' 71 'from being removed before a sufficient amount of ' 72 'data has been generated with the expectation ' 73 'active. Set to a negative value to disable.')) 74 parser.add_argument('--result-output-file', 75 help=('Output file to store the generated results. If ' 76 'not specified, will use a temporary file.')) 77 parser.add_argument('--bug-output-file', 78 help=('Output file to store "Bug:"/"Fixed:" text ' 79 'intended for use in CL descriptions. If not ' 80 'specified, will be printed to the terminal ' 81 'instead.')) 82 parser.add_argument('--jobs', 83 '-j', 84 type=int, 85 help=('DEPRECATED/NO-OP. Will be removed once all uses ' 86 'are removed.')) 87 parser.add_argument('--keep-unmatched-results', 88 action='store_true', 89 default=False, 90 help=('Store unmatched results and include them in the ' 91 'script output. Doing so can result in a ' 92 'significant increase in memory usage depending on ' 93 'the data being queried. This is meant for ' 94 'debugging purposes and should not be needed ' 95 'during normal use.')) 96 internal_group = parser.add_mutually_exclusive_group() 97 internal_group.add_argument('--include-internal-builders', 98 action='store_true', 99 dest='include_internal_builders', 100 default=None, 101 help=('Includes builders that are defined in ' 102 'src-internal in addition to the public ' 103 'ones. If left unset, will be ' 104 'automatically determined by the presence ' 105 'of src-internal.')) 106 internal_group.add_argument('--no-include-internal-builders', 107 action='store_false', 108 dest='include_internal_builders', 109 default=None, 110 help=('Does not include builders that are ' 111 'defined in src-internal. If left unset, ' 112 'will be automatically determined by the ' 113 'presence of src-internal.')) 114 115 116def PerformCommonPostParseSetup(args: argparse.Namespace) -> None: 117 """Helper function to perform all common post-parse setup. 118 119 Args: 120 args: Parsed arguments from an argparse.ArgumentParser. 121 """ 122 SetLoggingVerbosity(args) 123 SetInternalBuilderInclusion(args) 124 125 126def SetLoggingVerbosity(args: argparse.Namespace) -> None: 127 """Sets logging verbosity based on parsed arguments. 128 129 Args: 130 args: Parsed arguments from an argparse.ArgumentParser. 131 """ 132 if args.quiet: 133 args.verbose = -1 134 verbosity_level = args.verbose 135 if verbosity_level == -1: 136 level = logging.ERROR 137 elif verbosity_level == 0: 138 level = logging.WARNING 139 elif verbosity_level == 1: 140 level = logging.INFO 141 else: 142 level = logging.DEBUG 143 logging.getLogger().setLevel(level) 144 145 146def SetInternalBuilderInclusion(args: argparse.Namespace) -> None: 147 """Sets internal builder inclusion based on parsed arguments. 148 149 Args: 150 args: Parsed arguments from an argparse.ArgumentParser. 151 """ 152 if args.include_internal_builders is not None: 153 return 154 155 if os.path.isdir(constants.SRC_INTERNAL_DIR): 156 args.include_internal_builders = True 157 else: 158 args.include_internal_builders = False 159