We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
pip-compile honor pip.conf


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `piptools/repositories/pypi.py`
Content:
```
1 # coding: utf-8
2 from __future__ import (absolute_import, division, print_function,
3                         unicode_literals)
4 
5 import os
6 from shutil import rmtree
7 
8 from pip.download import PipSession
9 from pip.index import PackageFinder
10 from pip.req import InstallRequirement
11 from pip.req.req_set import RequirementSet
12 
13 from ..cache import CACHE_DIR
14 from ..exceptions import NoCandidateFound
15 from ..utils import is_pinned_requirement, lookup_table
16 from .base import BaseRepository
17 
18 try:
19     from tempfile import TemporaryDirectory  # added in 3.2
20 except ImportError:
21     from .._compat import TemporaryDirectory
22 
23 
24 class PyPIRepository(BaseRepository):
25     DEFAULT_INDEX_URL = 'https://pypi.python.org/simple/'
26 
27     """
28     The PyPIRepository will use the provided Finder instance to lookup
29     packages.  Typically, it looks up packages on PyPI (the default implicit
30     config), but any other PyPI mirror can be used if index_urls is
31     changed/configured on the Finder.
32     """
33     def __init__(self):
34         self.session = PipSession()
35         self.finder = PackageFinder(find_links=[],
36                                     index_urls=[self.DEFAULT_INDEX_URL],
37                                     session=self.session)
38 
39         # Caches
40         # stores project_name => InstallationCandidate mappings for all
41         # versions reported by PyPI, so we only have to ask once for each
42         # project
43         self._available_versions_cache = {}
44 
45         # Setup file paths
46         self.freshen_build_caches()
47         self._download_dir = os.path.join(CACHE_DIR, 'pkgs')
48         self._wheel_download_dir = os.path.join(CACHE_DIR, 'wheels')
49 
50     def freshen_build_caches(self):
51         """
52         Start with fresh build/source caches.  Will remove any old build
53         caches from disk automatically.
54         """
55         self._build_dir = TemporaryDirectory('build')
56         self._source_dir = TemporaryDirectory('source')
57 
58     @property
59     def build_dir(self):
60         return self._build_dir.name
61 
62     @property
63     def source_dir(self):
64         return self._source_dir.name
65 
66     def clear_caches(self):
67         rmtree(self._download_dir, ignore_errors=True)
68         rmtree(self._wheel_download_dir, ignore_errors=True)
69 
70     def find_all_versions(self, req_name):
71         if req_name not in self._available_versions_cache:
72             self._available_versions_cache[req_name] = self.finder._find_all_versions(req_name)
73         return self._available_versions_cache[req_name]
74 
75     def find_best_match(self, ireq, prereleases=None):
76         """
77         Returns a Version object that indicates the best match for the given
78         InstallRequirement according to the external repository.
79         """
80         if ireq.editable:
81             return ireq  # return itself as the best match
82 
83         all_candidates = self.find_all_versions(ireq.name)
84         candidates_by_version = lookup_table(all_candidates, key=lambda c: c.version, unique=True)
85         matching_versions = ireq.specifier.filter((candidate.version for candidate in all_candidates),
86                                                   prereleases=prereleases)
87 
88         # Reuses pip's internal candidate sort key to sort
89         matching_candidates = [candidates_by_version[ver] for ver in matching_versions]
90         if not matching_candidates:
91             raise NoCandidateFound(ireq, all_candidates)
92         best_candidate = max(matching_candidates, key=self.finder._candidate_sort_key)
93 
94         # Turn the candidate into a pinned InstallRequirement
95         return InstallRequirement.from_line('{}=={}'.format(best_candidate.project, str(best_candidate.version)))
96 
97     def get_dependencies(self, ireq):
98         """
99         Given a pinned or an editable InstallRequirement, returns a set of
100         dependencies (also InstallRequirements, but not necessarily pinned).
101         They indicate the secondary dependencies for the given requirement.
102         """
103         if not (ireq.editable or is_pinned_requirement(ireq)):
104             raise TypeError('Expected pinned or editable InstallRequirement, got {}'.format(ireq))
105 
106         if not os.path.isdir(self._download_dir):
107             os.makedirs(self._download_dir)
108         if not os.path.isdir(self._wheel_download_dir):
109             os.makedirs(self._wheel_download_dir)
110 
111         reqset = RequirementSet(self.build_dir,
112                                 self.source_dir,
113                                 download_dir=self._download_dir,
114                                 wheel_download_dir=self._wheel_download_dir,
115                                 session=self.session)
116         dependencies = reqset._prepare_file(self.finder, ireq)
117         return set(dependencies)
118
```

