3eabc8c3a3abef4be9586d933ff573efe0dd7e17
[stgit] / stgit / config.py
1 """Handles the Stacked GIT configuration files
2 """
3
4 __copyright__ = """
5 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation.
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, MA 02111-1307 USA
19 """
20
21 import os, re
22 from stgit import basedir
23 from stgit.exception import *
24 from stgit.run import *
25
26 class GitConfigException(StgException):
27 pass
28
29 class GitConfig:
30 __defaults={
31 'stgit.autoresolved': 'no',
32 'stgit.smtpserver': 'localhost:25',
33 'stgit.smtpdelay': '5',
34 'stgit.pullcmd': 'git-pull',
35 'stgit.fetchcmd': 'git-fetch',
36 'stgit.pull-policy': 'pull',
37 'stgit.merger': 'diff3 -L current -L ancestor -L patched -m -E ' \
38 '"%(branch1)s" "%(ancestor)s" "%(branch2)s" > "%(output)s"',
39 'stgit.autoimerge': 'no',
40 'stgit.keeporig': 'yes',
41 'stgit.keepoptimized': 'no',
42 'stgit.extensions': '.ancestor .current .patched',
43 'stgit.shortnr': '5'
44 }
45
46 __cache={}
47
48 def get(self, name):
49 if self.__cache.has_key(name):
50 return self.__cache[name]
51 try:
52 value = Run('git-repo-config', '--get', name).output_one_line()
53 except RunException:
54 value = self.__defaults.get(name, None)
55 self.__cache[name] = value
56 return value
57
58 def getall(self, name):
59 if self.__cache.has_key(name):
60 return self.__cache[name]
61 values = Run('git-repo-config', '--get-all', name
62 ).returns([0, 1]).output_lines()
63 self.__cache[name] = values
64 return values
65
66 def getint(self, name):
67 value = self.get(name)
68 if value.isdigit():
69 return int(value)
70 else:
71 raise GitConfigException, 'Value for "%s" is not an integer: "%s"' % (name, value)
72
73 def rename_section(self, from_name, to_name):
74 """Rename a section in the config file. Silently do nothing if
75 the section doesn't exist."""
76 Run('git-repo-config', '--rename-section', from_name, to_name
77 ).returns([0, 1]).run()
78 self.__cache.clear()
79
80 def remove_section(self, name):
81 """Remove a section in the config file. Silently do nothing if
82 the section doesn't exist."""
83 Run('git-repo-config', '--remove-section', name
84 ).returns([0, 1]).discard_stderr().discard_output()
85 self.__cache.clear()
86
87 def set(self, name, value):
88 Run('git-repo-config', name, value).run()
89 self.__cache[name] = value
90
91 def unset(self, name):
92 Run('git-repo-config', '--unset', name)
93 self.__cache[name] = None
94
95 def sections_matching(self, regexp):
96 """Takes a regexp with a single group, matches it against all
97 config variables, and returns a list whose members are the
98 group contents, for all variable names matching the regexp.
99 """
100 result = []
101 for line in Run('git-repo-config', '--get-regexp', '"^%s$"' % regexp
102 ).returns([0, 1]).output_lines():
103 m = re.match('^%s ' % regexp, line)
104 if m:
105 result.append(m.group(1))
106 return result
107
108 config=GitConfig()
109
110 def config_setup():
111 global config
112
113 # Set the PAGER environment to the config value (if any)
114 pager = config.get('stgit.pager')
115 if pager:
116 os.environ['PAGER'] = pager
117 # FIXME: handle EDITOR the same way ?
118
119 class ConfigOption:
120 """Delayed cached reading of a configuration option.
121 """
122 def __init__(self, section, option):
123 self.__section = section
124 self.__option = option
125 self.__value = None
126
127 def __str__(self):
128 if not self.__value:
129 self.__value = config.get(self.__section + '.' + self.__option)
130 return self.__value
131
132
133 # cached extensions
134 __extensions = None
135
136 def file_extensions():
137 """Returns a dictionary with the conflict file extensions
138 """
139 global __extensions
140
141 if not __extensions:
142 cfg_ext = config.get('stgit.extensions').split()
143 if len(cfg_ext) != 3:
144 raise CmdException, '"extensions" configuration error'
145
146 __extensions = { 'ancestor': cfg_ext[0],
147 'current': cfg_ext[1],
148 'patched': cfg_ext[2] }
149
150 return __extensions