/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
1185.85.75 by John Arbash Meinel
Adding bzrlib.osutils.unicode_filename to handle unicode normalization for file paths.
32
import unicodedata
1 by mbp at sourcefrog
import from baz patch-364
33
1185.1.41 by Robert Collins
massive patch from Alexander Belchenko - many PEP8 fixes, removes unused function uuid
34
import bzrlib
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
35
from bzrlib.errors import (BzrError,
1185.65.29 by Robert Collins
Implement final review suggestions.
36
                           BzrBadParameterNotUnicode,
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
37
                           NoSuchFile,
38
                           PathNotChild,
39
                           )
694 by Martin Pool
- weed out all remaining calls to bailout() and remove the function
40
from bzrlib.trace import mutter
1185.1.41 by Robert Collins
massive patch from Alexander Belchenko - many PEP8 fixes, removes unused function uuid
41
1 by mbp at sourcefrog
import from baz patch-364
42
43
def make_readonly(filename):
44
    """Make a filename read-only."""
45
    mod = os.stat(filename).st_mode
46
    mod = mod & 0777555
47
    os.chmod(filename, mod)
48
49
50
def make_writable(filename):
51
    mod = os.stat(filename).st_mode
52
    mod = mod | 0200
53
    os.chmod(filename, mod)
54
55
1077 by Martin Pool
- avoid compiling REs at module load time
56
_QUOTE_RE = None
969 by Martin Pool
- Add less-sucky is_within_any
57
58
1 by mbp at sourcefrog
import from baz patch-364
59
def quotefn(f):
779 by Martin Pool
- better quotefn for windows: use doublequotes for strings with
60
    """Return a quoted filename filename
61
62
    This previously used backslash quoting, but that works poorly on
63
    Windows."""
64
    # TODO: I'm not really sure this is the best format either.x
1077 by Martin Pool
- avoid compiling REs at module load time
65
    global _QUOTE_RE
66
    if _QUOTE_RE == None:
1185.1.41 by Robert Collins
massive patch from Alexander Belchenko - many PEP8 fixes, removes unused function uuid
67
        _QUOTE_RE = re.compile(r'([^a-zA-Z0-9.,:/\\_~-])')
1077 by Martin Pool
- avoid compiling REs at module load time
68
        
779 by Martin Pool
- better quotefn for windows: use doublequotes for strings with
69
    if _QUOTE_RE.search(f):
70
        return '"' + f + '"'
71
    else:
72
        return f
1 by mbp at sourcefrog
import from baz patch-364
73
74
75
def file_kind(f):
76
    mode = os.lstat(f)[ST_MODE]
77
    if S_ISREG(mode):
78
        return 'file'
79
    elif S_ISDIR(mode):
80
        return 'directory'
20 by mbp at sourcefrog
don't abort on trees that happen to contain symlinks
81
    elif S_ISLNK(mode):
82
        return 'symlink'
1185.3.28 by John Arbash Meinel
Adding knowledge about fifo/block/etc, they will be unknown/ignored.
83
    elif S_ISCHR(mode):
84
        return 'chardev'
85
    elif S_ISBLK(mode):
86
        return 'block'
87
    elif S_ISFIFO(mode):
88
        return 'fifo'
89
    elif S_ISSOCK(mode):
90
        return 'socket'
1 by mbp at sourcefrog
import from baz patch-364
91
    else:
1185.3.28 by John Arbash Meinel
Adding knowledge about fifo/block/etc, they will be unknown/ignored.
92
        return 'unknown'
488 by Martin Pool
- new helper function kind_marker()
93
94
95
def kind_marker(kind):
96
    if kind == 'file':
97
        return ''
98
    elif kind == 'directory':
99
        return '/'
100
    elif kind == 'symlink':
101
        return '@'
102
    else:
103
        raise BzrError('invalid file kind %r' % kind)
1 by mbp at sourcefrog
import from baz patch-364
104
1092.2.6 by Robert Collins
symlink support updated to work
105
def lexists(f):
1185.31.33 by John Arbash Meinel
A couple more path.join statements needed changing.
106
    if hasattr(os.path, 'lexists'):
107
        return os.path.lexists(f)
1092.2.6 by Robert Collins
symlink support updated to work
108
    try:
