~ubuntu-branches/ubuntu/raring/python-pip/raring-proposed

« back to all changes in this revision

Viewing changes to pip/commands/show.py

  • Committer: Package Import Robot
  • Author(s): Barry Warsaw
  • Date: 2013-04-17 10:45:19 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20130417104519-cn67sz4camrlkbsb
Tags: 1.3.1-0ubuntu1
* New upstream release.  (LP: #1167351)
  - debian/patches/test_urlparse_uses_fragment.patch: Removed; applied
    upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
import pkg_resources
 
3
from pip.basecommand import Command
 
4
from pip.log import logger
 
5
 
 
6
 
 
7
class ShowCommand(Command):
 
8
    """Show information about one or more installed packages."""
 
9
    name = 'show'
 
10
    usage = """
 
11
      %prog [options] <package> ..."""
 
12
    summary = 'Show information about installed packages.'
 
13
 
 
14
    def __init__(self, *args, **kw):
 
15
        super(ShowCommand, self).__init__(*args, **kw)
 
16
        self.cmd_opts.add_option(
 
17
            '-f', '--files',
 
18
            dest='files',
 
19
            action='store_true',
 
20
            default=False,
 
21
            help='Show the full list of installed files for each package.')
 
22
 
 
23
        self.parser.insert_option_group(0, self.cmd_opts)
 
24
 
 
25
    def run(self, options, args):
 
26
        if not args:
 
27
            logger.warn('ERROR: Please provide a package name or names.')
 
28
            return
 
29
        query = args
 
30
 
 
31
        results = search_packages_info(query)
 
32
        print_results(results, options.files)
 
33
 
 
34
 
 
35
def search_packages_info(query):
 
36
    """
 
37
    Gather details from installed distributions. Print distribution name,
 
38
    version, location, and installed files. Installed files requires a
 
39
    pip generated 'installed-files.txt' in the distributions '.egg-info'
 
40
    directory.
 
41
    """
 
42
    installed_packages = dict(
 
43
        [(p.project_name.lower(), p) for p in pkg_resources.working_set])
 
44
    for name in query:
 
45
        normalized_name = name.lower()
 
46
        if normalized_name in installed_packages:
 
47
            dist = installed_packages[normalized_name]
 
48
            package = {
 
49
                'name': dist.project_name,
 
50
                'version': dist.version,
 
51
                'location': dist.location,
 
52
                'requires': [dep.project_name for dep in dist.requires()],
 
53
            }
 
54
            filelist = os.path.join(
 
55
                       dist.location,
 
56
                       dist.egg_name() + '.egg-info',
 
57
                       'installed-files.txt')
 
58
            if os.path.isfile(filelist):
 
59
                package['files'] = filelist
 
60
            yield package
 
61
 
 
62
 
 
63
def print_results(distributions, list_all_files):
 
64
    """
 
65
    Print the informations from installed distributions found.
 
66
    """
 
67
    for dist in distributions:
 
68
        logger.notify("---")
 
69
        logger.notify("Name: %s" % dist['name'])
 
70
        logger.notify("Version: %s" % dist['version'])
 
71
        logger.notify("Location: %s" % dist['location'])
 
72
        logger.notify("Requires: %s" % ', '.join(dist['requires']))
 
73
        if list_all_files:
 
74
            logger.notify("Files:")
 
75
            if 'files' in dist:
 
76
                for line in open(dist['files']):
 
77
                    logger.notify("  %s" % line.strip())
 
78
            else:
 
79
                logger.notify("Cannot locate installed-files.txt")