Repository: jazzband/pip-tools

Issue:
pip-compile honor pip.conf



Discussion:

Resolving patch:
diff --git a/piptools/repositories/pypi.py b/piptools/repositories/pypi.py
--- a/piptools/repositories/pypi.py
+++ b/piptools/repositories/pypi.py
@@ -23,19 +23,30 @@
 
 class PyPIRepository(BaseRepository):
     DEFAULT_INDEX_URL = 'https://pypi.python.org/simple/'
-
+    
     """
     The PyPIRepository will use the provided Finder instance to lookup
     packages.  Typically, it looks up packages on PyPI (the default implicit
     config), but any other PyPI mirror can be used if index_urls is
     changed/configured on the Finder.
     """
-    def __init__(self):
+    def __init__(self, pip_options):
         self.session = PipSession()
-        self.finder = PackageFinder(find_links=[],
-                                    index_urls=[self.DEFAULT_INDEX_URL],
-                                    session=self.session)
 
+        index_urls = [pip_options.index_url] + pip_options.extra_index_urls
+        if pip_options.no_index:
+            logger.info('Ignoring indexes: %s', ','.join(index_urls))
+            index_urls = []
+
+        self.finder = PackageFinder(
+            find_links=pip_options.find_links,
+            index_urls=index_urls,
+            trusted_hosts=pip_options.trusted_hosts,
+            allow_all_prereleases=pip_options.pre,
+            process_dependency_links=pip_options.process_dependency_links,
+            session=self.session,
+        )
+        
         # Caches
         # stores project_name => InstallationCandidate mappings for all
         # versions reported by PyPI, so we only have to ask once for each
diff --git a/piptools/scripts/compile.py b/piptools/scripts/compile.py
--- a/piptools/scripts/compile.py
+++ b/piptools/scripts/compile.py
@@ -3,6 +3,7 @@
 from __future__ import (absolute_import, division, print_function,
                         unicode_literals)
 
+import optparse
 import sys
 import pip
 
@@ -23,6 +24,10 @@
 
 DEFAULT_REQUIREMENTS_FILE = 'requirements.in'
 
+# emulate pip's option parsing with a stub command
+class PipCommand(pip.basecommand.Command):
+    name = 'PipCommand'
+
 
 @click.command()
 @click.option('-v', '--verbose', is_flag=True, help="Show more output")
@@ -48,13 +53,31 @@ def cli(verbose, dry_run, pre, rebuild, find_links, index_url,
     ###
     # Setup
     ###
-    repository = PyPIRepository()
 
-    # Configure the finder
+    # use pip's parser for pip.conf management and defaults
+    # general options (find_links, index_url, extra_index_url abd pre) are defered to pip.
+    pip_options = PipCommand()
+    index_opts = pip.cmdoptions.make_option_group(
+        pip.cmdoptions.index_group,
+        pip_options.parser,
+    )
+    pip_options.parser.insert_option_group(0, index_opts)
+    #support pip pre
+    pip_options.parser.add_option(optparse.Option('--pre', action='store_true', default=False))
+
+    pip_args = []
+    if find_links:
+        pip_args.extend(['-f', find_links])
     if index_url:
-        repository.finder.index_urls = [index_url]
-    repository.finder.index_urls.extend(extra_index_url)
-    repository.finder.find_links.extend(find_links)
+        pip_args.extend(['-i', index_url])
+    if extra_index_url:
+        pip_args.extend(['--extra-index-url', extra_index_url])
+    if pre:
+        pip_args.extend(['--pre'])
+
+    pip_options, _ = pip_options.parse_args(pip_args)
+
+    repository = PyPIRepository(pip_options)
 
     log.debug('Using indexes:')
     for index_url in repository.finder.index_urls:
@@ -70,7 +93,7 @@ def cli(verbose, dry_run, pre, rebuild, find_links, index_url,
     # Parsing/collecting initial requirements
     ###
     constraints = []
-    for line in parse_requirements(src_file, finder=repository.finder, session=repository.session):
+    for line in parse_requirements(src_file, finder=repository.finder, session=repository.session, options=pip_options):
         constraints.append(line)
 
     try:


Test patch:
diff --git a/tests/test_cli.py b/tests/test_cli.py
new file mode 100644
--- /dev/null
+++ b/tests/test_cli.py
@@ -0,0 +1,56 @@
+import os
+import sys
+
+import pytest
+
+from click.testing import CliRunner
+from piptools.scripts.compile import cli
+
+@pytest.fixture
+def pip_conf(request):
+    test_conf = """
+[global]
+index-url = http://example.com
+trusted-host = example.com
+    """
+    #write pip.conf (pip.ini) at root of virtualenv
+    pip_conf_file = 'pip.conf' if os.name != 'nt' else 'pip.ini'
+    pip_conf_file = os.path.join(sys.prefix, pip_conf_file)
+    with open(pip_conf_file, 'w') as pip_conf:
+        pip_conf.write(test_conf)
+    
+    def tear_down():
+        os.remove(pip_conf_file)
+
+    request.addfinalizer(tear_down)
+    
+    return pip_conf_file
+
+
+def test_default_pip_conf_read(pip_conf):
+
+    assert os.path.exists(pip_conf)
+
+    runner = CliRunner()
+    with runner.isolated_filesystem():
+        # preconditions
+        open('requirements.in', 'w').close()
+        out = runner.invoke(cli, ['-v'])
+
+        # check that we have our index-url as specified in pip.conf
+        assert 'Using indexes:\n  http://example.com' in out.output
+        assert '--index-url http://example.com' in out.output
+
+
+def test_command_line_overrides_pip_conf(pip_conf):
+
+    assert os.path.exists(pip_conf)
+
+    runner = CliRunner()
+    with runner.isolated_filesystem():
+        # preconditions
+        open('requirements.in', 'w').close()
+        out = runner.invoke(cli, ['-v', '-i', 'http://override.com'])
+
+        # check that we have our index-url as specified in pip.conf
+        assert 'Using indexes:\n  http://override.com' in out.output