109
        if hasattr(os, 'lstat'):
110
            os.lstat(f)
111
        else:
112
            os.stat(f)
113
        return True
114
    except OSError,e:
115
        if e.errno == errno.ENOENT:
116
            return False;
117
        else:
118
            raise BzrError("lstat/stat of (%r): %r" % (f, e))
1 by mbp at sourcefrog
import from baz patch-364
119
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
120
def fancy_rename(old, new, rename_func, unlink_func):
121
    """A fancy rename, when you don't have atomic rename.
122
    
123
    :param old: The old path, to rename from
124
    :param new: The new path, to rename to
125
    :param rename_func: The potentially non-atomic rename function
126
    :param unlink_func: A way to delete the target file if the full rename succeeds
127
    """
128
129
    # sftp rename doesn't allow overwriting, so play tricks:
130
    import random
131
    base = os.path.basename(new)
132
    dirname = os.path.dirname(new)
133
    tmp_name = u'tmp.%s.%.9f.%d.%d' % (base, time.time(), os.getpid(), random.randint(0, 0x7FFFFFFF))
134
    tmp_name = pathjoin(dirname, tmp_name)
135
136
    # Rename the file out of the way, but keep track if it didn't exist
137
    # We don't want to grab just any exception
138
    # something like EACCES should prevent us from continuing
139
    # The downside is that the rename_func has to throw an exception
140
    # with an errno = ENOENT, or NoSuchFile
141
    file_existed = False
142
    try:
143
        rename_func(new, tmp_name)
144
    except (NoSuchFile,), e:
145
        pass
1532 by Robert Collins
Merge in John Meinels integration branch.
146
    except IOError, e:
147
        # RBC 20060103 abstraction leakage: the paramiko SFTP clients rename
148
        # function raises an IOError with errno == None when a rename fails.
149
        # This then gets caught here.
1185.50.37 by John Arbash Meinel
Fixed exception handling for fancy_rename
150
        if e.errno not in (None, errno.ENOENT, errno.ENOTDIR):
1532 by Robert Collins
Merge in John Meinels integration branch.
151
            raise
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
152
    except Exception, e:
153
        if (not hasattr(e, 'errno') 
154
            or e.errno not in (errno.ENOENT, errno.ENOTDIR)):
155
            raise
156
    else:
157
        file_existed = True
158
159
    success = False
160
    try:
161
        # This may throw an exception, in which case success will
162
        # not be set.
163
        rename_func(old, new)
164
        success = True
165
    finally:
166
        if file_existed:
167
            # If the file used to exist, rename it back into place
168
            # otherwise just delete it from the tmp location
169
            if success:
170
                unlink_func(tmp_name)
171
            else:
1185.31.49 by John Arbash Meinel
Some corrections using the new osutils.rename. **ALL TESTS PASS**
172
                rename_func(tmp_name, new)
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
173
174
# Default is to just use the python builtins
175
abspath = os.path.abspath
176
realpath = os.path.realpath
177
pathjoin = os.path.join
178
normpath = os.path.normpath
179
getcwd = os.getcwdu
180
mkdtemp = tempfile.mkdtemp
181
rename = os.rename
182
dirname = os.path.dirname
183
basename = os.path.basename
184
1185.16.70 by Martin Pool
- improved handling of non-ascii branch names and test
185
if os.name == "posix":
186
    # In Python 2.4.2 and older, os.path.abspath and os.path.realpath
187
    # choke on a Unicode string containing a relative path if
188
    # os.getcwd() returns a non-sys.getdefaultencoding()-encoded
189
    # string.
190
    _fs_enc = sys.getfilesystemencoding()
191
    def abspath(path):
192
        return os.path.abspath(path.encode(_fs_enc)).decode(_fs_enc)
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
193
1185.16.70 by Martin Pool
- improved handling of non-ascii branch names and test
194
    def realpath(path):
195
        return os.path.realpath(path.encode(_fs_enc)).decode(_fs_enc)
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
196
197
if sys.platform == 'win32':
1185.16.70 by Martin Pool
- improved handling of non-ascii branch names and test
198
    # We need to use the Unicode-aware os.path.abspath and
199
    # 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 \
200
    def abspath(path):
