19
19
import os, types, re, time, errno, sys
20
20
from stat import S_ISREG, S_ISDIR, S_ISLNK, ST_MODE, ST_SIZE
22
from bzrlib.errors import BzrError
23
from bzrlib.trace import mutter
22
from errors import bailout, BzrError
23
from trace import mutter
26
26
def make_readonly(filename):
91
def is_inside(dir, fname):
92
"""True if fname is inside dir.
94
return os.path.commonprefix([dir, fname]) == dir
97
def is_inside_any(dir_list, fname):
98
"""True if fname is inside any of given dirs."""
99
# quick scan for perfect match
100
if fname in dir_list:
103
for dirname in dir_list:
104
if is_inside(dirname, fname):
110
80
def pumpfile(fromfile, tofile):
111
81
"""Copy contents of one file to another."""
112
82
tofile.write(fromfile.read())
296
266
tt = time.localtime(t)
297
267
offset = local_time_offset(t)
299
raise BzrError("unsupported timezone format %r",
269
bailout("unsupported timezone format %r",
300
270
['options are "utc", "original", "local"'])
302
272
return (time.strftime("%a %Y-%m-%d %H:%M:%S", tt)
345
315
>>> splitpath('a/../b')
346
316
Traceback (most recent call last):
348
BzrError: sorry, '..' not allowed in path
318
BzrError: ("sorry, '..' not allowed in path", [])
350
320
assert isinstance(p, types.StringTypes)
382
352
mutter('external command: %s' % `cmd`)
383
353
if os.system(cmd):
384
354
if not ignore_errors:
385
raise BzrError('command failed')
388
def _read_config_value(name):
389
"""Read a config value from the file ~/.bzr.conf/<name>
390
Return None if the file does not exist"""
392
f = file(os.path.join(config_dir(), name), "r")
393
return f.read().decode(bzrlib.user_encoding).rstrip("\r\n")
395
if e.errno == errno.ENOENT:
401
"""Return a sequence of possible editor binaries for the current platform"""
402
e = _read_config_value("editor")
406
if os.name == "windows":
408
elif os.name == "posix":
410
yield os.environ["EDITOR"]
415
def _run_editor(filename):
416
"""Try to execute an editor to edit the commit message. Returns True on success,
418
for e in _get_editor():
419
x = os.spawnvp(os.P_WAIT, e, (e, filename))
426
raise BzrError("Could not start any editor. Please specify $EDITOR or use ~/.bzr.conf/editor")
430
def get_text_message(infotext, ignoreline = "default"):
433
if ignoreline == "default":
434
ignoreline = "-- This line and the following will be ignored --"
437
tmp_fileno, msgfilename = tempfile.mkstemp()
438
msgfile = os.close(tmp_fileno)
439
if infotext is not None and infotext != "":
441
msgfile = file(msgfilename, "w")
442
msgfile.write("\n\n%s\n\n%s" % (ignoreline, infotext))
447
if not _run_editor(msgfilename):
452
lastline, nlines = 0, 0
453
for line in file(msgfilename, "r"):
454
stripped_line = line.strip()
455
# strip empty line before the log message starts
457
if stripped_line != "":
461
# check for the ignore line only if there
462
# is additional information at the end
463
if hasinfo and stripped_line == ignoreline:
466
# keep track of the last line that had some content
467
if stripped_line != "":
473
# delete empty lines at the end
475
# add a newline at the end, if needed
476
if not msg[-1].endswith("\n"):
477
return "%s%s" % ("".join(msg), "\n")
481
# delete the msg file in any case
482
try: os.unlink(msgfilename)
355
bailout('command failed')