~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Martin Pool
  • Date: 2005-08-17 03:31:19 UTC
  • Revision ID: mbp@sourcefrog.net-20050817033119-1976931eac3199db
todo

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 = None
 
40
_QUOTE_RE = re.compile(r'([^a-zA-Z0-9.,:/_~-])')
41
41
 
 
42
_SLASH_RE = re.compile(r'[\\/]+')
42
43
 
43
44
def quotefn(f):
44
45
    """Return a quoted filename filename
46
47
    This previously used backslash quoting, but that works poorly on
47
48
    Windows."""
48
49
    # 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
 
        
53
50
    if _QUOTE_RE.search(f):
54
51
        return '"' + f + '"'
55
52
    else:
272
269
    return realname, (username + '@' + socket.gethostname())
273
270
 
274
271
 
275
 
def _get_user_id(branch):
 
272
def _get_user_id():
276
273
    """Return the full user id from a file or environment variable.
277
274
 
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
 
    """
 
275
    TODO: Allow taking this from a file in the branch directory too
 
276
    for per-branch ids."""
290
277
    v = os.environ.get('BZREMAIL')
291
278
    if v:
292
279
        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
305
280
    
306
281
    try:
307
282
        return (open(os.path.join(config_dir(), "email"))
319
294
        return None
320
295
 
321
296
 
322
 
def username(branch):
 
297
def username():
323
298
    """Return email-style username.
324
299
 
325
300
    Something similar to 'Martin Pool <mbp@sourcefrog.net>'
326
301
 
327
302
    TODO: Check it's reasonably well-formed.
328
303
    """
329
 
    v = _get_user_id(branch)
 
304
    v = _get_user_id()
330
305
    if v:
331
306
        return v
332
307
    
337
312
        return email
338
313
 
339
314
 
340
 
def user_email(branch):
 
315
_EMAIL_RE = re.compile(r'[\w+.-]+@[\w+.-]+')
 
316
def user_email():
341
317
    """Return just the email component of a username."""
342
 
    e = _get_user_id(branch)
 
318
    e = _get_user_id()
343
319
    if e:
344
 
        m = re.search(r'[\w+.-]+@[\w+.-]+', e)
 
320
        m = _EMAIL_RE.search(e)
345
321
        if not m:
346
322
            raise BzrError("%r doesn't seem to contain a reasonable email address" % e)
347
323
        return m.group(0)