~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Robert Collins
  • Date: 2005-08-25 12:46:42 UTC
  • mfrom: (1116)
  • mto: (974.1.50) (1185.1.10) (1092.3.1)
  • mto: This revision was merged to the branch mainline in revision 1139.
  • Revision ID: robertc@robertcollins.net-20050825124642-45ed1cd74db10370
merge from mpool

Show diffs side-by-side

added added

removed removed

Lines of Context:
407
407
    """Return size of given open file."""
408
408
    return os.fstat(f.fileno())[ST_SIZE]
409
409
 
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)
 
410
 
 
411
if hasattr(os, 'urandom'): # python 2.4 and later
415
412
    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
 
413
elif sys.platform == 'linux2':
 
414
    rand_bytes = file('/dev/urandom', 'rb').read
 
415
else:
 
416
    # not well seeded, but better than nothing
 
417
    def rand_bytes(n):
 
418
        import random
 
419
        s = ''
 
420
        while n:
 
421
            s += chr(random.randint(0, 255))
 
422
            n -= 1
 
423
        return s
 
424
 
431
425
 
432
426
## TODO: We could later have path objects that remember their list
433
427
## decomposition (might be too tricksy though.)
498
492
        raise
499
493
 
500
494
 
 
495
def _get_editor():
 
496
    """Return a sequence of possible editor binaries for the current platform"""
 
497
    e = _read_config_value("editor")
 
498
    if e is not None:
 
499
        yield e
 
500
        
 
501
    if os.name == "windows":
 
502
        yield "notepad.exe"
 
503
    elif os.name == "posix":
 
504
        try:
 
505
            yield os.environ["EDITOR"]
 
506
        except KeyError:
 
507
            yield "/usr/bin/vi"
 
508
 
 
509
 
 
510
def _run_editor(filename):
 
511
    """Try to execute an editor to edit the commit message. Returns True on success,
 
512
    False on failure"""
 
513
    for e in _get_editor():
 
514
        x = os.spawnvp(os.P_WAIT, e, (e, filename))
 
515
        if x == 0:
 
516
            return True
 
517
        elif x == 127:
 
518
            continue
 
519
        else:
 
520
            break
 
521
    raise BzrError("Could not start any editor. Please specify $EDITOR or use ~/.bzr.conf/editor")
 
522
    return False
 
523
                          
 
524
 
 
525
def get_text_message(infotext, ignoreline = "default"):
 
526
    import tempfile
 
527
    
 
528
    if ignoreline == "default":
 
529
        ignoreline = "-- This line and the following will be ignored --"
 
530
        
 
531
    try:
 
532
        tmp_fileno, msgfilename = tempfile.mkstemp()
 
533
        msgfile = os.close(tmp_fileno)
 
534
        if infotext is not None and infotext != "":
 
535
            hasinfo = True
 
536
            msgfile = file(msgfilename, "w")
 
537
            msgfile.write("\n\n%s\n\n%s" % (ignoreline, infotext))
 
538
            msgfile.close()
 
539
        else:
 
540
            hasinfo = False
 
541
 
 
542
        if not _run_editor(msgfilename):
 
543
            return None
 
544
        
 
545
        started = False
 
546
        msg = []
 
547
        lastline, nlines = 0, 0
 
548
        for line in file(msgfilename, "r"):
 
549
            stripped_line = line.strip()
 
550
            # strip empty line before the log message starts
 
551
            if not started:
 
552
                if stripped_line != "":
 
553
                    started = True
 
554
                else:
 
555
                    continue
 
556
            # check for the ignore line only if there
 
557
            # is additional information at the end
 
558
            if hasinfo and stripped_line == ignoreline:
 
559
                break
 
560
            nlines += 1
 
561
            # keep track of the last line that had some content
 
562
            if stripped_line != "":
 
563
                lastline = nlines
 
564
            msg.append(line)
 
565
            
 
566
        if len(msg) == 0:
 
567
            return None
 
568
        # delete empty lines at the end
 
569
        del msg[lastline:]
 
570
        # add a newline at the end, if needed
 
571
        if not msg[-1].endswith("\n"):
 
572
            return "%s%s" % ("".join(msg), "\n")
 
573
        else:
 
574
            return "".join(msg)
 
575
    finally:
 
576
        # delete the msg file in any case
 
577
        try: os.unlink(msgfilename)
 
578
        except IOError: pass