/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: Vincent Ladeuil
  • Date: 2011-08-16 13:12:40 UTC
  • mfrom: (6071 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6076.
  • Revision ID: v.ladeuil+lp@free.fr-20110816131240-gcyn9cik86dxwgz3
Merge into trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
53
53
    deprecated_in,
54
54
    )
55
55
 
56
 
# sha and md5 modules are deprecated in python2.6 but hashlib is available as
57
 
# of 2.5
58
 
if sys.version_info < (2, 5):
59
 
    import md5 as _mod_md5
60
 
    md5 = _mod_md5.new
61
 
    import sha as _mod_sha
62
 
    sha = _mod_sha.new
63
 
else:
64
 
    from hashlib import (
65
 
        md5,
66
 
        sha1 as sha,
67
 
        )
 
56
from hashlib import (
 
57
    md5,
 
58
    sha1 as sha,
 
59
    )
68
60
 
69
61
 
70
62
import bzrlib
96
88
        user_encoding = get_user_encoding()
97
89
        return [a.decode(user_encoding) for a in sys.argv[1:]]
98
90
    except UnicodeDecodeError:
99
 
        raise errors.BzrError(("Parameter '%r' is unsupported by the current "
100
 
                                                            "encoding." % a))
 
91
        raise errors.BzrError("Parameter %r encoding is unsupported by %s "
 
92
            "application locale." % (a, user_encoding))
101
93
 
102
94
 
103
95
def make_readonly(filename):
269
261
            else:
270
262
                rename_func(tmp_name, new)
271
263
    if failure_exc is not None:
272
 
        raise failure_exc[0], failure_exc[1], failure_exc[2]
 
264
        try:
 
265
            raise failure_exc[0], failure_exc[1], failure_exc[2]
 
266
        finally:
 
267
            del failure_exc
273
268
 
274
269
 
275
270
# In Python 2.4.2 and older, os.path.abspath and os.path.realpath
392
387
# These were already lazily imported into local scope
393
388
# mkdtemp = tempfile.mkdtemp
394
389
# rmtree = shutil.rmtree
 
390
lstat = os.lstat
 
391
fstat = os.fstat
 
392
 
 
393
def wrap_stat(st):
 
394
    return st
 
395
 
395
396
 
396
397
MIN_ABS_PATHLENGTH = 1
397
398
 
407
408
    getcwd = _win32_getcwd
408
409
    mkdtemp = _win32_mkdtemp
409
410
    rename = _win32_rename
 
411
    try:
 
412
        from bzrlib import _walkdirs_win32
 
413
    except ImportError:
 
414
        pass
 
415
    else:
 
416
        lstat = _walkdirs_win32.lstat
 
417
        fstat = _walkdirs_win32.fstat
 
418
        wrap_stat = _walkdirs_win32.wrap_stat
410
419
 
411
420
    MIN_ABS_PATHLENGTH = 3
412
421
 
2243
2252
            termios.tcsetattr(fd, termios.TCSADRAIN, settings)
2244
2253
        return ch
2245
2254
 
2246
 
 
2247
2255
if sys.platform == 'linux2':
2248
2256
    def _local_concurrency():
2249
 
        concurrency = None
2250
 
        prefix = 'processor'
2251
 
        for line in file('/proc/cpuinfo', 'rb'):
2252
 
            if line.startswith(prefix):
2253
 
                concurrency = int(line[line.find(':')+1:]) + 1
2254
 
        return concurrency
 
2257
        try:
 
2258
            return os.sysconf('SC_NPROCESSORS_ONLN')
 
2259
        except (ValueError, OSError, AttributeError):
 
2260
            return None
2255
2261
elif sys.platform == 'darwin':
2256
2262
    def _local_concurrency():
2257
2263
        return subprocess.Popen(['sysctl', '-n', 'hw.availcpu'],
2258
2264
                                stdout=subprocess.PIPE).communicate()[0]
2259
 
elif sys.platform[0:7] == 'freebsd':
 
2265
elif "bsd" in sys.platform:
2260
2266
    def _local_concurrency():
2261
2267
        return subprocess.Popen(['sysctl', '-n', 'hw.ncpu'],
2262
2268
                                stdout=subprocess.PIPE).communicate()[0]
2290
2296
    concurrency = os.environ.get('BZR_CONCURRENCY', None)
2291
2297
    if concurrency is None:
2292
2298
        try:
2293
 
            concurrency = _local_concurrency()
2294
 
        except (OSError, IOError):
2295
 
            pass
 
2299
            import multiprocessing
 
2300
        except ImportError:
 
2301
            # multiprocessing is only available on Python >= 2.6
 
2302
            try:
 
2303
                concurrency = _local_concurrency()
 
2304
            except (OSError, IOError):
 
2305
                pass
 
2306
        else:
 
2307
            concurrency = multiprocessing.cpu_count()
2296
2308
    try:
2297
2309
        concurrency = int(concurrency)
2298
2310
    except (TypeError, ValueError):
2369
2381
    except UnicodeDecodeError:
2370
2382
        raise errors.BzrError("Can't decode username as %s." % \
2371
2383
                user_encoding)
 
2384
    except ImportError, e:
 
2385
        if sys.platform != 'win32':
 
2386
            raise
 
2387
        if str(e) != 'No module named pwd':
 
2388
            raise
 
2389
        # https://bugs.launchpad.net/bzr/+bug/660174
 
2390
        # getpass.getuser() is unable to return username on Windows
 
2391
        # if there is no USERNAME environment variable set.
 
2392
        # That could be true if bzr is running as a service,
 
2393
        # e.g. running `bzr serve` as a service on Windows.
 
2394
        # We should not fail with traceback in this case.
 
2395
        username = u'UNKNOWN'
2372
2396
    return username
2373
2397
 
2374
2398
 
2436
2460
            if os.access(f, os.X_OK):
2437
2461
                return f
2438
2462
    return None
 
2463
 
 
2464
 
 
2465
def _posix_is_local_pid_dead(pid):
 
2466
    """True if pid doesn't correspond to live process on this machine"""
 
2467
    try:
 
2468
        # Special meaning of unix kill: just check if it's there.
 
2469
        os.kill(pid, 0)
 
2470
    except OSError, e:
 
2471
        if e.errno == errno.ESRCH:
 
2472
            # On this machine, and really not found: as sure as we can be
 
2473
            # that it's dead.
 
2474
            return True
 
2475
        elif e.errno == errno.EPERM:
 
2476
            # exists, though not ours
 
2477
            return False
 
2478
        else:
 
2479
            mutter("os.kill(%d, 0) failed: %s" % (pid, e))
 
2480
            # Don't really know.
 
2481
            return False
 
2482
    else:
 
2483
        # Exists and our process: not dead.
 
2484
        return False
 
2485
 
 
2486
if sys.platform == "win32":
 
2487
    is_local_pid_dead = win32utils.is_local_pid_dead
 
2488
else:
 
2489
    is_local_pid_dead = _posix_is_local_pid_dead
 
2490
 
 
2491
 
 
2492
def fdatasync(fileno):
 
2493
    """Flush file contents to disk if possible.
 
2494
    
 
2495
    :param fileno: Integer OS file handle.
 
2496
    :raises TransportNotPossible: If flushing to disk is not possible.
 
2497
    """
 
2498
    fn = getattr(os, 'fdatasync', getattr(os, 'fsync', None))
 
2499
    if fn is not None:
 
2500
        fn(fileno)