pip/pip/commands/list.py

225 lines
7.9 KiB
Python
Raw Normal View History

from __future__ import absolute_import
import logging
import warnings
2011-03-19 19:30:56 +01:00
from pip.basecommand import Command
from pip.exceptions import DistributionNotFound
from pip.index import FormatControl, fmt_ctl_formats, PackageFinder, Search
from pip.req import InstallRequirement
2015-09-06 00:23:45 +02:00
from pip.utils import (
2015-09-08 00:18:54 +02:00
get_installed_distributions, dist_is_editable, canonicalize_name)
from pip.utils.deprecation import RemovedInPip10Warning
from pip.wheel import WheelCache
from pip.cmdoptions import make_option_group, index_group
2011-03-19 19:30:56 +01:00
logger = logging.getLogger(__name__)
class ListCommand(Command):
"""
List installed packages, including editables.
Packages are listed in a case-insensitive sorted order.
"""
name = 'list'
2013-01-18 22:25:15 +01:00
usage = """
%prog [options]"""
summary = 'List installed packages.'
2011-03-19 19:30:56 +01:00
def __init__(self, *args, **kw):
super(ListCommand, self).__init__(*args, **kw)
cmd_opts = self.cmd_opts
cmd_opts.add_option(
'-o', '--outdated',
action='store_true',
default=False,
2012-12-16 08:31:39 +01:00
help='List outdated packages (excluding editables)')
cmd_opts.add_option(
'-u', '--uptodate',
action='store_true',
default=False,
2012-12-16 08:31:39 +01:00
help='List uptodate packages (excluding editables)')
2012-12-16 08:23:20 +01:00
cmd_opts.add_option(
'-e', '--editable',
2012-12-16 08:23:20 +01:00
action='store_true',
default=False,
2013-01-18 22:25:15 +01:00
help='List editable projects.')
2012-12-16 08:31:39 +01:00
cmd_opts.add_option(
'-l', '--local',
action='store_true',
default=False,
help=('If in a virtualenv that has global access, do not list '
'globally-installed packages.'),
)
self.cmd_opts.add_option(
'--user',
dest='user',
action='store_true',
default=False,
help='Only output packages installed in user-site.')
2012-12-16 08:31:39 +01:00
cmd_opts.add_option(
'--pre',
action='store_true',
default=False,
help=("Include pre-release and development versions. By default, "
"pip only finds stable versions."),
)
index_opts = make_option_group(index_group, self.parser)
self.parser.insert_option_group(0, index_opts)
self.parser.insert_option_group(0, cmd_opts)
2013-08-16 14:04:27 +02:00
def _build_package_finder(self, options, index_urls, session):
"""
2012-10-01 22:23:49 +02:00
Create a package finder appropriate to this list command.
"""
return PackageFinder(
find_links=options.find_links,
index_urls=index_urls,
allow_all_prereleases=options.pre,
trusted_hosts=options.trusted_hosts,
process_dependency_links=options.process_dependency_links,
session=session,
)
2011-03-19 19:30:56 +01:00
def run(self, options, args):
if options.allow_external:
warnings.warn(
"--allow-external has been deprecated and will be removed in "
"the future. Due to changes in the repository protocol, it no "
"longer has any effect.",
RemovedInPip10Warning,
)
if options.allow_all_external:
warnings.warn(
"--allow-all-external has been deprecated and will be removed "
"in the future. Due to changes in the repository protocol, it "
"no longer has any effect.",
RemovedInPip10Warning,
)
if options.allow_unverified:
warnings.warn(
"--allow-unverified has been deprecated and will be removed "
"in the future. Due to changes in the repository protocol, it "
"no longer has any effect.",
RemovedInPip10Warning,
)
if options.outdated:
2012-12-16 08:23:20 +01:00
self.run_outdated(options)
elif options.uptodate:
2012-12-16 08:23:20 +01:00
self.run_uptodate(options)
elif options.editable:
2012-12-16 08:23:20 +01:00
self.run_editables(options)
2012-09-13 00:50:25 +02:00
else:
2012-12-16 08:23:20 +01:00
self.run_listing(options)
2012-12-16 08:23:20 +01:00
def run_outdated(self, options):
for dist, version, typ in self.find_packages_latest_versions(options):
2014-07-04 00:56:26 +02:00
if version > dist.parsed_version:
logger.info(
'%s (Current: %s Latest: %s [%s])',
dist.project_name, dist.version, version, typ,
)
def find_packages_latest_versions(self, options):
index_urls = [options.index_url] + options.extra_index_urls
if options.no_index:
logger.info('Ignoring indexes: %s', ','.join(index_urls))
index_urls = []
2011-03-19 19:30:56 +01:00
dependency_links = []
for dist in get_installed_distributions(local_only=options.local,
user_only=options.user):
if dist.has_metadata('dependency_links.txt'):
dependency_links.extend(
dist.get_metadata_lines('dependency_links.txt'),
)
with self._build_session(options) as session:
finder = self._build_package_finder(options, index_urls, session)
finder.add_dependency_links(dependency_links)
2013-08-16 14:04:27 +02:00
installed_packages = get_installed_distributions(
local_only=options.local,
user_only=options.user,
include_editables=False,
)
format_control = FormatControl(set(), set())
wheel_cache = WheelCache(options.cache_dir, format_control)
for dist in installed_packages:
req = InstallRequirement.from_line(
dist.key, None, isolated=options.isolated_mode,
wheel_cache=wheel_cache
)
typ = 'unknown'
try:
link = finder.find_requirement(req, True)
# If link is None, means installed version is most
# up-to-date
if link is None:
continue
except DistributionNotFound:
continue
else:
2015-09-08 00:18:54 +02:00
canonical_name = canonicalize_name(req.name)
formats = fmt_ctl_formats(format_control, canonical_name)
search = Search(
req.name,
canonical_name,
formats)
remote_version = finder._link_package_versions(
link, search).version
2014-12-28 23:52:32 +01:00
if link.is_wheel:
typ = 'wheel'
else:
typ = 'sdist'
yield dist, remote_version, typ
2012-09-13 00:50:25 +02:00
2012-12-16 08:23:20 +01:00
def run_listing(self, options):
installed_packages = get_installed_distributions(
local_only=options.local,
user_only=options.user,
)
self.output_package_listing(installed_packages)
2012-12-16 08:23:20 +01:00
def run_editables(self, options):
installed_packages = get_installed_distributions(
local_only=options.local,
user_only=options.user,
editables_only=True,
)
2012-12-16 08:23:20 +01:00
self.output_package_listing(installed_packages)
def output_package_listing(self, installed_packages):
installed_packages = sorted(
installed_packages,
key=lambda dist: dist.project_name.lower(),
)
for dist in installed_packages:
2012-12-16 08:23:20 +01:00
if dist_is_editable(dist):
line = '%s (%s, %s)' % (
dist.project_name,
dist.version,
dist.location,
)
else:
line = '%s (%s)' % (dist.project_name, dist.version)
logger.info(line)
2012-09-13 00:50:25 +02:00
2012-12-16 08:23:20 +01:00
def run_uptodate(self, options):
uptodate = []
for dist, version, typ in self.find_packages_latest_versions(options):
2014-07-04 00:56:26 +02:00
if dist.parsed_version == version:
uptodate.append(dist)
self.output_package_listing(uptodate)