/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1 by mbp at sourcefrog
import from baz patch-364
1
# Bazaar-NG -- distributed version control
1185.1.41 by Robert Collins
massive patch from Alexander Belchenko - many PEP8 fixes, removes unused function uuid
2
#
1 by mbp at sourcefrog
import from baz patch-364
3
# Copyright (C) 2005 by Canonical Ltd
1185.1.41 by Robert Collins
massive patch from Alexander Belchenko - many PEP8 fixes, removes unused function uuid
4
#
1 by mbp at sourcefrog
import from baz patch-364
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
1185.1.41 by Robert Collins
massive patch from Alexander Belchenko - many PEP8 fixes, removes unused function uuid
9
#
1 by mbp at sourcefrog
import from baz patch-364
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
1185.1.41 by Robert Collins
massive patch from Alexander Belchenko - many PEP8 fixes, removes unused function uuid
14
#
1 by mbp at sourcefrog
import from baz patch-364
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
1185.1.46 by Robert Collins
Aarons branch --basis patch
19
from shutil import copyfile
1185.3.28 by John Arbash Meinel
Adding knowledge about fifo/block/etc, they will be unknown/ignored.
20
from stat import (S_ISREG, S_ISDIR, S_ISLNK, ST_MODE, ST_SIZE,
1185.1.41 by Robert Collins
massive patch from Alexander Belchenko - many PEP8 fixes, removes unused function uuid
21
                  S_ISCHR, S_ISBLK, S_ISFIFO, S_ISSOCK)
1390 by Robert Collins
pair programming worx... merge integration and weave
22
from cStringIO import StringIO
1185.1.41 by Robert Collins
massive patch from Alexander Belchenko - many PEP8 fixes, removes unused function uuid
23
import errno
24
import os
25
import re
1236 by Martin Pool
- fix up imports
26
import sha
1185.16.38 by Martin Pool
- move contains_whitespace and contains_linebreaks to osutils
27
import string
1185.1.41 by Robert Collins
massive patch from Alexander Belchenko - many PEP8 fixes, removes unused function uuid
28
import sys
29
import time
30
import types
1185.31.40 by John Arbash Meinel
Added osutils.mkdtemp()
31
import tempfile
1 by mbp at sourcefrog
import from baz patch-364
32
1185.1.41 by Robert Collins
massive patch from Alexander Belchenko - many PEP8 fixes, removes unused function uuid
33
import bzrlib
1185.31.41 by John Arbash Meinel
Creating a PathNotChild exception, and using relpath in HTTPTestUtil
34
from bzrlib.errors import BzrError, PathNotChild
694 by Martin Pool
- weed out all remaining calls to bailout() and remove the function
35
from bzrlib.trace import mutter
1185.1.41 by Robert Collins
massive patch from Alexander Belchenko - many PEP8 fixes, removes unused function uuid
36
1 by mbp at sourcefrog
import from baz patch-364
37
38
def make_readonly(filename):
39
    """Make a filename read-only."""
40
    mod = os.stat(filename).st_mode
41
    mod = mod & 0777555
42
    os.chmod(filename, mod)
43
44
45
def make_writable(filename):
46
    mod = os.stat(filename).st_mode
47
    mod = mod | 0200
48
    os.chmod(filename, mod)
49
50
1077 by Martin Pool
- avoid compiling REs at module load time
51
_QUOTE_RE = None
969 by Martin Pool
- Add less-sucky is_within_any
52
53
1 by mbp at sourcefrog
import from baz patch-364
54
def quotefn(f):
779 by Martin Pool
- better quotefn for windows: use doublequotes for strings with
55
    """Return a quoted filename filename
56
57
    This previously used backslash quoting, but that works poorly on
58
    Windows."""
59
    # TODO: I'm not really sure this is the best format either.x
1077 by Martin Pool
- avoid compiling REs at module load time
60
    global _QUOTE_RE
61
    if _QUOTE_RE == None:
