1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
from packaging.version import parse as Version
import requests
def get_pypi_xmlrpc_client():
"""This is actually deprecated client.
"""
import xmlrpc.client
return xmlrpc.client.ServerProxy("https://pypi.python.org/pypi", use_datetime=True)
class PyPIClient:
def __init__(self, host="https://pypi.org"):
self._host = host
self._session = requests.Session()
def project(self, package_name):
response = self._session.get(
"{host}/pypi/{project_name}/json".format(
host=self._host,
project_name=package_name
)
)
response.raise_for_status()
return response.json()
def project_release(self, package_name, version):
response = self._session.get(
"{host}/pypi/{project_name}/{version}/json".format(
host=self._host,
project_name=package_name,
version=version
)
)
response.raise_for_status()
return response.json()
def get_ordered_versions(self, package_name):
project = self.project(package_name)
versions = [
Version(package_version)
for package_version
in project["releases"].keys()
]
versions.sort()
return versions
def get_relevant_versions(self, package_name):
"""Return a tuple: (latest release, latest stable)
If there are different, it means the latest is not a stable
"""
versions = self.get_ordered_versions(package_name)
pre_releases = [version for version in versions if not version.is_prerelease]
return (
versions[-1],
pre_releases[-1]
)
|