/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
52
def _get_user_id(branch=None, parser = None):
53
    """Return the full user id from a file or environment variable.
54
55
    e.g. "John Hacker <jhacker@foo.org>"
56
57
    branch
58
        A branch to use for a per-branch configuration, or None.
59
60
    The following are searched in order:
61
62
    1. $BZREMAIL
63
    2. .bzr/email for this branch.
64
    3. ~/.bzr.conf/email
65
    4. $EMAIL
66
    """
67
    v = os.environ.get('BZREMAIL')
68
    if v:
69
        return v.decode(bzrlib.user_encoding)
70
71
    if branch:
72
        try:
73
            return (branch.controlfile("email", "r") 
74
                    .read()
75
                    .decode(bzrlib.user_encoding)
76
                    .rstrip("\r\n"))
77
        except IOError, e:
78
            if e.errno != errno.ENOENT:
79
                raise
80
        except BzrError, e:
81
            pass
82
    
83
    if not parser:
84
        parser = _get_config_parser()
85
    if parser.has_option('DEFAULT', 'email'):
86
        email = parser.get('DEFAULT', 'email')
87
        if email is not None:
88
            return email
89
90
    v = os.environ.get('EMAIL')
91
    if v:
92
        return v.decode(bzrlib.user_encoding)
93
    else:    
94
        return None
95
96
97
def _auto_user_id():
98
    """Calculate automatic user identification.
99
100
    Returns (realname, email).
101
102
    Only used when none is set in the environment or the id file.
103
104
    This previously used the FQDN as the default domain, but that can
105
    be very slow on machines where DNS is broken.  So now we simply
106
    use the hostname.
107
    """
108
    import socket
109
110
    # XXX: Any good way to get real user name on win32?
111
112
    try:
113
        import pwd
114
        uid = os.getuid()
115
        w = pwd.getpwuid(uid)
116
        gecos = w.pw_gecos.decode(bzrlib.user_encoding)
117
        username = w.pw_name.decode(bzrlib.user_encoding)
118
        comma = gecos.find(',')
119
        if comma == -1:
120
            realname = gecos
121
        else:
122
            realname = gecos[:comma]
123
        if not realname:
124
            realname = username
125
126
    except ImportError:
127
        import getpass
128
        realname = username = getpass.getuser().decode(bzrlib.user_encoding)
129
130
    return realname, (username + '@' + socket.gethostname())
131
132
133
def username(branch):
134
    """Return email-style username.
135
136
    Something similar to 'Martin Pool <mbp@sourcefrog.net>'
137
138
    TODO: Check it's reasonably well-formed.
139
    """
140
    v = _get_user_id(branch)
141
    if v:
142
        return v
143
    
144
    name, email = _auto_user_id()
145
    if name:
146
        return '%s <%s>' % (name, email)
147
    else:
148
        return email
149
150
151
def user_email(branch):
152
    """Return just the email component of a username."""
153
    e = _get_user_id(branch)
154
    if e:
155
        m = re.search(r'[\w+.-]+@[\w+.-]+', e)
156
        if not m:
157
            raise BzrError("%r doesn't seem to contain "
158
                           "a reasonable email address" % e)
159
        return m.group(0)
160
161
    return _auto_user_id()[1]
162
163