1185.1.41 by Robert Collins
massive patch from Alexander Belchenko - many PEP8 fixes, removes unused function uuid
62
        _QUOTE_RE = re.compile(r'([^a-zA-Z0-9.,:/\\_~-])')
1077 by Martin Pool
- avoid compiling REs at module load time
63
        
779 by Martin Pool
- better quotefn for windows: use doublequotes for strings with
64
    if _QUOTE_RE.search(f):
65
        return '"' + f + '"'
66
    else:
67
        return f
1 by mbp at sourcefrog
import from baz patch-364
68
69
70
def file_kind(f):
71
    mode = os.lstat(f)[ST_MODE]
72
    if S_ISREG(mode):
73
        return 'file'
74
    elif S_ISDIR(mode):
75
        return 'directory'
20 by mbp at sourcefrog
don't abort on trees that happen to contain symlinks
76
    elif S_ISLNK(mode):
77
        return 'symlink'
1185.3.28 by John Arbash Meinel
Adding knowledge about fifo/block/etc, they will be unknown/ignored.
78
    elif S_ISCHR(mode):
79
        return 'chardev'
80
    elif S_ISBLK(mode):
81
        return 'block'
82
    elif S_ISFIFO(mode):
83
        return 'fifo'
84
    elif S_ISSOCK(mode):
85
        return 'socket'
1 by mbp at sourcefrog
import from baz patch-364
86
    else:
1185.3.28 by John Arbash Meinel
Adding knowledge about fifo/block/etc, they will be unknown/ignored.
87
        return 'unknown'
488 by Martin Pool
- new helper function kind_marker()
88
89
90
def kind_marker(kind):
91
    if kind == 'file':
92
        return ''
93
    elif kind == 'directory':
94
        return '/'
95
    elif kind == 'symlink':
96
        return '@'
97
    else:
98
        raise BzrError('invalid file kind %r' % kind)
1 by mbp at sourcefrog
import from baz patch-364
99
1092.2.6 by Robert Collins
symlink support updated to work
100
def lexists(f):
1185.31.33 by John Arbash Meinel
A couple more path.join statements needed changing.
101
    if hasattr(os.path, 'lexists'):
102
        return os.path.lexists(f)
1092.2.6 by Robert Collins
symlink support updated to work
103
    try:
104
        if hasattr(os, 'lstat'):
105
            os.lstat(f)
106
        else:
107
            os.stat(f)
108
        return True
109
    except OSError,e:
110
        if e.errno == errno.ENOENT:
111
            return False;
112
        else:
113
            raise BzrError("lstat/stat of (%r): %r" % (f, e))
1 by mbp at sourcefrog
import from baz patch-364
114
1185.16.70 by Martin Pool
- improved handling of non-ascii branch names and test
115
if os.name == "posix":
116
    # In Python 2.4.2 and older, os.path.abspath and os.path.realpath
117
    # choke on a Unicode string containing a relative path if
118
    # os.getcwd() returns a non-sys.getdefaultencoding()-encoded
119
    # string.
120
    _fs_enc = sys.getfilesystemencoding()
121
    def abspath(path):
122
        return os.path.abspath(path.encode(_fs_enc)).decode(_fs_enc)
123
    def realpath(path):
124
        return os.path.realpath(path.encode(_fs_enc)).decode(_fs_enc)
1185.31.32 by John Arbash Meinel
Updated the bzr sourcecode to use bzrlib.osutils.pathjoin rather than os.path.join to enforce internal use of / instead of \
125
    pathjoin = os.path.join
1185.31.38 by John Arbash Meinel
Changing os.path.normpath to osutils.normpath
126
    normpath = os.path.normpath
1185.31.39 by John Arbash Meinel
Replacing os.getcwdu() with osutils.getcwd(),
127
    getcwd = os.getcwdu
1185.31.40 by John Arbash Meinel
Added osutils.mkdtemp()
128
    mkdtemp = tempfile.mkdtemp
