Mitar
Mitar

Reputation: 7030

Install oldest versions of dependencies of a Python package

So when installing a Python package using pip, it installs the newest versions of all dependencies of that package. But my package defines ranges of versions of supported dependencies. For testing purposes, I would like to run in CI not just tests against all the newest dependencies, but also against all the oldest versions of dependencies (so oldest versions claimed to be supported).

I know that I would also have to test all combinations of supported versions of dependencies, but I would be satisfied with just all the newest and all the oldest.

So the question is how to get those dependencies to be installed with the oldest supported versions.

Upvotes: 7

Views: 550

Answers (1)

Mitar
Mitar

Reputation: 7030

I made the following script:

import pkg_resources

package = pkg_resources.working_set.by_key[package_name]

oldest_dependencies = []

for requirement in package.requires():
    dependency = requirement.project_name
    if requirement.extras:
        dependency += '[' + ','.join(requirement.extras) + ']'
    for comparator, version in requirement.specs:
        if comparator == '==':
            if len(requirement.specs) != 1:
                raise ValueError('Invalid dependency: {requirement}'.format(requirement=requirement))
            dependency += '==' + version
        elif comparator == '<=':
            if len(requirement.specs) != 2:
                raise ValueError('Invalid dependency: {requirement}'.format(requirement=requirement))
        elif comparator == '>=':
            dependency += '==' + version

    oldest_dependencies.append(dependency)

for dependency in oldest_dependencies:
    print(dependency)

Which prints out the oldest dependencies. I can then run it and pass it to pip to install those versions.

The problem is only that it does not work with a lower bound on the dependency like >1.0 which would require to check PyPi which version exactly is the oldest but still newer than 1.0.

Upvotes: 1

Related Questions