/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
18
"""Configuration that affects the behaviour of Bazaar."""
19
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
20
from ConfigParser import ConfigParser
1442.1.1 by Robert Collins
move config_dir into bzrlib.config
21
import os
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
22
import errno
23
import re
24
25
import bzrlib
26
1442.1.1 by Robert Collins
move config_dir into bzrlib.config
27
28
def config_dir():
29
    """Return per-user configuration directory.
30
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
31
    By default this is ~/.bazaar/
1442.1.1 by Robert Collins
move config_dir into bzrlib.config
32
    
33
    TODO: Global option --config-dir to override this.
34
    """
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
35
    return os.path.join(os.path.expanduser("~"), ".bazaar")
36
37
38
def config_filename():
39
    """Return per-user configuration ini file filename."""
40
    return os.path.join(config_dir(), 'bazaar.conf')
41
42
43
def _get_config_parser(file=None):
44
    parser = ConfigParser()
45
    if file is not None:
46
        parser.readfp(file)
47
    else:
48
        parser.read([config_filename()])
49
    return parser
50
51
1442.1.3 by Robert Collins
move editor into the config file too
52
def get_editor(parser=None):
53
    if parser is None:
54
        parser = _get_config_parser()
55
    if parser.has_option('DEFAULT', 'editor'):
56
        return parser.get('DEFAULT', 'editor')
57
58
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
59
def _get_user_id(branch=None, parser = None):
60
    """Return the full user id from a file or environment variable.
61
62
    e.g. "John Hacker <jhacker@foo.org>"
63
64
    branch
65
        A branch to use for a per-branch configuration, or None.
66
67
    The following are searched in order:
68
69
    1. $BZREMAIL
70
    2. .bzr/email for this branch.
71
    3. ~/.bzr.conf/email
72
    4. $EMAIL
73
    """
74
    v = os.environ.get('BZREMAIL')
75
    if v:
76
        return v.decode(bzrlib.user_encoding)
77
78
    if branch:
79
        try:
80
            return (branch.controlfile("email", "r") 
81
                    .read()
82
                    .decode(bzrlib.user_encoding)
83
                    .rstrip("\r\n"))
84
        except IOError, e:
85
            if e.errno != errno.ENOENT:
86
                raise
87
        except BzrError, e:
88
            pass
89
    
1442.1.3 by Robert Collins
move editor into the config file too
90
    if parser is None:
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
91
        parser = _get_config_parser()
92
    if parser.has_option('DEFAULT', 'email'):
93
        email = parser.get('DEFAULT', 'email')
94
        if email is not None:
95
            return email
96
97
    v = os.environ.get('EMAIL')
98
    if v:
99
        return v.decode(bzrlib.user_encoding)
100
    else:    
101
        return None
102
103
104
def _auto_user_id():
105
    """Calculate automatic user identification.
106
107
    Returns (realname, email).
108
109
    Only used when none is set in the environment or the id file.
110
111
    This previously used the FQDN as the default domain, but that can
112
    be very slow on machines where DNS is broken.  So now we simply
113
    use the hostname.
114
    """
115
    import socket
116
117
    # XXX: Any good way to get real user name on win32?
118
119
    try:
120
        import pwd
121
        uid = os.getuid()
122
        w = pwd.getpwuid(uid)
123
        gecos = w.pw_gecos.decode(bzrlib.user_encoding)
124
        username = w.pw_name.decode(bzrlib.user_encoding)
125
        comma = gecos.find(',')
126
        if comma == -1:
127
            realname = gecos
128
        else:
129
            realname = gecos[:comma]
130
        if not realname:
131
            realname = username
132
133
    except ImportError:
134
        import getpass
135
        realname = username = getpass.getuser().decode(bzrlib.user_encoding)
136
137
    return realname, (username + '@' + socket.gethostname())
138
139
140
def username(branch):
141
    """Return email-style username.
142
143
    Something similar to 'Martin Pool <mbp@sourcefrog.net>'
144
145
    TODO: Check it's reasonably well-formed.
146
    """
147
    v = _get_user_id(branch)
148
    if v:
149
        return v
150
    
151
    name, email = _auto_user_id()
152
    if name:
153
        return '%s <%s>' % (name, email)
154
    else:
155
        return email
156
157
158
def user_email(branch):
159
    """Return just the email component of a username."""
160
    e = _get_user_id(branch)
161
    if e:
162
        m = re.search(r'[\w+.-]+@[\w+.-]+', e)
163
        if not m:
164
            raise BzrError("%r doesn't seem to contain "
165
                           "a reasonable email address" % e)
166
        return m.group(0)
167
168
    return _auto_user_id()[1]
169
170