We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
deprecation message for list format suggests wrong configuration value
* Pip version: 9.0.0
* Python version: 2.7.12
* Operating System: OS X

### Description:

The deprecation message emitted from `pip list` about the `--format` option suggests the wrong configuration value.

### What I've run:

```
$ pip list --outdated
DEPRECATION: The default format will switch to columns in the future. You can use --format=legacy (or define a list_format in your pip.conf) to disable this warning.
```

The correct configuration option is:

```ini
[list]
format =
```

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `pip/commands/list.py`
Content:
```
1 from __future__ import absolute_import
2 
3 import json
4 import logging
5 import warnings
6 try:
7     from itertools import zip_longest
8 except ImportError:
9     from itertools import izip_longest as zip_longest
10 
11 from pip._vendor import six
12 
13 from pip.basecommand import Command
14 from pip.exceptions import CommandError
15 from pip.index import PackageFinder
16 from pip.utils import (
17     get_installed_distributions, dist_is_editable)
18 from pip.utils.deprecation import RemovedInPip10Warning
19 from pip.cmdoptions import make_option_group, index_group
20 
21 logger = logging.getLogger(__name__)
22 
23 
24 class ListCommand(Command):
25     """
26     List installed packages, including editables.
27 
28     Packages are listed in a case-insensitive sorted order.
29     """
30     name = 'list'
31     usage = """
32       %prog [options]"""
33     summary = 'List installed packages.'
34 
35     def __init__(self, *args, **kw):
36         super(ListCommand, self).__init__(*args, **kw)
37 
38         cmd_opts = self.cmd_opts
39 
40         cmd_opts.add_option(
41             '-o', '--outdated',
42             action='store_true',
43             default=False,
44             help='List outdated packages')
45         cmd_opts.add_option(
46             '-u', '--uptodate',
47             action='store_true',
48             default=False,
49             help='List uptodate packages')
50         cmd_opts.add_option(
51             '-e', '--editable',
52             action='store_true',
53             default=False,
54             help='List editable projects.')
55         cmd_opts.add_option(
56             '-l', '--local',
57             action='store_true',
58             default=False,
59             help=('If in a virtualenv that has global access, do not list '
60                   'globally-installed packages.'),
61         )
62         self.cmd_opts.add_option(
63             '--user',
64             dest='user',
65             action='store_true',
66             default=False,
67             help='Only output packages installed in user-site.')
68 
69         cmd_opts.add_option(
70             '--pre',
71             action='store_true',
72             default=False,
73             help=("Include pre-release and development versions. By default, "
74                   "pip only finds stable versions."),
75         )
76 
77         cmd_opts.add_option(
78             '--format',
79             action='store',
80             dest='list_format',
81             choices=('legacy', 'columns', 'freeze', 'json'),
82             help="Select the output format among: legacy (default), columns, "
83                  "freeze or json.",
84         )
85 
86         cmd_opts.add_option(
87             '--not-required',
88             action='store_true',
89             dest='not_required',
90             help="List packages that are not dependencies of "
91                  "installed packages.",
92         )
93 
94         index_opts = make_option_group(index_group, self.parser)
95 
96         self.parser.insert_option_group(0, index_opts)
97         self.parser.insert_option_group(0, cmd_opts)
98 
99     def _build_package_finder(self, options, index_urls, session):
100         """
101         Create a package finder appropriate to this list command.
102         """
103         return PackageFinder(
104             find_links=options.find_links,
105             index_urls=index_urls,
106             allow_all_prereleases=options.pre,
107             trusted_hosts=options.trusted_hosts,
108             process_dependency_links=options.process_dependency_links,
109             session=session,
110         )
111 
112     def run(self, options, args):
113         if options.allow_external:
114             warnings.warn(
115                 "--allow-external has been deprecated and will be removed in "
116                 "the future. Due to changes in the repository protocol, it no "
117                 "longer has any effect.",
118                 RemovedInPip10Warning,
119             )
120 
121         if options.allow_all_external:
122             warnings.warn(
123                 "--allow-all-external has been deprecated and will be removed "
124                 "in the future. Due to changes in the repository protocol, it "
125                 "no longer has any effect.",
126                 RemovedInPip10Warning,
127             )
128 
129         if options.allow_unverified:
130             warnings.warn(
131                 "--allow-unverified has been deprecated and will be removed "
132                 "in the future. Due to changes in the repository protocol, it "
133                 "no longer has any effect.",
134                 RemovedInPip10Warning,
135             )
136 
137         if options.list_format is None:
138             warnings.warn(
139                 "The default format will switch to columns in the future. "
140                 "You can use --format=legacy (or define a list_format "
141                 "in your pip.conf) to disable this warning.",
142                 RemovedInPip10Warning,
143             )
144 
145         if options.outdated and options.uptodate:
146             raise CommandError(
147                 "Options --outdated and --uptodate cannot be combined.")
148 
149         packages = get_installed_distributions(
150             local_only=options.local,
151             user_only=options.user,
152             editables_only=options.editable,
153         )
154 
155         if options.outdated:
156             packages = self.get_outdated(packages, options)
157         elif options.uptodate:
158             packages = self.get_uptodate(packages, options)
159 
160         if options.not_required:
161             packages = self.get_not_required(packages, options)
162 
163         self.output_package_listing(packages, options)
164 
165     def get_outdated(self, packages, options):
166         return [
167             dist for dist in self.iter_packages_latest_infos(packages, options)
168             if dist.latest_version > dist.parsed_version
169         ]
170 
171     def get_uptodate(self, packages, options):
172         return [
173             dist for dist in self.iter_packages_latest_infos(packages, options)
174             if dist.latest_version == dist.parsed_version
175         ]
176 
177     def get_not_required(self, packages, options):
178         dep_keys = set()
179         for dist in packages:
180             dep_keys.update(requirement.key for requirement in dist.requires())
181         return set(pkg for pkg in packages if pkg.key not in dep_keys)
182 
183     def iter_packages_latest_infos(self, packages, options):
184         index_urls = [options.index_url] + options.extra_index_urls
185         if options.no_index:
186             logger.debug('Ignoring indexes: %s', ','.join(index_urls))
187             index_urls = []
188 
189         dependency_links = []
190         for dist in packages:
191             if dist.has_metadata('dependency_links.txt'):
192                 dependency_links.extend(
193                     dist.get_metadata_lines('dependency_links.txt'),
194                 )
195 
196         with self._build_session(options) as session:
197             finder = self._build_package_finder(options, index_urls, session)
198             finder.add_dependency_links(dependency_links)
199 
200             for dist in packages:
201                 typ = 'unknown'
202                 all_candidates = finder.find_all_candidates(dist.key)
203                 if not options.pre:
204                     # Remove prereleases
205                     all_candidates = [candidate for candidate in all_candidates
206                                       if not candidate.version.is_prerelease]
207 
208                 if not all_candidates:
209                     continue
210                 best_candidate = max(all_candidates,
211                                      key=finder._candidate_sort_key)
212                 remote_version = best_candidate.version
213                 if best_candidate.location.is_wheel:
214                     typ = 'wheel'
215                 else:
216                     typ = 'sdist'
217                 # This is dirty but makes the rest of the code much cleaner
218                 dist.latest_version = remote_version
219                 dist.latest_filetype = typ
220                 yield dist
221 
222     def output_legacy(self, dist):
223         if dist_is_editable(dist):
224             return '%s (%s, %s)' % (
225                 dist.project_name,
226                 dist.version,
227                 dist.location,
228             )
229         else:
230             return '%s (%s)' % (dist.project_name, dist.version)
231 
232     def output_legacy_latest(self, dist):
233         return '%s - Latest: %s [%s]' % (
234             self.output_legacy(dist),
235             dist.latest_version,
236             dist.latest_filetype,
237         )
238 
239     def output_package_listing(self, packages, options):
240         packages = sorted(
241             packages,
242             key=lambda dist: dist.project_name.lower(),
243         )
244         if options.list_format == 'columns' and packages:
245             data, header = format_for_columns(packages, options)
246             self.output_package_listing_columns(data, header)
247         elif options.list_format == 'freeze':
248             for dist in packages:
249                 logger.info("%s==%s", dist.project_name, dist.version)
250         elif options.list_format == 'json':
251             logger.info(format_for_json(packages, options))
252         else:  # legacy
253             for dist in packages:
254                 if options.outdated:
255                     logger.info(self.output_legacy_latest(dist))
256                 else:
257                     logger.info(self.output_legacy(dist))
258 
259     def output_package_listing_columns(self, data, header):
260         # insert the header first: we need to know the size of column names
261         if len(data) > 0:
262             data.insert(0, header)
263 
264         pkg_strings, sizes = tabulate(data)
265 
266         # Create and add a separator.
267         if len(data) > 0:
268             pkg_strings.insert(1, " ".join(map(lambda x: '-' * x, sizes)))
269 
270         for val in pkg_strings:
271             logger.info(val)
272 
273 
274 def tabulate(vals):
275     # From pfmoore on GitHub:
276     # https://github.com/pypa/pip/issues/3651#issuecomment-216932564
277     assert len(vals) > 0
278 
279     sizes = [0] * max(len(x) for x in vals)
280     for row in vals:
281         sizes = [max(s, len(str(c))) for s, c in zip_longest(sizes, row)]
282 
283     result = []
284     for row in vals:
285         display = " ".join([str(c).ljust(s) if c is not None else ''
286                             for s, c in zip_longest(sizes, row)])
287         result.append(display)
288 
289     return result, sizes
290 
291 
292 def format_for_columns(pkgs, options):
293     """
294     Convert the package data into something usable
295     by output_package_listing_columns.
296     """
297     running_outdated = options.outdated
298     # Adjust the header for the `pip list --outdated` case.
299     if running_outdated:
300         header = ["Package", "Version", "Latest", "Type"]
301     else:
302         header = ["Package", "Version"]
303 
304     data = []
305     if any(dist_is_editable(x) for x in pkgs):
306         header.append("Location")
307 
308     for proj in pkgs:
309         # if we're working on the 'outdated' list, separate out the
310         # latest_version and type
311         row = [proj.project_name, proj.version]
312 
313         if running_outdated:
314             row.append(proj.latest_version)
315             row.append(proj.latest_filetype)
316 
317         if dist_is_editable(proj):
318             row.append(proj.location)
319 
320         data.append(row)
321 
322     return data, header
323 
324 
325 def format_for_json(packages, options):
326     data = []
327     for dist in packages:
328         info = {
329             'name': dist.project_name,
330             'version': six.text_type(dist.version),
331         }
332         if options.outdated:
333             info['latest_version'] = six.text_type(dist.latest_version)
334             info['latest_filetype'] = dist.latest_filetype
335         data.append(info)
336     return json.dumps(data)
337
```
--- 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/pip/commands/list.py b/pip/commands/list.py
--- a/pip/commands/list.py
+++ b/pip/commands/list.py
@@ -137,8 +137,9 @@
         if options.list_format is None:
             warnings.warn(
                 "The default format will switch to columns in the future. "
-                "You can use --format=legacy (or define a list_format "
-                "in your pip.conf) to disable this warning.",
+                "You can use --format=(legacy|columns) (or define a "
+                "format=(legacy|columns) in your pip.conf under the [list] "
+                "section) to disable this warning.",
                 RemovedInPip10Warning,
             )

