~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Martin Pool
  • Date: 2005-09-06 02:26:28 UTC
  • Revision ID: mbp@sourcefrog.net-20050906022628-66d65f0feb4a9e80
- implement version 5 xml storage, and tests

  This stores files identified by the version that introduced the 
  text, and the version that introduced the name.  Entry kinds are
  given by the xml tag not an explicit kind field.

Show diffs side-by-side

added added

removed removed

Lines of Context:
492
492
        raise
493
493
 
494
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