/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Andrew Bennetts
  • Date: 2011-02-07 04:14:29 UTC
  • mfrom: (5535.4.26 fetch-all-tags-309682)
  • mto: This revision was merged to the branch mainline in revision 5648.
  • Revision ID: andrew.bennetts@canonical.com-20110207041429-3kc1blj34rvvxod9
Merge fetch-all-tags-309682.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005-2011 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
1461
1461
    # a similar effect.
1462
1462
 
1463
1463
    # If BZR_COLUMNS is set, take it, user is always right
 
1464
    # Except if they specified 0 in which case, impose no limit here
1464
1465
    try:
1465
 
        return int(os.environ['BZR_COLUMNS'])
 
1466
        width = int(os.environ['BZR_COLUMNS'])
1466
1467
    except (KeyError, ValueError):
1467
 
        pass
 
1468
        width = None
 
1469
    if width is not None:
 
1470
        if width > 0:
 
1471
            return width
 
1472
        else:
 
1473
            return None
1468
1474
 
1469
1475
    isatty = getattr(sys.stdout, 'isatty', None)
1470
1476
    if isatty is None or not isatty():
1995
2001
# data at once.
1996
2002
MAX_SOCKET_CHUNK = 64 * 1024
1997
2003
 
 
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
 
1998
2012
def read_bytes_from_socket(sock, report_activity=None,
1999
2013
        max_read_size=MAX_SOCKET_CHUNK):
2000
2014
    """Read up to max_read_size of bytes from sock and notify of progress.
2008
2022
            bytes = sock.recv(max_read_size)
2009
2023
        except socket.error, e:
2010
2024
            eno = e.args[0]
2011
 
            if eno == getattr(errno, "WSAECONNRESET", errno.ECONNRESET):
 
2025
            if eno in _end_of_stream_errors:
2012
2026
                # The connection was closed by the other side.  Callers expect
2013
2027
                # an empty string to signal end-of-stream.
2014
2028
                return ""
2376
2390
        counter += 1
2377
2391
        name = "%s.~%d~" % (base, counter)
2378
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