1185.16.70 by Martin Pool
- improved handling of non-ascii branch names and test
129
else:
130
    # We need to use the Unicode-aware os.path.abspath and
131
    # os.path.realpath on Windows systems.
1185.31.32 by John Arbash Meinel
Updated the bzr sourcecode to use bzrlib.osutils.pathjoin rather than os.path.join to enforce internal use of / instead of \
132
    def abspath(path):
133
        return os.path.abspath(path).replace('\\', '/')
134
    def realpath(path):
135
        return os.path.realpath(path).replace('\\', '/')
136
    def pathjoin(*args):
137
        return os.path.join(*args).replace('\\', '/')
1185.31.38 by John Arbash Meinel
Changing os.path.normpath to osutils.normpath
138
    def normpath(path):
139
        return os.path.normpath(path).replace('\\', '/')
1185.31.39 by John Arbash Meinel
Replacing os.getcwdu() with osutils.getcwd(),
140
    def getcwd():
141
        return os.getcwdu().replace('\\', '/')
1185.31.40 by John Arbash Meinel
Added osutils.mkdtemp()
142
    def mkdtemp(*args, **kwargs):
143
        return tempfile.mkdtemp(*args, **kwargs).replace('\\', '/')
1185.31.33 by John Arbash Meinel
A couple more path.join statements needed changing.
144
# Because these shrink the path, we can use the original
145
# versions on any platform
146
dirname = os.path.dirname
147
basename = os.path.basename
1185.31.32 by John Arbash Meinel
Updated the bzr sourcecode to use bzrlib.osutils.pathjoin rather than os.path.join to enforce internal use of / instead of \
148
149
def normalizepath(f):
150
    if hasattr(os.path, 'realpath'):
151
        F = realpath
152
    else:
153
        F = abspath
154
    [p,e] = os.path.split(f)
155
    if e == "" or e == "." or e == "..":
156
        return F(f)
157
    else:
158
        return pathjoin(F(p), e)
159
1 by mbp at sourcefrog
import from baz patch-364
160
779 by Martin Pool
- better quotefn for windows: use doublequotes for strings with
161
def backup_file(fn):
162
    """Copy a file to a backup.
163
164
    Backups are named in GNU-style, with a ~ suffix.
165
166
    If the file is already a backup, it's not copied.
167
    """
168
    if fn[-1] == '~':
169
        return
170
    bfn = fn + '~'
171
1448 by Robert Collins
revert symlinks correctly
172
    if has_symlinks() and os.path.islink(fn):
173
        target = os.readlink(fn)
174
        os.symlink(target, bfn)
175
        return
779 by Martin Pool
- better quotefn for windows: use doublequotes for strings with
176
    inf = file(fn, 'rb')
177
    try:
178
        content = inf.read()
179
    finally:
180
        inf.close()
181
    
182
    outf = file(bfn, 'wb')
183
    try:
184
        outf.write(content)
185
    finally:
186
        outf.close()
187
1185.1.40 by Robert Collins
Merge what applied of Alexander Belchenko's win32 patch.
188
if os.name == 'nt':
189
    import shutil
190
    rename = shutil.move
191
else:
192
    rename = os.rename
779 by Martin Pool
- better quotefn for windows: use doublequotes for strings with
193
194
1 by mbp at sourcefrog
import from baz patch-364
195
def isdir(f):
196
    """True if f is an accessible directory."""
197
    try:
198
        return S_ISDIR(os.lstat(f)[ST_MODE])
199
    except OSError:
200
        return False
201
202
203
def isfile(f):
204
    """True if f is a regular file."""
205
    try:
206
        return S_ISREG(os.lstat(f)[ST_MODE])
207
    except OSError:
208
        return False
209
1092.2.6 by Robert Collins
symlink support updated to work
210
def islink(f):
211
    """True if f is a symlink."""
212
    try:
213
        return S_ISLNK(os.lstat(f)[ST_MODE])
214
    except OSError:
