File: pypi.py

package info (click to toggle)
python-azure 20201208%2Bgit-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,437,920 kB
  • sloc: python: 4,287,452; javascript: 269; makefile: 198; sh: 187; xml: 106
file content (56 lines) | stat: -rw-r--r-- 1,751 bytes parent folder | download
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]
        )