~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Martin Pool
  • Date: 2005-08-18 04:25:42 UTC
  • Revision ID: mbp@sourcefrog.net-20050818042542-6af9da978f695195
- check for email address in BRANCH_ROOT/.bzr/email, so you can 
  easily use different per-project personas

Show diffs side-by-side

added added

removed removed

Lines of Context:
269
269
    return realname, (username + '@' + socket.gethostname())
270
270
 
271
271
 
272
 
def _get_user_id():
 
272
def _get_user_id(branch):
273
273
    """Return the full user id from a file or environment variable.
274
274
 
275
 
    TODO: Allow taking this from a file in the branch directory too
276
 
    for per-branch ids."""
 
275
    e.g. "John Hacker <jhacker@foo.org>"
 
276
 
 
277
    branch
 
278
        A branch to use for a per-branch configuration, or None.
 
279
 
 
280
    The following are searched in order:
 
281
 
 
282
    1. $BZREMAIL
 
283
    2. .bzr/email for this branch.
 
284
    3. ~/.bzr.conf/email
 
285
    4. $EMAIL
 
286
    """
277
287
    v = os.environ.get('BZREMAIL')
278
288
    if v:
279
289
        return v.decode(bzrlib.user_encoding)
 
290
 
 
291
    if branch:
 
292
        try:
 
293
            return (branch.controlfile("email", "r") 
 
294
                    .read()
 
295
                    .decode(bzrlib.user_encoding)
 
296
                    .rstrip("\r\n"))
 
297
        except IOError, e:
 
298
            if e.errno != errno.ENOENT:
 
299
                raise
 
300
        except BzrError, e:
 
301
            pass
280
302
    
281
303
    try:
282
304
        return (open(os.path.join(config_dir(), "email"))
294
316
        return None
295
317
 
296
318
 
297
 
def username():
 
319
def username(branch):
298
320
    """Return email-style username.
299
321
 
300
322
    Something similar to 'Martin Pool <mbp@sourcefrog.net>'
301
323
 
302
324
    TODO: Check it's reasonably well-formed.
303
325
    """
304
 
    v = _get_user_id()
 
326
    v = _get_user_id(branch)
305
327
    if v:
306
328
        return v
307
329
    
313
335
 
314
336
 
315
337
_EMAIL_RE = re.compile(r'[\w+.-]+@[\w+.-]+')
316
 
def user_email():
 
338
def user_email(branch):
317
339
    """Return just the email component of a username."""
318
 
    e = _get_user_id()
 
340
    e = _get_user_id(branch)
319
341
    if e:
320
342
        m = _EMAIL_RE.search(e)
321
343
        if not m: