~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-04-29 11:07:14 UTC
  • mfrom: (5813.1.1 realname-can-be-empty)
  • Revision ID: pqm@pqm.ubuntu.com-20110429110714-wr9f71ea9600lvb6
(jelmer) Allow realname to be empty in tests. (Jelmer Vernooij)

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
96
96
        user_encoding = get_user_encoding()
97
97
        return [a.decode(user_encoding) for a in sys.argv[1:]]
98
98
    except UnicodeDecodeError:
99
 
        raise errors.BzrError(("Parameter '%r' is unsupported by the current "
100
 
                                                            "encoding." % a))
 
99
        raise errors.BzrError("Parameter %r encoding is unsupported by %s "
 
100
            "application locale." % (a, user_encoding))
101
101
 
102
102
 
103
103
def make_readonly(filename):
392
392
# These were already lazily imported into local scope
393
393
# mkdtemp = tempfile.mkdtemp
394
394
# rmtree = shutil.rmtree
 
395
lstat = os.lstat
 
396
fstat = os.fstat
 
397
 
 
398
def wrap_stat(st):
 
399
    return st
 
400
 
395
401
 
396
402
MIN_ABS_PATHLENGTH = 1
397
403
 
407
413
    getcwd = _win32_getcwd
408
414
    mkdtemp = _win32_mkdtemp
409
415
    rename = _win32_rename
 
416
    try:
 
417
        from bzrlib import _walkdirs_win32
 
418
    except ImportError:
 
419
        pass
 
420
    else:
 
421
        lstat = _walkdirs_win32.lstat
 
422
        fstat = _walkdirs_win32.fstat
 
423
        wrap_stat = _walkdirs_win32.wrap_stat
410
424
 
411
425
    MIN_ABS_PATHLENGTH = 3
412
426
 
1461
1475
    # a similar effect.
1462
1476
 
1463
1477
    # If BZR_COLUMNS is set, take it, user is always right
 
1478
    # Except if they specified 0 in which case, impose no limit here
1464
1479
    try:
1465
 
        return int(os.environ['BZR_COLUMNS'])
 
1480
        width = int(os.environ['BZR_COLUMNS'])
1466
1481
    except (KeyError, ValueError):
1467
 
        pass
 
1482
        width = None
 
1483
    if width is not None:
 
1484
        if width > 0:
 
1485
            return width
 
1486
        else:
 
1487
            return None
1468
1488
 
1469
1489
    isatty = getattr(sys.stdout, 'isatty', None)
1470
1490
    if isatty is None or not isatty():
1995
2015
# data at once.
1996
2016
MAX_SOCKET_CHUNK = 64 * 1024
1997
2017
 
 
2018
_end_of_stream_errors = [errno.ECONNRESET]
 
2019
for _eno in ['WSAECONNRESET', 'WSAECONNABORTED']:
 
2020
    _eno = getattr(errno, _eno, None)
 
2021
    if _eno is not None:
 
2022
        _end_of_stream_errors.append(_eno)
 
2023
del _eno
 
2024
 
 
2025
 
1998
2026
def read_bytes_from_socket(sock, report_activity=None,
1999
2027
        max_read_size=MAX_SOCKET_CHUNK):
2000
2028
    """Read up to max_read_size of bytes from sock and notify of progress.
2008
2036
            bytes = sock.recv(max_read_size)
2009
2037
        except socket.error, e:
2010
2038
            eno = e.args[0]
2011
 
            if eno == getattr(errno, "WSAECONNRESET", errno.ECONNRESET):
 
2039
            if eno in _end_of_stream_errors:
2012
2040
                # The connection was closed by the other side.  Callers expect
2013
2041
                # an empty string to signal end-of-stream.
2014
2042
                return ""
2229
2257
            termios.tcsetattr(fd, termios.TCSADRAIN, settings)
2230
2258
        return ch
2231
2259
 
2232
 
 
2233
2260
if sys.platform == 'linux2':
2234
2261
    def _local_concurrency():
2235
 
        concurrency = None
2236
 
        prefix = 'processor'
2237
 
        for line in file('/proc/cpuinfo', 'rb'):
2238
 
            if line.startswith(prefix):
2239
 
                concurrency = int(line[line.find(':')+1:]) + 1
2240
 
        return concurrency
 
2262
        try:
 
2263
            return os.sysconf('SC_NPROCESSORS_ONLN')
 
2264
        except (ValueError, OSError, AttributeError):
 
2265
            return None
2241
2266
elif sys.platform == 'darwin':
2242
2267
    def _local_concurrency():
2243
2268
        return subprocess.Popen(['sysctl', '-n', 'hw.availcpu'],
2244
2269
                                stdout=subprocess.PIPE).communicate()[0]
2245
 
elif sys.platform[0:7] == 'freebsd':
 
2270
elif "bsd" in sys.platform:
2246
2271
    def _local_concurrency():
2247
2272
        return subprocess.Popen(['sysctl', '-n', 'hw.ncpu'],
2248
2273
                                stdout=subprocess.PIPE).communicate()[0]
2276
2301
    concurrency = os.environ.get('BZR_CONCURRENCY', None)
2277
2302
    if concurrency is None:
2278
2303
        try:
2279
 
            concurrency = _local_concurrency()
2280
 
        except (OSError, IOError):
2281
 
            pass
 
2304
            import multiprocessing
 
2305
        except ImportError:
 
2306
            # multiprocessing is only available on Python >= 2.6
 
2307
            try:
 
2308
                concurrency = _local_concurrency()
 
2309
            except (OSError, IOError):
 
2310
                pass
 
2311
        else:
 
2312
            concurrency = multiprocessing.cpu_count()
2282
2313
    try:
2283
2314
        concurrency = int(concurrency)
2284
2315
    except (TypeError, ValueError):
2376
2407
        counter += 1
2377
2408
        name = "%s.~%d~" % (base, counter)
2378
2409
    return name
 
2410
 
 
2411
 
 
2412
def set_fd_cloexec(fd):
 
2413
    """Set a Unix file descriptor's FD_CLOEXEC flag.  Do nothing if platform
 
2414
    support for this is not available.
 
2415
    """
 
2416
    try:
 
2417
        import fcntl
 
2418
        old = fcntl.fcntl(fd, fcntl.F_GETFD)
 
2419
        fcntl.fcntl(fd, fcntl.F_SETFD, old | fcntl.FD_CLOEXEC)
 
2420
    except (ImportError, AttributeError):
 
2421
        # Either the fcntl module or specific constants are not present
 
2422
        pass
 
2423
 
 
2424
 
 
2425
def find_executable_on_path(name):
 
2426
    """Finds an executable on the PATH.
 
2427
    
 
2428
    On Windows, this will try to append each extension in the PATHEXT
 
2429
    environment variable to the name, if it cannot be found with the name
 
2430
    as given.
 
2431
    
 
2432
    :param name: The base name of the executable.
 
2433
    :return: The path to the executable found or None.
 
2434
    """
 
2435
    path = os.environ.get('PATH')
 
2436
    if path is None:
 
2437
        return None
 
2438
    path = path.split(os.pathsep)
 
2439
    if sys.platform == 'win32':
 
2440
        exts = os.environ.get('PATHEXT', '').split(os.pathsep)
 
2441
        exts = [ext.lower() for ext in exts]
 
2442
        base, ext = os.path.splitext(name)
 
2443
        if ext != '':
 
2444
            if ext.lower() not in exts:
 
2445
                return None
 
2446
            name = base
 
2447
            exts = [ext]
 
2448
    else:
 
2449
        exts = ['']
 
2450
    for ext in exts:
 
2451
        for d in path:
 
2452
            f = os.path.join(d, name) + ext
 
2453
            if os.access(f, os.X_OK):
 
2454
                return f
 
2455
    return None