mirror of
https://github.com/pypa/pip
synced 2023-12-13 21:30:23 +01:00
94 lines
3.3 KiB
Python
94 lines
3.3 KiB
Python
import shutil
|
|
import os
|
|
import posixpath
|
|
import stat
|
|
from pip.backwardcompat import WindowsError
|
|
|
|
__all__ = ['rmtree', 'display_path', 'backup_dir',
|
|
'find_command', 'splitext', 'ask']
|
|
|
|
def rmtree(dir):
|
|
shutil.rmtree(dir, ignore_errors=True,
|
|
onerror=rmtree_errorhandler)
|
|
|
|
def rmtree_errorhandler(func, path, exc_info):
|
|
"""On Windows, the files in .svn are read-only, so when rmtree() tries to
|
|
remove them, an exception is thrown. We catch that here, remove the
|
|
read-only attribute, and hopefully continue without problems."""
|
|
exctype, value = exc_info[:2]
|
|
# lookin for a windows error
|
|
if exctype is not WindowsError or 'Access is denied' not in str(value):
|
|
raise
|
|
# file type should currently be read only
|
|
if ((os.stat(path).st_mode & stat.S_IREAD) != stat.S_IREAD):
|
|
raise
|
|
# convert to read/write
|
|
os.chmod(path, stat.S_IWRITE)
|
|
# use the original function to repeat the operation
|
|
func(path)
|
|
|
|
def display_path(path):
|
|
"""Gives the display value for a given path, making it relative to cwd
|
|
if possible."""
|
|
path = os.path.normcase(os.path.abspath(path))
|
|
if path.startswith(os.getcwd() + os.path.sep):
|
|
path = '.' + path[len(os.getcwd()):]
|
|
return path
|
|
|
|
def backup_dir(dir, ext='.bak'):
|
|
"""Figure out the name of a directory to back up the given dir to
|
|
(adding .bak, .bak2, etc)"""
|
|
n = 1
|
|
extension = ext
|
|
while os.path.exists(dir + extension):
|
|
n += 1
|
|
extension = ext + str(n)
|
|
return dir + extension
|
|
|
|
def splitext(path):
|
|
"""Like os.path.splitext, but take off .tar too"""
|
|
base, ext = posixpath.splitext(path)
|
|
if base.lower().endswith('.tar'):
|
|
ext = base[-4:] + ext
|
|
base = base[:-4]
|
|
return base, ext
|
|
|
|
def find_command(cmd, paths=None, pathext=None):
|
|
"""Searches the PATH for the given command and returns its path"""
|
|
if paths is None:
|
|
paths = os.environ.get('PATH', []).split(os.pathsep)
|
|
if isinstance(paths, basestring):
|
|
paths = [paths]
|
|
# check if there are funny path extensions for executables, e.g. Windows
|
|
if pathext is None:
|
|
pathext = os.environ.get('PATHEXT', '.COM;.EXE;.BAT;.CMD')
|
|
pathext = [ext for ext in pathext.lower().split(os.pathsep)]
|
|
# don't use extensions if the command ends with one of them
|
|
if os.path.splitext(cmd)[1].lower() in pathext:
|
|
pathext = ['']
|
|
# check if we find the command on PATH
|
|
for path in paths:
|
|
# try without extension first
|
|
cmd_path = os.path.join(path, cmd)
|
|
for ext in pathext:
|
|
# then including the extension
|
|
cmd_path_ext = cmd_path + ext
|
|
if os.path.exists(cmd_path_ext):
|
|
return cmd_path_ext
|
|
if os.path.exists(cmd_path):
|
|
return cmd_path
|
|
return None
|
|
|
|
def ask(message, options):
|
|
"""Ask the message interactively, with the given possible responses"""
|
|
while 1:
|
|
if os.environ.get('PIP_NO_INPUT'):
|
|
raise Exception('No input was expected ($PIP_NO_INPUT set); question: %s' % message)
|
|
response = raw_input(message)
|
|
response = response.strip().lower()
|
|
if response not in options:
|
|
print 'Your response (%r) was not one of the expected responses: %s' % (
|
|
response, ', '.join(options))
|
|
else:
|
|
return response
|