201
        return os.path.abspath(path).replace('\\', '/')
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
202
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 \
203
    def realpath(path):
204
        return os.path.realpath(path).replace('\\', '/')
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
205
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 \
206
    def pathjoin(*args):
207
        return os.path.join(*args).replace('\\', '/')
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
208
1185.31.38 by John Arbash Meinel
Changing os.path.normpath to osutils.normpath
209
    def normpath(path):
210
        return os.path.normpath(path).replace('\\', '/')
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
211
1185.31.39 by John Arbash Meinel
Replacing os.getcwdu() with osutils.getcwd(),
212
    def getcwd():
213
        return os.getcwdu().replace('\\', '/')
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
214
1185.31.40 by John Arbash Meinel
Added osutils.mkdtemp()
215
    def mkdtemp(*args, **kwargs):
216
        return tempfile.mkdtemp(*args, **kwargs).replace('\\', '/')
1185.31.47 by John Arbash Meinel
Added a fancy footwork rename to osutils, made SftpTransport use it.
217
218
    def rename(old, new):
219
        fancy_rename(old, new, rename_func=os.rename, unlink_func=os.unlink)
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 \
220
1532 by Robert Collins
Merge in John Meinels integration branch.
221
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 \
222
def normalizepath(f):
223
    if hasattr(os.path, 'realpath'):
224
        F = realpath
225
    else:
226
        F = abspath
227
    [p,e] = os.path.split(f)
228
    if e == "" or e == "." or e == "..":
229
        return F(f)
230
    else:
231
        return pathjoin(F(p), e)
232
1 by mbp at sourcefrog
import from baz patch-364
233
779 by Martin Pool
- better quotefn for windows: use doublequotes for strings with
234
def backup_file(fn):
235
    """Copy a file to a backup.
236
237
    Backups are named in GNU-style, with a ~ suffix.
238
239
    If the file is already a backup, it's not copied.
240
    """
241
    if fn[-1] == '~':
242
        return
243
    bfn = fn + '~'
244
1448 by Robert Collins
revert symlinks correctly
245
    if has_symlinks() and os.path.islink(fn):
246
        target = os.readlink(fn)
247
        os.symlink(target, bfn)
248
        return
779 by Martin Pool
- better quotefn for windows: use doublequotes for strings with
249
    inf = file(fn, 'rb')
250
    try:
251
        content = inf.read()
252
    finally:
253
        inf.close()
254
    
255
    outf = file(bfn, 'wb')
256
    try:
257
        outf.write(content)
258
    finally:
259
        outf.close()
260
261
1 by mbp at sourcefrog
import from baz patch-364
262
def isdir(f):
263
    """True if f is an accessible directory."""
264
    try:
265
        return S_ISDIR(os.lstat(f)[ST_MODE])
266
    except OSError:
267
        return False
268
269
270
def isfile(f):
271
    """True if f is a regular file."""
272
    try:
273
        return S_ISREG(os.lstat(f)[ST_MODE])
274
    except OSError:
275
        return False
276
1092.2.6 by Robert Collins
symlink support updated to work
277
def islink(f):
278
    """True if f is a symlink."""
279
    try:
280
        return S_ISLNK(os.lstat(f)[ST_MODE])
281
    except OSError:
282
        return False
1 by mbp at sourcefrog
import from baz patch-364
283
485 by Martin Pool
- move commit code into its own module
284
def is_inside(dir, fname):
285
    """True if fname is inside dir.
969 by Martin Pool
- Add less-sucky is_within_any
286
    
1185.31.38 by John Arbash Meinel
Changing os.path.normpath to osutils.normpath
287
    The parameters should typically be passed to osutils.normpath first, so
969 by Martin Pool
- Add less-sucky is_within_any
288
    that . and .. and repeated slashes are eliminated, and the separators
289
    are canonical for the platform.
290
    
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
291
    The empty string as a dir name is taken as top-of-tree and matches 
292
    everything.
293
    
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 \
294
    >>> is_inside('src', pathjoin('src', 'foo.c'))
969 by Martin Pool
- Add less-sucky is_within_any
295
    True
296
    >>> is_inside('src', 'srccontrol')
297
    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 \
298
    >>> is_inside('src', pathjoin('src', 'a', 'a', 'a', 'foo.c'))
969 by Martin Pool
- Add less-sucky is_within_any
299
    True
300
    >>> is_inside('foo.c', 'foo.c')
301
    True
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
302
    >>> is_inside('foo.c', '')
303
    False
304
    >>> is_inside('', 'foo.c')
305
    True
485 by Martin Pool
- move commit code into its own module
306
    """
