~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: aaron.bentley at utoronto
  • Date: 2005-08-27 04:42:41 UTC
  • mfrom: (1092.1.43)
  • mto: (1185.3.4)
  • mto: This revision was merged to the branch mainline in revision 1178.
  • Revision ID: aaron.bentley@utoronto.ca-20050827044241-23d676133b9fc981
Merge of robertc@robertcollins.net-20050826013321-52eee1f1da679ee9

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
    os.chmod(filename, mod)
38
38
 
39
39
 
40
 
_QUOTE_RE = re.compile(r'([^a-zA-Z0-9.,:/_~-])')
 
40
_QUOTE_RE = None
41
41
 
42
 
_SLASH_RE = re.compile(r'[\\/]+')
43
42
 
44
43
def quotefn(f):
45
44
    """Return a quoted filename filename
47
46
    This previously used backslash quoting, but that works poorly on
48
47
    Windows."""
49
48
    # TODO: I'm not really sure this is the best format either.x
 
49
    global _QUOTE_RE
 
50
    if _QUOTE_RE == None:
 
51
        _QUOTE_RE = re.compile(r'([^a-zA-Z0-9.,:/_~-])')
 
52
        
50
53
    if _QUOTE_RE.search(f):
51
54
        return '"' + f + '"'
52
55
    else:
269
272
    return realname, (username + '@' + socket.gethostname())
270
273
 
271
274
 
272
 
def _get_user_id():
 
275
def _get_user_id(branch):
273
276
    """Return the full user id from a file or environment variable.
274
277
 
275
 
    TODO: Allow taking this from a file in the branch directory too
276
 
    for per-branch ids."""
 
278
    e.g. "John Hacker <jhacker@foo.org>"
 
279
 
 
280
    branch
 
281
        A branch to use for a per-branch configuration, or None.
 
282
 
 
283
    The following are searched in order:
 
284
 
 
285
    1. $BZREMAIL
 
286
    2. .bzr/email for this branch.
 
287
    3. ~/.bzr.conf/email
 
288
    4. $EMAIL
 
289
    """
277
290
    v = os.environ.get('BZREMAIL')
278
291
    if v:
279
292
        return v.decode(bzrlib.user_encoding)
 
293
 
 
294
    if branch:
 
295
        try:
 
296
            return (branch.controlfile("email", "r") 
 
297
                    .read()
 
298
                    .decode(bzrlib.user_encoding)
 
299
                    .rstrip("\r\n"))
 
300
        except IOError, e:
 
301
            if e.errno != errno.ENOENT:
 
302
                raise
 
303
        except BzrError, e:
 
304
            pass
280
305
    
281
306
    try:
282
307
        return (open(os.path.join(config_dir(), "email"))
294
319
        return None
295
320
 
296
321
 
297
 
def username():
 
322
def username(branch):
298
323
    """Return email-style username.
299
324
 
300
325
    Something similar to 'Martin Pool <mbp@sourcefrog.net>'
301
326
 
302
327
    TODO: Check it's reasonably well-formed.
303
328
    """
304
 
    v = _get_user_id()
 
329
    v = _get_user_id(branch)
305
330
    if v:
306
331
        return v
307
332
    
312
337
        return email
313
338
 
314
339
 
315
 
_EMAIL_RE = re.compile(r'[\w+.-]+@[\w+.-]+')
316
 
def user_email():
 
340
def user_email(branch):
317
341
    """Return just the email component of a username."""
318
 
    e = _get_user_id()
 
342
    e = _get_user_id(branch)
319
343
    if e:
320
 
        m = _EMAIL_RE.search(e)
 
344
        m = re.search(r'[\w+.-]+@[\w+.-]+', e)
321
345
        if not m:
322
346
            raise BzrError("%r doesn't seem to contain a reasonable email address" % e)
323
347
        return m.group(0)