215
        return False
1 by mbp at sourcefrog
import from baz patch-364
216
485 by Martin Pool
- move commit code into its own module
217
def is_inside(dir, fname):
218
    """True if fname is inside dir.
969 by Martin Pool
- Add less-sucky is_within_any
219
    
1185.31.38 by John Arbash Meinel
Changing os.path.normpath to osutils.normpath
220
    The parameters should typically be passed to osutils.normpath first, so
969 by Martin Pool
- Add less-sucky is_within_any
221
    that . and .. and repeated slashes are eliminated, and the separators
222
    are canonical for the platform.
223
    
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
224
    The empty string as a dir name is taken as top-of-tree and matches 
225
    everything.
226
    
1185.31.32 by John Arbash Meinel
Updated the bzr sourcecode to use bzrlib.osutils.pathjoin rather than os.path.join to enforce internal use of / instead of \
227
    >>> is_inside('src', pathjoin('src', 'foo.c'))
969 by Martin Pool
- Add less-sucky is_within_any
228
    True
229
    >>> is_inside('src', 'srccontrol')
230
    False
1185.31.32 by John Arbash Meinel
Updated the bzr sourcecode to use bzrlib.osutils.pathjoin rather than os.path.join to enforce internal use of / instead of \
231
    >>> is_inside('src', pathjoin('src', 'a', 'a', 'a', 'foo.c'))
969 by Martin Pool
- Add less-sucky is_within_any
232
    True
233
    >>> is_inside('foo.c', 'foo.c')
234
    True
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
235
    >>> is_inside('foo.c', '')
236
    False
237
    >>> is_inside('', 'foo.c')
238
    True
485 by Martin Pool
- move commit code into its own module
239
    """
969 by Martin Pool
- Add less-sucky is_within_any
240
    # XXX: Most callers of this can actually do something smarter by 
241
    # looking at the inventory
972 by Martin Pool
- less dodgy is_inside function
242
    if dir == fname:
243
        return True
244
    
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
245
    if dir == '':
246
        return True
1185.1.41 by Robert Collins
massive patch from Alexander Belchenko - many PEP8 fixes, removes unused function uuid
247
1185.31.34 by John Arbash Meinel
Removing instances of os.sep
248
    if dir[-1] != '/':
249
        dir += '/'
1185.1.41 by Robert Collins
massive patch from Alexander Belchenko - many PEP8 fixes, removes unused function uuid
250
972 by Martin Pool
- less dodgy is_inside function
251
    return fname.startswith(dir)
252
485 by Martin Pool
- move commit code into its own module
253
254
def is_inside_any(dir_list, fname):
255
    """True if fname is inside any of given dirs."""
256
    for dirname in dir_list:
257
        if is_inside(dirname, fname):
258
            return True
259
    else:
260
        return False
261
262
1 by mbp at sourcefrog
import from baz patch-364
263
def pumpfile(fromfile, tofile):
264
    """Copy contents of one file to another."""
1185.49.12 by John Arbash Meinel
Changed pumpfile to work on blocks, rather than reading the entire file at once.
265
    BUFSIZE = 32768
266
    while True:
267
        b = fromfile.read(BUFSIZE)
268
        if not b:
269
            break
1185.49.13 by John Arbash Meinel
Removed delayed setup, since it broke some tests. Fixed other small bugs. All tests pass.
270
        tofile.write(b)
1 by mbp at sourcefrog
import from baz patch-364
271
272
273
def sha_file(f):
274
    if hasattr(f, 'tell'):
275
        assert f.tell() == 0
276
    s = sha.new()
320 by Martin Pool
- Compute SHA-1 of files in chunks
277
    BUFSIZE = 128<<10
278
    while True:
279
        b = f.read(BUFSIZE)
280
        if not b:
281
            break
282
        s.update(b)
1 by mbp at sourcefrog
import from baz patch-364
283
    return s.hexdigest()
