From 8e0517859e69f3539a6f2beab7d706ff57c8c2d8 Mon Sep 17 00:00:00 2001 From: deathaxe Date: Wed, 22 Nov 2023 17:53:27 +0100 Subject: [PATCH] Fix PyPIClient python 3.3 compatibility Python 3.3 raises an exception at `reversed(dict.items())` as `items()` are not treated as Sequence. Additionally dictionary keys' order is undefined. Therefore release keys (versions) are sorted and enumerated separately. --- package_control/clients/pypi_client.py | 13 +++--- package_control/tests/test_clients.py | 57 +++++++++++++++++++++++--- 2 files changed, 59 insertions(+), 11 deletions(-) diff --git a/package_control/clients/pypi_client.py b/package_control/clients/pypi_client.py index 289e3eb1..28f358a1 100644 --- a/package_control/clients/pypi_client.py +++ b/package_control/clients/pypi_client.py @@ -2,6 +2,7 @@ from ..pep440 import PEP440Version from ..pep440 import PEP440VersionSpecifier +from ..package_version import version_sort from .json_api_client import JSONApiClient @@ -162,10 +163,9 @@ def _download_info_from_fixed_version(self, name, version, asset_templates): """ pypi_url = "https://pypi.org/pypi/{}/{}/json".format(name, version) - info = self.fetch_json(pypi_url) + assets = self.fetch_json(pypi_url)["urls"] asset_templates = self._expand_asset_variables(asset_templates) - assets = info["urls"] output = [] for pattern, selectors in asset_templates: @@ -192,21 +192,21 @@ def _download_info_from_latest_version(self, name, asset_templates): """ pypi_url = "https://pypi.org/pypi/{}/json".format(name) - info = self.fetch_json(pypi_url) + releases = self.fetch_json(pypi_url)["releases"] asset_templates = self._expand_asset_variables(asset_templates) max_releases = self.settings.get("max_releases", 0) num_releases = [0] * len(asset_templates) - output = [] - # get latest compatible release for each asset template - for version, assets in reversed(info["releases"].items()): + output = [] + for version in reversed(version_sort(releases)): # we don"t want beta releases! if not PEP440Version(version).is_final: continue + assets = releases[version] for idx, (pattern, selectors) in enumerate(asset_templates): if max_releases > 0 and num_releases[idx] >= max_releases: continue @@ -215,6 +215,7 @@ def _download_info_from_latest_version(self, name, asset_templates): continue output.append(info) num_releases[idx] += 1 + if max_releases > 0 and min(num_releases) >= max_releases: break diff --git a/package_control/tests/test_clients.py b/package_control/tests/test_clients.py index f1cf174e..f331987e 100644 --- a/package_control/tests/test_clients.py +++ b/package_control/tests/test_clients.py @@ -1217,7 +1217,9 @@ def download_info_from_tags(self, url, result): ( ( # name - "01", + "py33_pinned_01", + # settings + None, # url "https://pypi.org/project/coverage/4.0", # asset_templates @@ -1263,7 +1265,8 @@ def download_info_from_tags(self, url, result): ] ), ( - "02", + "py33_pinned_02", + None, "https://pypi.org/project/coverage/4.0", [ ( @@ -1305,7 +1308,8 @@ def download_info_from_tags(self, url, result): ] ), ( - "03", + "py33_pinned_03", + None, "https://pypi.org/project/coverage/4.0", [ ( @@ -1346,9 +1350,52 @@ def download_info_from_tags(self, url, result): } ] ), + ( + "py33_latest", + {"max_releases": 1}, + "https://pypi.org/project/coverage", + [ + ( + "coverage-${version}-cp${py_version}-*-macosx_*_x86_64.whl", + { + "platforms": ["osx-x64"], + "python_versions": ["3.3"] + } + ), + ( + "coverage-${version}-cp${py_version}-*-win_amd64.whl", + { + "platforms": ["windows-x64"], + "python_versions": ["3.3"] + } + ) + ], + [ + { + "date": "2019-07-29 15:29:28", + "version": "4.5.4", + "url": "https://files.pythonhosted.org/packages" + "/3b/2f/c641609b79e292a4a29375c4af0cf8156c36a0613000513b05eb1a838a59" + "/coverage-4.5.4-cp33-cp33m-macosx_10_10_x86_64.whl", + "sha256": "6b62544bb68106e3f00b21c8930e83e584fdca005d4fffd29bb39fb3ffa03cb5", + "platforms": ["osx-x64"], + "python_versions": ["3.3"], + }, + { + "date": "2016-07-26 21:09:17", + "version": "4.2", + "url": "https://files.pythonhosted.org/packages" + "/b1/55/02815cb8abb091033abb979ebde5122bb33b85c5987dede9ccd019033d19" + "/coverage-4.2-cp33-cp33m-win_amd64.whl", + "sha256": "bd4eba631f07cae8cdb9c55c144f165649e6701b962f9d604b4e00cf8802406c", + "platforms": ["windows-x64"], + "python_versions": ["3.3"], + } + ] + ), ), first_param_name_suffix=True ) - def download_info_from_releases(self, url, asset_templates, result): - client = PyPiClient(self.settings()) + def download_info_from_releases(self, extra_settings, url, asset_templates, result): + client = PyPiClient(self.settings(extra_settings)) self.assertEqual(result, client.download_info_from_releases(url, asset_templates))