969 by Martin Pool
- Add less-sucky is_within_any
307
    # XXX: Most callers of this can actually do something smarter by 
308
    # looking at the inventory
972 by Martin Pool
- less dodgy is_inside function
309
    if dir == fname:
310
        return True
311
    
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
312
    if dir == '':
313
        return True
1185.1.41 by Robert Collins
massive patch from Alexander Belchenko - many PEP8 fixes, removes unused function uuid
314
1185.31.34 by John Arbash Meinel
Removing instances of os.sep
315
    if dir[-1] != '/':
316
        dir += '/'
1185.1.41 by Robert Collins
massive patch from Alexander Belchenko - many PEP8 fixes, removes unused function uuid
317
972 by Martin Pool
- less dodgy is_inside function
318
    return fname.startswith(dir)
319
485 by Martin Pool
- move commit code into its own module
320
321
def is_inside_any(dir_list, fname):
322
    """True if fname is inside any of given dirs."""
323
    for dirname in dir_list:
324
        if is_inside(dirname, fname):
325
            return True
326
    else:
327
        return False
328
329
1 by mbp at sourcefrog
import from baz patch-364
330
def pumpfile(fromfile, tofile):
331
    """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.
332
    BUFSIZE = 32768
333
    while True:
334
        b = fromfile.read(BUFSIZE)
335
        if not b:
336
            break
1185.49.13 by John Arbash Meinel
Removed delayed setup, since it broke some tests. Fixed other small bugs. All tests pass.
337
        tofile.write(b)
1 by mbp at sourcefrog
import from baz patch-364
338
339
1185.67.7 by Aaron Bentley
Refactored a bit
340
def file_iterator(input_file, readsize=32768):
341
    while True:
342
        b = input_file.read(readsize)
343
        if len(b) == 0:
344
            break
345
        yield b
346
347
1 by mbp at sourcefrog
import from baz patch-364
348
def sha_file(f):
349
    if hasattr(f, 'tell'):
350
        assert f.tell() == 0
351
    s = sha.new()
320 by Martin Pool
- Compute SHA-1 of files in chunks
352
    BUFSIZE = 128<<10
353
    while True:
354
        b = f.read(BUFSIZE)
355
        if not b:
356
            break
357
        s.update(b)
1 by mbp at sourcefrog
import from baz patch-364
358
    return s.hexdigest()
359
360
1235 by Martin Pool
- split sha_strings into osutils
361
362
def sha_strings(strings):
363
    """Return the sha-1 of concatenation of strings"""
364
    s = sha.new()
365
    map(s.update, strings)
366
    return s.hexdigest()
367
368
1 by mbp at sourcefrog
import from baz patch-364
369
def sha_string(f):
370
    s = sha.new()
371
    s.update(f)
372
    return s.hexdigest()
373
374
124 by mbp at sourcefrog
- check file text for past revisions is correct
375
def fingerprint_file(f):
376
    s = sha.new()
126 by mbp at sourcefrog
Use just one big read to fingerprint files
377
    b = f.read()
378
    s.update(b)
379
    size = len(b)
124 by mbp at sourcefrog
- check file text for past revisions is correct
380
    return {'size': size,
381
            'sha1': s.hexdigest()}
382
383
1 by mbp at sourcefrog
import from baz patch-364
384
def compare_files(a, b):
385
    """Returns true if equal in contents"""
74 by mbp at sourcefrog
compare_files: read in one page at a time rather than
386
    BUFSIZE = 4096
387
    while True:
388
        ai = a.read(BUFSIZE)
389
        bi = b.read(BUFSIZE)
390
        if ai != bi:
391
            return False
392
        if ai == '':
393
            return True
1 by mbp at sourcefrog
import from baz patch-364
394
395
49 by mbp at sourcefrog
fix local-time-offset calculation
396
def local_time_offset(t=None):
397
    """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
398
    # python2.3 localtime() can't take None
