Blame view
tools/patman/settings.py
10.3 KB
83d290c56
|
1 |
# SPDX-License-Identifier: GPL-2.0+ |
0d24de9d5
|
2 3 |
# Copyright (c) 2011 The Chromium OS Authors. # |
0d24de9d5
|
4 |
|
a920a17b2
|
5 |
from __future__ import print_function |
2ce7b21e6
|
6 7 8 9 |
try: import configparser as ConfigParser except: import ConfigParser |
0d24de9d5
|
10 11 12 13 |
import os import re import command |
87d65558e
|
14 |
import gitutil |
513eace47
|
15 |
import tools |
0d24de9d5
|
16 |
|
a1dcee84c
|
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
"""Default settings per-project. These are used by _ProjectConfigParser. Settings names should match the "dest" of the option parser from patman.py. """ _default_settings = { "u-boot": {}, "linux": { "process_tags": "False", } } class _ProjectConfigParser(ConfigParser.SafeConfigParser): """ConfigParser that handles projects. There are two main goals of this class: - Load project-specific default settings. - Merge general default settings/aliases with project-specific ones. # Sample config used for tests below... |
f5d44b9ba
|
37 38 39 40 |
>>> try: ... from StringIO import StringIO ... except ImportError: ... from io import StringIO |
a1dcee84c
|
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
>>> sample_config = ''' ... [alias] ... me: Peter P. <likesspiders@example.com> ... enemies: Evil <evil@example.com> ... ... [sm_alias] ... enemies: Green G. <ugly@example.com> ... ... [sm2_alias] ... enemies: Doc O. <pus@example.com> ... ... [settings] ... am_hero: True ... ''' # Check to make sure that bogus project gets general alias. >>> config = _ProjectConfigParser("zzz") |
f5d44b9ba
|
58 |
>>> config.readfp(StringIO(sample_config)) |
7ebb45c7e
|
59 60 |
>>> str(config.get("alias", "enemies")) 'Evil <evil@example.com>' |
a1dcee84c
|
61 62 63 |
# Check to make sure that alias gets overridden by project. >>> config = _ProjectConfigParser("sm") |
f5d44b9ba
|
64 |
>>> config.readfp(StringIO(sample_config)) |
7ebb45c7e
|
65 66 |
>>> str(config.get("alias", "enemies")) 'Green G. <ugly@example.com>' |
a1dcee84c
|
67 68 69 |
# Check to make sure that settings get merged with project. >>> config = _ProjectConfigParser("linux") |
f5d44b9ba
|
70 |
>>> config.readfp(StringIO(sample_config)) |
7ebb45c7e
|
71 72 |
>>> sorted((str(a), str(b)) for (a, b) in config.items("settings")) [('am_hero', 'True'), ('process_tags', 'False')] |
a1dcee84c
|
73 74 75 |
# Check to make sure that settings works with unknown project. >>> config = _ProjectConfigParser("unknown") |
f5d44b9ba
|
76 |
>>> config.readfp(StringIO(sample_config)) |
7ebb45c7e
|
77 78 |
>>> sorted((str(a), str(b)) for (a, b) in config.items("settings")) [('am_hero', 'True')] |
a1dcee84c
|
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
""" def __init__(self, project_name): """Construct _ProjectConfigParser. In addition to standard SafeConfigParser initialization, this also loads project defaults. Args: project_name: The name of the project. """ self._project_name = project_name ConfigParser.SafeConfigParser.__init__(self) # Update the project settings in the config based on # the _default_settings global. project_settings = "%s_settings" % project_name if not self.has_section(project_settings): self.add_section(project_settings) project_defaults = _default_settings.get(project_name, {}) |
c9eac38a2
|
98 |
for setting_name, setting_value in project_defaults.items(): |
a1dcee84c
|
99 100 101 102 103 104 105 106 107 108 109 |
self.set(project_settings, setting_name, setting_value) def get(self, section, option, *args, **kwargs): """Extend SafeConfigParser to try project_section before section. Args: See SafeConfigParser. Returns: See SafeConfigParser. """ try: |
ec9e0f471
|
110 |
val = ConfigParser.SafeConfigParser.get( |
a1dcee84c
|
111 112 113 114 |
self, "%s_%s" % (self._project_name, section), option, *args, **kwargs ) except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): |
ec9e0f471
|
115 |
val = ConfigParser.SafeConfigParser.get( |
a1dcee84c
|
116 117 |
self, section, option, *args, **kwargs ) |
513eace47
|
118 |
return tools.ToUnicode(val) |
a1dcee84c
|
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
def items(self, section, *args, **kwargs): """Extend SafeConfigParser to add project_section to section. Args: See SafeConfigParser. Returns: See SafeConfigParser. """ project_items = [] has_project_section = False top_items = [] # Get items from the project section try: project_items = ConfigParser.SafeConfigParser.items( self, "%s_%s" % (self._project_name, section), *args, **kwargs ) has_project_section = True except ConfigParser.NoSectionError: pass # Get top-level items try: top_items = ConfigParser.SafeConfigParser.items( self, section, *args, **kwargs ) except ConfigParser.NoSectionError: # If neither section exists raise the error on... if not has_project_section: raise item_dict = dict(top_items) item_dict.update(project_items) |
513eace47
|
153 |
return {(tools.ToUnicode(item), tools.ToUnicode(val)) |
4a4c5dd43
|
154 |
for item, val in item_dict.items()} |
a1dcee84c
|
155 |
|
0d24de9d5
|
156 157 158 159 160 161 162 163 164 165 |
def ReadGitAliases(fname): """Read a git alias file. This is in the form used by git: alias uboot u-boot@lists.denx.de alias wd Wolfgang Denk <wd@denx.de> Args: fname: Filename to read """ try: |
272cd85de
|
166 |
fd = open(fname, 'r', encoding='utf-8') |
0d24de9d5
|
167 |
except IOError: |
a920a17b2
|
168 |
print("Warning: Cannot find alias file '%s'" % fname) |
0d24de9d5
|
169 170 171 172 173 174 175 176 177 178 |
return re_line = re.compile('alias\s+(\S+)\s+(.*)') for line in fd.readlines(): line = line.strip() if not line or line[0] == '#': continue m = re_line.match(line) if not m: |
a920a17b2
|
179 |
print("Warning: Alias file line '%s' not understood" % line) |
0d24de9d5
|
180 181 182 183 184 185 186 187 188 189 |
continue list = alias.get(m.group(1), []) for item in m.group(2).split(','): item = item.strip() if item: list.append(item) alias[m.group(1)] = list fd.close() |
87d65558e
|
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
def CreatePatmanConfigFile(config_fname): """Creates a config file under $(HOME)/.patman if it can't find one. Args: config_fname: Default config filename i.e., $(HOME)/.patman Returns: None """ name = gitutil.GetDefaultUserName() if name == None: name = raw_input("Enter name: ") email = gitutil.GetDefaultUserEmail() if email == None: email = raw_input("Enter email: ") try: f = open(config_fname, 'w') except IOError: |
a920a17b2
|
211 212 |
print("Couldn't create patman config file ") |
87d65558e
|
213 |
raise |
ad8931400
|
214 215 216 217 218 219 |
print('''[alias] me: %s <%s> [bounces] nxp = Zhikang Zhang <zhikang.zhang@nxp.com> ''' % (name, email), file=f) |
87d65558e
|
220 |
f.close(); |
8568baed3
|
221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
def _UpdateDefaults(parser, config): """Update the given OptionParser defaults based on config. We'll walk through all of the settings from the parser For each setting we'll look for a default in the option parser. If it's found we'll update the option parser default. The idea here is that the .patman file should be able to update defaults but that command line flags should still have the final say. Args: parser: An instance of an OptionParser whose defaults will be updated. |
a1dcee84c
|
235 |
config: An instance of _ProjectConfigParser that we will query |
8568baed3
|
236 237 238 239 240 241 242 243 244 245 246 247 |
for settings. """ defaults = parser.get_default_values() for name, val in config.items('settings'): if hasattr(defaults, name): default_val = getattr(defaults, name) if isinstance(default_val, bool): val = config.getboolean('settings', name) elif isinstance(default_val, int): val = config.getint('settings', name) parser.set_default(name, val) else: |
a920a17b2
|
248 |
print("WARNING: Unknown setting %s" % name) |
8568baed3
|
249 |
|
8895b3e16
|
250 251 252 253 254 255 256 257 |
def _ReadAliasFile(fname): """Read in the U-Boot git alias file if it exists. Args: fname: Filename to read. """ if os.path.exists(fname): bad_line = None |
272cd85de
|
258 |
with open(fname, encoding='utf-8') as fd: |
8895b3e16
|
259 260 261 262 263 264 |
linenum = 0 for line in fd: linenum += 1 line = line.strip() if not line or line.startswith('#'): continue |
b8a48fbca
|
265 |
words = line.split(None, 2) |
8895b3e16
|
266 267 268 269 270 271 272 |
if len(words) < 3 or words[0] != 'alias': if not bad_line: bad_line = "%s:%d:Invalid line '%s'" % (fname, linenum, line) continue alias[words[1]] = [s.strip() for s in words[2].split(',')] if bad_line: |
a920a17b2
|
273 |
print(bad_line) |
8895b3e16
|
274 |
|
e11aa602a
|
275 276 277 278 279 280 281 282 283 284 285 286 |
def _ReadBouncesFile(fname): """Read in the bounces file if it exists Args: fname: Filename to read. """ if os.path.exists(fname): with open(fname) as fd: for line in fd: if line.startswith('#'): continue bounces.add(line.strip()) |
ad8931400
|
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
def GetItems(config, section): """Get the items from a section of the config. Args: config: _ProjectConfigParser object containing settings section: name of section to retrieve Returns: List of (name, value) tuples for the section """ try: return config.items(section) except ConfigParser.NoSectionError as e: return [] except: raise |
a1dcee84c
|
303 |
def Setup(parser, project_name, config_fname=''): |
0d24de9d5
|
304 305 306 |
"""Set up the settings module by reading config files. Args: |
8568baed3
|
307 |
parser: The parser to update |
a1dcee84c
|
308 309 |
project_name: Name of project that we're working on; we'll look for sections named "project_section" as well. |
0d24de9d5
|
310 311 |
config_fname: Config filename to read ('' for default) """ |
8895b3e16
|
312 313 |
# First read the git alias file if available _ReadAliasFile('doc/git-mailrc') |
a1dcee84c
|
314 |
config = _ProjectConfigParser(project_name) |
0d24de9d5
|
315 |
if config_fname == '': |
2b36c75d7
|
316 |
config_fname = '%s/.patman' % os.getenv('HOME') |
87d65558e
|
317 318 |
if not os.path.exists(config_fname): |
a920a17b2
|
319 320 321 |
print("No config file found ~/.patman Creating one... ") |
87d65558e
|
322 |
CreatePatmanConfigFile(config_fname) |
8568baed3
|
323 |
config.read(config_fname) |
0d24de9d5
|
324 |
|
ad8931400
|
325 |
for name, value in GetItems(config, 'alias'): |
0d24de9d5
|
326 |
alias[name] = value.split(',') |
e11aa602a
|
327 |
_ReadBouncesFile('doc/bounces') |
ad8931400
|
328 |
for name, value in GetItems(config, 'bounces'): |
e11aa602a
|
329 |
bounces.add(value) |
8568baed3
|
330 |
_UpdateDefaults(parser, config) |
0d24de9d5
|
331 332 333 |
# These are the aliases we understand, indexed by alias. Each member is a list. alias = {} |
e11aa602a
|
334 |
bounces = set() |
a1dcee84c
|
335 336 337 338 339 |
if __name__ == "__main__": import doctest doctest.testmod() |