mirror of https://github.com/pypa/pip
Refactory and support to package name in any case.
This commit is contained in:
parent
0f09b5ccd7
commit
b78dff8d2c
|
@ -18,34 +18,51 @@ class StatusCommand(Command):
|
|||
return
|
||||
query = args
|
||||
|
||||
print_results(query)
|
||||
results = search_packages_info(query)
|
||||
print_results(results)
|
||||
|
||||
|
||||
def print_results(query):
|
||||
def search_packages_info(query):
|
||||
"""
|
||||
Gather details from installed distributions. Print distribution name,
|
||||
version, location, and installed files. Installed files requires a
|
||||
pip generated 'installed-files.txt' in the distributions '.egg-info'
|
||||
directory.
|
||||
"""
|
||||
installed_packages = [p.project_name for p in pkg_resources.working_set]
|
||||
installed_packages = dict([(p.project_name.lower(), p.project_name) for p in pkg_resources.working_set])
|
||||
for name in query:
|
||||
if name in installed_packages:
|
||||
dist = pkg_resources.get_distribution(name)
|
||||
logger.notify("---")
|
||||
logger.notify("Name: %s" % name)
|
||||
logger.notify("Version: %s" % dist.version)
|
||||
logger.notify("Location: %s" % dist.location)
|
||||
logger.notify("Files:")
|
||||
normalized_name = name.lower()
|
||||
if normalized_name in installed_packages:
|
||||
dist = pkg_resources.get_distribution(installed_packages[normalized_name])
|
||||
package = {
|
||||
'name': dist.project_name,
|
||||
'version': dist.version,
|
||||
'location': dist.location
|
||||
}
|
||||
filelist = os.path.join(
|
||||
dist.location,
|
||||
dist.egg_name() + '.egg-info',
|
||||
'installed-files.txt')
|
||||
dist.location,
|
||||
dist.egg_name() + '.egg-info',
|
||||
'installed-files.txt')
|
||||
if os.path.isfile(filelist):
|
||||
for line in open(filelist):
|
||||
logger.notify(" %s" % line.strip())
|
||||
else:
|
||||
logger.notify("Cannot locate installed-files.txt")
|
||||
package['files'] = filelist
|
||||
yield package
|
||||
|
||||
|
||||
def print_results(distributions):
|
||||
"""
|
||||
Print the informations from installed distributions found.
|
||||
"""
|
||||
for dist in distributions:
|
||||
logger.notify("---")
|
||||
logger.notify("Name: %s" % dist['name'])
|
||||
logger.notify("Version: %s" % dist['version'])
|
||||
logger.notify("Location: %s" % dist['location'])
|
||||
logger.notify("Files:")
|
||||
filelist = dist.get('files', None)
|
||||
if filelist:
|
||||
for line in open(filelist):
|
||||
logger.notify(" %s" % line.strip())
|
||||
else:
|
||||
logger.notify("Cannot locate installed-files.txt")
|
||||
|
||||
|
||||
StatusCommand()
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import re
|
||||
import pkg_resources
|
||||
from pip import __version__
|
||||
from pip.commands.status import search_packages_info
|
||||
from tests.test_pip import reset_env, run_pip
|
||||
|
||||
def test_status():
|
||||
|
@ -12,10 +13,29 @@ def test_status():
|
|||
reset_env()
|
||||
result = run_pip('status', 'pip')
|
||||
lines = result.stdout.split('\n')
|
||||
assert 7 == len(lines)
|
||||
assert len(lines) == 7
|
||||
assert '---', lines[0]
|
||||
assert re.match('^Name\: pip$', lines[1])
|
||||
assert re.match('^Version\: %s$' % __version__, lines[2])
|
||||
assert 'Location: %s' % dist.location, lines[3]
|
||||
assert 'Files:' == lines[4]
|
||||
assert 'Cannot locate installed-files.txt' == lines[5]
|
||||
|
||||
|
||||
def test_find_package_not_found():
|
||||
"""
|
||||
Test trying to get info about a inexistent package.
|
||||
|
||||
"""
|
||||
result = search_packages_info(['abcd3'])
|
||||
assert len(list(result)) == 0
|
||||
|
||||
|
||||
def test_search_any_case():
|
||||
"""
|
||||
Search for a package in any case.
|
||||
|
||||
"""
|
||||
result = list(search_packages_info(['PIP']))
|
||||
assert len(result) == 1
|
||||
assert 'pip' == result[0]['name']
|
||||
|
|
Loading…
Reference in New Issue