Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 1 | # Copyright (c) 2011 The Chromium OS Authors. |
| 2 | # |
| 3 | # See file CREDITS for list of people who contributed to this |
| 4 | # project. |
| 5 | # |
| 6 | # This program is free software; you can redistribute it and/or |
| 7 | # modify it under the terms of the GNU General Public License as |
| 8 | # published by the Free Software Foundation; either version 2 of |
| 9 | # the License, or (at your option) any later version. |
| 10 | # |
| 11 | # This program is distributed in the hope that it will be useful, |
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | # GNU General Public License for more details. |
| 15 | # |
| 16 | # You should have received a copy of the GNU General Public License |
| 17 | # along with this program; if not, write to the Free Software |
| 18 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 19 | # MA 02111-1307 USA |
| 20 | # |
| 21 | |
| 22 | import ConfigParser |
| 23 | import os |
| 24 | import re |
| 25 | |
| 26 | import command |
Vikram Narayanan | 87d6555 | 2012-05-23 09:01:06 +0000 | [diff] [blame] | 27 | import gitutil |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 28 | |
| 29 | def ReadGitAliases(fname): |
| 30 | """Read a git alias file. This is in the form used by git: |
| 31 | |
| 32 | alias uboot u-boot@lists.denx.de |
| 33 | alias wd Wolfgang Denk <wd@denx.de> |
| 34 | |
| 35 | Args: |
| 36 | fname: Filename to read |
| 37 | """ |
| 38 | try: |
| 39 | fd = open(fname, 'r') |
| 40 | except IOError: |
| 41 | print "Warning: Cannot find alias file '%s'" % fname |
| 42 | return |
| 43 | |
| 44 | re_line = re.compile('alias\s+(\S+)\s+(.*)') |
| 45 | for line in fd.readlines(): |
| 46 | line = line.strip() |
| 47 | if not line or line[0] == '#': |
| 48 | continue |
| 49 | |
| 50 | m = re_line.match(line) |
| 51 | if not m: |
| 52 | print "Warning: Alias file line '%s' not understood" % line |
| 53 | continue |
| 54 | |
| 55 | list = alias.get(m.group(1), []) |
| 56 | for item in m.group(2).split(','): |
| 57 | item = item.strip() |
| 58 | if item: |
| 59 | list.append(item) |
| 60 | alias[m.group(1)] = list |
| 61 | |
| 62 | fd.close() |
| 63 | |
Vikram Narayanan | 87d6555 | 2012-05-23 09:01:06 +0000 | [diff] [blame] | 64 | def CreatePatmanConfigFile(config_fname): |
| 65 | """Creates a config file under $(HOME)/.patman if it can't find one. |
| 66 | |
| 67 | Args: |
| 68 | config_fname: Default config filename i.e., $(HOME)/.patman |
| 69 | |
| 70 | Returns: |
| 71 | None |
| 72 | """ |
| 73 | name = gitutil.GetDefaultUserName() |
| 74 | if name == None: |
| 75 | name = raw_input("Enter name: ") |
| 76 | |
| 77 | email = gitutil.GetDefaultUserEmail() |
| 78 | |
| 79 | if email == None: |
| 80 | email = raw_input("Enter email: ") |
| 81 | |
| 82 | try: |
| 83 | f = open(config_fname, 'w') |
| 84 | except IOError: |
| 85 | print "Couldn't create patman config file\n" |
| 86 | raise |
| 87 | |
| 88 | print >>f, "[alias]\nme: %s <%s>" % (name, email) |
| 89 | f.close(); |
| 90 | |
Doug Anderson | 8568bae | 2012-12-03 14:43:17 +0000 | [diff] [blame^] | 91 | def _UpdateDefaults(parser, config): |
| 92 | """Update the given OptionParser defaults based on config. |
| 93 | |
| 94 | We'll walk through all of the settings from the parser |
| 95 | For each setting we'll look for a default in the option parser. |
| 96 | If it's found we'll update the option parser default. |
| 97 | |
| 98 | The idea here is that the .patman file should be able to update |
| 99 | defaults but that command line flags should still have the final |
| 100 | say. |
| 101 | |
| 102 | Args: |
| 103 | parser: An instance of an OptionParser whose defaults will be |
| 104 | updated. |
| 105 | config: An instance of SafeConfigParser that we will query |
| 106 | for settings. |
| 107 | """ |
| 108 | defaults = parser.get_default_values() |
| 109 | for name, val in config.items('settings'): |
| 110 | if hasattr(defaults, name): |
| 111 | default_val = getattr(defaults, name) |
| 112 | if isinstance(default_val, bool): |
| 113 | val = config.getboolean('settings', name) |
| 114 | elif isinstance(default_val, int): |
| 115 | val = config.getint('settings', name) |
| 116 | parser.set_default(name, val) |
| 117 | else: |
| 118 | print "WARNING: Unknown setting %s" % name |
| 119 | |
| 120 | def Setup(parser, config_fname=''): |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 121 | """Set up the settings module by reading config files. |
| 122 | |
| 123 | Args: |
Doug Anderson | 8568bae | 2012-12-03 14:43:17 +0000 | [diff] [blame^] | 124 | parser: The parser to update |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 125 | config_fname: Config filename to read ('' for default) |
| 126 | """ |
Doug Anderson | 8568bae | 2012-12-03 14:43:17 +0000 | [diff] [blame^] | 127 | config = ConfigParser.SafeConfigParser() |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 128 | if config_fname == '': |
Vikram Narayanan | 2b36c75 | 2012-05-23 08:58:58 +0000 | [diff] [blame] | 129 | config_fname = '%s/.patman' % os.getenv('HOME') |
Vikram Narayanan | 87d6555 | 2012-05-23 09:01:06 +0000 | [diff] [blame] | 130 | |
| 131 | if not os.path.exists(config_fname): |
| 132 | print "No config file found ~/.patman\nCreating one...\n" |
| 133 | CreatePatmanConfigFile(config_fname) |
| 134 | |
Doug Anderson | 8568bae | 2012-12-03 14:43:17 +0000 | [diff] [blame^] | 135 | config.read(config_fname) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 136 | |
Doug Anderson | 8568bae | 2012-12-03 14:43:17 +0000 | [diff] [blame^] | 137 | for name, value in config.items('alias'): |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 138 | alias[name] = value.split(',') |
| 139 | |
Doug Anderson | 8568bae | 2012-12-03 14:43:17 +0000 | [diff] [blame^] | 140 | _UpdateDefaults(parser, config) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 141 | |
| 142 | # These are the aliases we understand, indexed by alias. Each member is a list. |
| 143 | alias = {} |