user916367
user916367

Reputation:

In Python's Pip, how can you search for all possible versions of a package, which match a version pattern?

I'm trying to build a support matrix of requirements in my project. I use the ~= syntax to match minor versions, however because I'm packaging a PiPy client to corporate clients I really need to test all possible versions.

I know how to search for all versions of a PiPy package using the hack pip install xyz==, but I really want to know how to search for all compatible versions given a requirement string.

i.e what versions could possibly be installed (or won't conflict) for

pip install xyz<=10

Or

pip install xyz~=9.0.0

(Pip install rules are quite complex)

Upvotes: 2

Views: 114

Answers (1)

user916367
user916367

Reputation:

While writing this question, I came up with this solution. Unfortunately it uses the internal Pip code, and I'm sure it can be can be condensed down, but it works with pip version 19.

from pip._internal.models.format_control import FormatControl
from pip._internal.download import PipSession
from pip._internal.index import PackageFinder
from pip._internal.req import InstallRequirement
from pip._vendor.packaging.requirements import Requirement


package_finder = PackageFinder(
    find_links=[],
    format_control=FormatControl(set(), set()),
    index_urls=['https://pypi.org/simple'],
    trusted_hosts=[],
    allow_all_prereleases=False,
    session=PipSession(),
)


def get_valid_versions(requirement_string, include_preleases=False):
    install_req= InstallRequirement(
        Requirement(requirement_string), comes_from=None
    )
    all_candidates = package_finder.find_all_candidates(install_req.name)
    return set(
        install_req.specifier.filter(
            [str(c.version) for c in all_candidates],
            prereleases=include_preleases,
        )
    )

Usage:

 >>> get_valid_versions('dask~=2.8.0')
 {'2.8.0', '2.8.1'}

Upvotes: 4

Related Questions