Path: `piptools/scripts/compile.py`
Content:
```
1 # coding: utf-8
2 # isort:skip_file
3 from __future__ import (absolute_import, division, print_function,
4                         unicode_literals)
5 
6 import sys
7 import pip
8 
9 # Make sure we're using a reasonably modern version of pip
10 if not tuple(int(digit) for digit in pip.__version__.split('.')[:2]) >= (6, 1):
11     print('pip-compile requires at least version 6.1 of pip ({} found), '
12           'perhaps run `pip install --upgrade pip`?'.format(pip.__version__))
13     sys.exit(4)
14 
15 from .. import click  # noqa
16 from pip.req import parse_requirements  # noqa
17 
18 from ..exceptions import PipToolsError  # noqa
19 from ..logging import log  # noqa
20 from ..repositories import PyPIRepository  # noqa
21 from ..resolver import Resolver  # noqa
22 from ..writer import OutputWriter  # noqa
23 
24 DEFAULT_REQUIREMENTS_FILE = 'requirements.in'
25 
26 
27 @click.command()
28 @click.option('-v', '--verbose', is_flag=True, help="Show more output")
29 @click.option('--dry-run', is_flag=True, help="Only show what would happen, don't change anything")
30 @click.option('-p', '--pre', is_flag=True, default=None, help="Allow resolving to prereleases (default is not)")
31 @click.option('-r', '--rebuild', is_flag=True, help="Clear any caches upfront, rebuild from scratch")
32 @click.option('-f', '--find-links', multiple=True, help="Look for archives in this directory or on this HTML page", envvar='PIP_FIND_LINKS')  # noqa
33 @click.option('-i', '--index-url', help="Change index URL (defaults to PyPI)", envvar='PIP_INDEX_URL')
34 @click.option('--extra-index-url', multiple=True, help="Add additional index URL to search", envvar='PIP_EXTRA_INDEX_URL')  # noqa
35 @click.option('--header/--no-header', is_flag=True, default=True, help="Add header to generated file")
36 @click.option('--annotate/--no-annotate', is_flag=True, default=True,
37               help="Annotate results, indicating where dependencies come from")
38 @click.argument('src_file', required=False, type=click.Path(exists=True), default=DEFAULT_REQUIREMENTS_FILE)
39 def cli(verbose, dry_run, pre, rebuild, find_links, index_url,
40         extra_index_url, header, annotate, src_file):
41     """Compiles requirements.txt from requirements.in specs."""
42     log.verbose = verbose
43 
44     if not src_file:
45         log.warning('No input files to process')
46         sys.exit(2)
47 
48     ###
49     # Setup
50     ###
51     repository = PyPIRepository()
52 
53     # Configure the finder
54     if index_url:
55         repository.finder.index_urls = [index_url]
56     repository.finder.index_urls.extend(extra_index_url)
57     repository.finder.find_links.extend(find_links)
58 
59     log.debug('Using indexes:')
60     for index_url in repository.finder.index_urls:
61         log.debug('  {}'.format(index_url))
62 
63     if repository.finder.find_links:
64         log.debug('')
65         log.debug('Configuration:')
66         for find_link in repository.finder.find_links:
67             log.debug('  -f {}'.format(find_link))
68 
69     ###
70     # Parsing/collecting initial requirements
71     ###
72     constraints = []
73     for line in parse_requirements(src_file, finder=repository.finder, session=repository.session):
74         constraints.append(line)
75 
76     try:
77         resolver = Resolver(constraints, repository, prereleases=pre, clear_caches=rebuild)
78         results = resolver.resolve()
79     except PipToolsError as e:
80         log.error(str(e))
81         sys.exit(2)
82 
83     log.debug('')
84 
85     ##
86     # Output
87     ##
88 
89     # Compute reverse dependency annotations statically, from the
90     # dependency cache that the resolver has populated by now.
91     #
92     # TODO (1a): reverse deps for any editable package are lost
93     #            what SHOULD happen is that they are cached in memory, just
94     #            not persisted to disk!
95     #
96     # TODO (1b): perhaps it's easiest if the dependency cache has an API
97     #            that could take InstallRequirements directly, like:
98     #
99     #                cache.set(ireq, ...)
100     #
101     #            then, when ireq is editable, it would store in
102     #
103     #              editables[egg_name][link_without_fragment] = deps
104     #              editables['pip-tools']['git+...ols.git@future'] = {'click>=3.0', 'six'}
105     #
106     #            otherwise:
107     #
108     #              self[as_name_version_tuple(ireq)] = {'click>=3.0', 'six'}
109     #
110     reverse_dependencies = None
111     if annotate:
112         reverse_dependencies = resolver.reverse_dependencies(results)
113 
114     writer = OutputWriter(src_file, dry_run=dry_run, header=header,
115                           annotate=annotate,
116                           default_index_url=repository.DEFAULT_INDEX_URL,
117                           index_urls=repository.finder.index_urls)
118     writer.write(results=results,
119                  reverse_dependencies=reverse_dependencies,
120                  primary_packages={ireq.req.key for ireq in constraints})
121 
122     if dry_run:
123         log.warning('Dry-run, so nothing updated.')
124
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

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 @@
     ###
     # 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 @@
     # 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:

{"golden_diff": "diff --git a/piptools/repositories/pypi.py b/piptools/repositories/pypi.py\n--- a/piptools/repositories/pypi.py\n+++ b/piptools/repositories/pypi.py\n@@ -23,19 +23,30 @@\n \n class PyPIRepository(BaseRepository):\n     DEFAULT_INDEX_URL = 'https://pypi.python.org/simple/'\n-\n+    \n     \"\"\"\n     The PyPIRepository will use the provided Finder instance to lookup\n     packages.  Typically, it looks up packages on PyPI (the default implicit\n     config), but any other PyPI mirror can be used if index_urls is\n     changed/configured on the Finder.\n     \"\"\"\n-    def __init__(self):\n+    def __init__(self, pip_options):\n         self.session = PipSession()\n-        self.finder = PackageFinder(find_links=[],\n-                                    index_urls=[self.DEFAULT_INDEX_URL],\n-                                    session=self.session)\n \n+        index_urls = [pip_options.index_url] + pip_options.extra_index_urls\n+        if pip_options.no_index:\n+            logger.info('Ignoring indexes: %s', ','.join(index_urls))\n+            index_urls = []\n+\n+        self.finder = PackageFinder(\n+            find_links=pip_options.find_links,\n+            index_urls=index_urls,\n+            trusted_hosts=pip_options.trusted_hosts,\n+            allow_all_prereleases=pip_options.pre,\n+            process_dependency_links=pip_options.process_dependency_links,\n+            session=self.session,\n+        )\n+        \n         # Caches\n         # stores project_name => InstallationCandidate mappings for all\n         # versions reported by PyPI, so we only have to ask once for each\ndiff --git a/piptools/scripts/compile.py b/piptools/scripts/compile.py\n--- a/piptools/scripts/compile.py\n+++ b/piptools/scripts/compile.py\n@@ -3,6 +3,7 @@\n from __future__ import (absolute_import, division, print_function,\n                         unicode_literals)\n \n+import optparse\n import sys\n import pip\n \n@@ -23,6 +24,10 @@\n \n DEFAULT_REQUIREMENTS_FILE = 'requirements.in'\n \n+# emulate pip's option parsing with a stub command\n+class PipCommand(pip.basecommand.Command):\n+    name = 'PipCommand'\n+\n \n @click.command()\n @click.option('-v', '--verbose', is_flag=True, help=\"Show more output\")\n@@ -48,13 +53,31 @@\n     ###\n     # Setup\n     ###\n-    repository = PyPIRepository()\n \n-    # Configure the finder\n+    # use pip's parser for pip.conf management and defaults\n+    # general options (find_links, index_url, extra_index_url abd pre) are defered to pip.\n+    pip_options = PipCommand()\n+    index_opts = pip.cmdoptions.make_option_group(\n+        pip.cmdoptions.index_group,\n+        pip_options.parser,\n+    )\n+    pip_options.parser.insert_option_group(0, index_opts)\n+    #support pip pre\n+    pip_options.parser.add_option(optparse.Option('--pre', action='store_true', default=False))\n+\n+    pip_args = []\n+    if find_links:\n+        pip_args.extend(['-f', find_links])\n     if index_url:\n-        repository.finder.index_urls = [index_url]\n-    repository.finder.index_urls.extend(extra_index_url)\n-    repository.finder.find_links.extend(find_links)\n+        pip_args.extend(['-i', index_url])\n+    if extra_index_url:\n+        pip_args.extend(['--extra-index-url', extra_index_url])\n+    if pre:\n+        pip_args.extend(['--pre'])\n+\n+    pip_options, _ = pip_options.parse_args(pip_args)\n+\n+    repository = PyPIRepository(pip_options)\n \n     log.debug('Using indexes:')\n     for index_url in repository.finder.index_urls:\n@@ -70,7 +93,7 @@\n     # Parsing/collecting initial requirements\n     ###\n     constraints = []\n-    for line in parse_requirements(src_file, finder=repository.finder, session=repository.session):\n+    for line in parse_requirements(src_file, finder=repository.finder, session=repository.session, options=pip_options):\n         constraints.append(line)\n \n     try:\n", "issue": "pip-compile honor pip.conf\n\n", "before_files": [{"content": "# coding: utf-8\nfrom __future__ import (absolute_import, division, print_function,\n                        unicode_literals)\n\nimport os\nfrom shutil import rmtree\n\nfrom pip.download import PipSession\nfrom pip.index import PackageFinder\nfrom pip.req import InstallRequirement\nfrom pip.req.req_set import RequirementSet\n\nfrom ..cache import CACHE_DIR\nfrom ..exceptions import NoCandidateFound\nfrom ..utils import is_pinned_requirement, lookup_table\nfrom .base import BaseRepository\n\ntry:\n    from tempfile import TemporaryDirectory  # added in 3.2\nexcept ImportError:\n    from .._compat import TemporaryDirectory\n\n\nclass PyPIRepository(BaseRepository):\n    DEFAULT_INDEX_URL = 'https://pypi.python.org/simple/'\n\n    \"\"\"\n    The PyPIRepository will use the provided Finder instance to lookup\n    packages.  Typically, it looks up packages on PyPI (the default implicit\n    config), but any other PyPI mirror can be used if index_urls is\n    changed/configured on the Finder.\n    \"\"\"\n    def __init__(self):\n        self.session = PipSession()\n        self.finder = PackageFinder(find_links=[],\n                                    index_urls=[self.DEFAULT_INDEX_URL],\n                                    session=self.session)\n\n        # Caches\n        # stores project_name => InstallationCandidate mappings for all\n        # versions reported by PyPI, so we only have to ask once for each\n        # project\n        self._available_versions_cache = {}\n\n        # Setup file paths\n        self.freshen_build_caches()\n        self._download_dir = os.path.join(CACHE_DIR, 'pkgs')\n        self._wheel_download_dir = os.path.join(CACHE_DIR, 'wheels')\n\n    def freshen_build_caches(self):\n        \"\"\"\n        Start with fresh build/source caches.  Will remove any old build\n        caches from disk automatically.\n        \"\"\"\n        self._build_dir = TemporaryDirectory('build')\n        self._source_dir = TemporaryDirectory('source')\n\n    @property\n    def build_dir(self):\n        return self._build_dir.name\n\n    @property\n    def source_dir(self):\n        return self._source_dir.name\n\n    def clear_caches(self):\n        rmtree(self._download_dir, ignore_errors=True)\n        rmtree(self._wheel_download_dir, ignore_errors=True)\n\n    def find_all_versions(self, req_name):\n        if req_name not in self._available_versions_cache:\n            self._available_versions_cache[req_name] = self.finder._find_all_versions(req_name)\n        return self._available_versions_cache[req_name]\n\n    def find_best_match(self, ireq, prereleases=None):\n        \"\"\"\n        Returns a Version object that indicates the best match for the given\n        InstallRequirement according to the external repository.\n        \"\"\"\n        if ireq.editable:\n            return ireq  # return itself as the best match\n\n        all_candidates = self.find_all_versions(ireq.name)\n        candidates_by_version = lookup_table(all_candidates, key=lambda c: c.version, unique=True)\n        matching_versions = ireq.specifier.filter((candidate.version for candidate in all_candidates),\n                                                  prereleases=prereleases)\n\n        # Reuses pip's internal candidate sort key to sort\n        matching_candidates = [candidates_by_version[ver] for ver in matching_versions]\n        if not matching_candidates:\n            raise NoCandidateFound(ireq, all_candidates)\n        best_candidate = max(matching_candidates, key=self.finder._candidate_sort_key)\n\n        # Turn the candidate into a pinned InstallRequirement\n        return InstallRequirement.from_line('{}=={}'.format(best_candidate.project, str(best_candidate.version)))\n\n    def get_dependencies(self, ireq):\n        \"\"\"\n        Given a pinned or an editable InstallRequirement, returns a set of\n        dependencies (also InstallRequirements, but not necessarily pinned).\n        They indicate the secondary dependencies for the given requirement.\n        \"\"\"\n        if not (ireq.editable or is_pinned_requirement(ireq)):\n            raise TypeError('Expected pinned or editable InstallRequirement, got {}'.format(ireq))\n\n        if not os.path.isdir(self._download_dir):\n            os.makedirs(self._download_dir)\n        if not os.path.isdir(self._wheel_download_dir):\n            os.makedirs(self._wheel_download_dir)\n\n        reqset = RequirementSet(self.build_dir,\n                                self.source_dir,\n                                download_dir=self._download_dir,\n                                wheel_download_dir=self._wheel_download_dir,\n                                session=self.session)\n        dependencies = reqset._prepare_file(self.finder, ireq)\n        return set(dependencies)\n", "path": "piptools/repositories/pypi.py"}, {"content": "# coding: utf-8\n# isort:skip_file\nfrom __future__ import (absolute_import, division, print_function,\n                        unicode_literals)\n\nimport sys\nimport pip\n\n# Make sure we're using a reasonably modern version of pip\nif not tuple(int(digit) for digit in pip.__version__.split('.')[:2]) >= (6, 1):\n    print('pip-compile requires at least version 6.1 of pip ({} found), '\n          'perhaps run `pip install --upgrade pip`?'.format(pip.__version__))\n    sys.exit(4)\n\nfrom .. import click  # noqa\nfrom pip.req import parse_requirements  # noqa\n\nfrom ..exceptions import PipToolsError  # noqa\nfrom ..logging import log  # noqa\nfrom ..repositories import PyPIRepository  # noqa\nfrom ..resolver import Resolver  # noqa\nfrom ..writer import OutputWriter  # noqa\n\nDEFAULT_REQUIREMENTS_FILE = 'requirements.in'\n\n\n@click.command()\n@click.option('-v', '--verbose', is_flag=True, help=\"Show more output\")\n@click.option('--dry-run', is_flag=True, help=\"Only show what would happen, don't change anything\")\n@click.option('-p', '--pre', is_flag=True, default=None, help=\"Allow resolving to prereleases (default is not)\")\n@click.option('-r', '--rebuild', is_flag=True, help=\"Clear any caches upfront, rebuild from scratch\")\n@click.option('-f', '--find-links', multiple=True, help=\"Look for archives in this directory or on this HTML page\", envvar='PIP_FIND_LINKS')  # noqa\n@click.option('-i', '--index-url', help=\"Change index URL (defaults to PyPI)\", envvar='PIP_INDEX_URL')\n@click.option('--extra-index-url', multiple=True, help=\"Add additional index URL to search\", envvar='PIP_EXTRA_INDEX_URL')  # noqa\n@click.option('--header/--no-header', is_flag=True, default=True, help=\"Add header to generated file\")\n@click.option('--annotate/--no-annotate', is_flag=True, default=True,\n              help=\"Annotate results, indicating where dependencies come from\")\n@click.argument('src_file', required=False, type=click.Path(exists=True), default=DEFAULT_REQUIREMENTS_FILE)\ndef cli(verbose, dry_run, pre, rebuild, find_links, index_url,\n        extra_index_url, header, annotate, src_file):\n    \"\"\"Compiles requirements.txt from requirements.in specs.\"\"\"\n    log.verbose = verbose\n\n    if not src_file:\n        log.warning('No input files to process')\n        sys.exit(2)\n\n    ###\n    # Setup\n    ###\n    repository = PyPIRepository()\n\n    # Configure the finder\n    if index_url:\n        repository.finder.index_urls = [index_url]\n    repository.finder.index_urls.extend(extra_index_url)\n    repository.finder.find_links.extend(find_links)\n\n    log.debug('Using indexes:')\n    for index_url in repository.finder.index_urls:\n        log.debug('  {}'.format(index_url))\n\n    if repository.finder.find_links:\n        log.debug('')\n        log.debug('Configuration:')\n        for find_link in repository.finder.find_links:\n            log.debug('  -f {}'.format(find_link))\n\n    ###\n    # Parsing/collecting initial requirements\n    ###\n    constraints = []\n    for line in parse_requirements(src_file, finder=repository.finder, session=repository.session):\n        constraints.append(line)\n\n    try:\n        resolver = Resolver(constraints, repository, prereleases=pre, clear_caches=rebuild)\n        results = resolver.resolve()\n    except PipToolsError as e:\n        log.error(str(e))\n        sys.exit(2)\n\n    log.debug('')\n\n    ##\n    # Output\n    ##\n\n    # Compute reverse dependency annotations statically, from the\n    # dependency cache that the resolver has populated by now.\n    #\n    # TODO (1a): reverse deps for any editable package are lost\n    #            what SHOULD happen is that they are cached in memory, just\n    #            not persisted to disk!\n    #\n    # TODO (1b): perhaps it's easiest if the dependency cache has an API\n    #            that could take InstallRequirements directly, like:\n    #\n    #                cache.set(ireq, ...)\n    #\n    #            then, when ireq is editable, it would store in\n    #\n    #              editables[egg_name][link_without_fragment] = deps\n    #              editables['pip-tools']['git+...ols.git@future'] = {'click>=3.0', 'six'}\n    #\n    #            otherwise:\n    #\n    #              self[as_name_version_tuple(ireq)] = {'click>=3.0', 'six'}\n    #\n    reverse_dependencies = None\n    if annotate:\n        reverse_dependencies = resolver.reverse_dependencies(results)\n\n    writer = OutputWriter(src_file, dry_run=dry_run, header=header,\n                          annotate=annotate,\n                          default_index_url=repository.DEFAULT_INDEX_URL,\n                          index_urls=repository.finder.index_urls)\n    writer.write(results=results,\n                 reverse_dependencies=reverse_dependencies,\n                 primary_packages={ireq.req.key for ireq in constraints})\n\n    if dry_run:\n        log.warning('Dry-run, so nothing updated.')\n", "path": "piptools/scripts/compile.py"}], "after_files": [{"content": "# coding: utf-8\nfrom __future__ import (absolute_import, division, print_function,\n                        unicode_literals)\n\nimport os\nfrom shutil import rmtree\n\nfrom pip.download import PipSession\nfrom pip.index import PackageFinder\nfrom pip.req import InstallRequirement\nfrom pip.req.req_set import RequirementSet\n\nfrom ..cache import CACHE_DIR\nfrom ..exceptions import NoCandidateFound\nfrom ..utils import is_pinned_requirement, lookup_table\nfrom .base import BaseRepository\n\ntry:\n    from tempfile import TemporaryDirectory  # added in 3.2\nexcept ImportError:\n    from .._compat import TemporaryDirectory\n\n\nclass PyPIRepository(BaseRepository):\n    DEFAULT_INDEX_URL = 'https://pypi.python.org/simple/'\n    \n    \"\"\"\n    The PyPIRepository will use the provided Finder instance to lookup\n    packages.  Typically, it looks up packages on PyPI (the default implicit\n    config), but any other PyPI mirror can be used if index_urls is\n    changed/configured on the Finder.\n    \"\"\"\n    def __init__(self, pip_options):\n        self.session = PipSession()\n\n        index_urls = [pip_options.index_url] + pip_options.extra_index_urls\n        if pip_options.no_index:\n            logger.info('Ignoring indexes: %s', ','.join(index_urls))\n            index_urls = []\n\n        self.finder = PackageFinder(\n            find_links=pip_options.find_links,\n            index_urls=index_urls,\n            trusted_hosts=pip_options.trusted_hosts,\n            allow_all_prereleases=pip_options.pre,\n            process_dependency_links=pip_options.process_dependency_links,\n            session=self.session,\n        )\n        \n        # Caches\n        # stores project_name => InstallationCandidate mappings for all\n        # versions reported by PyPI, so we only have to ask once for each\n        # project\n        self._available_versions_cache = {}\n\n        # Setup file paths\n        self.freshen_build_caches()\n        self._download_dir = os.path.join(CACHE_DIR, 'pkgs')\n        self._wheel_download_dir = os.path.join(CACHE_DIR, 'wheels')\n\n    def freshen_build_caches(self):\n        \"\"\"\n        Start with fresh build/source caches.  Will remove any old build\n        caches from disk automatically.\n        \"\"\"\n        self._build_dir = TemporaryDirectory('build')\n        self._source_dir = TemporaryDirectory('source')\n\n    @property\n    def build_dir(self):\n        return self._build_dir.name\n\n    @property\n    def source_dir(self):\n        return self._source_dir.name\n\n    def clear_caches(self):\n        rmtree(self._download_dir, ignore_errors=True)\n        rmtree(self._wheel_download_dir, ignore_errors=True)\n\n    def find_all_versions(self, req_name):\n        if req_name not in self._available_versions_cache:\n            self._available_versions_cache[req_name] = self.finder._find_all_versions(req_name)\n        return self._available_versions_cache[req_name]\n\n    def find_best_match(self, ireq, prereleases=None):\n        \"\"\"\n        Returns a Version object that indicates the best match for the given\n        InstallRequirement according to the external repository.\n        \"\"\"\n        if ireq.editable:\n            return ireq  # return itself as the best match\n\n        all_candidates = self.find_all_versions(ireq.name)\n        candidates_by_version = lookup_table(all_candidates, key=lambda c: c.version, unique=True)\n        matching_versions = ireq.specifier.filter((candidate.version for candidate in all_candidates),\n                                                  prereleases=prereleases)\n\n        # Reuses pip's internal candidate sort key to sort\n        matching_candidates = [candidates_by_version[ver] for ver in matching_versions]\n        if not matching_candidates:\n            raise NoCandidateFound(ireq, all_candidates)\n        best_candidate = max(matching_candidates, key=self.finder._candidate_sort_key)\n\n        # Turn the candidate into a pinned InstallRequirement\n        return InstallRequirement.from_line('{}=={}'.format(best_candidate.project, str(best_candidate.version)))\n\n    def get_dependencies(self, ireq):\n        \"\"\"\n        Given a pinned or an editable InstallRequirement, returns a set of\n        dependencies (also InstallRequirements, but not necessarily pinned).\n        They indicate the secondary dependencies for the given requirement.\n        \"\"\"\n        if not (ireq.editable or is_pinned_requirement(ireq)):\n            raise TypeError('Expected pinned or editable InstallRequirement, got {}'.format(ireq))\n\n        if not os.path.isdir(self._download_dir):\n            os.makedirs(self._download_dir)\n        if not os.path.isdir(self._wheel_download_dir):\n            os.makedirs(self._wheel_download_dir)\n\n        reqset = RequirementSet(self.build_dir,\n                                self.source_dir,\n                                download_dir=self._download_dir,\n                                wheel_download_dir=self._wheel_download_dir,\n                                session=self.session)\n        dependencies = reqset._prepare_file(self.finder, ireq)\n        return set(dependencies)\n", "path": "piptools/repositories/pypi.py"}, {"content": "# coding: utf-8\n# isort:skip_file\nfrom __future__ import (absolute_import, division, print_function,\n                        unicode_literals)\n\nimport optparse\nimport sys\nimport pip\n\n# Make sure we're using a reasonably modern version of pip\nif not tuple(int(digit) for digit in pip.__version__.split('.')[:2]) >= (6, 1):\n    print('pip-compile requires at least version 6.1 of pip ({} found), '\n          'perhaps run `pip install --upgrade pip`?'.format(pip.__version__))\n    sys.exit(4)\n\nfrom .. import click  # noqa\nfrom pip.req import parse_requirements  # noqa\n\nfrom ..exceptions import PipToolsError  # noqa\nfrom ..logging import log  # noqa\nfrom ..repositories import PyPIRepository  # noqa\nfrom ..resolver import Resolver  # noqa\nfrom ..writer import OutputWriter  # noqa\n\nDEFAULT_REQUIREMENTS_FILE = 'requirements.in'\n\n# emulate pip's option parsing with a stub command\nclass PipCommand(pip.basecommand.Command):\n    name = 'PipCommand'\n\n\n@click.command()\n@click.option('-v', '--verbose', is_flag=True, help=\"Show more output\")\n@click.option('--dry-run', is_flag=True, help=\"Only show what would happen, don't change anything\")\n@click.option('-p', '--pre', is_flag=True, default=None, help=\"Allow resolving to prereleases (default is not)\")\n@click.option('-r', '--rebuild', is_flag=True, help=\"Clear any caches upfront, rebuild from scratch\")\n@click.option('-f', '--find-links', multiple=True, help=\"Look for archives in this directory or on this HTML page\", envvar='PIP_FIND_LINKS')  # noqa\n@click.option('-i', '--index-url', help=\"Change index URL (defaults to PyPI)\", envvar='PIP_INDEX_URL')\n@click.option('--extra-index-url', multiple=True, help=\"Add additional index URL to search\", envvar='PIP_EXTRA_INDEX_URL')  # noqa\n@click.option('--header/--no-header', is_flag=True, default=True, help=\"Add header to generated file\")\n@click.option('--annotate/--no-annotate', is_flag=True, default=True,\n              help=\"Annotate results, indicating where dependencies come from\")\n@click.argument('src_file', required=False, type=click.Path(exists=True), default=DEFAULT_REQUIREMENTS_FILE)\ndef cli(verbose, dry_run, pre, rebuild, find_links, index_url,\n        extra_index_url, header, annotate, src_file):\n    \"\"\"Compiles requirements.txt from requirements.in specs.\"\"\"\n    log.verbose = verbose\n\n    if not src_file:\n        log.warning('No input files to process')\n        sys.exit(2)\n\n    ###\n    # Setup\n    ###\n\n    # use pip's parser for pip.conf management and defaults\n    # general options (find_links, index_url, extra_index_url abd pre) are defered to pip.\n    pip_options = PipCommand()\n    index_opts = pip.cmdoptions.make_option_group(\n        pip.cmdoptions.index_group,\n        pip_options.parser,\n    )\n    pip_options.parser.insert_option_group(0, index_opts)\n    #support pip pre\n    pip_options.parser.add_option(optparse.Option('--pre', action='store_true', default=False))\n\n    pip_args = []\n    if find_links:\n        pip_args.extend(['-f', find_links])\n    if index_url:\n        pip_args.extend(['-i', index_url])\n    if extra_index_url:\n        pip_args.extend(['--extra-index-url', extra_index_url])\n    if pre:\n        pip_args.extend(['--pre'])\n\n    pip_options, _ = pip_options.parse_args(pip_args)\n\n    repository = PyPIRepository(pip_options)\n\n    log.debug('Using indexes:')\n    for index_url in repository.finder.index_urls:\n        log.debug('  {}'.format(index_url))\n\n    if repository.finder.find_links:\n        log.debug('')\n        log.debug('Configuration:')\n        for find_link in repository.finder.find_links:\n            log.debug('  -f {}'.format(find_link))\n\n    ###\n    # Parsing/collecting initial requirements\n    ###\n    constraints = []\n    for line in parse_requirements(src_file, finder=repository.finder, session=repository.session, options=pip_options):\n        constraints.append(line)\n\n    try:\n        resolver = Resolver(constraints, repository, prereleases=pre, clear_caches=rebuild)\n        results = resolver.resolve()\n    except PipToolsError as e:\n        log.error(str(e))\n        sys.exit(2)\n\n    log.debug('')\n\n    ##\n    # Output\n    ##\n\n    # Compute reverse dependency annotations statically, from the\n    # dependency cache that the resolver has populated by now.\n    #\n    # TODO (1a): reverse deps for any editable package are lost\n    #            what SHOULD happen is that they are cached in memory, just\n    #            not persisted to disk!\n    #\n    # TODO (1b): perhaps it's easiest if the dependency cache has an API\n    #            that could take InstallRequirements directly, like:\n    #\n    #                cache.set(ireq, ...)\n    #\n    #            then, when ireq is editable, it would store in\n    #\n    #              editables[egg_name][link_without_fragment] = deps\n    #              editables['pip-tools']['git+...ols.git@future'] = {'click>=3.0', 'six'}\n    #\n    #            otherwise:\n    #\n    #              self[as_name_version_tuple(ireq)] = {'click>=3.0', 'six'}\n    #\n    reverse_dependencies = None\n    if annotate:\n        reverse_dependencies = resolver.reverse_dependencies(results)\n\n    writer = OutputWriter(src_file, dry_run=dry_run, header=header,\n                          annotate=annotate,\n                          default_index_url=repository.DEFAULT_INDEX_URL,\n                          index_urls=repository.finder.index_urls)\n    writer.write(results=results,\n                 reverse_dependencies=reverse_dependencies,\n                 primary_packages={ireq.req.key for ireq in constraints})\n\n    if dry_run:\n        log.warning('Dry-run, so nothing updated.')\n", "path": "piptools/scripts/compile.py"}]}