~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Martin Pool
  • Date: 2005-08-22 16:31:16 UTC
  • Revision ID: mbp@sourcefrog.net-20050822163116-935a433f338e7d5b
- new shell-complete command to help zsh completion

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:
139
142
    that . and .. and repeated slashes are eliminated, and the separators
140
143
    are canonical for the platform.
141
144
    
 
145
    The empty string as a dir name is taken as top-of-tree and matches 
 
146
    everything.
 
147
    
142
148
    >>> is_inside('src', 'src/foo.c')
143
149
    True
144
150
    >>> is_inside('src', 'srccontrol')
147
153
    True
148
154
    >>> is_inside('foo.c', 'foo.c')
149
155
    True
 
156
    >>> is_inside('foo.c', '')
 
157
    False
 
158
    >>> is_inside('', 'foo.c')
 
159
    True
150
160
    """
151
161
    # XXX: Most callers of this can actually do something smarter by 
152
162
    # looking at the inventory
153
 
 
154
163
    if dir == fname:
155
164
        return True
156
165
    
 
166
    if dir == '':
 
167
        return True
 
168
    
157
169
    if dir[-1] != os.sep:
158
170
        dir += os.sep
159
171
    
260
272
    return realname, (username + '@' + socket.gethostname())
261
273
 
262
274
 
263
 
def _get_user_id():
 
275
def _get_user_id(branch):
264
276
    """Return the full user id from a file or environment variable.
265
277
 
266
 
    TODO: Allow taking this from a file in the branch directory too
267
 
    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
    """
268
290
    v = os.environ.get('BZREMAIL')
269
291
    if v:
270
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
271
305
    
272
306
    try:
273
307
        return (open(os.path.join(config_dir(), "email"))
285
319
        return None
286
320
 
287
321
 
288
 
def username():
 
322
def username(branch):
289
323
    """Return email-style username.
290
324
 
291
325
    Something similar to 'Martin Pool <mbp@sourcefrog.net>'
292
326
 
293
327
    TODO: Check it's reasonably well-formed.
294
328
    """
295
 
    v = _get_user_id()
 
329
    v = _get_user_id(branch)
296
330
    if v:
297
331
        return v
298
332
    
303
337
        return email
304
338
 
305
339
 
306
 
_EMAIL_RE = re.compile(r'[\w+.-]+@[\w+.-]+')
307
 
def user_email():
 
340
def user_email(branch):
308
341
    """Return just the email component of a username."""
309
 
    e = _get_user_id()
 
342
    e = _get_user_id(branch)
310
343
    if e:
311
 
        m = _EMAIL_RE.search(e)
 
344
        m = re.search(r'[\w+.-]+@[\w+.-]+', e)
312
345
        if not m:
313
346
            raise BzrError("%r doesn't seem to contain a reasonable email address" % e)
314
347
        return m.group(0)
358
391
        tt = time.localtime(t)
359
392
        offset = local_time_offset(t)
360
393
    else:
361
 
        raise BzrError("unsupported timezone format %r",
362
 
                ['options are "utc", "original", "local"'])
 
394
        raise BzrError("unsupported timezone format %r" % timezone,
 
395
                       ['options are "utc", "original", "local"'])
363
396
 
364
397
    return (time.strftime("%a %Y-%m-%d %H:%M:%S", tt)
365
398
            + ' %+03d%02d' % (offset / 3600, (offset / 60) % 60))