~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Robert J. Tanner
  • Date: 2009-06-10 03:56:49 UTC
  • mfrom: (4423 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4425.
  • Revision ID: tanner@real-time.com-20090610035649-7rfx4cls4550zc3c
Merge 1.15.1 back to trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
from shutil import (
39
39
    rmtree,
40
40
    )
 
41
import subprocess
41
42
import tempfile
42
43
from tempfile import (
43
44
    mkdtemp,
77
78
O_BINARY = getattr(os, 'O_BINARY', 0)
78
79
 
79
80
 
 
81
def get_unicode_argv():
 
82
    try:
 
83
        user_encoding = get_user_encoding()
 
84
        return [a.decode(user_encoding) for a in sys.argv[1:]]
 
85
    except UnicodeDecodeError:
 
86
        raise errors.BzrError(("Parameter '%r' is unsupported by the current "
 
87
                                                            "encoding." % a))
 
88
 
 
89
 
80
90
def make_readonly(filename):
81
91
    """Make a filename read-only."""
82
92
    mod = os.lstat(filename).st_mode
390
400
    def rmtree(path, ignore_errors=False, onerror=_win32_delete_readonly):
391
401
        """Replacer for shutil.rmtree: could remove readonly dirs/files"""
392
402
        return shutil.rmtree(path, ignore_errors, onerror)
 
403
 
 
404
    f = win32utils.get_unicode_argv     # special function or None
 
405
    if f is not None:
 
406
        get_unicode_argv = f
 
407
 
393
408
elif sys.platform == 'darwin':
394
409
    getcwd = _mac_getcwd
395
410
 
853
868
    return pathjoin(*p)
854
869
 
855
870
 
 
871
def parent_directories(filename):
 
872
    """Return the list of parent directories, deepest first.
 
873
    
 
874
    For example, parent_directories("a/b/c") -> ["a/b", "a"].
 
875
    """
 
876
    parents = []
 
877
    parts = splitpath(dirname(filename))
 
878
    while parts:
 
879
        parents.append(joinpath(parts))
 
880
        parts.pop()
 
881
    return parents
 
882
 
 
883
 
856
884
try:
857
885
    from bzrlib._chunks_to_lines_pyx import chunks_to_lines
858
886
except ImportError:
1799
1827
        finally:
1800
1828
            termios.tcsetattr(fd, termios.TCSADRAIN, settings)
1801
1829
        return ch
 
1830
 
 
1831
 
 
1832
if sys.platform == 'linux2':
 
1833
    def _local_concurrency():
 
1834
        concurrency = None
 
1835
        prefix = 'processor'
 
1836
        for line in file('/proc/cpuinfo', 'rb'):
 
1837
            if line.startswith(prefix):
 
1838
                concurrency = int(line[line.find(':')+1:]) + 1
 
1839
        return concurrency
 
1840
elif sys.platform == 'darwin':
 
1841
    def _local_concurrency():
 
1842
        return subprocess.Popen(['sysctl', '-n', 'hw.availcpu'],
 
1843
                                stdout=subprocess.PIPE).communicate()[0]
 
1844
elif sys.platform[0:7] == 'freebsd':
 
1845
    def _local_concurrency():
 
1846
        return subprocess.Popen(['sysctl', '-n', 'hw.ncpu'],
 
1847
                                stdout=subprocess.PIPE).communicate()[0]
 
1848
elif sys.platform == 'sunos5':
 
1849
    def _local_concurrency():
 
1850
        return subprocess.Popen(['psrinfo', '-p',],
 
1851
                                stdout=subprocess.PIPE).communicate()[0]
 
1852
elif sys.platform == "win32":
 
1853
    def _local_concurrency():
 
1854
        # This appears to return the number of cores.
 
1855
        return os.environ.get('NUMBER_OF_PROCESSORS')
 
1856
else:
 
1857
    def _local_concurrency():
 
1858
        # Who knows ?
 
1859
        return None
 
1860
 
 
1861
 
 
1862
_cached_local_concurrency = None
 
1863
 
 
1864
def local_concurrency(use_cache=True):
 
1865
    """Return how many processes can be run concurrently.
 
1866
 
 
1867
    Rely on platform specific implementations and default to 1 (one) if
 
1868
    anything goes wrong.
 
1869
    """
 
1870
    global _cached_local_concurrency
 
1871
    if _cached_local_concurrency is not None and use_cache:
 
1872
        return _cached_local_concurrency
 
1873
 
 
1874
    try:
 
1875
        concurrency = _local_concurrency()
 
1876
    except (OSError, IOError):
 
1877
        concurrency = None
 
1878
    try:
 
1879
        concurrency = int(concurrency)
 
1880
    except (TypeError, ValueError):
 
1881
        concurrency = 1
 
1882
    if use_cache:
 
1883
        _cached_concurrency = concurrency
 
1884
    return concurrency