407
407
"""Return size of given open file."""
408
408
return os.fstat(f.fileno())[ST_SIZE]
410
# Define rand_bytes based on platform.
412
# Python 2.4 and later have os.urandom,
413
# but it doesn't work on some arches
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
423
# not well seeded, but better than nothing
428
s += chr(random.randint(0, 255))
413
elif sys.platform == 'linux2':
414
rand_bytes = file('/dev/urandom', 'rb').read
416
# not well seeded, but better than nothing
421
s += chr(random.randint(0, 255))
432
426
## TODO: We could later have path objects that remember their list
433
427
## decomposition (might be too tricksy though.)
496
"""Return a sequence of possible editor binaries for the current platform"""
497
e = _read_config_value("editor")
501
if os.name == "windows":
503
elif os.name == "posix":
505
yield os.environ["EDITOR"]
510
def _run_editor(filename):
511
"""Try to execute an editor to edit the commit message. Returns True on success,
513
for e in _get_editor():
514
x = os.spawnvp(os.P_WAIT, e, (e, filename))
521
raise BzrError("Could not start any editor. Please specify $EDITOR or use ~/.bzr.conf/editor")
525
def get_text_message(infotext, ignoreline = "default"):
528
if ignoreline == "default":
529
ignoreline = "-- This line and the following will be ignored --"
532
tmp_fileno, msgfilename = tempfile.mkstemp()
533
msgfile = os.close(tmp_fileno)
534
if infotext is not None and infotext != "":
536
msgfile = file(msgfilename, "w")
537
msgfile.write("\n\n%s\n\n%s" % (ignoreline, infotext))
542
if not _run_editor(msgfilename):
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
552
if stripped_line != "":
556
# check for the ignore line only if there
557
# is additional information at the end
558
if hasinfo and stripped_line == ignoreline:
561
# keep track of the last line that had some content
562
if stripped_line != "":
568
# delete empty lines at the end
570
# add a newline at the end, if needed
571
if not msg[-1].endswith("\n"):
572
return "%s%s" % ("".join(msg), "\n")
576
# delete the msg file in any case
577
try: os.unlink(msgfilename)