~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Martin Pool
  • Date: 2010-06-02 05:03:31 UTC
  • mto: This revision was merged to the branch mainline in revision 5279.
  • Revision ID: mbp@canonical.com-20100602050331-n2p1qt8hfsahspnv
Correct more sloppy use of the term 'Linux'

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
 
1
# Copyright (C) 2005-2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
18
18
import os
19
19
import re
20
20
import stat
 
21
from stat import S_ISREG, S_ISDIR, S_ISLNK, ST_MODE, ST_SIZE
21
22
import sys
22
23
import time
23
24
import codecs
26
27
lazy_import(globals(), """
27
28
from datetime import datetime
28
29
import getpass
29
 
import ntpath
 
30
from ntpath import (abspath as _nt_abspath,
 
31
                    join as _nt_join,
 
32
                    normpath as _nt_normpath,
 
33
                    realpath as _nt_realpath,
 
34
                    splitdrive as _nt_splitdrive,
 
35
                    )
30
36
import posixpath
31
 
# We need to import both shutil and rmtree as we export the later on posix
32
 
# and need the former on windows
33
37
import shutil
34
 
from shutil import rmtree
 
38
from shutil import (
 
39
    rmtree,
 
40
    )
35
41
import socket
36
42
import subprocess
37
 
# We need to import both tempfile and mkdtemp as we export the later on posix
38
 
# and need the former on windows
39
43
import tempfile
40
 
from tempfile import mkdtemp
 
44
from tempfile import (
 
45
    mkdtemp,
 
46
    )
41
47
import unicodedata
42
48
 
43
49
from bzrlib import (
298
304
    running python.exe under cmd.exe return capital C:\\
299
305
    running win32 python inside a cygwin shell returns lowercase c:\\
300
306
    """
301
 
    drive, path = ntpath.splitdrive(path)
 
307
    drive, path = _nt_splitdrive(path)
302
308
    return drive.upper() + path
303
309
 
304
310
 
305
311
def _win32_abspath(path):
306
 
    # Real ntpath.abspath doesn't have a problem with a unicode cwd
307
 
    return _win32_fixdrive(ntpath.abspath(unicode(path)).replace('\\', '/'))
 
312
    # Real _nt_abspath doesn't have a problem with a unicode cwd
 
313
    return _win32_fixdrive(_nt_abspath(unicode(path)).replace('\\', '/'))
308
314
 
309
315
 
310
316
def _win98_abspath(path):
321
327
    #   /path       => C:/path
322
328
    path = unicode(path)
323
329
    # check for absolute path
324
 
    drive = ntpath.splitdrive(path)[0]
 
330
    drive = _nt_splitdrive(path)[0]
325
331
    if drive == '' and path[:2] not in('//','\\\\'):
326
332
        cwd = os.getcwdu()
327
333
        # we cannot simply os.path.join cwd and path
328
334
        # because os.path.join('C:','/path') produce '/path'
329
335
        # and this is incorrect
330
336
        if path[:1] in ('/','\\'):
331
 
            cwd = ntpath.splitdrive(cwd)[0]
 
337
            cwd = _nt_splitdrive(cwd)[0]
332
338
            path = path[1:]
333
339
        path = cwd + '\\' + path
334
 
    return _win32_fixdrive(ntpath.normpath(path).replace('\\', '/'))
 
340
    return _win32_fixdrive(_nt_normpath(path).replace('\\', '/'))
335
341
 
336
342
 
337
343
def _win32_realpath(path):
338
 
    # Real ntpath.realpath doesn't have a problem with a unicode cwd
339
 
    return _win32_fixdrive(ntpath.realpath(unicode(path)).replace('\\', '/'))
 
344
    # Real _nt_realpath doesn't have a problem with a unicode cwd
 
345
    return _win32_fixdrive(_nt_realpath(unicode(path)).replace('\\', '/'))
340
346
 
341
347
 
342
348
def _win32_pathjoin(*args):
343
 
    return ntpath.join(*args).replace('\\', '/')
 
349
    return _nt_join(*args).replace('\\', '/')
344
350
 
345
351
 
346
352
def _win32_normpath(path):
347
 
    return _win32_fixdrive(ntpath.normpath(unicode(path)).replace('\\', '/'))
 
353
    return _win32_fixdrive(_nt_normpath(unicode(path)).replace('\\', '/'))
348
354
 
349
355
 
350
356
def _win32_getcwd():
389
395
basename = os.path.basename
390
396
split = os.path.split
391
397
splitext = os.path.splitext
392
 
# These were already lazily imported into local scope
 
398
# These were already imported into local scope
393
399
# mkdtemp = tempfile.mkdtemp
394
400
# rmtree = shutil.rmtree
395
401
 
435
441
    getcwd = _mac_getcwd
436
442
 
437
443
 
438
 
def get_terminal_encoding(trace=False):
 
444
def get_terminal_encoding():
439
445
    """Find the best encoding for printing to the screen.
440
446
 
441
447
    This attempts to check both sys.stdout and sys.stdin to see
447
453
 
448
454
    On my standard US Windows XP, the preferred encoding is
449
455
    cp1252, but the console is cp437
450
 
 
451
 
    :param trace: If True trace the selected encoding via mutter().
452
456
    """
453
457
    from bzrlib.trace import mutter
454
458
    output_encoding = getattr(sys.stdout, 'encoding', None)
456
460
        input_encoding = getattr(sys.stdin, 'encoding', None)
457
461
        if not input_encoding:
458
462
            output_encoding = get_user_encoding()
459
 
            if trace:
460
 
                mutter('encoding stdout as osutils.get_user_encoding() %r',
 
463
            mutter('encoding stdout as osutils.get_user_encoding() %r',
461
464
                   output_encoding)
462
465
        else:
463
466
            output_encoding = input_encoding
464
 
            if trace:
465
 
                mutter('encoding stdout as sys.stdin encoding %r',
466
 
                    output_encoding)
 
467
            mutter('encoding stdout as sys.stdin encoding %r', output_encoding)
467
468
    else:
468
 
        if trace:
469
 
            mutter('encoding stdout as sys.stdout encoding %r', output_encoding)
 
469
        mutter('encoding stdout as sys.stdout encoding %r', output_encoding)
470
470
    if output_encoding == 'cp0':
471
471
        # invalid encoding (cp0 means 'no codepage' on Windows)
472
472
        output_encoding = get_user_encoding()
473
 
        if trace:
474
 
            mutter('cp0 is invalid encoding.'
 
473
        mutter('cp0 is invalid encoding.'
475
474
               ' encoding stdout as osutils.get_user_encoding() %r',
476
475
               output_encoding)
477
476
    # check encoding
503
502
def isdir(f):
504
503
    """True if f is an accessible directory."""
505
504
    try:
506
 
        return stat.S_ISDIR(os.lstat(f)[stat.ST_MODE])
 
505
        return S_ISDIR(os.lstat(f)[ST_MODE])
507
506
    except OSError:
508
507
        return False
509
508
 
511
510
def isfile(f):
512
511
    """True if f is a regular file."""
513
512
    try:
514
 
        return stat.S_ISREG(os.lstat(f)[stat.ST_MODE])
 
513
        return S_ISREG(os.lstat(f)[ST_MODE])
515
514
    except OSError:
516
515
        return False
517
516
 
518
517
def islink(f):
519
518
    """True if f is a symlink."""
520
519
    try:
521
 
        return stat.S_ISLNK(os.lstat(f)[stat.ST_MODE])
 
520
        return S_ISLNK(os.lstat(f)[ST_MODE])
522
521
    except OSError:
523
522
        return False
524
523
 
864
863
 
865
864
def filesize(f):
866
865
    """Return size of given open file."""
867
 
    return os.fstat(f.fileno())[stat.ST_SIZE]
 
866
    return os.fstat(f.fileno())[ST_SIZE]
868
867
 
869
868
 
870
869
# Define rand_bytes based on platform.
932
931
 
933
932
def parent_directories(filename):
934
933
    """Return the list of parent directories, deepest first.
935
 
 
 
934
    
936
935
    For example, parent_directories("a/b/c") -> ["a/b", "a"].
937
936
    """
938
937
    parents = []
962
961
    # NB: This docstring is just an example, not a doctest, because doctest
963
962
    # currently can't cope with the use of lazy imports in this namespace --
964
963
    # mbp 20090729
965
 
 
 
964
    
966
965
    # This currently doesn't report the failure at the time it occurs, because
967
966
    # they tend to happen very early in startup when we can't check config
968
967
    # files etc, and also we want to report all failures but not spam the user
969
968
    # with 10 warnings.
 
969
    from bzrlib import trace
970
970
    exception_str = str(exception)
971
971
    if exception_str not in _extension_load_failures:
972
972
        trace.mutter("failed to load compiled extension: %s" % exception_str)
1037
1037
 
1038
1038
 
1039
1039
def delete_any(path):
1040
 
    """Delete a file, symlink or directory.
1041
 
 
 
1040
    """Delete a file, symlink or directory.  
 
1041
    
1042
1042
    Will delete even if readonly.
1043
1043
    """
1044
1044
    try:
1233
1233
    # but for now, we haven't optimized...
1234
1234
    return [canonical_relpath(base, p) for p in paths]
1235
1235
 
1236
 
 
1237
 
def decode_filename(filename):
1238
 
    """Decode the filename using the filesystem encoding
1239
 
 
1240
 
    If it is unicode, it is returned.
1241
 
    Otherwise it is decoded from the the filesystem's encoding. If decoding
1242
 
    fails, a errors.BadFilenameEncoding exception is raised.
1243
 
    """
1244
 
    if type(filename) is unicode:
1245
 
        return filename
1246
 
    try:
1247
 
        return filename.decode(_fs_enc)
1248
 
    except UnicodeDecodeError:
1249
 
        raise errors.BadFilenameEncoding(filename, _fs_enc)
1250
 
 
1251
 
 
1252
1236
def safe_unicode(unicode_or_utf8_string):
1253
1237
    """Coerce unicode_or_utf8_string into unicode.
1254
1238
 
1337
1321
def normalizes_filenames():
1338
1322
    """Return True if this platform normalizes unicode filenames.
1339
1323
 
1340
 
    Only Mac OSX.
 
1324
    Mac OSX does, Windows/Linux do not.
1341
1325
    """
1342
1326
    return _platform_normalizes_filenames
1343
1327
 
1348
1332
    On platforms where the system normalizes filenames (Mac OSX),
1349
1333
    you can access a file by any path which will normalize correctly.
1350
1334
    On platforms where the system does not normalize filenames
1351
 
    (everything else), you have to access a file by its exact path.
 
1335
    (Windows, Linux), you have to access a file by its exact path.
1352
1336
 
1353
1337
    Internally, bzr only supports NFC normalization, since that is
1354
1338
    the standard for XML documents.
1461
1445
    # a similar effect.
1462
1446
 
1463
1447
    # If BZR_COLUMNS is set, take it, user is always right
1464
 
    # Except if they specified 0 in which case, impose no limit here
1465
1448
    try:
1466
 
        width = int(os.environ['BZR_COLUMNS'])
 
1449
        return int(os.environ['BZR_COLUMNS'])
1467
1450
    except (KeyError, ValueError):
1468
 
        width = None
1469
 
    if width is not None:
1470
 
        if width > 0:
1471
 
            return width
1472
 
        else:
1473
 
            return None
 
1451
        pass
1474
1452
 
1475
1453
    isatty = getattr(sys.stdout, 'isatty', None)
1476
1454
    if isatty is None or not isatty():
1666
1644
        dirblock = []
1667
1645
        append = dirblock.append
1668
1646
        try:
1669
 
            names = sorted(map(decode_filename, _listdir(top)))
 
1647
            names = sorted(_listdir(top))
1670
1648
        except OSError, e:
1671
1649
            if not _is_error_enotdir(e):
1672
1650
                raise
1880
1858
        s = os.stat(src)
1881
1859
        chown(dst, s.st_uid, s.st_gid)
1882
1860
    except OSError, e:
1883
 
        trace.warning(
1884
 
            'Unable to copy ownership from "%s" to "%s". '
1885
 
            'You may want to set it manually.', src, dst)
1886
 
        trace.log_exception_quietly()
 
1861
        trace.warning("Unable to copy ownership from '%s' to '%s': IOError: %s." % (src, dst, e))
1887
1862
 
1888
1863
 
1889
1864
def path_prefix_key(path):
1977
1952
    return user_encoding
1978
1953
 
1979
1954
 
1980
 
def get_diff_header_encoding():
1981
 
    return get_terminal_encoding()
1982
 
 
1983
 
 
1984
1955
def get_host_name():
1985
1956
    """Return the current unicode host name.
1986
1957
 
2001
1972
# data at once.
2002
1973
MAX_SOCKET_CHUNK = 64 * 1024
2003
1974
 
2004
 
_end_of_stream_errors = [errno.ECONNRESET]
2005
 
for _eno in ['WSAECONNRESET', 'WSAECONNABORTED']:
2006
 
    _eno = getattr(errno, _eno, None)
2007
 
    if _eno is not None:
2008
 
        _end_of_stream_errors.append(_eno)
2009
 
del _eno
2010
 
 
2011
 
 
2012
1975
def read_bytes_from_socket(sock, report_activity=None,
2013
1976
        max_read_size=MAX_SOCKET_CHUNK):
2014
1977
    """Read up to max_read_size of bytes from sock and notify of progress.
2022
1985
            bytes = sock.recv(max_read_size)
2023
1986
        except socket.error, e:
2024
1987
            eno = e.args[0]
2025
 
            if eno in _end_of_stream_errors:
 
1988
            if eno == getattr(errno, "WSAECONNRESET", errno.ECONNRESET):
2026
1989
                # The connection was closed by the other side.  Callers expect
2027
1990
                # an empty string to signal end-of-stream.
2028
1991
                return ""
2057
2020
 
2058
2021
def send_all(sock, bytes, report_activity=None):
2059
2022
    """Send all bytes on a socket.
2060
 
 
 
2023
 
2061
2024
    Breaks large blocks in smaller chunks to avoid buffering limitations on
2062
2025
    some platforms, and catches EINTR which may be thrown if the send is
2063
2026
    interrupted by a signal.
2064
2027
 
2065
2028
    This is preferred to socket.sendall(), because it avoids portability bugs
2066
2029
    and provides activity reporting.
2067
 
 
 
2030
 
2068
2031
    :param report_activity: Call this as bytes are read, see
2069
2032
        Transport._report_activity
2070
2033
    """
2081
2044
            report_activity(sent, 'write')
2082
2045
 
2083
2046
 
2084
 
def connect_socket(address):
2085
 
    # Slight variation of the socket.create_connection() function (provided by
2086
 
    # python-2.6) that can fail if getaddrinfo returns an empty list. We also
2087
 
    # provide it for previous python versions. Also, we don't use the timeout
2088
 
    # parameter (provided by the python implementation) so we don't implement
2089
 
    # it either).
2090
 
    err = socket.error('getaddrinfo returns an empty list')
2091
 
    host, port = address
2092
 
    for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
2093
 
        af, socktype, proto, canonname, sa = res
2094
 
        sock = None
2095
 
        try:
2096
 
            sock = socket.socket(af, socktype, proto)
2097
 
            sock.connect(sa)
2098
 
            return sock
2099
 
 
2100
 
        except socket.error, err:
2101
 
            # 'err' is now the most recent error
2102
 
            if sock is not None:
2103
 
                sock.close()
2104
 
    raise err
2105
 
 
2106
 
 
2107
2047
def dereference_path(path):
2108
2048
    """Determine the real path to a file.
2109
2049
 
2181
2121
 
2182
2122
def until_no_eintr(f, *a, **kw):
2183
2123
    """Run f(*a, **kw), retrying if an EINTR error occurs.
2184
 
 
 
2124
    
2185
2125
    WARNING: you must be certain that it is safe to retry the call repeatedly
2186
2126
    if EINTR does occur.  This is typically only true for low-level operations
2187
2127
    like os.read.  If in any doubt, don't use this.
2202
2142
            raise
2203
2143
 
2204
2144
 
2205
 
@deprecated_function(deprecated_in((2, 2, 0)))
2206
2145
def re_compile_checked(re_string, flags=0, where=""):
2207
2146
    """Return a compiled re, or raise a sensible error.
2208
2147
 
2218
2157
        re_obj = re.compile(re_string, flags)
2219
2158
        re_obj.search("")
2220
2159
        return re_obj
2221
 
    except errors.InvalidPattern, e:
 
2160
    except re.error, e:
2222
2161
        if where:
2223
2162
            where = ' in ' + where
2224
2163
        # despite the name 'error' is a type
2225
 
        raise errors.BzrCommandError('Invalid regular expression%s: %s'
2226
 
            % (where, e.msg))
 
2164
        raise errors.BzrCommandError('Invalid regular expression%s: %r: %s'
 
2165
            % (where, re_string, e))
2227
2166
 
2228
2167
 
2229
2168
if sys.platform == "win32":
2319
2258
if sys.platform == 'win32':
2320
2259
    def open_file(filename, mode='r', bufsize=-1):
2321
2260
        """This function is used to override the ``open`` builtin.
2322
 
 
 
2261
        
2323
2262
        But it uses O_NOINHERIT flag so the file handle is not inherited by
2324
2263
        child processes.  Deleting or renaming a closed file opened with this
2325
2264
        function is not blocking child processes.
2370
2309
        raise errors.BzrError("Can't decode username as %s." % \
2371
2310
                user_encoding)
2372
2311
    return username
2373
 
 
2374
 
 
2375
 
def available_backup_name(base, exists):
2376
 
    """Find a non-existing backup file name.
2377
 
 
2378
 
    This will *not* create anything, this only return a 'free' entry.  This
2379
 
    should be used for checking names in a directory below a locked
2380
 
    tree/branch/repo to avoid race conditions. This is LBYL (Look Before You
2381
 
    Leap) and generally discouraged.
2382
 
 
2383
 
    :param base: The base name.
2384
 
 
2385
 
    :param exists: A callable returning True if the path parameter exists.
2386
 
    """
2387
 
    counter = 1
2388
 
    name = "%s.~%d~" % (base, counter)
2389
 
    while exists(name):
2390
 
        counter += 1
2391
 
        name = "%s.~%d~" % (base, counter)
2392
 
    return name
2393
 
 
2394
 
 
2395
 
def set_fd_cloexec(fd):
2396
 
    """Set a Unix file descriptor's FD_CLOEXEC flag.  Do nothing if platform
2397
 
    support for this is not available.
2398
 
    """
2399
 
    try:
2400
 
        import fcntl
2401
 
        old = fcntl.fcntl(fd, fcntl.F_GETFD)
2402
 
        fcntl.fcntl(fd, fcntl.F_SETFD, old | fcntl.FD_CLOEXEC)
2403
 
    except (ImportError, AttributeError):
2404
 
        # Either the fcntl module or specific constants are not present
2405
 
        pass
2406
 
 
2407
 
 
2408
 
def find_executable_on_path(name):
2409
 
    """Finds an executable on the PATH.
2410
 
    
2411
 
    On Windows, this will try to append each extension in the PATHEXT
2412
 
    environment variable to the name, if it cannot be found with the name
2413
 
    as given.
2414
 
    
2415
 
    :param name: The base name of the executable.
2416
 
    :return: The path to the executable found or None.
2417
 
    """
2418
 
    path = os.environ.get('PATH')
2419
 
    if path is None:
2420
 
        return None
2421
 
    path = path.split(os.pathsep)
2422
 
    if sys.platform == 'win32':
2423
 
        exts = os.environ.get('PATHEXT', '').split(os.pathsep)
2424
 
        exts = [ext.lower() for ext in exts]
2425
 
        base, ext = os.path.splitext(name)
2426
 
        if ext != '':
2427
 
            if ext.lower() not in exts:
2428
 
                return None
2429
 
            name = base
2430
 
            exts = [ext]
2431
 
    else:
2432
 
        exts = ['']
2433
 
    for ext in exts:
2434
 
        for d in path:
2435
 
            f = os.path.join(d, name) + ext
2436
 
            if os.access(f, os.X_OK):
2437
 
                return f
2438
 
    return None