183 by mbp at sourcefrog
pychecker fixups
399
    if t == None:
73 by mbp at sourcefrog
fix time.localtime call for python 2.3
400
        t = time.time()
401
        
49 by mbp at sourcefrog
fix local-time-offset calculation
402
    if time.localtime(t).tm_isdst and time.daylight:
8 by mbp at sourcefrog
store committer's timezone in revision and show
403
        return -time.altzone
404
    else:
405
        return -time.timezone
406
407
    
1185.12.24 by Aaron Bentley
Made format_date more flexible
408
def format_date(t, offset=0, timezone='original', date_fmt=None, 
409
                show_offset=True):
1 by mbp at sourcefrog
import from baz patch-364
410
    ## TODO: Perhaps a global option to use either universal or local time?
411
    ## Or perhaps just let people set $TZ?
412
    assert isinstance(t, float)
413
    
8 by mbp at sourcefrog
store committer's timezone in revision and show
414
    if timezone == 'utc':
1 by mbp at sourcefrog
import from baz patch-364
415
        tt = time.gmtime(t)
416
        offset = 0
8 by mbp at sourcefrog
store committer's timezone in revision and show
417
    elif timezone == 'original':
23 by mbp at sourcefrog
format_date: handle revisions with no timezone offset
418
        if offset == None:
419
            offset = 0
16 by mbp at sourcefrog
fix inverted calculation for original timezone -> utc
420
        tt = time.gmtime(t + offset)
12 by mbp at sourcefrog
new --timezone option for bzr log
421
    elif timezone == 'local':
1 by mbp at sourcefrog
import from baz patch-364
422
        tt = time.localtime(t)
49 by mbp at sourcefrog
fix local-time-offset calculation
423
        offset = local_time_offset(t)
12 by mbp at sourcefrog
new --timezone option for bzr log
424
    else:
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
425
        raise BzrError("unsupported timezone format %r" % timezone,
426
                       ['options are "utc", "original", "local"'])
1185.12.24 by Aaron Bentley
Made format_date more flexible
427
    if date_fmt is None:
428
        date_fmt = "%a %Y-%m-%d %H:%M:%S"
429
    if show_offset:
430
        offset_str = ' %+03d%02d' % (offset / 3600, (offset / 60) % 60)
431
    else:
432
        offset_str = ''
433
    return (time.strftime(date_fmt, tt) +  offset_str)
1 by mbp at sourcefrog
import from baz patch-364
434
435
436
def compact_date(when):
437
    return time.strftime('%Y%m%d%H%M%S', time.gmtime(when))
438
    
439
440
441
def filesize(f):
442
    """Return size of given open file."""
443
    return os.fstat(f.fileno())[ST_SIZE]
444
1185.1.7 by Robert Collins
Nathaniel McCallums patch for urandom friendliness on aix.
445
# Define rand_bytes based on platform.
446
try:
447
    # Python 2.4 and later have os.urandom,
448
    # but it doesn't work on some arches
449
    os.urandom(1)
1 by mbp at sourcefrog
import from baz patch-364
450
    rand_bytes = os.urandom
1185.1.7 by Robert Collins
Nathaniel McCallums patch for urandom friendliness on aix.
451
except (NotImplementedError, AttributeError):
452
    # If python doesn't have os.urandom, or it doesn't work,
453
    # then try to first pull random data from /dev/urandom
454
    if os.path.exists("/dev/urandom"):
455
        rand_bytes = file('/dev/urandom', 'rb').read
456
    # Otherwise, use this hack as a last resort
457
    else:
458
        # not well seeded, but better than nothing
459
        def rand_bytes(n):
460
            import random
461
            s = ''
462
            while n:
463
                s += chr(random.randint(0, 255))
464
                n -= 1
465
            return s
1 by mbp at sourcefrog
import from baz patch-364
466
467
## TODO: We could later have path objects that remember their list
468
## decomposition (might be too tricksy though.)
469
470
def splitpath(p):
471
    """Turn string into list of parts.
472
473
    >>> splitpath('a')
474
    ['a']
475
    >>> splitpath('a/b')
476
    ['a', 'b']
477
    >>> splitpath('a/./b')
478
    ['a', 'b']
479
    >>> splitpath('a/.b')
480
    ['a', '.b']
481
    >>> splitpath('a/../b')
184 by mbp at sourcefrog
pychecker fixups
482
    Traceback (most recent call last):
1 by mbp at sourcefrog
import from baz patch-364
483
    ...
694 by Martin Pool
- weed out all remaining calls to bailout() and remove the function
484
    BzrError: sorry, '..' not allowed in path
1 by mbp at sourcefrog
import from baz patch-364
485
    """
