Skip to content

Commit

Permalink
Fix PyPIClient python 3.3 compatibility
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
deathaxe committed Nov 22, 2023
1 parent cabc1cb commit 8e05178
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 11 deletions.
13 changes: 7 additions & 6 deletions package_control/clients/pypi_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from ..pep440 import PEP440Version
from ..pep440 import PEP440VersionSpecifier
from ..package_version import version_sort

from .json_api_client import JSONApiClient

Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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

Expand Down
57 changes: 52 additions & 5 deletions package_control/tests/test_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1263,7 +1265,8 @@ def download_info_from_tags(self, url, result):
]
),
(
"02",
"py33_pinned_02",
None,
"https://pypi.org/project/coverage/4.0",
[
(
Expand Down Expand Up @@ -1305,7 +1308,8 @@ def download_info_from_tags(self, url, result):
]
),
(
"03",
"py33_pinned_03",
None,
"https://pypi.org/project/coverage/4.0",
[
(
Expand Down Expand Up @@ -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))

0 comments on commit 8e05178

Please sign in to comment.