{"golden_diff": "diff --git a/pip/commands/list.py b/pip/commands/list.py\n--- a/pip/commands/list.py\n+++ b/pip/commands/list.py\n@@ -137,8 +137,9 @@\n         if options.list_format is None:\n             warnings.warn(\n                 \"The default format will switch to columns in the future. \"\n-                \"You can use --format=legacy (or define a list_format \"\n-                \"in your pip.conf) to disable this warning.\",\n+                \"You can use --format=(legacy|columns) (or define a \"\n+                \"format=(legacy|columns) in your pip.conf under the [list] \"\n+                \"section) to disable this warning.\",\n                 RemovedInPip10Warning,\n             )\n", "issue": "deprecation message for list format suggests wrong configuration value\n* Pip version: 9.0.0\r\n* Python version: 2.7.12\r\n* Operating System: OS X\r\n\r\n### Description:\r\n\r\nThe deprecation message emitted from `pip list` about the `--format` option suggests the wrong configuration value.\r\n\r\n### What I've run:\r\n\r\n```\r\n$ pip list --outdated\r\nDEPRECATION: The default format will switch to columns in the future. You can use --format=legacy (or define a list_format in your pip.conf) to disable this warning.\r\n```\r\n\r\nThe correct configuration option is:\r\n\r\n```ini\r\n[list]\r\nformat =\r\n```\n", "before_files": [{"content": "from __future__ import absolute_import\n\nimport json\nimport logging\nimport warnings\ntry:\n    from itertools import zip_longest\nexcept ImportError:\n    from itertools import izip_longest as zip_longest\n\nfrom pip._vendor import six\n\nfrom pip.basecommand import Command\nfrom pip.exceptions import CommandError\nfrom pip.index import PackageFinder\nfrom pip.utils import (\n    get_installed_distributions, dist_is_editable)\nfrom pip.utils.deprecation import RemovedInPip10Warning\nfrom pip.cmdoptions import make_option_group, index_group\n\nlogger = logging.getLogger(__name__)\n\n\nclass ListCommand(Command):\n    \"\"\"\n    List installed packages, including editables.\n\n    Packages are listed in a case-insensitive sorted order.\n    \"\"\"\n    name = 'list'\n    usage = \"\"\"\n      %prog [options]\"\"\"\n    summary = 'List installed packages.'\n\n    def __init__(self, *args, **kw):\n        super(ListCommand, self).__init__(*args, **kw)\n\n        cmd_opts = self.cmd_opts\n\n        cmd_opts.add_option(\n            '-o', '--outdated',\n            action='store_true',\n            default=False,\n            help='List outdated packages')\n        cmd_opts.add_option(\n            '-u', '--uptodate',\n            action='store_true',\n            default=False,\n            help='List uptodate packages')\n        cmd_opts.add_option(\n            '-e', '--editable',\n            action='store_true',\n            default=False,\n            help='List editable projects.')\n        cmd_opts.add_option(\n            '-l', '--local',\n            action='store_true',\n            default=False,\n            help=('If in a virtualenv that has global access, do not list '\n                  'globally-installed packages.'),\n        )\n        self.cmd_opts.add_option(\n            '--user',\n            dest='user',\n            action='store_true',\n            default=False,\n            help='Only output packages installed in user-site.')\n\n        cmd_opts.add_option(\n            '--pre',\n            action='store_true',\n            default=False,\n            help=(\"Include pre-release and development versions. By default, \"\n                  \"pip only finds stable versions.\"),\n        )\n\n        cmd_opts.add_option(\n            '--format',\n            action='store',\n            dest='list_format',\n            choices=('legacy', 'columns', 'freeze', 'json'),\n            help=\"Select the output format among: legacy (default), columns, \"\n                 \"freeze or json.\",\n        )\n\n        cmd_opts.add_option(\n            '--not-required',\n            action='store_true',\n            dest='not_required',\n            help=\"List packages that are not dependencies of \"\n                 \"installed packages.\",\n        )\n\n        index_opts = make_option_group(index_group, self.parser)\n\n        self.parser.insert_option_group(0, index_opts)\n        self.parser.insert_option_group(0, cmd_opts)\n\n    def _build_package_finder(self, options, index_urls, session):\n        \"\"\"\n        Create a package finder appropriate to this list command.\n        \"\"\"\n        return PackageFinder(\n            find_links=options.find_links,\n            index_urls=index_urls,\n            allow_all_prereleases=options.pre,\n            trusted_hosts=options.trusted_hosts,\n            process_dependency_links=options.process_dependency_links,\n            session=session,\n        )\n\n    def run(self, options, args):\n        if options.allow_external:\n            warnings.warn(\n                \"--allow-external has been deprecated and will be removed in \"\n                \"the future. Due to changes in the repository protocol, it no \"\n                \"longer has any effect.\",\n                RemovedInPip10Warning,\n            )\n\n        if options.allow_all_external:\n            warnings.warn(\n                \"--allow-all-external has been deprecated and will be removed \"\n                \"in the future. Due to changes in the repository protocol, it \"\n                \"no longer has any effect.\",\n                RemovedInPip10Warning,\n            )\n\n        if options.allow_unverified:\n            warnings.warn(\n                \"--allow-unverified has been deprecated and will be removed \"\n                \"in the future. Due to changes in the repository protocol, it \"\n                \"no longer has any effect.\",\n                RemovedInPip10Warning,\n            )\n\n        if options.list_format is None:\n            warnings.warn(\n                \"The default format will switch to columns in the future. \"\n                \"You can use --format=legacy (or define a list_format \"\n                \"in your pip.conf) to disable this warning.\",\n                RemovedInPip10Warning,\n            )\n\n        if options.outdated and options.uptodate:\n            raise CommandError(\n                \"Options --outdated and --uptodate cannot be combined.\")\n\n        packages = get_installed_distributions(\n            local_only=options.local,\n            user_only=options.user,\n            editables_only=options.editable,\n        )\n\n        if options.outdated:\n            packages = self.get_outdated(packages, options)\n        elif options.uptodate:\n            packages = self.get_uptodate(packages, options)\n\n        if options.not_required:\n            packages = self.get_not_required(packages, options)\n\n        self.output_package_listing(packages, options)\n\n    def get_outdated(self, packages, options):\n        return [\n            dist for dist in self.iter_packages_latest_infos(packages, options)\n            if dist.latest_version > dist.parsed_version\n        ]\n\n    def get_uptodate(self, packages, options):\n        return [\n            dist for dist in self.iter_packages_latest_infos(packages, options)\n            if dist.latest_version == dist.parsed_version\n        ]\n\n    def get_not_required(self, packages, options):\n        dep_keys = set()\n        for dist in packages:\n            dep_keys.update(requirement.key for requirement in dist.requires())\n        return set(pkg for pkg in packages if pkg.key not in dep_keys)\n\n    def iter_packages_latest_infos(self, packages, options):\n        index_urls = [options.index_url] + options.extra_index_urls\n        if options.no_index:\n            logger.debug('Ignoring indexes: %s', ','.join(index_urls))\n            index_urls = []\n\n        dependency_links = []\n        for dist in packages:\n            if dist.has_metadata('dependency_links.txt'):\n                dependency_links.extend(\n                    dist.get_metadata_lines('dependency_links.txt'),\n                )\n\n        with self._build_session(options) as session:\n            finder = self._build_package_finder(options, index_urls, session)\n            finder.add_dependency_links(dependency_links)\n\n            for dist in packages:\n                typ = 'unknown'\n                all_candidates = finder.find_all_candidates(dist.key)\n                if not options.pre:\n                    # Remove prereleases\n                    all_candidates = [candidate for candidate in all_candidates\n                                      if not candidate.version.is_prerelease]\n\n                if not all_candidates:\n                    continue\n                best_candidate = max(all_candidates,\n                                     key=finder._candidate_sort_key)\n                remote_version = best_candidate.version\n                if best_candidate.location.is_wheel:\n                    typ = 'wheel'\n                else:\n                    typ = 'sdist'\n                # This is dirty but makes the rest of the code much cleaner\n                dist.latest_version = remote_version\n                dist.latest_filetype = typ\n                yield dist\n\n    def output_legacy(self, dist):\n        if dist_is_editable(dist):\n            return '%s (%s, %s)' % (\n                dist.project_name,\n                dist.version,\n                dist.location,\n            )\n        else:\n            return '%s (%s)' % (dist.project_name, dist.version)\n\n    def output_legacy_latest(self, dist):\n        return '%s - Latest: %s [%s]' % (\n            self.output_legacy(dist),\n            dist.latest_version,\n            dist.latest_filetype,\n        )\n\n    def output_package_listing(self, packages, options):\n        packages = sorted(\n            packages,\n            key=lambda dist: dist.project_name.lower(),\n        )\n        if options.list_format == 'columns' and packages:\n            data, header = format_for_columns(packages, options)\n            self.output_package_listing_columns(data, header)\n        elif options.list_format == 'freeze':\n            for dist in packages:\n                logger.info(\"%s==%s\", dist.project_name, dist.version)\n        elif options.list_format == 'json':\n            logger.info(format_for_json(packages, options))\n        else:  # legacy\n            for dist in packages:\n                if options.outdated:\n                    logger.info(self.output_legacy_latest(dist))\n                else:\n                    logger.info(self.output_legacy(dist))\n\n    def output_package_listing_columns(self, data, header):\n        # insert the header first: we need to know the size of column names\n        if len(data) > 0:\n            data.insert(0, header)\n\n        pkg_strings, sizes = tabulate(data)\n\n        # Create and add a separator.\n        if len(data) > 0:\n            pkg_strings.insert(1, \" \".join(map(lambda x: '-' * x, sizes)))\n\n        for val in pkg_strings:\n            logger.info(val)\n\n\ndef tabulate(vals):\n    # From pfmoore on GitHub:\n    # https://github.com/pypa/pip/issues/3651#issuecomment-216932564\n    assert len(vals) > 0\n\n    sizes = [0] * max(len(x) for x in vals)\n    for row in vals:\n        sizes = [max(s, len(str(c))) for s, c in zip_longest(sizes, row)]\n\n    result = []\n    for row in vals:\n        display = \" \".join([str(c).ljust(s) if c is not None else ''\n                            for s, c in zip_longest(sizes, row)])\n        result.append(display)\n\n    return result, sizes\n\n\ndef format_for_columns(pkgs, options):\n    \"\"\"\n    Convert the package data into something usable\n    by output_package_listing_columns.\n    \"\"\"\n    running_outdated = options.outdated\n    # Adjust the header for the `pip list --outdated` case.\n    if running_outdated:\n        header = [\"Package\", \"Version\", \"Latest\", \"Type\"]\n    else:\n        header = [\"Package\", \"Version\"]\n\n    data = []\n    if any(dist_is_editable(x) for x in pkgs):\n        header.append(\"Location\")\n\n    for proj in pkgs:\n        # if we're working on the 'outdated' list, separate out the\n        # latest_version and type\n        row = [proj.project_name, proj.version]\n\n        if running_outdated:\n            row.append(proj.latest_version)\n            row.append(proj.latest_filetype)\n\n        if dist_is_editable(proj):\n            row.append(proj.location)\n\n        data.append(row)\n\n    return data, header\n\n\ndef format_for_json(packages, options):\n    data = []\n    for dist in packages:\n        info = {\n            'name': dist.project_name,\n            'version': six.text_type(dist.version),\n        }\n        if options.outdated:\n            info['latest_version'] = six.text_type(dist.latest_version)\n            info['latest_filetype'] = dist.latest_filetype\n        data.append(info)\n    return json.dumps(data)\n", "path": "pip/commands/list.py"}], "after_files": [{"content": "from __future__ import absolute_import\n\nimport json\nimport logging\nimport warnings\ntry:\n    from itertools import zip_longest\nexcept ImportError:\n    from itertools import izip_longest as zip_longest\n\nfrom pip._vendor import six\n\nfrom pip.basecommand import Command\nfrom pip.exceptions import CommandError\nfrom pip.index import PackageFinder\nfrom pip.utils import (\n    get_installed_distributions, dist_is_editable)\nfrom pip.utils.deprecation import RemovedInPip10Warning\nfrom pip.cmdoptions import make_option_group, index_group\n\nlogger = logging.getLogger(__name__)\n\n\nclass ListCommand(Command):\n    \"\"\"\n    List installed packages, including editables.\n\n    Packages are listed in a case-insensitive sorted order.\n    \"\"\"\n    name = 'list'\n    usage = \"\"\"\n      %prog [options]\"\"\"\n    summary = 'List installed packages.'\n\n    def __init__(self, *args, **kw):\n        super(ListCommand, self).__init__(*args, **kw)\n\n        cmd_opts = self.cmd_opts\n\n        cmd_opts.add_option(\n            '-o', '--outdated',\n            action='store_true',\n            default=False,\n            help='List outdated packages')\n        cmd_opts.add_option(\n            '-u', '--uptodate',\n            action='store_true',\n            default=False,\n            help='List uptodate packages')\n        cmd_opts.add_option(\n            '-e', '--editable',\n            action='store_true',\n            default=False,\n            help='List editable projects.')\n        cmd_opts.add_option(\n            '-l', '--local',\n            action='store_true',\n            default=False,\n            help=('If in a virtualenv that has global access, do not list '\n                  'globally-installed packages.'),\n        )\n        self.cmd_opts.add_option(\n            '--user',\n            dest='user',\n            action='store_true',\n            default=False,\n            help='Only output packages installed in user-site.')\n\n        cmd_opts.add_option(\n            '--pre',\n            action='store_true',\n            default=False,\n            help=(\"Include pre-release and development versions. By default, \"\n                  \"pip only finds stable versions.\"),\n        )\n\n        cmd_opts.add_option(\n            '--format',\n            action='store',\n            dest='list_format',\n            choices=('legacy', 'columns', 'freeze', 'json'),\n            help=\"Select the output format among: legacy (default), columns, \"\n                 \"freeze or json.\",\n        )\n\n        cmd_opts.add_option(\n            '--not-required',\n            action='store_true',\n            dest='not_required',\n            help=\"List packages that are not dependencies of \"\n                 \"installed packages.\",\n        )\n\n        index_opts = make_option_group(index_group, self.parser)\n\n        self.parser.insert_option_group(0, index_opts)\n        self.parser.insert_option_group(0, cmd_opts)\n\n    def _build_package_finder(self, options, index_urls, session):\n        \"\"\"\n        Create a package finder appropriate to this list command.\n        \"\"\"\n        return PackageFinder(\n            find_links=options.find_links,\n            index_urls=index_urls,\n            allow_all_prereleases=options.pre,\n            trusted_hosts=options.trusted_hosts,\n            process_dependency_links=options.process_dependency_links,\n            session=session,\n        )\n\n    def run(self, options, args):\n        if options.allow_external:\n            warnings.warn(\n                \"--allow-external has been deprecated and will be removed in \"\n                \"the future. Due to changes in the repository protocol, it no \"\n                \"longer has any effect.\",\n                RemovedInPip10Warning,\n            )\n\n        if options.allow_all_external:\n            warnings.warn(\n                \"--allow-all-external has been deprecated and will be removed \"\n                \"in the future. Due to changes in the repository protocol, it \"\n                \"no longer has any effect.\",\n                RemovedInPip10Warning,\n            )\n\n        if options.allow_unverified:\n            warnings.warn(\n                \"--allow-unverified has been deprecated and will be removed \"\n                \"in the future. Due to changes in the repository protocol, it \"\n                \"no longer has any effect.\",\n                RemovedInPip10Warning,\n            )\n\n        if options.list_format is None:\n            warnings.warn(\n                \"The default format will switch to columns in the future. \"\n                \"You can use --format=(legacy|columns) (or define a \"\n                \"format=(legacy|columns) in your pip.conf under the [list] \"\n                \"section) to disable this warning.\",\n                RemovedInPip10Warning,\n            )\n\n        if options.outdated and options.uptodate:\n            raise CommandError(\n                \"Options --outdated and --uptodate cannot be combined.\")\n\n        packages = get_installed_distributions(\n            local_only=options.local,\n            user_only=options.user,\n            editables_only=options.editable,\n        )\n\n        if options.outdated:\n            packages = self.get_outdated(packages, options)\n        elif options.uptodate:\n            packages = self.get_uptodate(packages, options)\n\n        if options.not_required:\n            packages = self.get_not_required(packages, options)\n\n        self.output_package_listing(packages, options)\n\n    def get_outdated(self, packages, options):\n        return [\n            dist for dist in self.iter_packages_latest_infos(packages, options)\n            if dist.latest_version > dist.parsed_version\n        ]\n\n    def get_uptodate(self, packages, options):\n        return [\n            dist for dist in self.iter_packages_latest_infos(packages, options)\n            if dist.latest_version == dist.parsed_version\n        ]\n\n    def get_not_required(self, packages, options):\n        dep_keys = set()\n        for dist in packages:\n            dep_keys.update(requirement.key for requirement in dist.requires())\n        return set(pkg for pkg in packages if pkg.key not in dep_keys)\n\n    def iter_packages_latest_infos(self, packages, options):\n        index_urls = [options.index_url] + options.extra_index_urls\n        if options.no_index:\n            logger.debug('Ignoring indexes: %s', ','.join(index_urls))\n            index_urls = []\n\n        dependency_links = []\n        for dist in packages:\n            if dist.has_metadata('dependency_links.txt'):\n                dependency_links.extend(\n                    dist.get_metadata_lines('dependency_links.txt'),\n                )\n\n        with self._build_session(options) as session:\n            finder = self._build_package_finder(options, index_urls, session)\n            finder.add_dependency_links(dependency_links)\n\n            for dist in packages:\n                typ = 'unknown'\n                all_candidates = finder.find_all_candidates(dist.key)\n                if not options.pre:\n                    # Remove prereleases\n                    all_candidates = [candidate for candidate in all_candidates\n                                      if not candidate.version.is_prerelease]\n\n                if not all_candidates:\n                    continue\n                best_candidate = max(all_candidates,\n                                     key=finder._candidate_sort_key)\n                remote_version = best_candidate.version\n                if best_candidate.location.is_wheel:\n                    typ = 'wheel'\n                else:\n                    typ = 'sdist'\n                # This is dirty but makes the rest of the code much cleaner\n                dist.latest_version = remote_version\n                dist.latest_filetype = typ\n                yield dist\n\n    def output_legacy(self, dist):\n        if dist_is_editable(dist):\n            return '%s (%s, %s)' % (\n                dist.project_name,\n                dist.version,\n                dist.location,\n            )\n        else:\n            return '%s (%s)' % (dist.project_name, dist.version)\n\n    def output_legacy_latest(self, dist):\n        return '%s - Latest: %s [%s]' % (\n            self.output_legacy(dist),\n            dist.latest_version,\n            dist.latest_filetype,\n        )\n\n    def output_package_listing(self, packages, options):\n        packages = sorted(\n            packages,\n            key=lambda dist: dist.project_name.lower(),\n        )\n        if options.list_format == 'columns' and packages:\n            data, header = format_for_columns(packages, options)\n            self.output_package_listing_columns(data, header)\n        elif options.list_format == 'freeze':\n            for dist in packages:\n                logger.info(\"%s==%s\", dist.project_name, dist.version)\n        elif options.list_format == 'json':\n            logger.info(format_for_json(packages, options))\n        else:  # legacy\n            for dist in packages:\n                if options.outdated:\n                    logger.info(self.output_legacy_latest(dist))\n                else:\n                    logger.info(self.output_legacy(dist))\n\n    def output_package_listing_columns(self, data, header):\n        # insert the header first: we need to know the size of column names\n        if len(data) > 0:\n            data.insert(0, header)\n\n        pkg_strings, sizes = tabulate(data)\n\n        # Create and add a separator.\n        if len(data) > 0:\n            pkg_strings.insert(1, \" \".join(map(lambda x: '-' * x, sizes)))\n\n        for val in pkg_strings:\n            logger.info(val)\n\n\ndef tabulate(vals):\n    # From pfmoore on GitHub:\n    # https://github.com/pypa/pip/issues/3651#issuecomment-216932564\n    assert len(vals) > 0\n\n    sizes = [0] * max(len(x) for x in vals)\n    for row in vals:\n        sizes = [max(s, len(str(c))) for s, c in zip_longest(sizes, row)]\n\n    result = []\n    for row in vals:\n        display = \" \".join([str(c).ljust(s) if c is not None else ''\n                            for s, c in zip_longest(sizes, row)])\n        result.append(display)\n\n    return result, sizes\n\n\ndef format_for_columns(pkgs, options):\n    \"\"\"\n    Convert the package data into something usable\n    by output_package_listing_columns.\n    \"\"\"\n    running_outdated = options.outdated\n    # Adjust the header for the `pip list --outdated` case.\n    if running_outdated:\n        header = [\"Package\", \"Version\", \"Latest\", \"Type\"]\n    else:\n        header = [\"Package\", \"Version\"]\n\n    data = []\n    if any(dist_is_editable(x) for x in pkgs):\n        header.append(\"Location\")\n\n    for proj in pkgs:\n        # if we're working on the 'outdated' list, separate out the\n        # latest_version and type\n        row = [proj.project_name, proj.version]\n\n        if running_outdated:\n            row.append(proj.latest_version)\n            row.append(proj.latest_filetype)\n\n        if dist_is_editable(proj):\n            row.append(proj.location)\n\n        data.append(row)\n\n    return data, header\n\n\ndef format_for_json(packages, options):\n    data = []\n    for dist in packages:\n        info = {\n            'name': dist.project_name,\n            'version': six.text_type(dist.version),\n        }\n        if options.outdated:\n            info['latest_version'] = six.text_type(dist.latest_version)\n            info['latest_filetype'] = dist.latest_filetype\n        data.append(info)\n    return json.dumps(data)\n", "path": "pip/commands/list.py"}]}