486
    assert isinstance(p, types.StringTypes)
271 by Martin Pool
- Windows path fixes
487
488
    # split on either delimiter because people might use either on
489
    # Windows
490
    ps = re.split(r'[\\/]', p)
491
492
    rps = []
1 by mbp at sourcefrog
import from baz patch-364
493
    for f in ps:
494
        if f == '..':
694 by Martin Pool
- weed out all remaining calls to bailout() and remove the function
495
            raise BzrError("sorry, %r not allowed in path" % f)
271 by Martin Pool
- Windows path fixes
496
        elif (f == '.') or (f == ''):
497
            pass
498
        else:
499
            rps.append(f)
500
    return rps
1 by mbp at sourcefrog
import from baz patch-364
501
502
def joinpath(p):
503
    assert isinstance(p, list)
504
    for f in p:
183 by mbp at sourcefrog
pychecker fixups
505
        if (f == '..') or (f == None) or (f == ''):
694 by Martin Pool
- weed out all remaining calls to bailout() and remove the function
506
            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 \
507
    return pathjoin(*p)
1 by mbp at sourcefrog
import from baz patch-364
508
509
510
def appendpath(p1, p2):
511
    if p1 == '':
512
        return p2
513
    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 \
514
        return pathjoin(p1, p2)
1 by mbp at sourcefrog
import from baz patch-364
515
    
516
1231 by Martin Pool
- more progress on fetch on top of weaves
517
def split_lines(s):
518
    """Split s into lines, but without removing the newline characters."""
519
    return StringIO(s).readlines()
1391 by Robert Collins
merge from integration
520
521
1185.10.4 by Aaron Bentley
Disabled hardlinks on cygwin, mac OS
522
def hardlinks_good():
1185.10.5 by Aaron Bentley
Fixed hardlinks_good test
523
    return sys.platform not in ('win32', 'cygwin', 'darwin')
1185.10.4 by Aaron Bentley
Disabled hardlinks on cygwin, mac OS
524
1185.1.46 by Robert Collins
Aarons branch --basis patch
525
1185.10.3 by Aaron Bentley
Made copy_multi_immutable create hardlinks opportunistically
526
def link_or_copy(src, dest):
527
    """Hardlink a file, or copy it if it can't be hardlinked."""
1185.10.4 by Aaron Bentley
Disabled hardlinks on cygwin, mac OS
528
    if not hardlinks_good():
1185.10.3 by Aaron Bentley
Made copy_multi_immutable create hardlinks opportunistically
529
        copyfile(src, dest)
530
        return
531
    try:
532
        os.link(src, dest)
533
    except (OSError, IOError), e:
534
        if e.errno != errno.EXDEV:
535
            raise
536
        copyfile(src, dest)
1399.1.4 by Robert Collins
move diff and symlink conditionals into inventory.py from diff.py
537
538
539
def has_symlinks():
540
    if hasattr(os, 'symlink'):
541
        return True
542
    else:
543
        return False
1185.16.38 by Martin Pool
- move contains_whitespace and contains_linebreaks to osutils
544
        
545
546
def contains_whitespace(s):
547
    """True if there are any whitespace characters in s."""
548
    for ch in string.whitespace:
549
        if ch in s:
550
            return True
551
    else:
552
        return False
553
554
555
def contains_linebreaks(s):
556
    """True if there is any vertical whitespace in s."""
557
    for ch in '\f\n\r':
558
        if ch in s:
559
            return True
560
    else:
561
        return False
1457.1.2 by Robert Collins
move branch._relpath into osutils as relpath
562
563
564
def relpath(base, path):
565
    """Return path relative to base, or raise exception.
566
567
    The path may be either an absolute path or a path relative to the
568
    current working directory.
569
570
    os.path.commonprefix (python2.4) has a bad bug that it works just
571
    on string prefixes, assuming that '/u' is a prefix of '/u2'.  This
572
    avoids that problem."""
