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