284
285
1235 by Martin Pool
- split sha_strings into osutils
286
287
def sha_strings(strings):
288
    """Return the sha-1 of concatenation of strings"""
289
    s = sha.new()
290
    map(s.update, strings)
291
    return s.hexdigest()
292
293
1 by mbp at sourcefrog
import from baz patch-364
294
def sha_string(f):
295
    s = sha.new()
296
    s.update(f)
297
    return s.hexdigest()
298
299
124 by mbp at sourcefrog
- check file text for past revisions is correct
300
def fingerprint_file(f):
301
    s = sha.new()
126 by mbp at sourcefrog
Use just one big read to fingerprint files
302
    b = f.read()
303
    s.update(b)
304
    size = len(b)
124 by mbp at sourcefrog
- check file text for past revisions is correct
305
    return {'size': size,
306
            'sha1': s.hexdigest()}
307
308
1 by mbp at sourcefrog
import from baz patch-364
309
def compare_files(a, b):
310
    """Returns true if equal in contents"""
74 by mbp at sourcefrog
compare_files: read in one page at a time rather than
311
    BUFSIZE = 4096
312
    while True:
313
        ai = a.read(BUFSIZE)
314
        bi = b.read(BUFSIZE)
315
        if ai != bi:
316
            return False
317
        if ai == '':
318
            return True
1 by mbp at sourcefrog
import from baz patch-364
319
320
49 by mbp at sourcefrog
fix local-time-offset calculation
321
def local_time_offset(t=None):
322
    """Return offset of local zone from GMT, either at present or at time t."""
73 by mbp at sourcefrog
fix time.localtime call for python 2.3
323
    # python2.3 localtime() can't take None
183 by mbp at sourcefrog
pychecker fixups
324
    if t == None:
73 by mbp at sourcefrog
fix time.localtime call for python 2.3
325
        t = time.time()
326
        
49 by mbp at sourcefrog
fix local-time-offset calculation
327
    if time.localtime(t).tm_isdst and time.daylight:
8 by mbp at sourcefrog
store committer's timezone in revision and show
328
        return -time.altzone
329
    else:
330
        return -time.timezone
331
332
    
1185.12.24 by Aaron Bentley
Made format_date more flexible
333
def format_date(t, offset=0, timezone='original', date_fmt=None, 
334
                show_offset=True):
1 by mbp at sourcefrog
import from baz patch-364
335
    ## TODO: Perhaps a global option to use either universal or local time?
336
    ## Or perhaps just let people set $TZ?
337
    assert isinstance(t, float)
338
    
8 by mbp at sourcefrog
store committer's timezone in revision and show
339
    if timezone == 'utc':
1 by mbp at sourcefrog
import from baz patch-364
340
        tt = time.gmtime(t)
341
        offset = 0
8 by mbp at sourcefrog
store committer's timezone in revision and show
342
    elif timezone == 'original':
23 by mbp at sourcefrog
format_date: handle revisions with no timezone offset
343
        if offset == None:
344
            offset = 0
16 by mbp at sourcefrog
fix inverted calculation for original timezone -> utc
345
        tt = time.gmtime(t + offset)
12 by mbp at sourcefrog
new --timezone option for bzr log
346
    elif timezone == 'local':
1 by mbp at sourcefrog
import from baz patch-364
347
        tt = time.localtime(t)
49 by mbp at sourcefrog
fix local-time-offset calculation
348
        offset = local_time_offset(t)
12 by mbp at sourcefrog
new --timezone option for bzr log
349
    else:
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
350
        raise BzrError("unsupported timezone format %r" % timezone,
351
                       ['options are "utc", "original", "local"'])
1185.12.24 by Aaron Bentley
Made format_date more flexible
352
    if date_fmt is None:
353
        date_fmt = "%a %Y-%m-%d %H:%M:%S"
354
    if show_offset:
355
        offset_str = ' %+03d%02d' % (offset / 3600, (offset / 60) % 60)
356
    else:
357
        offset_str = ''
