Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 2 | # Copyright (c) 2011 The Chromium OS Authors. |
Maxim Cournoyer | cb8d3b1 | 2022-12-20 00:38:41 -0500 | [diff] [blame] | 3 | # Copyright (c) 2022 Maxim Cournoyer <maxim.cournoyer@savoirfairelinux.com> |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 4 | # |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 5 | |
Paul Burton | 352f110 | 2016-09-27 16:03:52 +0100 | [diff] [blame] | 6 | try: |
| 7 | import configparser as ConfigParser |
Maxim Cournoyer | 97f4d76 | 2022-12-20 00:38:36 -0500 | [diff] [blame] | 8 | except Exception: |
Paul Burton | 352f110 | 2016-09-27 16:03:52 +0100 | [diff] [blame] | 9 | import ConfigParser |
| 10 | |
Simon Glass | 9bb0a8d | 2020-11-03 13:54:13 -0700 | [diff] [blame] | 11 | import argparse |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 12 | import os |
| 13 | import re |
| 14 | |
Maxim Cournoyer | a81a41f | 2022-12-20 00:38:38 -0500 | [diff] [blame] | 15 | from patman import gitutil |
| 16 | |
Doug Anderson | 31ffd7f | 2012-12-03 14:43:18 +0000 | [diff] [blame] | 17 | """Default settings per-project. |
| 18 | |
| 19 | These are used by _ProjectConfigParser. Settings names should match |
| 20 | the "dest" of the option parser from patman.py. |
| 21 | """ |
| 22 | _default_settings = { |
| 23 | "u-boot": {}, |
| 24 | "linux": { |
| 25 | "process_tags": "False", |
Douglas Anderson | be4f271 | 2022-07-19 14:56:27 -0700 | [diff] [blame] | 26 | "check_patch_use_tree": "True", |
Philipp Tomsich | 98373d4 | 2020-11-24 18:14:53 +0100 | [diff] [blame] | 27 | }, |
| 28 | "gcc": { |
| 29 | "process_tags": "False", |
| 30 | "add_signoff": "False", |
| 31 | "check_patch": "False", |
| 32 | }, |
Doug Anderson | 31ffd7f | 2012-12-03 14:43:18 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Maxim Cournoyer | 97f4d76 | 2022-12-20 00:38:36 -0500 | [diff] [blame] | 35 | |
Maxim Cournoyer | cc38d35 | 2022-12-20 00:38:37 -0500 | [diff] [blame] | 36 | class _ProjectConfigParser(ConfigParser.ConfigParser): |
Doug Anderson | 31ffd7f | 2012-12-03 14:43:18 +0000 | [diff] [blame] | 37 | """ConfigParser that handles projects. |
| 38 | |
| 39 | There are two main goals of this class: |
| 40 | - Load project-specific default settings. |
| 41 | - Merge general default settings/aliases with project-specific ones. |
| 42 | |
| 43 | # Sample config used for tests below... |
Simon Glass | 945328b | 2020-04-17 18:08:55 -0600 | [diff] [blame] | 44 | >>> from io import StringIO |
Doug Anderson | 31ffd7f | 2012-12-03 14:43:18 +0000 | [diff] [blame] | 45 | >>> sample_config = ''' |
| 46 | ... [alias] |
| 47 | ... me: Peter P. <likesspiders@example.com> |
| 48 | ... enemies: Evil <evil@example.com> |
| 49 | ... |
| 50 | ... [sm_alias] |
| 51 | ... enemies: Green G. <ugly@example.com> |
| 52 | ... |
| 53 | ... [sm2_alias] |
| 54 | ... enemies: Doc O. <pus@example.com> |
| 55 | ... |
| 56 | ... [settings] |
| 57 | ... am_hero: True |
| 58 | ... ''' |
| 59 | |
| 60 | # Check to make sure that bogus project gets general alias. |
| 61 | >>> config = _ProjectConfigParser("zzz") |
Paul Burton | 44fc9ba | 2016-09-27 16:03:55 +0100 | [diff] [blame] | 62 | >>> config.readfp(StringIO(sample_config)) |
Simon Glass | aa82db6 | 2019-05-14 15:53:52 -0600 | [diff] [blame] | 63 | >>> str(config.get("alias", "enemies")) |
| 64 | 'Evil <evil@example.com>' |
Doug Anderson | 31ffd7f | 2012-12-03 14:43:18 +0000 | [diff] [blame] | 65 | |
| 66 | # Check to make sure that alias gets overridden by project. |
| 67 | >>> config = _ProjectConfigParser("sm") |
Paul Burton | 44fc9ba | 2016-09-27 16:03:55 +0100 | [diff] [blame] | 68 | >>> config.readfp(StringIO(sample_config)) |
Simon Glass | aa82db6 | 2019-05-14 15:53:52 -0600 | [diff] [blame] | 69 | >>> str(config.get("alias", "enemies")) |
| 70 | 'Green G. <ugly@example.com>' |
Doug Anderson | 31ffd7f | 2012-12-03 14:43:18 +0000 | [diff] [blame] | 71 | |
| 72 | # Check to make sure that settings get merged with project. |
| 73 | >>> config = _ProjectConfigParser("linux") |
Paul Burton | 44fc9ba | 2016-09-27 16:03:55 +0100 | [diff] [blame] | 74 | >>> config.readfp(StringIO(sample_config)) |
Simon Glass | aa82db6 | 2019-05-14 15:53:52 -0600 | [diff] [blame] | 75 | >>> sorted((str(a), str(b)) for (a, b) in config.items("settings")) |
Douglas Anderson | be4f271 | 2022-07-19 14:56:27 -0700 | [diff] [blame] | 76 | [('am_hero', 'True'), ('check_patch_use_tree', 'True'), ('process_tags', 'False')] |
Doug Anderson | 31ffd7f | 2012-12-03 14:43:18 +0000 | [diff] [blame] | 77 | |
| 78 | # Check to make sure that settings works with unknown project. |
| 79 | >>> config = _ProjectConfigParser("unknown") |
Paul Burton | 44fc9ba | 2016-09-27 16:03:55 +0100 | [diff] [blame] | 80 | >>> config.readfp(StringIO(sample_config)) |
Simon Glass | aa82db6 | 2019-05-14 15:53:52 -0600 | [diff] [blame] | 81 | >>> sorted((str(a), str(b)) for (a, b) in config.items("settings")) |
| 82 | [('am_hero', 'True')] |
Doug Anderson | 31ffd7f | 2012-12-03 14:43:18 +0000 | [diff] [blame] | 83 | """ |
| 84 | def __init__(self, project_name): |
| 85 | """Construct _ProjectConfigParser. |
| 86 | |
Maxim Cournoyer | cc38d35 | 2022-12-20 00:38:37 -0500 | [diff] [blame] | 87 | In addition to standard ConfigParser initialization, this also |
| 88 | loads project defaults. |
Doug Anderson | 31ffd7f | 2012-12-03 14:43:18 +0000 | [diff] [blame] | 89 | |
| 90 | Args: |
| 91 | project_name: The name of the project. |
| 92 | """ |
| 93 | self._project_name = project_name |
Maxim Cournoyer | cc38d35 | 2022-12-20 00:38:37 -0500 | [diff] [blame] | 94 | ConfigParser.ConfigParser.__init__(self) |
Doug Anderson | 31ffd7f | 2012-12-03 14:43:18 +0000 | [diff] [blame] | 95 | |
| 96 | # Update the project settings in the config based on |
| 97 | # the _default_settings global. |
| 98 | project_settings = "%s_settings" % project_name |
| 99 | if not self.has_section(project_settings): |
| 100 | self.add_section(project_settings) |
| 101 | project_defaults = _default_settings.get(project_name, {}) |
Paul Burton | 4466f1a | 2016-09-27 16:03:54 +0100 | [diff] [blame] | 102 | for setting_name, setting_value in project_defaults.items(): |
Doug Anderson | 31ffd7f | 2012-12-03 14:43:18 +0000 | [diff] [blame] | 103 | self.set(project_settings, setting_name, setting_value) |
| 104 | |
| 105 | def get(self, section, option, *args, **kwargs): |
Maxim Cournoyer | cc38d35 | 2022-12-20 00:38:37 -0500 | [diff] [blame] | 106 | """Extend ConfigParser to try project_section before section. |
Doug Anderson | 31ffd7f | 2012-12-03 14:43:18 +0000 | [diff] [blame] | 107 | |
| 108 | Args: |
Maxim Cournoyer | cc38d35 | 2022-12-20 00:38:37 -0500 | [diff] [blame] | 109 | See ConfigParser. |
Doug Anderson | 31ffd7f | 2012-12-03 14:43:18 +0000 | [diff] [blame] | 110 | Returns: |
Maxim Cournoyer | cc38d35 | 2022-12-20 00:38:37 -0500 | [diff] [blame] | 111 | See ConfigParser. |
Doug Anderson | 31ffd7f | 2012-12-03 14:43:18 +0000 | [diff] [blame] | 112 | """ |
| 113 | try: |
Maxim Cournoyer | cc38d35 | 2022-12-20 00:38:37 -0500 | [diff] [blame] | 114 | val = ConfigParser.ConfigParser.get( |
Doug Anderson | 31ffd7f | 2012-12-03 14:43:18 +0000 | [diff] [blame] | 115 | self, "%s_%s" % (self._project_name, section), option, |
| 116 | *args, **kwargs |
| 117 | ) |
| 118 | except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): |
Maxim Cournoyer | cc38d35 | 2022-12-20 00:38:37 -0500 | [diff] [blame] | 119 | val = ConfigParser.ConfigParser.get( |
Doug Anderson | 31ffd7f | 2012-12-03 14:43:18 +0000 | [diff] [blame] | 120 | self, section, option, *args, **kwargs |
| 121 | ) |
Simon Glass | 9dfb311 | 2020-11-08 20:36:18 -0700 | [diff] [blame] | 122 | return val |
Doug Anderson | 31ffd7f | 2012-12-03 14:43:18 +0000 | [diff] [blame] | 123 | |
| 124 | def items(self, section, *args, **kwargs): |
Maxim Cournoyer | cc38d35 | 2022-12-20 00:38:37 -0500 | [diff] [blame] | 125 | """Extend ConfigParser to add project_section to section. |
Doug Anderson | 31ffd7f | 2012-12-03 14:43:18 +0000 | [diff] [blame] | 126 | |
| 127 | Args: |
Maxim Cournoyer | cc38d35 | 2022-12-20 00:38:37 -0500 | [diff] [blame] | 128 | See ConfigParser. |
Doug Anderson | 31ffd7f | 2012-12-03 14:43:18 +0000 | [diff] [blame] | 129 | Returns: |
Maxim Cournoyer | cc38d35 | 2022-12-20 00:38:37 -0500 | [diff] [blame] | 130 | See ConfigParser. |
Doug Anderson | 31ffd7f | 2012-12-03 14:43:18 +0000 | [diff] [blame] | 131 | """ |
| 132 | project_items = [] |
| 133 | has_project_section = False |
| 134 | top_items = [] |
| 135 | |
| 136 | # Get items from the project section |
| 137 | try: |
Maxim Cournoyer | cc38d35 | 2022-12-20 00:38:37 -0500 | [diff] [blame] | 138 | project_items = ConfigParser.ConfigParser.items( |
Doug Anderson | 31ffd7f | 2012-12-03 14:43:18 +0000 | [diff] [blame] | 139 | self, "%s_%s" % (self._project_name, section), *args, **kwargs |
| 140 | ) |
| 141 | has_project_section = True |
| 142 | except ConfigParser.NoSectionError: |
| 143 | pass |
| 144 | |
| 145 | # Get top-level items |
| 146 | try: |
Maxim Cournoyer | cc38d35 | 2022-12-20 00:38:37 -0500 | [diff] [blame] | 147 | top_items = ConfigParser.ConfigParser.items( |
Doug Anderson | 31ffd7f | 2012-12-03 14:43:18 +0000 | [diff] [blame] | 148 | self, section, *args, **kwargs |
| 149 | ) |
| 150 | except ConfigParser.NoSectionError: |
| 151 | # If neither section exists raise the error on... |
| 152 | if not has_project_section: |
| 153 | raise |
| 154 | |
| 155 | item_dict = dict(top_items) |
| 156 | item_dict.update(project_items) |
Simon Glass | 9dfb311 | 2020-11-08 20:36:18 -0700 | [diff] [blame] | 157 | return {(item, val) for item, val in item_dict.items()} |
Doug Anderson | 31ffd7f | 2012-12-03 14:43:18 +0000 | [diff] [blame] | 158 | |
Maxim Cournoyer | 97f4d76 | 2022-12-20 00:38:36 -0500 | [diff] [blame] | 159 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 160 | def ReadGitAliases(fname): |
| 161 | """Read a git alias file. This is in the form used by git: |
| 162 | |
| 163 | alias uboot u-boot@lists.denx.de |
| 164 | alias wd Wolfgang Denk <wd@denx.de> |
| 165 | |
| 166 | Args: |
| 167 | fname: Filename to read |
| 168 | """ |
| 169 | try: |
Simon Glass | f544a2d | 2019-10-31 07:42:51 -0600 | [diff] [blame] | 170 | fd = open(fname, 'r', encoding='utf-8') |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 171 | except IOError: |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 172 | print("Warning: Cannot find alias file '%s'" % fname) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 173 | return |
| 174 | |
Maxim Cournoyer | 97f4d76 | 2022-12-20 00:38:36 -0500 | [diff] [blame] | 175 | re_line = re.compile(r'alias\s+(\S+)\s+(.*)') |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 176 | for line in fd.readlines(): |
| 177 | line = line.strip() |
| 178 | if not line or line[0] == '#': |
| 179 | continue |
| 180 | |
| 181 | m = re_line.match(line) |
| 182 | if not m: |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 183 | print("Warning: Alias file line '%s' not understood" % line) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 184 | continue |
| 185 | |
| 186 | list = alias.get(m.group(1), []) |
| 187 | for item in m.group(2).split(','): |
| 188 | item = item.strip() |
| 189 | if item: |
| 190 | list.append(item) |
| 191 | alias[m.group(1)] = list |
| 192 | |
| 193 | fd.close() |
| 194 | |
Maxim Cournoyer | 97f4d76 | 2022-12-20 00:38:36 -0500 | [diff] [blame] | 195 | |
Maxim Cournoyer | a81a41f | 2022-12-20 00:38:38 -0500 | [diff] [blame] | 196 | def CreatePatmanConfigFile(config_fname): |
Vikram Narayanan | 12fb29a | 2012-05-23 09:01:06 +0000 | [diff] [blame] | 197 | """Creates a config file under $(HOME)/.patman if it can't find one. |
| 198 | |
| 199 | Args: |
| 200 | config_fname: Default config filename i.e., $(HOME)/.patman |
| 201 | |
| 202 | Returns: |
| 203 | None |
| 204 | """ |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 205 | name = gitutil.get_default_user_name() |
Maxim Cournoyer | 97f4d76 | 2022-12-20 00:38:36 -0500 | [diff] [blame] | 206 | if name is None: |
Simon Glass | 547cba6 | 2022-02-11 13:23:18 -0700 | [diff] [blame] | 207 | name = input("Enter name: ") |
Vikram Narayanan | 12fb29a | 2012-05-23 09:01:06 +0000 | [diff] [blame] | 208 | |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 209 | email = gitutil.get_default_user_email() |
Vikram Narayanan | 12fb29a | 2012-05-23 09:01:06 +0000 | [diff] [blame] | 210 | |
Maxim Cournoyer | 97f4d76 | 2022-12-20 00:38:36 -0500 | [diff] [blame] | 211 | if email is None: |
Simon Glass | 547cba6 | 2022-02-11 13:23:18 -0700 | [diff] [blame] | 212 | email = input("Enter email: ") |
Vikram Narayanan | 12fb29a | 2012-05-23 09:01:06 +0000 | [diff] [blame] | 213 | |
| 214 | try: |
| 215 | f = open(config_fname, 'w') |
| 216 | except IOError: |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 217 | print("Couldn't create patman config file\n") |
Vikram Narayanan | 12fb29a | 2012-05-23 09:01:06 +0000 | [diff] [blame] | 218 | raise |
| 219 | |
Simon Glass | 52a0864 | 2017-09-12 20:30:28 -0600 | [diff] [blame] | 220 | print('''[alias] |
| 221 | me: %s <%s> |
| 222 | |
| 223 | [bounces] |
| 224 | nxp = Zhikang Zhang <zhikang.zhang@nxp.com> |
| 225 | ''' % (name, email), file=f) |
Maxim Cournoyer | 97f4d76 | 2022-12-20 00:38:36 -0500 | [diff] [blame] | 226 | f.close() |
| 227 | |
Vikram Narayanan | 12fb29a | 2012-05-23 09:01:06 +0000 | [diff] [blame] | 228 | |
Simon Glass | 9bb0a8d | 2020-11-03 13:54:13 -0700 | [diff] [blame] | 229 | def _UpdateDefaults(main_parser, config): |
Doug Anderson | 3d3077c | 2012-12-03 14:43:17 +0000 | [diff] [blame] | 230 | """Update the given OptionParser defaults based on config. |
| 231 | |
Simon Glass | 9bb0a8d | 2020-11-03 13:54:13 -0700 | [diff] [blame] | 232 | We'll walk through all of the settings from all parsers. |
Doug Anderson | 3d3077c | 2012-12-03 14:43:17 +0000 | [diff] [blame] | 233 | For each setting we'll look for a default in the option parser. |
| 234 | If it's found we'll update the option parser default. |
| 235 | |
| 236 | The idea here is that the .patman file should be able to update |
| 237 | defaults but that command line flags should still have the final |
| 238 | say. |
| 239 | |
| 240 | Args: |
Simon Glass | 9bb0a8d | 2020-11-03 13:54:13 -0700 | [diff] [blame] | 241 | parser: An instance of an ArgumentParser whose defaults will be |
Doug Anderson | 3d3077c | 2012-12-03 14:43:17 +0000 | [diff] [blame] | 242 | updated. |
Doug Anderson | 31ffd7f | 2012-12-03 14:43:18 +0000 | [diff] [blame] | 243 | config: An instance of _ProjectConfigParser that we will query |
Doug Anderson | 3d3077c | 2012-12-03 14:43:17 +0000 | [diff] [blame] | 244 | for settings. |
| 245 | """ |
Simon Glass | 9bb0a8d | 2020-11-03 13:54:13 -0700 | [diff] [blame] | 246 | # Find all the parsers and subparsers |
| 247 | parsers = [main_parser] |
| 248 | parsers += [subparser for action in main_parser._actions |
Maxim Cournoyer | 97f4d76 | 2022-12-20 00:38:36 -0500 | [diff] [blame] | 249 | if isinstance(action, argparse._SubParsersAction) |
| 250 | for _, subparser in action.choices.items()] |
Simon Glass | 9bb0a8d | 2020-11-03 13:54:13 -0700 | [diff] [blame] | 251 | |
| 252 | # Collect the defaults from each parser |
| 253 | defaults = {} |
Sean Anderson | 461df86 | 2022-04-29 10:53:34 -0400 | [diff] [blame] | 254 | parser_defaults = [] |
Simon Glass | 9bb0a8d | 2020-11-03 13:54:13 -0700 | [diff] [blame] | 255 | for parser in parsers: |
| 256 | pdefs = parser.parse_known_args()[0] |
Sean Anderson | 461df86 | 2022-04-29 10:53:34 -0400 | [diff] [blame] | 257 | parser_defaults.append(pdefs) |
Simon Glass | 9bb0a8d | 2020-11-03 13:54:13 -0700 | [diff] [blame] | 258 | defaults.update(vars(pdefs)) |
| 259 | |
| 260 | # Go through the settings and collect defaults |
Doug Anderson | 3d3077c | 2012-12-03 14:43:17 +0000 | [diff] [blame] | 261 | for name, val in config.items('settings'): |
Simon Glass | eb101ac | 2020-07-05 21:41:53 -0600 | [diff] [blame] | 262 | if name in defaults: |
| 263 | default_val = defaults[name] |
Doug Anderson | 3d3077c | 2012-12-03 14:43:17 +0000 | [diff] [blame] | 264 | if isinstance(default_val, bool): |
| 265 | val = config.getboolean('settings', name) |
| 266 | elif isinstance(default_val, int): |
| 267 | val = config.getint('settings', name) |
Simon Glass | 9bb0a8d | 2020-11-03 13:54:13 -0700 | [diff] [blame] | 268 | elif isinstance(default_val, str): |
| 269 | val = config.get('settings', name) |
Simon Glass | eb101ac | 2020-07-05 21:41:53 -0600 | [diff] [blame] | 270 | defaults[name] = val |
Doug Anderson | 3d3077c | 2012-12-03 14:43:17 +0000 | [diff] [blame] | 271 | else: |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 272 | print("WARNING: Unknown setting %s" % name) |
Simon Glass | 9bb0a8d | 2020-11-03 13:54:13 -0700 | [diff] [blame] | 273 | |
Sean Anderson | 461df86 | 2022-04-29 10:53:34 -0400 | [diff] [blame] | 274 | # Set all the defaults and manually propagate them to subparsers |
Simon Glass | 9bb0a8d | 2020-11-03 13:54:13 -0700 | [diff] [blame] | 275 | main_parser.set_defaults(**defaults) |
Sean Anderson | 461df86 | 2022-04-29 10:53:34 -0400 | [diff] [blame] | 276 | for parser, pdefs in zip(parsers, parser_defaults): |
Maxim Cournoyer | 97f4d76 | 2022-12-20 00:38:36 -0500 | [diff] [blame] | 277 | parser.set_defaults(**{k: v for k, v in defaults.items() |
| 278 | if k in pdefs}) |
| 279 | |
Doug Anderson | 3d3077c | 2012-12-03 14:43:17 +0000 | [diff] [blame] | 280 | |
Simon Glass | 3338269 | 2015-01-29 11:35:17 -0700 | [diff] [blame] | 281 | def _ReadAliasFile(fname): |
| 282 | """Read in the U-Boot git alias file if it exists. |
| 283 | |
| 284 | Args: |
| 285 | fname: Filename to read. |
| 286 | """ |
| 287 | if os.path.exists(fname): |
| 288 | bad_line = None |
Simon Glass | f544a2d | 2019-10-31 07:42:51 -0600 | [diff] [blame] | 289 | with open(fname, encoding='utf-8') as fd: |
Simon Glass | 3338269 | 2015-01-29 11:35:17 -0700 | [diff] [blame] | 290 | linenum = 0 |
| 291 | for line in fd: |
| 292 | linenum += 1 |
| 293 | line = line.strip() |
| 294 | if not line or line.startswith('#'): |
| 295 | continue |
Adam Sampson | 0265132 | 2018-06-27 14:38:58 +0100 | [diff] [blame] | 296 | words = line.split(None, 2) |
Simon Glass | 3338269 | 2015-01-29 11:35:17 -0700 | [diff] [blame] | 297 | if len(words) < 3 or words[0] != 'alias': |
| 298 | if not bad_line: |
| 299 | bad_line = "%s:%d:Invalid line '%s'" % (fname, linenum, |
| 300 | line) |
| 301 | continue |
| 302 | alias[words[1]] = [s.strip() for s in words[2].split(',')] |
| 303 | if bad_line: |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 304 | print(bad_line) |
Simon Glass | 3338269 | 2015-01-29 11:35:17 -0700 | [diff] [blame] | 305 | |
Maxim Cournoyer | 97f4d76 | 2022-12-20 00:38:36 -0500 | [diff] [blame] | 306 | |
Chris Packham | e8d2a12 | 2017-09-01 20:57:53 +1200 | [diff] [blame] | 307 | def _ReadBouncesFile(fname): |
| 308 | """Read in the bounces file if it exists |
| 309 | |
| 310 | Args: |
| 311 | fname: Filename to read. |
| 312 | """ |
| 313 | if os.path.exists(fname): |
| 314 | with open(fname) as fd: |
| 315 | for line in fd: |
| 316 | if line.startswith('#'): |
| 317 | continue |
| 318 | bounces.add(line.strip()) |
| 319 | |
Maxim Cournoyer | 97f4d76 | 2022-12-20 00:38:36 -0500 | [diff] [blame] | 320 | |
Simon Glass | 52a0864 | 2017-09-12 20:30:28 -0600 | [diff] [blame] | 321 | def GetItems(config, section): |
| 322 | """Get the items from a section of the config. |
| 323 | |
| 324 | Args: |
| 325 | config: _ProjectConfigParser object containing settings |
| 326 | section: name of section to retrieve |
| 327 | |
| 328 | Returns: |
| 329 | List of (name, value) tuples for the section |
| 330 | """ |
| 331 | try: |
| 332 | return config.items(section) |
Maxim Cournoyer | 97f4d76 | 2022-12-20 00:38:36 -0500 | [diff] [blame] | 333 | except ConfigParser.NoSectionError: |
Simon Glass | 52a0864 | 2017-09-12 20:30:28 -0600 | [diff] [blame] | 334 | return [] |
Maxim Cournoyer | 97f4d76 | 2022-12-20 00:38:36 -0500 | [diff] [blame] | 335 | |
Simon Glass | 52a0864 | 2017-09-12 20:30:28 -0600 | [diff] [blame] | 336 | |
Maxim Cournoyer | da6f7cf | 2022-12-20 00:38:39 -0500 | [diff] [blame] | 337 | def Setup(parser, project_name, config_fname=None): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 338 | """Set up the settings module by reading config files. |
| 339 | |
Maxim Cournoyer | cb8d3b1 | 2022-12-20 00:38:41 -0500 | [diff] [blame] | 340 | Unless `config_fname` is specified, a `.patman` config file local |
| 341 | to the git repository is consulted, followed by the global |
| 342 | `$HOME/.patman`. If none exists, the later is created. Values |
| 343 | defined in the local config file take precedence over those |
| 344 | defined in the global one. |
| 345 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 346 | Args: |
Maxim Cournoyer | da6f7cf | 2022-12-20 00:38:39 -0500 | [diff] [blame] | 347 | parser: The parser to update. |
Doug Anderson | 31ffd7f | 2012-12-03 14:43:18 +0000 | [diff] [blame] | 348 | project_name: Name of project that we're working on; we'll look |
| 349 | for sections named "project_section" as well. |
Maxim Cournoyer | 64a690f | 2022-12-20 00:38:40 -0500 | [diff] [blame] | 350 | config_fname: Config filename to read. An error is raised if it |
| 351 | does not exist. |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 352 | """ |
Simon Glass | 3338269 | 2015-01-29 11:35:17 -0700 | [diff] [blame] | 353 | # First read the git alias file if available |
| 354 | _ReadAliasFile('doc/git-mailrc') |
Doug Anderson | 31ffd7f | 2012-12-03 14:43:18 +0000 | [diff] [blame] | 355 | config = _ProjectConfigParser(project_name) |
Maxim Cournoyer | da6f7cf | 2022-12-20 00:38:39 -0500 | [diff] [blame] | 356 | |
Maxim Cournoyer | 64a690f | 2022-12-20 00:38:40 -0500 | [diff] [blame] | 357 | if config_fname and not os.path.exists(config_fname): |
| 358 | raise Exception(f'provided {config_fname} does not exist') |
| 359 | |
Maxim Cournoyer | da6f7cf | 2022-12-20 00:38:39 -0500 | [diff] [blame] | 360 | if not config_fname: |
Vikram Narayanan | c387d36d | 2012-05-23 08:58:58 +0000 | [diff] [blame] | 361 | config_fname = '%s/.patman' % os.getenv('HOME') |
Maxim Cournoyer | cb8d3b1 | 2022-12-20 00:38:41 -0500 | [diff] [blame] | 362 | has_config = os.path.exists(config_fname) |
Vikram Narayanan | 12fb29a | 2012-05-23 09:01:06 +0000 | [diff] [blame] | 363 | |
Maxim Cournoyer | cb8d3b1 | 2022-12-20 00:38:41 -0500 | [diff] [blame] | 364 | git_local_config_fname = os.path.join(gitutil.get_top_level(), '.patman') |
| 365 | has_git_local_config = os.path.exists(git_local_config_fname) |
Vikram Narayanan | 12fb29a | 2012-05-23 09:01:06 +0000 | [diff] [blame] | 366 | |
Maxim Cournoyer | cb8d3b1 | 2022-12-20 00:38:41 -0500 | [diff] [blame] | 367 | # Read the git local config last, so that its values override |
| 368 | # those of the global config, if any. |
| 369 | if has_config: |
| 370 | config.read(config_fname) |
| 371 | if has_git_local_config: |
| 372 | config.read(git_local_config_fname) |
| 373 | |
| 374 | if not (has_config or has_git_local_config): |
| 375 | print("No config file found.\nCreating ~/.patman...\n") |
| 376 | CreatePatmanConfigFile(config_fname) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 377 | |
Simon Glass | 52a0864 | 2017-09-12 20:30:28 -0600 | [diff] [blame] | 378 | for name, value in GetItems(config, 'alias'): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 379 | alias[name] = value.split(',') |
| 380 | |
Chris Packham | e8d2a12 | 2017-09-01 20:57:53 +1200 | [diff] [blame] | 381 | _ReadBouncesFile('doc/bounces') |
Simon Glass | 52a0864 | 2017-09-12 20:30:28 -0600 | [diff] [blame] | 382 | for name, value in GetItems(config, 'bounces'): |
Chris Packham | e8d2a12 | 2017-09-01 20:57:53 +1200 | [diff] [blame] | 383 | bounces.add(value) |
| 384 | |
Doug Anderson | 3d3077c | 2012-12-03 14:43:17 +0000 | [diff] [blame] | 385 | _UpdateDefaults(parser, config) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 386 | |
Maxim Cournoyer | 97f4d76 | 2022-12-20 00:38:36 -0500 | [diff] [blame] | 387 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 388 | # These are the aliases we understand, indexed by alias. Each member is a list. |
| 389 | alias = {} |
Chris Packham | e8d2a12 | 2017-09-01 20:57:53 +1200 | [diff] [blame] | 390 | bounces = set() |
Doug Anderson | 31ffd7f | 2012-12-03 14:43:18 +0000 | [diff] [blame] | 391 | |
| 392 | if __name__ == "__main__": |
| 393 | import doctest |
| 394 | |
| 395 | doctest.testmod() |