Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove requests dependency from PyPIClient #26954

Merged
merged 4 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eng/tox/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ setenv =
PROXY_URL=http://localhost:5008
deps =
{[packaging]pkgs}
requests
cryptography<4
urllib3
commands =
{envbindir}/python -m pip install {toxinidir}/../../../tools/azure-sdk-tools --no-deps
{envbindir}/python {toxinidir}/../../../eng/tox/sanitize_setup.py -t {toxinidir}
Expand Down
20 changes: 10 additions & 10 deletions tools/azure-sdk-tools/pypi_tools/pypi.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from packaging.version import parse as Version
import sys

import pdb
from urllib3 import Retry, PoolManager
import json

def get_pypi_xmlrpc_client():
"""This is actually deprecated client."""
Expand All @@ -11,26 +13,24 @@ def get_pypi_xmlrpc_client():

class PyPIClient:
def __init__(self, host="https://pypi.org"):
import requests

self._host = host
self._session = requests.Session()
self._http = PoolManager(retries=Retry(raise_on_status=True))

def project(self, package_name):
response = self._session.get(
response = self._http.request(
'get',
"{host}/pypi/{project_name}/json".format(host=self._host, project_name=package_name)
)
response.raise_for_status()
return response.json()
return json.loads(response.data.decode('utf-8'))

def project_release(self, package_name, version):
response = self._session.get(
response = self._http.request(
'get',
"{host}/pypi/{project_name}/{version}/json".format(
host=self._host, project_name=package_name, version=version
)
)
response.raise_for_status()
return response.json()
return json.loads(response.data.decode('utf-8'))

def filter_packages_for_compatibility(self, package_name, version_set):
# only need the packaging.specifiers import if we're actually executing this filter.
Expand Down
1 change: 1 addition & 0 deletions tools/azure-sdk-tools/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"pyopenssl",
"python-dotenv",
"PyYAML",
"urllib3"
]

setup(
Expand Down
45 changes: 45 additions & 0 deletions tools/azure-sdk-tools/tests/test_pypi_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from pypi_tools.pypi import PyPIClient
from unittest.mock import patch
import os
import pytest
import pdb


class TestPyPiClient:
@pytest.mark.skipif(
os.environ.get("TF_BUILD", "None") == True,
reason=f"This test isn't worth recording and could be flaky. Skipping in CI.",
)
def test_package_retrieve(self):
client = PyPIClient()
result = client.project("azure-core")

# we won't _exhaustively_ check this, but we can sanity check a few proxy values to ensure we haven't broken anything
assert result["info"]["name"] == "azure-core"
assert len(result["releases"].keys()) > 47
assert "1.25.1" in result["releases"]
assert "1.10.0" in result["releases"]

@pytest.mark.skipif(
os.environ.get("TF_BUILD", "None") == True,
reason=f"This test isn't worth recording and could be flaky. Skipping in CI.",
)
def test_package_version_retrieve(self):
client = PyPIClient()
result = client.project_release("azure-core", "1.8.0")

assert result["info"]["name"] == "azure-core"
assert result["info"]["release_url"] == "https://pypi.org/project/azure-core/1.8.0/"

@pytest.mark.skipif(
os.environ.get("TF_BUILD", "None") == True,
reason=f"This test isn't worth recording and could be flaky. Skipping in CI.",
)
@patch("pypi_tools.pypi.sys")
def test_package_filter_for_compatibility(self, mock_sys):
mock_sys.version_info = (2, 7, 0)
client = PyPIClient()
result = client.get_ordered_versions("azure-core", True)
unfiltered_results = client.get_ordered_versions("azure-core", False)

assert len(result) < len(unfiltered_results)