~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: 2009-06-05 08:10:39 UTC
  • mfrom: (4412.1.1 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20090605081039-abvojdsxjbg5i4ff
(vila) Add cpu # detection for windows and Solaris

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,
1826
1827
        finally:
1827
1828
            termios.tcsetattr(fd, termios.TCSADRAIN, settings)
1828
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 == 'sunos5':
 
1845
    def _local_concurrency():
 
1846
        return subprocess.Popen(['psrinfo', '-p',],
 
1847
                                stdout=subprocess.PIPE).communicate()[0]
 
1848
elif sys.platform == "win32":
 
1849
    def _local_concurrency():
 
1850
        # This appears to return the number of cores.
 
1851
        return os.environ.get('NUMBER_OF_PROCESSORS')
 
1852
else:
 
1853
    def _local_concurrency():
 
1854
        # Who knows ?
 
1855
        return None
 
1856
 
 
1857
 
 
1858
_cached_local_concurrency = None
 
1859
 
 
1860
def local_concurrency(use_cache=True):
 
1861
    """Return how many processes can be run concurrently.
 
1862
 
 
1863
    Rely on platform specific implementations and default to 1 (one) if
 
1864
    anything goes wrong.
 
1865
    """
 
1866
    global _cached_local_concurrency
 
1867
    if _cached_local_concurrency is not None and use_cache:
 
1868
        return _cached_local_concurrency
 
1869
 
 
1870
    try:
 
1871
        concurrency = _local_concurrency()
 
1872
    except (OSError, IOError):
 
1873
        concurrency = None
 
1874
    try:
 
1875
        concurrency = int(concurrency)
 
1876
    except (TypeError, ValueError):
 
1877
        concurrency = 1
 
1878
    if use_cache:
 
1879
        _cached_concurrency = concurrency
 
1880
    return concurrency