di / pip-api

An unofficial, importable pip API
https://pypi.org/p/pip-api
Apache License 2.0
110 stars 15 forks source link

Provide API for `pip show` info by invoking pip as a subprocess (and offer to contribute) #55

Open stevecj opened 3 years ago

stevecj commented 3 years ago

I recently needed to read information from a requirements file and a frozen constraints file (which I did using pip-api and works wonderfully — thanks) and then walk the tree of dependencies to identify installed packages that are directly or implicitly required by those in the requirements file.

Walking through the dependency hierarchy requires information from pip show, so I wrote code to obtain that information by running pip show in a subprocess and parsing its textual output. I think that capability would make a nice addition to the pip-api package.

Is there interest in having me contribute that functionality?

Any preferences regarding what the Python API should look like for this?

di commented 3 years ago

Hi @stevecj, thanks for the feature request. TL;DR: yes.

The purpose of this package to providing a drop-in replacement for existing uses of pip's internal API by wrapping command-line calls to pip. It looks like a function that replaces the pip._internal.commands.show.search_packages_info would be the function we'd want to replace by wrapping pip show.

This internal function takes a list of package names like ['six'] and yields a dictionary like:

{
    "name": "six",
    "version": "1.14.0",
    "location": "/Users/dustiningram/.pyenv/versions/3.8.2/lib/python3.8/site-packages",
    "requires": [],
    "required_by": [
        "virtualenv",
        "uncurl",
        "twilio",
        "tweepy",
        "tox",
        "readme-renderer",
        "python-frontmatter",
        "pip-tools",
        "bleach",
    ],
    "installer": "pip",
    "metadata-version": "2.1",
    "summary": "Python 2 and 3 compatibility utilities",
    "home-page": "https://github.com/benjaminp/six",
    "author": "Benjamin Peterson",
    "author-email": "benjamin@python.org",
    "license": "MIT",
    "classifiers": [
        "Development Status :: 5 - Production/Stable",
        "Programming Language :: Python :: 2",
        "Programming Language :: Python :: 3",
        "Intended Audience :: Developers",
        "License :: OSI Approved :: MIT License",
        "Topic :: Software Development :: Libraries",
        "Topic :: Utilities",
    ],
    "files": [
        "__pycache__/six.cpython-38.pyc",
        "six-1.14.0.dist-info/INSTALLER",
        "six-1.14.0.dist-info/LICENSE",
        "six-1.14.0.dist-info/METADATA",
        "six-1.14.0.dist-info/RECORD",
        "six-1.14.0.dist-info/WHEEL",
        "six-1.14.0.dist-info/top_level.txt",
        "six.py",
    ],
}

It looks like we can get all of this if using the following:

$ pip show -f --verbose six
Name: six
Version: 1.14.0
Summary: Python 2 and 3 compatibility utilities
Home-page: https://github.com/benjaminp/six
Author: Benjamin Peterson
Author-email: benjamin@python.org
License: MIT
Location: /Users/dustiningram/.pyenv/versions/3.8.2/lib/python3.8/site-packages
Requires:
Required-by: virtualenv, uncurl, twilio, tweepy, tox, readme-renderer, python-frontmatter, pip-tools, bleach
Metadata-Version: 2.1
Installer: pip
Classifiers:
  Development Status :: 5 - Production/Stable
  Programming Language :: Python :: 2
  Programming Language :: Python :: 3
  Intended Audience :: Developers
  License :: OSI Approved :: MIT License
  Topic :: Software Development :: Libraries
  Topic :: Utilities
Entry-points:
Files:
  __pycache__/six.cpython-38.pyc
  six-1.14.0.dist-info/INSTALLER
  six-1.14.0.dist-info/LICENSE
  six-1.14.0.dist-info/METADATA
  six-1.14.0.dist-info/RECORD
  six-1.14.0.dist-info/WHEEL
  six-1.14.0.dist-info/top_level.txt
  six.py

Any preferences regarding what the Python API should look like for this?

I think this should mimic search_packages_info as much as possible. I'd imagine the docs for this would look something like:

stevecj commented 3 years ago

Acknowledged. 🙂

stevecj commented 2 years ago

I started on this quite a while ago and then got swamped with other things. Hopefully, I will eventually get back to it and make an MR. If anyone wants to beat me to it, feel free though. :)