~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Matt Nordhoff
  • Date: 2009-06-23 05:12:07 UTC
  • mto: This revision was merged to the branch mainline in revision 4474.
  • Revision ID: mnordhoff@mattnordhoff.com-20090623051207-fksdtbzkwtnrw9dd
Update _add_text docstrings that still referred to add_text.

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
42
41
import tempfile
43
42
from tempfile import (
44
43
    mkdtemp,
722
721
               _format_date(t, offset, timezone, date_fmt, show_offset)
723
722
    date_str = time.strftime(date_fmt, tt)
724
723
    if not isinstance(date_str, unicode):
725
 
        date_str = date_str.decode(get_user_encoding(), 'replace')
 
724
        date_str = date_str.decode(bzrlib.user_encoding, 'replace')
726
725
    return date_str + offset_str
727
726
 
728
727
def _format_date(t, offset, timezone, date_fmt, show_offset):
927
926
        shutil.copyfile(src, dest)
928
927
 
929
928
 
 
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.
930
934
def delete_any(path):
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.
 
935
    """Delete a file or directory."""
955
936
    if isdir(path): # Takes care of symlinks
956
937
        os.rmdir(path)
957
938
    else:
1040
1021
 
1041
1022
    s = []
1042
1023
    head = rp
1043
 
    while True:
1044
 
        if len(head) <= len(base) and head != base:
1045
 
            raise errors.PathNotChild(rp, base)
 
1024
    while len(head) >= len(base):
1046
1025
        if head == base:
1047
1026
            break
1048
 
        head, tail = split(head)
 
1027
        head, tail = os.path.split(head)
1049
1028
        if tail:
1050
 
            s.append(tail)
 
1029
            s.insert(0, tail)
 
1030
    else:
 
1031
        raise errors.PathNotChild(rp, base)
1051
1032
 
1052
1033
    if s:
1053
 
        return pathjoin(*reversed(s))
 
1034
        return pathjoin(*s)
1054
1035
    else:
1055
1036
        return ''
1056
1037
 
1845
1826
        finally:
1846
1827
            termios.tcsetattr(fd, termios.TCSADRAIN, settings)
1847
1828
        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