diff --git a/pip/basecommand.py b/pip/basecommand.py index cc03b3ded..df4621ca1 100644 --- a/pip/basecommand.py +++ b/pip/basecommand.py @@ -75,6 +75,9 @@ class Command(object): if options.no_input: os.environ['PIP_NO_INPUT'] = '1' + + if options.exists_action: + os.environ['PIP_EXISTS_ACTION'] = ''.join(options.exists_action) if options.require_venv: # If a venv is required check if it can really be found diff --git a/pip/baseparser.py b/pip/baseparser.py index 6af54c7d4..218b87528 100644 --- a/pip/baseparser.py +++ b/pip/baseparser.py @@ -200,4 +200,20 @@ parser.add_option( default='', help=optparse.SUPPRESS_HELP) +parser.add_option( + # Option when path already exist + '--exists-action', + dest='exists_action', + type='choice', + choices=('s', 'i', 'w', 'b'), + default=[], + action='append', + help="Default action when a path already exists." + "Use this option more then one time to specify another action when a certain option is not available, choices: " + "(s)witch," + "(i)gnore," + "(w)ipe," + "(b)ackup" +) + parser.disable_interspersed_args() diff --git a/pip/download.py b/pip/download.py index b11d1e8c4..90cf1d732 100644 --- a/pip/download.py +++ b/pip/download.py @@ -10,7 +10,7 @@ from pip.backwardcompat import (md5, copytree, xmlrpclib, urllib, urllib2, urlparse, string_types, HTTPError) from pip.exceptions import InstallationError from pip.util import (splitext, rmtree, - format_size, display_path, backup_dir, ask, + format_size, display_path, backup_dir, ask, path_exists, unpack_file, create_download_cache_folder, cache_download) from pip.vcs import vcs from pip.log import logger @@ -388,7 +388,7 @@ def _copy_file(filename, location, content_type, link): copy = True download_location = os.path.join(location, link.filename) if os.path.exists(download_location): - response = ask('The file %s exists. (i)gnore, (w)ipe, (b)ackup ' + response = path_exists('The file %s exists. (i)gnore, (w)ipe, (b)ackup ' % display_path(download_location), ('i', 'w', 'b')) if response == 'i': copy = False diff --git a/pip/req.py b/pip/req.py index 4f2d5fad2..33c9b8750 100644 --- a/pip/req.py +++ b/pip/req.py @@ -10,7 +10,7 @@ from pip.exceptions import InstallationError, UninstallationError from pip.vcs import vcs from pip.log import logger from pip.util import display_path, rmtree -from pip.util import ask, backup_dir +from pip.util import ask, path_exists, backup_dir from pip.util import is_installable_dir, is_local, dist_is_local from pip.util import renames, normalize_path, egg_link_path from pip.util import make_path_relative @@ -502,7 +502,7 @@ exec(compile(open(__file__).read().replace('\\r\\n', '\\n'), __file__, 'exec')) archive_name = '%s-%s.zip' % (self.name, self.installed_version) archive_path = os.path.join(build_dir, archive_name) if os.path.exists(archive_path): - response = ask('The file %s exists. (i)gnore, (w)ipe, (b)ackup ' + response = path_exists('The file %s exists. (i)gnore, (w)ipe, (b)ackup ' % display_path(archive_path), ('i', 'w', 'b')) if response == 'i': create_archive = False diff --git a/pip/util.py b/pip/util.py index 0be090853..96202aafb 100644 --- a/pip/util.py +++ b/pip/util.py @@ -102,6 +102,11 @@ def get_pathext(default_pathext=None): pathext = os.environ.get('PATHEXT', default_pathext) return pathext +def path_exists(message, options): + for action in os.environ.get('PIP_EXISTS_ACTION', ''): + if action in options: + return action + return ask(message, options) def ask(message, options): """Ask the message interactively, with the given possible responses""" diff --git a/pip/vcs/__init__.py b/pip/vcs/__init__.py index 33c9c7c5d..f3101bd37 100644 --- a/pip/vcs/__init__.py +++ b/pip/vcs/__init__.py @@ -5,7 +5,7 @@ import shutil from pip.backwardcompat import urlparse, urllib from pip.log import logger -from pip.util import display_path, backup_dir, find_command, ask, rmtree +from pip.util import display_path, backup_dir, find_command, ask, rmtree, path_exists __all__ = ['vcs', 'get_src_requirement'] @@ -198,7 +198,7 @@ class VersionControl(object): if prompt: logger.warn('The plan is to install the %s repository %s' % (self.name, url)) - response = ask('What to do? %s' % prompt[0], prompt[1]) + response = path_exists('What to do? %s' % prompt[0], prompt[1]) if response == 's': logger.notify('Switching %s %s to %s%s'