settings.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. import ConfigParser
  22. import os
  23. import re
  24. import command
  25. import gitutil
  26. def ReadGitAliases(fname):
  27. """Read a git alias file. This is in the form used by git:
  28. alias uboot u-boot@lists.denx.de
  29. alias wd Wolfgang Denk <wd@denx.de>
  30. Args:
  31. fname: Filename to read
  32. """
  33. try:
  34. fd = open(fname, 'r')
  35. except IOError:
  36. print "Warning: Cannot find alias file '%s'" % fname
  37. return
  38. re_line = re.compile('alias\s+(\S+)\s+(.*)')
  39. for line in fd.readlines():
  40. line = line.strip()
  41. if not line or line[0] == '#':
  42. continue
  43. m = re_line.match(line)
  44. if not m:
  45. print "Warning: Alias file line '%s' not understood" % line
  46. continue
  47. list = alias.get(m.group(1), [])
  48. for item in m.group(2).split(','):
  49. item = item.strip()
  50. if item:
  51. list.append(item)
  52. alias[m.group(1)] = list
  53. fd.close()
  54. def CreatePatmanConfigFile(config_fname):
  55. """Creates a config file under $(HOME)/.patman if it can't find one.
  56. Args:
  57. config_fname: Default config filename i.e., $(HOME)/.patman
  58. Returns:
  59. None
  60. """
  61. name = gitutil.GetDefaultUserName()
  62. if name == None:
  63. name = raw_input("Enter name: ")
  64. email = gitutil.GetDefaultUserEmail()
  65. if email == None:
  66. email = raw_input("Enter email: ")
  67. try:
  68. f = open(config_fname, 'w')
  69. except IOError:
  70. print "Couldn't create patman config file\n"
  71. raise
  72. print >>f, "[alias]\nme: %s <%s>" % (name, email)
  73. f.close();
  74. def Setup(config_fname=''):
  75. """Set up the settings module by reading config files.
  76. Args:
  77. config_fname: Config filename to read ('' for default)
  78. """
  79. settings = ConfigParser.SafeConfigParser()
  80. if config_fname == '':
  81. config_fname = '%s/.patman' % os.getenv('HOME')
  82. if not os.path.exists(config_fname):
  83. print "No config file found ~/.patman\nCreating one...\n"
  84. CreatePatmanConfigFile(config_fname)
  85. settings.read(config_fname)
  86. for name, value in settings.items('alias'):
  87. alias[name] = value.split(',')
  88. # These are the aliases we understand, indexed by alias. Each member is a list.
  89. alias = {}