1185.16.70 by Martin Pool
- improved handling of non-ascii branch names and test
573
    rp = abspath(path)
1457.1.2 by Robert Collins
move branch._relpath into osutils as relpath
574
575
    s = []
576
    head = rp
577
    while len(head) >= len(base):
578
        if head == base:
579
            break
580
        head, tail = os.path.split(head)
581
        if tail:
582
            s.insert(0, tail)
583
    else:
584
        # XXX This should raise a NotChildPath exception, as its not tied
585
        # to branch anymore.
1185.31.41 by John Arbash Meinel
Creating a PathNotChild exception, and using relpath in HTTPTestUtil
586
        raise PathNotChild(rp, base)
1457.1.2 by Robert Collins
move branch._relpath into osutils as relpath
587
1185.31.35 by John Arbash Meinel
Couple small fixes, all tests pass on cygwin.
588
    if s:
589
        return pathjoin(*s)
590
    else:
591
        return ''
1185.33.60 by Martin Pool
Use full terminal width for verbose test output.
592
593
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
594
def safe_unicode(unicode_or_utf8_string):
595
    """Coerce unicode_or_utf8_string into unicode.
596
597
    If it is unicode, it is returned.
598
    Otherwise it is decoded from utf-8. If a decoding error
599
    occurs, it is wrapped as a If the decoding fails, the exception is wrapped 
600
    as a BzrBadParameter exception.
601
    """
602
    if isinstance(unicode_or_utf8_string, unicode):
603
        return unicode_or_utf8_string
604
    try:
605
        return unicode_or_utf8_string.decode('utf8')
606
    except UnicodeDecodeError:
1185.65.29 by Robert Collins
Implement final review suggestions.
607
        raise BzrBadParameterNotUnicode(unicode_or_utf8_string)
1534.3.1 by Robert Collins
* bzrlib.osutils.safe_unicode now exists to provide parameter coercion
608
609
1185.85.75 by John Arbash Meinel
Adding bzrlib.osutils.unicode_filename to handle unicode normalization for file paths.
610
_platform_normalizes_filenames = False
611
if sys.platform == 'darwin':
612
    _platform_normalizes_filenames = True
613
614
615
def normalizes_filenames():
616
    """Return True if this platform normalizes unicode filenames.
617
618
    Mac OSX does, Windows/Linux do not.
619
    """
620
    return _platform_normalizes_filenames
621
622
623
if _platform_normalizes_filenames:
624
    def unicode_filename(path):
625
        """Make sure 'path' is a properly normalized filename.
626
627
        On platforms where the system normalizes filenames (Mac OSX),
628
        you can access a file by any path which will normalize
629
        correctly.
630
        Internally, bzr only supports NFC/NFKC normalization, since
631
        that is the standard for XML documents.
632
        So we return an normalized path, and indicate this has been
633
        properly normalized.
634
635
        :return: (path, is_normalized) Return a path which can
636
                access the file, and whether or not this path is
637
                normalized.
638
        """
639
        return unicodedata.normalize('NFKC', path), True
640
else:
641
    def unicode_filename(path):
642
        """Make sure 'path' is a properly normalized filename.
643
644
        On platforms where the system does not normalize filenames 
645
        (Windows, Linux), you have to access a file by its exact path.
646
        Internally, bzr only supports NFC/NFKC normalization, since
647
        that is the standard for XML documents.
648
        So we return the original path, and indicate if this is
649
        properly normalized.
650
651
        :return: (path, is_normalized) Return a path which can
652
                access the file, and whether or not this path is
653
                normalized.
654
        """
655
        return path, unicodedata.normalize('NFKC', path) == path
656
657
1185.33.60 by Martin Pool
Use full terminal width for verbose test output.
658
def terminal_width():
659
    """Return estimated terminal width."""
660
661
    # TODO: Do something smart on Windows?
662
663
    # TODO: Is there anything that gets a better update when the window
664
    # is resized while the program is running? We could use the Python termcap
665
    # library.
666
    try:
667
        return int(os.environ['COLUMNS'])
668
    except (IndexError, KeyError, ValueError):
669
        return 80