~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: John Arbash Meinel
  • Date: 2009-07-08 14:37:25 UTC
  • mfrom: (4516 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4517.
  • Revision ID: john@arbash-meinel.com-20090708143725-sc9sjy3mz4cxwxzz
Merge bzr.dev 4516

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,
926
927
        shutil.copyfile(src, dest)
927
928
 
928
929
 
929
 
# Look Before You Leap (LBYL) is appropriate here instead of Easier to Ask for
930
 
# Forgiveness than Permission (EAFP) because:
931
 
# - root can damage a solaris file system by using unlink,
932
 
# - unlink raises different exceptions on different OSes (linux: EISDIR, win32:
933
 
#   EACCES, OSX: EPERM) when invoked on a directory.
934
930
def delete_any(path):
935
 
    """Delete a file or directory."""
 
931
    """Delete a file, symlink or directory.  
 
932
    
 
933
    Will delete even if readonly.
 
934
    """
 
935
    try:
 
936
       _delete_file_or_dir(path)
 
937
    except (OSError, IOError), e:
 
938
        if e.errno in (errno.EPERM, errno.EACCES):
 
939
            # make writable and try again
 
940
            try:
 
941
                make_writable(path)
 
942
            except (OSError, IOError):
 
943
                pass
 
944
            _delete_file_or_dir(path)
 
945
        else:
 
946
            raise
 
947
 
 
948
 
 
949
def _delete_file_or_dir(path):
 
950
    # Look Before You Leap (LBYL) is appropriate here instead of Easier to Ask for
 
951
    # Forgiveness than Permission (EAFP) because:
 
952
    # - root can damage a solaris file system by using unlink,
 
953
    # - unlink raises different exceptions on different OSes (linux: EISDIR, win32:
 
954
    #   EACCES, OSX: EPERM) when invoked on a directory.
936
955
    if isdir(path): # Takes care of symlinks
937
956
        os.rmdir(path)
938
957
    else:
1826
1845
        finally:
1827
1846
            termios.tcsetattr(fd, termios.TCSADRAIN, settings)
1828
1847
        return ch
 
1848
 
 
1849
 
 
1850
if sys.platform == 'linux2':
 
1851
    def _local_concurrency():
 
1852
        concurrency = None
 
1853
        prefix = 'processor'
 
1854
        for line in file('/proc/cpuinfo', 'rb'):
 
1855
            if line.startswith(prefix):
 
1856
                concurrency = int(line[line.find(':')+1:]) + 1
 
1857
        return concurrency
 
1858
elif sys.platform == 'darwin':
 
1859
    def _local_concurrency():
 
1860
        return subprocess.Popen(['sysctl', '-n', 'hw.availcpu'],
 
1861
                                stdout=subprocess.PIPE).communicate()[0]
 
1862
elif sys.platform[0:7] == 'freebsd':
 
1863
    def _local_concurrency():
 
1864
        return subprocess.Popen(['sysctl', '-n', 'hw.ncpu'],
 
1865
                                stdout=subprocess.PIPE).communicate()[0]
 
1866
elif sys.platform == 'sunos5':
 
1867
    def _local_concurrency():
 
1868
        return subprocess.Popen(['psrinfo', '-p',],
 
1869
                                stdout=subprocess.PIPE).communicate()[0]
 
1870
elif sys.platform == "win32":
 
1871
    def _local_concurrency():
 
1872
        # This appears to return the number of cores.
 
1873
        return os.environ.get('NUMBER_OF_PROCESSORS')
 
1874
else:
 
1875
    def _local_concurrency():
 
1876
        # Who knows ?
 
1877
        return None
 
1878
 
 
1879
 
 
1880
_cached_local_concurrency = None
 
1881
 
 
1882
def local_concurrency(use_cache=True):
 
1883
    """Return how many processes can be run concurrently.
 
1884
 
 
1885
    Rely on platform specific implementations and default to 1 (one) if
 
1886
    anything goes wrong.
 
1887
    """
 
1888
    global _cached_local_concurrency
 
1889
    if _cached_local_concurrency is not None and use_cache:
 
1890
        return _cached_local_concurrency
 
1891
 
 
1892
    try:
 
1893
        concurrency = _local_concurrency()
 
1894
    except (OSError, IOError):
 
1895
        concurrency = None
 
1896
    try:
 
1897
        concurrency = int(concurrency)
 
1898
    except (TypeError, ValueError):
 
1899
        concurrency = 1
 
1900
    if use_cache:
 
1901
        _cached_concurrency = concurrency
 
1902
    return concurrency