2018-05-07 05:58:06 +08:00
|
|
|
# SPDX-License-Identifier: GPL-2.0+
|
2012-01-14 23:12:45 +08:00
|
|
|
# Copyright (c) 2011 The Chromium OS Authors.
|
|
|
|
#
|
|
|
|
|
2016-09-27 23:03:50 +08:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2016-09-27 23:03:52 +08:00
|
|
|
try:
|
|
|
|
import configparser as ConfigParser
|
|
|
|
except:
|
|
|
|
import ConfigParser
|
|
|
|
|
2012-01-14 23:12:45 +08:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
|
|
|
import command
|
2012-05-23 17:01:06 +08:00
|
|
|
import gitutil
|
2019-05-15 05:53:50 +08:00
|
|
|
import tools
|
2012-01-14 23:12:45 +08:00
|
|
|
|
2012-12-03 22:43:18 +08:00
|
|
|
"""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...
|
2016-09-27 23:03:55 +08:00
|
|
|
>>> try:
|
|
|
|
... from StringIO import StringIO
|
|
|
|
... except ImportError:
|
|
|
|
... from io import StringIO
|
2012-12-03 22:43:18 +08:00
|
|
|
>>> 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")
|
2016-09-27 23:03:55 +08:00
|
|
|
>>> config.readfp(StringIO(sample_config))
|
2012-12-03 22:43:18 +08:00
|
|
|
>>> config.get("alias", "enemies")
|
2018-10-02 11:12:33 +08:00
|
|
|
u'Evil <evil@example.com>'
|
2012-12-03 22:43:18 +08:00
|
|
|
|
|
|
|
# Check to make sure that alias gets overridden by project.
|
|
|
|
>>> config = _ProjectConfigParser("sm")
|
2016-09-27 23:03:55 +08:00
|
|
|
>>> config.readfp(StringIO(sample_config))
|
2012-12-03 22:43:18 +08:00
|
|
|
>>> config.get("alias", "enemies")
|
2018-10-02 11:12:33 +08:00
|
|
|
u'Green G. <ugly@example.com>'
|
2012-12-03 22:43:18 +08:00
|
|
|
|
|
|
|
# Check to make sure that settings get merged with project.
|
|
|
|
>>> config = _ProjectConfigParser("linux")
|
2016-09-27 23:03:55 +08:00
|
|
|
>>> config.readfp(StringIO(sample_config))
|
2012-12-03 22:43:18 +08:00
|
|
|
>>> sorted(config.items("settings"))
|
2018-10-02 11:12:33 +08:00
|
|
|
[(u'am_hero', u'True'), (u'process_tags', u'False')]
|
2012-12-03 22:43:18 +08:00
|
|
|
|
|
|
|
# Check to make sure that settings works with unknown project.
|
|
|
|
>>> config = _ProjectConfigParser("unknown")
|
2016-09-27 23:03:55 +08:00
|
|
|
>>> config.readfp(StringIO(sample_config))
|
2012-12-03 22:43:18 +08:00
|
|
|
>>> sorted(config.items("settings"))
|
2018-10-02 11:12:33 +08:00
|
|
|
[(u'am_hero', u'True')]
|
2012-12-03 22:43:18 +08:00
|
|
|
"""
|
|
|
|
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, {})
|
2016-09-27 23:03:54 +08:00
|
|
|
for setting_name, setting_value in project_defaults.items():
|
2012-12-03 22:43:18 +08:00
|
|
|
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:
|
2018-10-02 11:12:33 +08:00
|
|
|
val = ConfigParser.SafeConfigParser.get(
|
2012-12-03 22:43:18 +08:00
|
|
|
self, "%s_%s" % (self._project_name, section), option,
|
|
|
|
*args, **kwargs
|
|
|
|
)
|
|
|
|
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
|
2018-10-02 11:12:33 +08:00
|
|
|
val = ConfigParser.SafeConfigParser.get(
|
2012-12-03 22:43:18 +08:00
|
|
|
self, section, option, *args, **kwargs
|
|
|
|
)
|
2019-05-15 05:53:50 +08:00
|
|
|
return tools.ToUnicode(val)
|
2012-12-03 22:43:18 +08:00
|
|
|
|
|
|
|
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)
|
2019-05-15 05:53:50 +08:00
|
|
|
return {(tools.ToUnicode(item), tools.ToUnicode(val))
|
2019-05-15 05:53:40 +08:00
|
|
|
for item, val in item_dict.items()}
|
2012-12-03 22:43:18 +08:00
|
|
|
|
2012-01-14 23:12:45 +08:00
|
|
|
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:
|
|
|
|
fd = open(fname, 'r')
|
|
|
|
except IOError:
|
2016-09-27 23:03:50 +08:00
|
|
|
print("Warning: Cannot find alias file '%s'" % fname)
|
2012-01-14 23:12:45 +08:00
|
|
|
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:
|
2016-09-27 23:03:50 +08:00
|
|
|
print("Warning: Alias file line '%s' not understood" % line)
|
2012-01-14 23:12:45 +08:00
|
|
|
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()
|
|
|
|
|
2012-05-23 17:01:06 +08:00
|
|
|
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:
|
2016-09-27 23:03:50 +08:00
|
|
|
print("Couldn't create patman config file\n")
|
2012-05-23 17:01:06 +08:00
|
|
|
raise
|
|
|
|
|
2017-09-13 10:30:28 +08:00
|
|
|
print('''[alias]
|
|
|
|
me: %s <%s>
|
|
|
|
|
|
|
|
[bounces]
|
|
|
|
nxp = Zhikang Zhang <zhikang.zhang@nxp.com>
|
|
|
|
''' % (name, email), file=f)
|
2012-05-23 17:01:06 +08:00
|
|
|
f.close();
|
|
|
|
|
2012-12-03 22:43:17 +08:00
|
|
|
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.
|
2012-12-03 22:43:18 +08:00
|
|
|
config: An instance of _ProjectConfigParser that we will query
|
2012-12-03 22:43:17 +08:00
|
|
|
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:
|
2016-09-27 23:03:50 +08:00
|
|
|
print("WARNING: Unknown setting %s" % name)
|
2012-12-03 22:43:17 +08:00
|
|
|
|
2015-01-30 02:35:17 +08:00
|
|
|
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
|
|
|
|
with open(fname) as fd:
|
|
|
|
linenum = 0
|
|
|
|
for line in fd:
|
|
|
|
linenum += 1
|
|
|
|
line = line.strip()
|
|
|
|
if not line or line.startswith('#'):
|
|
|
|
continue
|
2018-06-27 21:38:58 +08:00
|
|
|
words = line.split(None, 2)
|
2015-01-30 02:35:17 +08:00
|
|
|
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:
|
2016-09-27 23:03:50 +08:00
|
|
|
print(bad_line)
|
2015-01-30 02:35:17 +08:00
|
|
|
|
2017-09-01 16:57:53 +08:00
|
|
|
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())
|
|
|
|
|
2017-09-13 10:30:28 +08:00
|
|
|
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
|
|
|
|
|
2012-12-03 22:43:18 +08:00
|
|
|
def Setup(parser, project_name, config_fname=''):
|
2012-01-14 23:12:45 +08:00
|
|
|
"""Set up the settings module by reading config files.
|
|
|
|
|
|
|
|
Args:
|
2012-12-03 22:43:17 +08:00
|
|
|
parser: The parser to update
|
2012-12-03 22:43:18 +08:00
|
|
|
project_name: Name of project that we're working on; we'll look
|
|
|
|
for sections named "project_section" as well.
|
2012-01-14 23:12:45 +08:00
|
|
|
config_fname: Config filename to read ('' for default)
|
|
|
|
"""
|
2015-01-30 02:35:17 +08:00
|
|
|
# First read the git alias file if available
|
|
|
|
_ReadAliasFile('doc/git-mailrc')
|
2012-12-03 22:43:18 +08:00
|
|
|
config = _ProjectConfigParser(project_name)
|
2012-01-14 23:12:45 +08:00
|
|
|
if config_fname == '':
|
2012-05-23 16:58:58 +08:00
|
|
|
config_fname = '%s/.patman' % os.getenv('HOME')
|
2012-05-23 17:01:06 +08:00
|
|
|
|
|
|
|
if not os.path.exists(config_fname):
|
2016-09-27 23:03:50 +08:00
|
|
|
print("No config file found ~/.patman\nCreating one...\n")
|
2012-05-23 17:01:06 +08:00
|
|
|
CreatePatmanConfigFile(config_fname)
|
|
|
|
|
2012-12-03 22:43:17 +08:00
|
|
|
config.read(config_fname)
|
2012-01-14 23:12:45 +08:00
|
|
|
|
2017-09-13 10:30:28 +08:00
|
|
|
for name, value in GetItems(config, 'alias'):
|
2012-01-14 23:12:45 +08:00
|
|
|
alias[name] = value.split(',')
|
|
|
|
|
2017-09-01 16:57:53 +08:00
|
|
|
_ReadBouncesFile('doc/bounces')
|
2017-09-13 10:30:28 +08:00
|
|
|
for name, value in GetItems(config, 'bounces'):
|
2017-09-01 16:57:53 +08:00
|
|
|
bounces.add(value)
|
|
|
|
|
2012-12-03 22:43:17 +08:00
|
|
|
_UpdateDefaults(parser, config)
|
2012-01-14 23:12:45 +08:00
|
|
|
|
|
|
|
# These are the aliases we understand, indexed by alias. Each member is a list.
|
|
|
|
alias = {}
|
2017-09-01 16:57:53 +08:00
|
|
|
bounces = set()
|
2012-12-03 22:43:18 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import doctest
|
|
|
|
|
|
|
|
doctest.testmod()
|