358
    return (time.strftime(date_fmt, tt) +  offset_str)
1 by mbp at sourcefrog
import from baz patch-364
359
360
361
def compact_date(when):
362
    return time.strftime('%Y%m%d%H%M%S', time.gmtime(when))
363
    
364
365
366
def filesize(f):
367
    """Return size of given open file."""
368
    return os.fstat(f.fileno())[ST_SIZE]
369
1185.1.7 by Robert Collins
Nathaniel McCallums patch for urandom friendliness on aix.
370
# Define rand_bytes based on platform.
371
try:
372
    # Python 2.4 and later have os.urandom,
373
    # but it doesn't work on some arches
374
    os.urandom(1)
1 by mbp at sourcefrog
import from baz patch-364
375
    rand_bytes = os.urandom
1185.1.7 by Robert Collins
Nathaniel McCallums patch for urandom friendliness on aix.
376
except (NotImplementedError, AttributeError):
377
    # If python doesn't have os.urandom, or it doesn't work,
378
    # then try to first pull random data from /dev/urandom
379
    if os.path.exists("/dev/urandom"):
380
        rand_bytes = file('/dev/urandom', 'rb').read
381
    # Otherwise, use this hack as a last resort
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
1 by mbp at sourcefrog
import from baz patch-364
391
392
## TODO: We could later have path objects that remember their list
393
## decomposition (might be too tricksy though.)
394
395
def splitpath(p):
396
    """Turn string into list of parts.
397
398
    >>> splitpath('a')
399
    ['a']
400
    >>> splitpath('a/b')
401
    ['a', 'b']
402
    >>> splitpath('a/./b')
403
    ['a', 'b']
404
    >>> splitpath('a/.b')
405
    ['a', '.b']
406
    >>> splitpath('a/../b')
184 by mbp at sourcefrog
pychecker fixups
407
    Traceback (most recent call last):
1 by mbp at sourcefrog
import from baz patch-364
408
    ...
694 by Martin Pool
- weed out all remaining calls to bailout() and remove the function
409
    BzrError: sorry, '..' not allowed in path
1 by mbp at sourcefrog
import from baz patch-364
410
    """
411
    assert isinstance(p, types.StringTypes)
271 by Martin Pool
- Windows path fixes
412
413
    # split on either delimiter because people might use either on
414
    # Windows
415
    ps = re.split(r'[\\/]', p)
416
417
    rps = []
1 by mbp at sourcefrog
import from baz patch-364
418
    for f in ps:
419
        if f == '..':
694 by Martin Pool
- weed out all remaining calls to bailout() and remove the function
420
            raise BzrError("sorry, %r not allowed in path" % f)
271 by Martin Pool
- Windows path fixes
421
        elif (f == '.') or (f == ''):
422
            pass
423
        else:
424
            rps.append(f)
425
    return rps
1 by mbp at sourcefrog
import from baz patch-364
426
427
def joinpath(p):
428
    assert isinstance(p, list)
429
    for f in p:
183 by mbp at sourcefrog
pychecker fixups
430
        if (f == '..') or (f == None) or (f == ''):
694 by Martin Pool
- weed out all remaining calls to bailout() and remove the function
431
            raise BzrError("sorry, %r not allowed in path" % f)
1185.31.32 by John Arbash Meinel
Updated the bzr sourcecode to use bzrlib.osutils.pathjoin rather than os.path.join to enforce internal use of / instead of \
432
    return pathjoin(*p)
1 by mbp at sourcefrog
import from baz patch-364
433
434
435
def appendpath(p1, p2):
436
    if p1 == '':
437
        return p2
438
    else:
1185.31.32 by John Arbash Meinel
Updated the bzr sourcecode to use bzrlib.osutils.pathjoin rather than os.path.join to enforce internal use of / instead of \
439
        return pathjoin(p1, p2)
1 by mbp at sourcefrog
import from baz patch-364
440
    
