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