Skip to content

Commit

Permalink
Show default index url provided by pip
Browse files Browse the repository at this point in the history
  • Loading branch information
atugushev committed Feb 28, 2019
1 parent ebc0e10 commit ce413df
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 29 deletions.
30 changes: 30 additions & 0 deletions piptools/pip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import optparse

from ._compat import Command, cmdoptions


class PipCommand(Command):
name = 'PipCommand'


def get_pip_command():
# Use pip's parser for pip.conf management and defaults.
# General options (find_links, index_url, extra_index_url, trusted_host,
# and pre) are defered to pip.
pip_command = PipCommand()
pip_command.parser.add_option(cmdoptions.no_binary())
pip_command.parser.add_option(cmdoptions.only_binary())
index_opts = cmdoptions.make_option_group(
cmdoptions.index_group,
pip_command.parser,
)
pip_command.parser.insert_option_group(0, index_opts)
pip_command.parser.add_option(optparse.Option('--pre', action='store_true', default=False))

return pip_command


pip_command = get_pip_command()

# Get default values of the pip's options (including options from pip.conf).
pip_defaults = pip_command.parser.get_default_values()
27 changes: 2 additions & 25 deletions piptools/scripts/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@
from __future__ import (absolute_import, division, print_function,
unicode_literals)

import optparse
import os
import sys
import tempfile

from .._compat import (
install_req_from_line,
parse_requirements,
cmdoptions,
Command,
)

from .. import click
from ..exceptions import PipToolsError
from ..logging import log
from ..pip import get_pip_command, pip_defaults
from ..repositories import LocalRequirementsRepository, PyPIRepository
from ..resolver import Resolver
from ..utils import (dedup, is_pinned_requirement, key_from_req, UNSAFE_PACKAGES)
Expand All @@ -25,10 +23,6 @@
DEFAULT_REQUIREMENTS_FILE = 'requirements.in'


class PipCommand(Command):
name = 'PipCommand'


@click.command()
@click.version_option()
@click.option('-v', '--verbose', count=True, help="Show more output")
Expand All @@ -37,7 +31,7 @@ class PipCommand(Command):
@click.option('-p', '--pre', is_flag=True, default=None, help="Allow resolving to prereleases (default is not)")
@click.option('-r', '--rebuild', is_flag=True, help="Clear any caches upfront, rebuild from scratch")
@click.option('-f', '--find-links', multiple=True, help="Look for archives in this directory or on this HTML page", envvar='PIP_FIND_LINKS') # noqa
@click.option('-i', '--index-url', help="Change index URL (defaults to PyPI)", envvar='PIP_INDEX_URL')
@click.option('-i', '--index-url', help="Change index URL (defaults to {})".format(pip_defaults.index_url), envvar='PIP_INDEX_URL') # noqa
@click.option('--extra-index-url', multiple=True, help="Add additional index URL to search", envvar='PIP_EXTRA_INDEX_URL') # noqa
@click.option('--cert', help="Path to alternate CA bundle.")
@click.option('--client-cert', help="Path to SSL client certificate, a single file containing the private key and the certificate in PEM format.") # noqa
Expand Down Expand Up @@ -254,20 +248,3 @@ def cli(verbose, quiet, dry_run, pre, rebuild, find_links, index_url, extra_inde

if dry_run:
log.warning('Dry-run, so nothing updated.')


def get_pip_command():
# Use pip's parser for pip.conf management and defaults.
# General options (find_links, index_url, extra_index_url, trusted_host,
# and pre) are defered to pip.
pip_command = PipCommand()
pip_command.parser.add_option(cmdoptions.no_binary())
pip_command.parser.add_option(cmdoptions.only_binary())
index_opts = cmdoptions.make_option_group(
cmdoptions.index_group,
pip_command.parser,
)
pip_command.parser.insert_option_group(0, index_opts)
pip_command.parser.add_option(optparse.Option('--pre', action='store_true', default=False))

return pip_command
16 changes: 16 additions & 0 deletions tests/test_cli_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,19 @@ def test_no_candidates_pre():

assert out.exit_code == 2
assert 'Tried pre-versions:' in out.output


@pytest.mark.usefixtures('pip_conf')
def test_default_index_url():
status, output = invoke([sys.executable, '-m', 'piptools', 'compile', '--help'])
output = output.decode('utf-8')

# Click's subprocess output has \r\r\n line endings on win py27. Fix it.
output = output.replace('\r\r', '\r')

assert status == 0
expected = (
' -i, --index-url TEXT Change index URL (defaults to' + os.linesep +
' http://example.com)' + os.linesep
)
assert expected in output
2 changes: 1 addition & 1 deletion tests/test_repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from piptools._compat import PackageFinder, install_req_from_line

from piptools.repositories.pypi import PyPIRepository
from piptools.scripts.compile import get_pip_command
from piptools.pip import get_pip_command
import pytest


Expand Down
2 changes: 1 addition & 1 deletion tests/test_repository_local.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from piptools.scripts.compile import get_pip_command
from piptools.repositories.local import LocalRequirementsRepository
from piptools.repositories.pypi import PyPIRepository
from piptools.utils import name_from_req
from piptools.pip import get_pip_command

EXPECTED = {
'sha256:04b133ef629ae2bc05f83d0b079a964494a9cd17914943e690c57209b44aae20',
Expand Down
2 changes: 1 addition & 1 deletion tests/test_repository_pypi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from piptools.scripts.compile import get_pip_command
from piptools.pip import get_pip_command
from piptools.repositories.pypi import PyPIRepository


Expand Down
2 changes: 1 addition & 1 deletion tests/test_top_level_editable.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pytest

from piptools.repositories import PyPIRepository
from piptools.scripts.compile import get_pip_command
from piptools.pip import get_pip_command


class MockedPyPIRepository(PyPIRepository):
Expand Down

0 comments on commit ce413df

Please sign in to comment.