441
1231 by Martin Pool
- more progress on fetch on top of weaves
442
def split_lines(s):
443
    """Split s into lines, but without removing the newline characters."""
444
    return StringIO(s).readlines()
1391 by Robert Collins
merge from integration
445
446
1185.10.4 by Aaron Bentley
Disabled hardlinks on cygwin, mac OS
447
def hardlinks_good():
1185.10.5 by Aaron Bentley
Fixed hardlinks_good test
448
    return sys.platform not in ('win32', 'cygwin', 'darwin')
1185.10.4 by Aaron Bentley
Disabled hardlinks on cygwin, mac OS
449
1185.1.46 by Robert Collins
Aarons branch --basis patch
450
1185.10.3 by Aaron Bentley
Made copy_multi_immutable create hardlinks opportunistically
451
def link_or_copy(src, dest):
452
    """Hardlink a file, or copy it if it can't be hardlinked."""
1185.10.4 by Aaron Bentley
Disabled hardlinks on cygwin, mac OS
453
    if not hardlinks_good():
1185.10.3 by Aaron Bentley
Made copy_multi_immutable create hardlinks opportunistically
454
        copyfile(src, dest)
455
        return
456
    try:
457
        os.link(src, dest)
458
    except (OSError, IOError), e:
459
        if e.errno != errno.EXDEV:
460
            raise
461
        copyfile(src, dest)
1399.1.4 by Robert Collins
move diff and symlink conditionals into inventory.py from diff.py
462
463
464
def has_symlinks():
465
    if hasattr(os, 'symlink'):
466
        return True
467
    else:
468
        return False
1185.16.38 by Martin Pool
- move contains_whitespace and contains_linebreaks to osutils
469
        
470
471
def contains_whitespace(s):
472
    """True if there are any whitespace characters in s."""
473
    for ch in string.whitespace:
474
        if ch in s:
475
            return True
476
    else:
477
        return False
478
479
480
def contains_linebreaks(s):
481
    """True if there is any vertical whitespace in s."""
482
    for ch in '\f\n\r':
483
        if ch in s:
484
            return True
485
    else:
486
        return False
1457.1.2 by Robert Collins
move branch._relpath into osutils as relpath
487
488
489
def relpath(base, path):
490
    """Return path relative to base, or raise exception.
491
492
    The path may be either an absolute path or a path relative to the
493
    current working directory.
494
495
    os.path.commonprefix (python2.4) has a bad bug that it works just
496
    on string prefixes, assuming that '/u' is a prefix of '/u2'.  This
497
    avoids that problem."""
1185.16.70 by Martin Pool
- improved handling of non-ascii branch names and test
498
    rp = abspath(path)
1457.1.2 by Robert Collins
move branch._relpath into osutils as relpath
499
500
    s = []
501
    head = rp
502
    while len(head) >= len(base):
503
        if head == base:
504
            break
505
        head, tail = os.path.split(head)
506
        if tail:
507
            s.insert(0, tail)
508
    else:
509
        # XXX This should raise a NotChildPath exception, as its not tied
510
        # to branch anymore.
1185.31.41 by John Arbash Meinel
Creating a PathNotChild exception, and using relpath in HTTPTestUtil
511
        raise PathNotChild(rp, base)
1457.1.2 by Robert Collins
move branch._relpath into osutils as relpath
512
1185.31.35 by John Arbash Meinel
Couple small fixes, all tests pass on cygwin.
513
    if s:
514
        return pathjoin(*s)
515
    else:
516
        return ''
1185.33.60 by Martin Pool
Use full terminal width for verbose test output.
517
518
519
def terminal_width():
520
    """Return estimated terminal width."""
521
522
    # TODO: Do something smart on Windows?
523
524
    # TODO: Is there anything that gets a better update when the window
525
    # is resized while the program is running? We could use the Python termcap
526
    # library.
527
    try:
528
        return int(os.environ['COLUMNS'])
529
    except (IndexError, KeyError, ValueError):
530
        return 80