~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Aaron Bentley
  • Date: 2005-08-10 15:05:26 UTC
  • mto: (1092.1.41) (1185.3.4) (974.1.47)
  • mto: This revision was merged to the branch mainline in revision 1110.
  • Revision ID: abentley@panoramicfeedback.com-20050810150526-468a7e2e3dc299e4
Switched to using a set of interesting file_ids instead of SourceFile attribute

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:
142
139
    that . and .. and repeated slashes are eliminated, and the separators
143
140
    are canonical for the platform.
144
141
    
145
 
    The empty string as a dir name is taken as top-of-tree and matches 
146
 
    everything.
147
 
    
148
142
    >>> is_inside('src', 'src/foo.c')
149
143
    True
150
144
    >>> is_inside('src', 'srccontrol')
153
147
    True
154
148
    >>> is_inside('foo.c', 'foo.c')
155
149
    True
156
 
    >>> is_inside('foo.c', '')
157
 
    False
158
 
    >>> is_inside('', 'foo.c')
159
 
    True
160
150
    """
161
151
    # XXX: Most callers of this can actually do something smarter by 
162
152
    # looking at the inventory
 
153
 
163
154
    if dir == fname:
164
155
        return True
165
156
    
166
 
    if dir == '':
167
 
        return True
168
 
    
169
157
    if dir[-1] != os.sep:
170
158
        dir += os.sep
171
159
    
272
260
    return realname, (username + '@' + socket.gethostname())
273
261
 
274
262
 
275
 
def _get_user_id(branch):
 
263
def _get_user_id():
276
264
    """Return the full user id from a file or environment variable.
277
265
 
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
 
    """
 
266
    TODO: Allow taking this from a file in the branch directory too
 
267
    for per-branch ids."""
290
268
    v = os.environ.get('BZREMAIL')
291
269
    if v:
292
270
        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
271
    
306
272
    try:
