Skip to content

Commit

Permalink
Fixes pypa#4390 by allowing explicitly provided --prefix and --target…
Browse files Browse the repository at this point in the history
… options to overrule implicitly discovered --user.
  • Loading branch information
zooba committed Jul 7, 2020
1 parent ebfa663 commit 6f338df
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
10 changes: 10 additions & 0 deletions src/pip/_internal/cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ def expand_default(self, option):
return optparse.IndentedHelpFormatter.expand_default(self, option)


class CustomOption(optparse.Option):

def process(self, opt, value, values, parser):
parser.provided_options.add(opt)
return super(CustomOption, self).process(opt, value, values, parser)


class CustomOptionParser(optparse.OptionParser):

def insert_option_group(self, idx, *args, **kwargs):
Expand Down Expand Up @@ -138,6 +145,9 @@ def __init__(self, *args, **kwargs):

isolated = kwargs.pop("isolated", False)
self.config = Configuration(isolated)
self.provided_options = set()

kwargs.setdefault("option_class", CustomOption)

assert self.name
optparse.OptionParser.__init__(self, *args, **kwargs)
Expand Down
37 changes: 26 additions & 11 deletions src/pip/_internal/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,18 +226,33 @@ def run(self, options, args):
options.src_dir = os.path.abspath(options.src_dir)
install_options = options.install_options or []
if options.use_user_site:
explicit_opt = self.parser.provided_options
if "--user" not in explicit_opt:
# user option was implicit, so allow it to be disabled
# by explicit options
if options.prefix_path and "--prefix" in explicit_opt:
options.use_user_site = False
if options.target_dir and "--target" in explicit_opt:
options.use_user_site = False
if options.use_user_site:
if options.prefix_path:
raise CommandError(
"Can not combine '--user' and '--prefix' as they imply "
"different installation locations"
)
if options.target_dir:
raise CommandError(
"Can not combine '--user' and '--target' as they imply "
"different installation locations"
)
if virtualenv_no_global():
raise InstallationError(
"Can not perform a '--user' install. User site-packages "
"are not visible in this virtualenv."
)
install_options.append('--user')
if options.prefix_path:
raise CommandError(
"Can not combine '--user' and '--prefix' as they imply "
"different installation locations"
)
if virtualenv_no_global():
raise InstallationError(
"Can not perform a '--user' install. User site-packages "
"are not visible in this virtualenv."
)
install_options.append('--user')
install_options.append('--prefix=')
install_options.append('--prefix=')

target_temp_dir = TempDirectory(kind="target")
if options.target_dir:
Expand Down

0 comments on commit 6f338df

Please sign in to comment.