1442.1.1
by Robert Collins
move config_dir into bzrlib.config |
1 |
# Copyright (C) 2005 by Canonical Ltd
|
2 |
# Authors: Robert Collins <robert.collins@canonical.com>
|
|
3 |
#
|
|
4 |
# This program is free software; you can redistribute it and/or modify
|
|
5 |
# it under the terms of the GNU General Public License as published by
|
|
6 |
# the Free Software Foundation; either version 2 of the License, or
|
|
7 |
# (at your option) any later version.
|
|
8 |
#
|
|
9 |
# This program is distributed in the hope that it will be useful,
|
|
10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
# GNU General Public License for more details.
|
|
13 |
#
|
|
14 |
# You should have received a copy of the GNU General Public License
|
|
15 |
# along with this program; if not, write to the Free Software
|
|
16 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
17 |
||
1442.1.20
by Robert Collins
add some documentation on options |
18 |
"""Configuration that affects the behaviour of Bazaar.
|
19 |
||
20 |
Currently this configuration resides in ~/.bazaar/bazaar.conf
|
|
21 |
and ~/.bazaar/branches.conf, which is written to by bzr.
|
|
22 |
||
23 |
In bazaar.config the following options may be set:
|
|
24 |
[DEFAULT]
|
|
25 |
editor=name-of-program
|
|
26 |
email=Your Name <your@email.address>
|
|
27 |
check_signatures=require|ignore|check-available(default)
|
|
28 |
create_signatures=always|never|when-required(default)
|
|
29 |
||
30 |
in branches.conf, you specify the url of a branch and options for it.
|
|
31 |
Wildcards may be used - * and ? as normal in shell completion. Options
|
|
32 |
set in both bazaar.conf and branches.conf are overriden by the branches.conf
|
|
33 |
setting.
|
|
34 |
[/home/robertc/source]
|
|
35 |
recurse=False|True(default)
|
|
36 |
email= as above
|
|
37 |
check_signatures= as abive
|
|
38 |
create_signatures= as above.
|
|
39 |
||
40 |
explanation of options
|
|
41 |
----------------------
|
|
42 |
editor - this option sets the pop up editor to use during commits.
|
|
43 |
email - this option sets the user id bzr will use when committing.
|
|
44 |
check_signatures - this option controls whether bzr will require good gpg
|
|
45 |
signatures, ignore them, or check them if they are
|
|
46 |
present.
|
|
47 |
create_signatures - this option controls whether bzr will always create
|
|
48 |
gpg signatures, never create them, or create them if the
|
|
49 |
branch is configured to require them.
|
|
1442.1.21
by Robert Collins
create signature_needed() call for commit to trigger creating signatures |
50 |
NB: This option is planned, but not implemented yet.
|
1442.1.20
by Robert Collins
add some documentation on options |
51 |
"""
|
1442.1.1
by Robert Collins
move config_dir into bzrlib.config |
52 |
|
1442.1.2
by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing. |
53 |
from ConfigParser import ConfigParser |
1442.1.1
by Robert Collins
move config_dir into bzrlib.config |
54 |
import os |
1442.1.12
by Robert Collins
LocationConfig section retrieval falls into my lap |
55 |
from fnmatch import fnmatch |
1442.1.2
by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing. |
56 |
import errno |
57 |
import re |
|
58 |
||
59 |
import bzrlib |
|
1442.1.6
by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs |
60 |
import bzrlib.errors as errors |
61 |
||
62 |
||
1442.1.14
by Robert Collins
Create a default signature checking policy of CHECK_IF_POSSIBLE |
63 |
CHECK_IF_POSSIBLE=0 |
64 |
CHECK_ALWAYS=1 |
|
65 |
CHECK_NEVER=2 |
|
66 |
||
67 |
||
1442.1.6
by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs |
68 |
class Config(object): |
69 |
"""A configuration policy - what username, editor, gpg needs etc."""
|
|
70 |
||
71 |
def get_editor(self): |
|
72 |
"""Get the users pop up editor."""
|
|
73 |
raise NotImplementedError |
|
74 |
||
1442.1.15
by Robert Collins
make getting the signature checking policy a template method |
75 |
def _get_signature_checking(self): |
76 |
"""Template method to override signature checking policy."""
|
|
77 |
||
1442.1.6
by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs |
78 |
def __init__(self): |
79 |
super(Config, self).__init__() |
|
80 |
||
81 |
def user_email(self): |
|
82 |
"""Return just the email component of a username."""
|
|
83 |
e = self.username() |
|
84 |
m = re.search(r'[\w+.-]+@[\w+.-]+', e) |
|
85 |
if not m: |
|
86 |
raise BzrError("%r doesn't seem to contain " |
|
87 |
"a reasonable email address" % e) |
|
88 |
return m.group(0) |
|
89 |
||
90 |
def username(self): |
|
91 |
"""Return email-style username.
|
|
92 |
|
|
93 |
Something similar to 'Martin Pool <mbp@sourcefrog.net>'
|
|
94 |
|
|
95 |
$BZREMAIL can be set to override this, then
|
|
96 |
the concrete policy type is checked, and finally
|
|
97 |
$EMAIL is examinged.
|
|
98 |
but if none is found, a reasonable default is (hopefully)
|
|
99 |
created.
|
|
100 |
|
|
101 |
TODO: Check it's reasonably well-formed.
|
|
102 |
"""
|
|
103 |
v = os.environ.get('BZREMAIL') |
|
104 |
if v: |
|
105 |
return v.decode(bzrlib.user_encoding) |
|
106 |
||
107 |
v = self._get_user_id() |
|
108 |
if v: |
|
109 |
return v |
|
110 |
||
111 |
v = os.environ.get('EMAIL') |
|
112 |
if v: |
|
113 |
return v.decode(bzrlib.user_encoding) |
|
114 |
||
115 |
name, email = _auto_user_id() |
|
116 |
if name: |
|
117 |
return '%s <%s>' % (name, email) |
|
118 |
else: |
|
119 |
return email |
|
120 |
||
1442.1.14
by Robert Collins
Create a default signature checking policy of CHECK_IF_POSSIBLE |
121 |
def signature_checking(self): |
122 |
"""What is the current policy for signature checking?."""
|
|
1442.1.15
by Robert Collins
make getting the signature checking policy a template method |
123 |
policy = self._get_signature_checking() |
124 |
if policy is not None: |
|
125 |
return policy |
|
1442.1.14
by Robert Collins
Create a default signature checking policy of CHECK_IF_POSSIBLE |
126 |
return CHECK_IF_POSSIBLE |
1442.1.6
by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs |
127 |
|
1442.1.21
by Robert Collins
create signature_needed() call for commit to trigger creating signatures |
128 |
def signature_needed(self): |
129 |
"""Is a signature needed when committing ?."""
|
|
130 |
policy = self._get_signature_checking() |
|
131 |
if policy == CHECK_ALWAYS: |
|
132 |
return True |
|
133 |
return False |
|
134 |
||
1442.1.15
by Robert Collins
make getting the signature checking policy a template method |
135 |
|
1442.1.18
by Robert Collins
permit per branch location overriding of signature checking policy |
136 |
class IniBasedConfig(Config): |
137 |
"""A configuration policy that draws from ini files."""
|
|
1442.1.6
by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs |
138 |
|
1442.1.18
by Robert Collins
permit per branch location overriding of signature checking policy |
139 |
def _get_parser(self, file=None): |
140 |
if self._parser is not None: |
|
141 |
return self._parser |
|
1442.1.6
by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs |
142 |
parser = ConfigParser() |
143 |
if file is not None: |
|
144 |
parser.readfp(file) |
|
145 |
else: |
|
1442.1.18
by Robert Collins
permit per branch location overriding of signature checking policy |
146 |
parser.read([self._get_filename()]) |
147 |
self._parser = parser |
|
1442.1.6
by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs |
148 |
return parser |
149 |
||
1442.1.18
by Robert Collins
permit per branch location overriding of signature checking policy |
150 |
def _get_section(self): |
151 |
"""Override this to define the section used by the config."""
|
|
152 |
return "DEFAULT" |
|
153 |
||
1442.1.16
by Robert Collins
allow global overriding of signature policy to never check |
154 |
def _get_signature_checking(self): |
155 |
"""See Config._get_signature_checking."""
|
|
1442.1.18
by Robert Collins
permit per branch location overriding of signature checking policy |
156 |
section = self._get_section() |
157 |
if section is None: |
|
158 |
return None |
|
1442.1.21
by Robert Collins
create signature_needed() call for commit to trigger creating signatures |
159 |
if self._get_parser().has_option(section, 'check_signatures'): |
1442.1.16
by Robert Collins
allow global overriding of signature policy to never check |
160 |
return self._string_to_signature_policy( |
1442.1.21
by Robert Collins
create signature_needed() call for commit to trigger creating signatures |
161 |
self._get_parser().get(section, 'check_signatures')) |
1442.1.18
by Robert Collins
permit per branch location overriding of signature checking policy |
162 |
|
163 |
def _get_user_id(self): |
|
164 |
"""Get the user id from the 'email' key in the current section."""
|
|
165 |
section = self._get_section() |
|
166 |
if section is not None: |
|
167 |
if self._get_parser().has_option(section, 'email'): |
|
168 |
return self._get_parser().get(section, 'email') |
|
169 |
||
170 |
def __init__(self, get_filename): |
|
171 |
super(IniBasedConfig, self).__init__() |
|
172 |
self._get_filename = get_filename |
|
1442.1.6
by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs |
173 |
self._parser = None |
174 |
||
1442.1.16
by Robert Collins
allow global overriding of signature policy to never check |
175 |
def _string_to_signature_policy(self, signature_string): |
176 |
"""Convert a string to a signing policy."""
|
|
1442.1.17
by Robert Collins
allow global overriding of signature policy to force checking, or (pointless but allowed) to set auto checking |
177 |
if signature_string.lower() == 'check-available': |
178 |
return CHECK_IF_POSSIBLE |
|
1442.1.16
by Robert Collins
allow global overriding of signature policy to never check |
179 |
if signature_string.lower() == 'ignore': |
180 |
return CHECK_NEVER |
|
1442.1.17
by Robert Collins
allow global overriding of signature policy to force checking, or (pointless but allowed) to set auto checking |
181 |
if signature_string.lower() == 'require': |
182 |
return CHECK_ALWAYS |
|
1442.1.16
by Robert Collins
allow global overriding of signature policy to never check |
183 |
raise errors.BzrError("Invalid signatures policy '%s'" |
184 |
% signature_string) |
|
1442.1.6
by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs |
185 |
|
1442.1.18
by Robert Collins
permit per branch location overriding of signature checking policy |
186 |
|
187 |
class GlobalConfig(IniBasedConfig): |
|
188 |
"""The configuration that should be used for a specific location."""
|
|
189 |
||
190 |
def get_editor(self): |
|
191 |
if self._get_parser().has_option(self._get_section(), 'editor'): |
|
192 |
return self._get_parser().get(self._get_section(), 'editor') |
|
193 |
||
194 |
def __init__(self): |
|
195 |
super(GlobalConfig, self).__init__(config_filename) |
|
196 |
||
197 |
||
198 |
class LocationConfig(IniBasedConfig): |
|
1442.1.6
by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs |
199 |
"""A configuration object that gives the policy for a location."""
|
200 |
||
201 |
def __init__(self, location): |
|
1442.1.18
by Robert Collins
permit per branch location overriding of signature checking policy |
202 |
super(LocationConfig, self).__init__(branches_config_filename) |
1442.1.6
by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs |
203 |
self._global_config = None |
204 |
self.location = location |
|
205 |
||
206 |
def _get_global_config(self): |
|
207 |
if self._global_config is None: |
|
208 |
self._global_config = GlobalConfig() |
|
209 |
return self._global_config |
|
210 |
||
1442.1.9
by Robert Collins
exact section test passes |
211 |
def _get_section(self): |
212 |
"""Get the section we should look in for config items.
|
|
213 |
||
214 |
Returns None if none exists.
|
|
215 |
TODO: perhaps return a NullSection that thunks through to the
|
|
216 |
global config.
|
|
217 |
"""
|
|
1442.1.18
by Robert Collins
permit per branch location overriding of signature checking policy |
218 |
sections = self._get_parser().sections() |
1442.1.12
by Robert Collins
LocationConfig section retrieval falls into my lap |
219 |
location_names = self.location.split('/') |
1442.1.18
by Robert Collins
permit per branch location overriding of signature checking policy |
220 |
if self.location.endswith('/'): |
221 |
del location_names[-1] |
|
1442.1.12
by Robert Collins
LocationConfig section retrieval falls into my lap |
222 |
matches=[] |
1442.1.10
by Robert Collins
explicit over glob test passes |
223 |
for section in sections: |
1442.1.12
by Robert Collins
LocationConfig section retrieval falls into my lap |
224 |
section_names = section.split('/') |
225 |
if section.endswith('/'): |
|
226 |
del section_names[-1] |
|
227 |
names = zip(location_names, section_names) |
|
228 |
matched = True |
|
229 |
for name in names: |
|
230 |
if not fnmatch(name[0], name[1]): |
|
231 |
matched = False |
|
232 |
break
|
|
233 |
if not matched: |
|
234 |
continue
|
|
235 |
# so, for the common prefix they matched.
|
|
236 |
# if section is longer, no match.
|
|
237 |
if len(section_names) > len(location_names): |
|
238 |
continue
|
|
239 |
# if path is longer, and recurse is not true, no match
|
|
240 |
if len(section_names) < len(location_names): |
|
1442.1.18
by Robert Collins
permit per branch location overriding of signature checking policy |
241 |
if (self._get_parser().has_option(section, 'recurse') |
242 |
and not self._get_parser().getboolean(section, 'recurse')): |
|
1442.1.12
by Robert Collins
LocationConfig section retrieval falls into my lap |
243 |
continue
|
244 |
matches.append((len(section_names), section)) |
|
245 |
if not len(matches): |
|
246 |
return None |
|
247 |
matches.sort(reverse=True) |
|
248 |
return matches[0][1] |
|
1442.1.9
by Robert Collins
exact section test passes |
249 |
|
1442.1.6
by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs |
250 |
def _get_user_id(self): |
1442.1.18
by Robert Collins
permit per branch location overriding of signature checking policy |
251 |
user_id = super(LocationConfig, self)._get_user_id() |
252 |
if user_id is not None: |
|
253 |
return user_id |
|
1442.1.6
by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs |
254 |
return self._get_global_config()._get_user_id() |
255 |
||
1442.1.18
by Robert Collins
permit per branch location overriding of signature checking policy |
256 |
def _get_signature_checking(self): |
257 |
"""See Config._get_signature_checking."""
|
|
258 |
check = super(LocationConfig, self)._get_signature_checking() |
|
259 |
if check is not None: |
|
260 |
return check |
|
261 |
return self._get_global_config()._get_signature_checking() |
|
262 |
||
1442.1.6
by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs |
263 |
|
264 |
class BranchConfig(Config): |
|
265 |
"""A configuration object giving the policy for a branch."""
|
|
266 |
||
267 |
def _get_location_config(self): |
|
268 |
if self._location_config is None: |
|
269 |
self._location_config = LocationConfig(self.branch.base) |
|
270 |
return self._location_config |
|
271 |
||
272 |
def _get_user_id(self): |
|
273 |
"""Return the full user id for the branch.
|
|
274 |
|
|
275 |
e.g. "John Hacker <jhacker@foo.org>"
|
|
276 |
This is looked up in the email controlfile for the branch.
|
|
277 |
"""
|
|
278 |
try: |
|
279 |
return (self.branch.controlfile("email", "r") |
|
280 |
.read() |
|
281 |
.decode(bzrlib.user_encoding) |
|
282 |
.rstrip("\r\n")) |
|
283 |
except errors.NoSuchFile, e: |
|
284 |
pass
|
|
285 |
||
286 |
return self._get_location_config()._get_user_id() |
|
287 |
||
1442.1.19
by Robert Collins
BranchConfigs inherit signature_checking policy from their LocationConfig. |
288 |
def _get_signature_checking(self): |
289 |
"""See Config._get_signature_checking."""
|
|
290 |
return self._get_location_config()._get_signature_checking() |
|
291 |
||
1442.1.6
by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs |
292 |
def __init__(self, branch): |
293 |
super(BranchConfig, self).__init__() |
|
294 |
self._location_config = None |
|
295 |
self.branch = branch |
|
1442.1.2
by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing. |
296 |
|
1442.1.1
by Robert Collins
move config_dir into bzrlib.config |
297 |
|
298 |
def config_dir(): |
|
299 |
"""Return per-user configuration directory.
|
|
300 |
||
1442.1.2
by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing. |
301 |
By default this is ~/.bazaar/
|
1442.1.1
by Robert Collins
move config_dir into bzrlib.config |
302 |
|
303 |
TODO: Global option --config-dir to override this.
|
|
304 |
"""
|
|
1442.1.2
by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing. |
305 |
return os.path.join(os.path.expanduser("~"), ".bazaar") |
306 |
||
307 |
||
308 |
def config_filename(): |
|
309 |
"""Return per-user configuration ini file filename."""
|
|
310 |
return os.path.join(config_dir(), 'bazaar.conf') |
|
311 |
||
312 |
||
1442.1.6
by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs |
313 |
def branches_config_filename(): |
314 |
"""Return per-user configuration ini file filename."""
|
|
315 |
return os.path.join(config_dir(), 'branches.conf') |
|
1442.1.2
by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing. |
316 |
|
317 |
||
318 |
def _auto_user_id(): |
|
319 |
"""Calculate automatic user identification.
|
|
320 |
||
321 |
Returns (realname, email).
|
|
322 |
||
323 |
Only used when none is set in the environment or the id file.
|
|
324 |
||
325 |
This previously used the FQDN as the default domain, but that can
|
|
326 |
be very slow on machines where DNS is broken. So now we simply
|
|
327 |
use the hostname.
|
|
328 |
"""
|
|
329 |
import socket |
|
330 |
||
331 |
# XXX: Any good way to get real user name on win32?
|
|
332 |
||
333 |
try: |
|
334 |
import pwd |
|
335 |
uid = os.getuid() |
|
336 |
w = pwd.getpwuid(uid) |
|
337 |
gecos = w.pw_gecos.decode(bzrlib.user_encoding) |
|
338 |
username = w.pw_name.decode(bzrlib.user_encoding) |
|
339 |
comma = gecos.find(',') |
|
340 |
if comma == -1: |
|
341 |
realname = gecos |
|
342 |
else: |
|
343 |
realname = gecos[:comma] |
|
344 |
if not realname: |
|
345 |
realname = username |
|
346 |
||
347 |
except ImportError: |
|
348 |
import getpass |
|
349 |
realname = username = getpass.getuser().decode(bzrlib.user_encoding) |
|
350 |
||
351 |
return realname, (username + '@' + socket.gethostname()) |
|
352 |
||
353 |