926
927
shutil.copyfile(src, dest)
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.
933
Will delete even if readonly.
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
942
except (OSError, IOError):
944
_delete_file_or_dir(path)
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
1827
1846
termios.tcsetattr(fd, termios.TCSADRAIN, settings)
1850
if sys.platform == 'linux2':
1851
def _local_concurrency():
1853
prefix = 'processor'
1854
for line in file('/proc/cpuinfo', 'rb'):
1855
if line.startswith(prefix):
1856
concurrency = int(line[line.find(':')+1:]) + 1
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')
1875
def _local_concurrency():
1880
_cached_local_concurrency = None
1882
def local_concurrency(use_cache=True):
1883
"""Return how many processes can be run concurrently.
1885
Rely on platform specific implementations and default to 1 (one) if
1886
anything goes wrong.
1888
global _cached_local_concurrency
1889
if _cached_local_concurrency is not None and use_cache:
1890
return _cached_local_concurrency
1893
concurrency = _local_concurrency()
1894
except (OSError, IOError):
1897
concurrency = int(concurrency)
1898
except (TypeError, ValueError):
1901
_cached_concurrency = concurrency