10d24de9dSSimon Glass# Copyright (c) 2011 The Chromium OS Authors. 20d24de9dSSimon Glass# 30d24de9dSSimon Glass# See file CREDITS for list of people who contributed to this 40d24de9dSSimon Glass# project. 50d24de9dSSimon Glass# 60d24de9dSSimon Glass# This program is free software; you can redistribute it and/or 70d24de9dSSimon Glass# modify it under the terms of the GNU General Public License as 80d24de9dSSimon Glass# published by the Free Software Foundation; either version 2 of 90d24de9dSSimon Glass# the License, or (at your option) any later version. 100d24de9dSSimon Glass# 110d24de9dSSimon Glass# This program is distributed in the hope that it will be useful, 120d24de9dSSimon Glass# but WITHOUT ANY WARRANTY; without even the implied warranty of 130d24de9dSSimon Glass# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 140d24de9dSSimon Glass# GNU General Public License for more details. 150d24de9dSSimon Glass# 160d24de9dSSimon Glass# You should have received a copy of the GNU General Public License 170d24de9dSSimon Glass# along with this program; if not, write to the Free Software 180d24de9dSSimon Glass# Foundation, Inc., 59 Temple Place, Suite 330, Boston, 190d24de9dSSimon Glass# MA 02111-1307 USA 200d24de9dSSimon Glass# 210d24de9dSSimon Glass 220d24de9dSSimon Glassimport ConfigParser 230d24de9dSSimon Glassimport os 240d24de9dSSimon Glassimport re 250d24de9dSSimon Glass 260d24de9dSSimon Glassimport command 2787d65558SVikram Narayananimport gitutil 280d24de9dSSimon Glass 290d24de9dSSimon Glassdef ReadGitAliases(fname): 300d24de9dSSimon Glass """Read a git alias file. This is in the form used by git: 310d24de9dSSimon Glass 320d24de9dSSimon Glass alias uboot u-boot@lists.denx.de 330d24de9dSSimon Glass alias wd Wolfgang Denk <wd@denx.de> 340d24de9dSSimon Glass 350d24de9dSSimon Glass Args: 360d24de9dSSimon Glass fname: Filename to read 370d24de9dSSimon Glass """ 380d24de9dSSimon Glass try: 390d24de9dSSimon Glass fd = open(fname, 'r') 400d24de9dSSimon Glass except IOError: 410d24de9dSSimon Glass print "Warning: Cannot find alias file '%s'" % fname 420d24de9dSSimon Glass return 430d24de9dSSimon Glass 440d24de9dSSimon Glass re_line = re.compile('alias\s+(\S+)\s+(.*)') 450d24de9dSSimon Glass for line in fd.readlines(): 460d24de9dSSimon Glass line = line.strip() 470d24de9dSSimon Glass if not line or line[0] == '#': 480d24de9dSSimon Glass continue 490d24de9dSSimon Glass 500d24de9dSSimon Glass m = re_line.match(line) 510d24de9dSSimon Glass if not m: 520d24de9dSSimon Glass print "Warning: Alias file line '%s' not understood" % line 530d24de9dSSimon Glass continue 540d24de9dSSimon Glass 550d24de9dSSimon Glass list = alias.get(m.group(1), []) 560d24de9dSSimon Glass for item in m.group(2).split(','): 570d24de9dSSimon Glass item = item.strip() 580d24de9dSSimon Glass if item: 590d24de9dSSimon Glass list.append(item) 600d24de9dSSimon Glass alias[m.group(1)] = list 610d24de9dSSimon Glass 620d24de9dSSimon Glass fd.close() 630d24de9dSSimon Glass 6487d65558SVikram Narayanandef CreatePatmanConfigFile(config_fname): 6587d65558SVikram Narayanan """Creates a config file under $(HOME)/.patman if it can't find one. 6687d65558SVikram Narayanan 6787d65558SVikram Narayanan Args: 6887d65558SVikram Narayanan config_fname: Default config filename i.e., $(HOME)/.patman 6987d65558SVikram Narayanan 7087d65558SVikram Narayanan Returns: 7187d65558SVikram Narayanan None 7287d65558SVikram Narayanan """ 7387d65558SVikram Narayanan name = gitutil.GetDefaultUserName() 7487d65558SVikram Narayanan if name == None: 7587d65558SVikram Narayanan name = raw_input("Enter name: ") 7687d65558SVikram Narayanan 7787d65558SVikram Narayanan email = gitutil.GetDefaultUserEmail() 7887d65558SVikram Narayanan 7987d65558SVikram Narayanan if email == None: 8087d65558SVikram Narayanan email = raw_input("Enter email: ") 8187d65558SVikram Narayanan 8287d65558SVikram Narayanan try: 8387d65558SVikram Narayanan f = open(config_fname, 'w') 8487d65558SVikram Narayanan except IOError: 8587d65558SVikram Narayanan print "Couldn't create patman config file\n" 8687d65558SVikram Narayanan raise 8787d65558SVikram Narayanan 8887d65558SVikram Narayanan print >>f, "[alias]\nme: %s <%s>" % (name, email) 8987d65558SVikram Narayanan f.close(); 9087d65558SVikram Narayanan 91*8568baedSDoug Andersondef _UpdateDefaults(parser, config): 92*8568baedSDoug Anderson """Update the given OptionParser defaults based on config. 93*8568baedSDoug Anderson 94*8568baedSDoug Anderson We'll walk through all of the settings from the parser 95*8568baedSDoug Anderson For each setting we'll look for a default in the option parser. 96*8568baedSDoug Anderson If it's found we'll update the option parser default. 97*8568baedSDoug Anderson 98*8568baedSDoug Anderson The idea here is that the .patman file should be able to update 99*8568baedSDoug Anderson defaults but that command line flags should still have the final 100*8568baedSDoug Anderson say. 101*8568baedSDoug Anderson 102*8568baedSDoug Anderson Args: 103*8568baedSDoug Anderson parser: An instance of an OptionParser whose defaults will be 104*8568baedSDoug Anderson updated. 105*8568baedSDoug Anderson config: An instance of SafeConfigParser that we will query 106*8568baedSDoug Anderson for settings. 107*8568baedSDoug Anderson """ 108*8568baedSDoug Anderson defaults = parser.get_default_values() 109*8568baedSDoug Anderson for name, val in config.items('settings'): 110*8568baedSDoug Anderson if hasattr(defaults, name): 111*8568baedSDoug Anderson default_val = getattr(defaults, name) 112*8568baedSDoug Anderson if isinstance(default_val, bool): 113*8568baedSDoug Anderson val = config.getboolean('settings', name) 114*8568baedSDoug Anderson elif isinstance(default_val, int): 115*8568baedSDoug Anderson val = config.getint('settings', name) 116*8568baedSDoug Anderson parser.set_default(name, val) 117*8568baedSDoug Anderson else: 118*8568baedSDoug Anderson print "WARNING: Unknown setting %s" % name 119*8568baedSDoug Anderson 120*8568baedSDoug Andersondef Setup(parser, config_fname=''): 1210d24de9dSSimon Glass """Set up the settings module by reading config files. 1220d24de9dSSimon Glass 1230d24de9dSSimon Glass Args: 124*8568baedSDoug Anderson parser: The parser to update 1250d24de9dSSimon Glass config_fname: Config filename to read ('' for default) 1260d24de9dSSimon Glass """ 127*8568baedSDoug Anderson config = ConfigParser.SafeConfigParser() 1280d24de9dSSimon Glass if config_fname == '': 1292b36c75dSVikram Narayanan config_fname = '%s/.patman' % os.getenv('HOME') 13087d65558SVikram Narayanan 13187d65558SVikram Narayanan if not os.path.exists(config_fname): 13287d65558SVikram Narayanan print "No config file found ~/.patman\nCreating one...\n" 13387d65558SVikram Narayanan CreatePatmanConfigFile(config_fname) 13487d65558SVikram Narayanan 135*8568baedSDoug Anderson config.read(config_fname) 1360d24de9dSSimon Glass 137*8568baedSDoug Anderson for name, value in config.items('alias'): 1380d24de9dSSimon Glass alias[name] = value.split(',') 1390d24de9dSSimon Glass 140*8568baedSDoug Anderson _UpdateDefaults(parser, config) 1410d24de9dSSimon Glass 1420d24de9dSSimon Glass# These are the aliases we understand, indexed by alias. Each member is a list. 1430d24de9dSSimon Glassalias = {} 144