307
273
        return (open(os.path.join(config_dir(), "email"))
319
285
        return None
320
286
 
321
287
 
322
 
def username(branch):
 
288
def username():
323
289
    """Return email-style username.
324
290
 
325
291
    Something similar to 'Martin Pool <mbp@sourcefrog.net>'
326
292
 
327
293
    TODO: Check it's reasonably well-formed.
328
294
    """
329
 
    v = _get_user_id(branch)
 
295
    v = _get_user_id()
330
296
    if v:
331
297
        return v
332
298
    
337
303
        return email
338
304
 
339
305
 
340
 
def user_email(branch):
 
306
_EMAIL_RE = re.compile(r'[\w+.-]+@[\w+.-]+')
 
307
def user_email():
341
308
    """Return just the email component of a username."""
342
 
    e = _get_user_id(branch)
 
309
    e = _get_user_id()
343
310
    if e:
344
 
        m = re.search(r'[\w+.-]+@[\w+.-]+', e)
 
311
        m = _EMAIL_RE.search(e)
345
312
        if not m:
346
313
            raise BzrError("%r doesn't seem to contain a reasonable email address" % e)
347
314
        return m.group(0)
391
358
        tt = time.localtime(t)
392
359
        offset = local_time_offset(t)
393
360
    else:
394
 
        raise BzrError("unsupported timezone format %r" % timezone,
395
 
                       ['options are "utc", "original", "local"'])
 
361
        raise BzrError("unsupported timezone format %r",
 
362
                ['options are "utc", "original", "local"'])
396
363
 
397
364
    return (time.strftime("%a %Y-%m-%d %H:%M:%S", tt)
398
365
            + ' %+03d%02d' % (offset / 3600, (offset / 60) % 60))
407
374
    """Return size of given open file."""
408
375
    return os.fstat(f.fileno())[ST_SIZE]
409
376
 
410
 
# Define rand_bytes based on platform.
411
 
try:
412
 
    # Python 2.4 and later have os.urandom,
413
 
    # but it doesn't work on some arches
414
 
    os.urandom(1)
 
377
 
 
378
if hasattr(os, 'urandom'): # python 2.4 and later
415
379
    rand_bytes = os.urandom
416
 
except (NotImplementedError, AttributeError):
417
 
    # If python doesn't have os.urandom, or it doesn't work,
418
 
    # then try to first pull random data from /dev/urandom
419
 
    if os.path.exists("/dev/urandom"):
420
 
        rand_bytes = file('/dev/urandom', 'rb').read
421
 
    # Otherwise, use this hack as a last resort
422
 
    else:
423
 
        # not well seeded, but better than nothing
424
 
        def rand_bytes(n):
425
 
            import random
426
 
            s = ''
427
 
            while n:
428
 
                s += chr(random.randint(0, 255))
429
 
                n -= 1
430
 
            return s
 
380
elif sys.platform == 'linux2':
 
381
    rand_bytes = file('/dev/urandom', 'rb').read
 
382
else:
 
383
    # not well seeded, but better than nothing
 
384
    def rand_bytes(n):
 
385
        import random
 
386
        s = ''
 
387
        while n:
 
388
            s += chr(random.randint(0, 255))
 
389
            n -= 1
 
390
        return s
 
391
 
431
392
 
432
393
## TODO: We could later have path objects that remember their list
433
394
## decomposition (might be too tricksy though.)
498
459
        raise
499
460
 
500
461
 
 
462
def _get_editor():
 
463
    """Return a sequence of possible editor binaries for the current platform"""
 
464
    e = _read_config_value("editor")
 
465
    if e is not None:
 
466
        yield e
 
467
        
 
468
    if os.name == "windows":
 
469
        yield "notepad.exe"
 
470
    elif os.name == "posix":
 
471
        try:
 
472
            yield os.environ["EDITOR"]
 
473
        except KeyError:
 
474
            yield "/usr/bin/vi"
 
475
 
 
476
 
 
477
def _run_editor(filename):
 
478
    """Try to execute an editor to edit the commit message. Returns True on success,
 
479
    False on failure"""
 
480
    for e in _get_editor():
 
481
        x = os.spawnvp(os.P_WAIT, e, (e, filename))
 
482
        if x == 0:
 
483
            return True
 
484
        elif x == 127:
 
485
            continue
 
486
        else:
 
487
            break
 
488
    raise BzrError("Could not start any editor. Please specify $EDITOR or use ~/.bzr.conf/editor")
 
489
    return False
 
490
                          
 
491
 
 
492
def get_text_message(infotext, ignoreline = "default"):
 
493
    import tempfile
 
494
    
 
495
    if ignoreline == "default":
 
496
        ignoreline = "-- This line and the following will be ignored --"
 
497
        
 
498
    try:
 
499
        tmp_fileno, msgfilename = tempfile.mkstemp()
 
500
        msgfile = os.close(tmp_fileno)
 
501
        if infotext is not None and infotext != "":
 
502
            hasinfo = True
 
503
            msgfile = file(msgfilename, "w")
 
504
            msgfile.write("\n\n%s\n\n%s" % (ignoreline, infotext))
 
505
            msgfile.close()
 
506
        else:
 
507
            hasinfo = False
 
508
 
 
509
        if not _run_editor(msgfilename):
 
510
            return None
 
511
        
 
512
        started = False
 
513
        msg = []
 
514
        lastline, nlines = 0, 0
 
515
        for line in file(msgfilename, "r"):
 
516
            stripped_line = line.strip()
 
517
            # strip empty line before the log message starts
 
518
            if not started:
 
519
                if stripped_line != "":
 
520
                    started = True
 
521
                else:
 
522
                    continue
 
523
            # check for the ignore line only if there
 
524
            # is additional information at the end
 
525
            if hasinfo and stripped_line == ignoreline:
 
526
                break
 
527
            nlines += 1
 
528
            # keep track of the last line that had some content
 
529
            if stripped_line != "":
 
530
                lastline = nlines
 
531
            msg.append(line)
 
532
            
 
533
        if len(msg) == 0:
 
534
            return None
 
535
        # delete empty lines at the end
 
536
        del msg[lastline:]
 
537
        # add a newline at the end, if needed
 
538
        if not msg[-1].endswith("\n"):
 
539
            return "%s%s" % ("".join(msg), "\n")
 
540
        else:
 
541
            return "".join(msg)
 
542
    finally:
 
543
        # delete the msg file in any case
 
544
        try: os.unlink(msgfilename)
 
545
        except IOError: pass