1
1
Fork 0
mirror of https://github.com/pypa/pip synced 2023-12-13 21:30:23 +01:00

Custom optparse.Option type that calls expanduser (#7534)

This commit is contained in:
Pradyun Gedam 2020-01-07 12:26:38 +00:00 committed by GitHub
commit ea27521359
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 11 deletions

8
news/980.feature Normal file
View file

@ -0,0 +1,8 @@
Expand ``~`` prefix to user directory in path options, configs, and
environment variables. Values that may be either URL or path are not
currently supported, to avoid ambiguity:
* ``--find-links``
* ``--constraint``, ``-c``
* ``--requirement``, ``-r``
* ``--editable``, ``-e``

View file

@ -130,6 +130,17 @@ def check_dist_restriction(options, check_target=False):
)
def _path_option_check(option, opt, value):
# type: (Option, str, str) -> str
return os.path.expanduser(value)
class PipOption(Option):
TYPES = Option.TYPES + ("path",)
TYPE_CHECKER = Option.TYPE_CHECKER.copy()
TYPE_CHECKER["path"] = _path_option_check
###########
# options #
###########
@ -217,10 +228,11 @@ progress_bar = partial(
) # type: Callable[..., Option]
log = partial(
Option,
PipOption,
"--log", "--log-file", "--local-log",
dest="log",
metavar="path",
type="path",
help="Path to a verbose appending log."
) # type: Callable[..., Option]
@ -291,19 +303,19 @@ def exists_action():
cert = partial(
Option,
PipOption,
'--cert',
dest='cert',
type='str',
type='path',
metavar='path',
help="Path to alternate CA bundle.",
) # type: Callable[..., Option]
client_cert = partial(
Option,
PipOption,
'--client-cert',
dest='client_cert',
type='str',
type='path',
default=None,
metavar='path',
help="Path to SSL client certificate, a single file containing the "
@ -420,10 +432,10 @@ def _handle_src(option, opt_str, value, parser):
src = partial(
Option,
PipOption,
'--src', '--source', '--source-dir', '--source-directory',
dest='src_dir',
type='str',
type='path',
metavar='dir',
default=get_src_prefix(),
action='callback',
@ -626,11 +638,12 @@ def prefer_binary():
cache_dir = partial(
Option,
PipOption,
"--cache-dir",
dest="cache_dir",
default=USER_CACHE_DIR,
metavar="dir",
type='path',
help="Store the cache data in <dir>."
) # type: Callable[..., Option]
@ -690,10 +703,10 @@ def _handle_build_dir(option, opt, value, parser):
build_dir = partial(
Option,
PipOption,
'-b', '--build', '--build-dir', '--build-directory',
dest='build_dir',
type='str',
type='path',
metavar='dir',
action='callback',
callback=_handle_build_dir,
@ -874,9 +887,10 @@ require_hashes = partial(
list_path = partial(
Option,
PipOption,
'--path',
dest='path',
type='path',
action='append',
help='Restrict to the specified installation path for listing '
'packages (can be used multiple times).'

View file

@ -421,3 +421,25 @@ class TestOptionsConfigFiles(object):
cmd._determine_file(options, need_value=False)
else:
assert expect == cmd._determine_file(options, need_value=False)
class TestOptionsExpandUser(AddFakeCommandMixin):
def test_cache_dir(self):
options, args = main(['--cache-dir', '~/cache/dir', 'fake'])
assert options.cache_dir == os.path.expanduser('~/cache/dir')
def test_log(self):
options, args = main(['--log', '~/path', 'fake'])
assert options.log == os.path.expanduser('~/path')
def test_local_log(self):
options, args = main(['--local-log', '~/path', 'fake'])
assert options.log == os.path.expanduser('~/path')
def test_cert(self):
options, args = main(['--cert', '~/path', 'fake'])
assert options.cert == os.path.expanduser('~/path')
def test_client_cert(self):
options, args = main(['--client-cert', '~/path', 'fake'])
assert options.client_cert == os.path.expanduser('~/path')