Example #1
0
 def search(self, query, options):
     index_url = options.index
     with self._build_session(options) as session:
         transport = PipXmlrpcTransport(index_url, session)
         pypi = xmlrpc_client.ServerProxy(index_url, transport)
         hits = pypi.search({'name': query, 'summary': query}, 'or')
         return hits
Example #2
0
def search_packages_info(query, index_url=None, session=None):
    """
    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 = dict([(p.project_name.lower(), p)
                      for p in pkg_resources.working_set])
    query_names = [name.lower() for name in query]
    for dist in [installed[pkg] for pkg in query_names if pkg in installed]:

        required_by = []
        for _, p in installed.iteritems():
            if dist.project_name.lower() in [
                    dep.project_name.lower() for dep in p.requires()
            ]:
                required_by.append(p.project_name)
            else:
                for e in p.extras:
                    if dist.project_name.lower() in [
                            dep.project_name.lower() for dep in p.requires([e])
                    ]:
                        required_by.append("%s[%s]" % (p.project_name, e))
        extras = {}
        requires = [dep.project_name for dep in dist.requires()]
        make_ext = lambda pkg_name: (pkg_name, True
                                     if pkg_name in installed else False)
        for e in dist.extras:
            extras[e] = [
                make_ext(dep.project_name.lower())
                for dep in dist.requires([e])
                if dep.project_name not in requires
            ]

        if session:
            transport = PipXmlrpcTransport(index_url, session)
            pypi = xmlrpc_client.ServerProxy(index_url, transport)
            pypi_releases = pypi.package_releases(dist.project_name)
            pypi_version = pypi_releases[0] if pypi_releases else 'UNKNOWN'
        else:
            pypi_version = 'UNKNOWN'

        package = {
            'name': dist.project_name,
            'version': dist.version,
            'pypi_version': pypi_version,
            'location': dist.location,
            'requires': requires,
            'required_by': required_by,
            'extras': extras
        }
        file_list = None
        metadata = None
        if isinstance(dist, pkg_resources.DistInfoDistribution):
            # RECORDs should be part of .dist-info metadatas
            if dist.has_metadata('RECORD'):
                lines = dist.get_metadata_lines('RECORD')
                paths = [l.split(',')[0] for l in lines]
                paths = [os.path.join(dist.location, p) for p in paths]
                file_list = [os.path.relpath(p, dist.location) for p in paths]

            if dist.has_metadata('METADATA'):
                metadata = dist.get_metadata('METADATA')
        else:
            # Otherwise use pip's log for .egg-info's
            if dist.has_metadata('installed-files.txt'):
                paths = dist.get_metadata_lines('installed-files.txt')
                paths = [os.path.join(dist.egg_info, p) for p in paths]
                file_list = [os.path.relpath(p, dist.location) for p in paths]
            if dist.has_metadata('PKG-INFO'):
                metadata = dist.get_metadata('PKG-INFO')

        if dist.has_metadata('entry_points.txt'):
            entry_points = dist.get_metadata_lines('entry_points.txt')
            package['entry_points'] = entry_points

        # @todo: Should pkg_resources.Distribution have a
        # `get_pkg_info` method?
        feed_parser = FeedParser()
        feed_parser.feed(metadata)
        pkg_info_dict = feed_parser.close()
        for key in ('metadata-version', 'summary', 'home-page', 'author',
                    'author-email', 'license'):
            package[key] = pkg_info_dict.get(key)

        # use and short-circuit to check for None
        package['files'] = file_list and sorted(file_list)
        yield package