Skip to content

Commit

Permalink
Add in PEP700 simple API v1.1 support (#1557)
Browse files Browse the repository at this point in the history
* Add in PEP700 simple API v1.1 support

- Move simple module to generate API 1.1 (JSON changes only)
  - Since there is only additions we don't really break 1.0 so just moving to 1.1
  - Seems pypi did this too
  - PEP700: https://peps.python.org/pep-0700/

Tests:
- Fix tests for v1.1 new fields based on our mocked metadata

* Move to directly getting size as it's expected to always be there
  • Loading branch information
cooperlees authored Sep 26, 2023
1 parent 963f9ef commit 28d63fd
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 6.4.0 (Unreleased)

- Move JSON Simple API to version 1.1 (as per PEP700) `PR #1557`
- Move to >= 3.10 project `PR #1457`

## Bug Fixes
Expand Down
1 change: 1 addition & 0 deletions src/bandersnatch/default.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[mirror]
; The directory where the mirror data will be stored.
directory = /srv/pypi

; Save JSON metadata into the web tree:
; URL/pypi/PKG_NAME/json (Symlink) -> URL/json/PKG_NAME
json = false
Expand Down
14 changes: 11 additions & 3 deletions src/bandersnatch/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ class SimpleAPI:

# PEP620 Simple API Version
pypi_repository_version = "1.0"
# PEP691 Simple API Version
pypi_simple_api_version = "1.0"
# PEP691 Simple API Version 1.0
# PEP700 defines 1.1
pypi_simple_api_version = "1.1"

def __init__(
self,
Expand Down Expand Up @@ -215,6 +216,8 @@ def generate_json_simple_page(
"_last-serial": str(package.last_serial),
},
"name": package.name,
# TODO: Just sorting by default sort - Maybe specify order in future PEP
"versions": sorted(package.releases.keys()),
}

release_files = package.release_files
Expand All @@ -229,6 +232,8 @@ def generate_json_simple_page(
self.digest_name: r["digests"][self.digest_name],
},
"requires-python": r.get("requires_python", ""),
"size": r["size"],
"upload-time": r.get("upload_time_iso_8601", ""),
"url": self._file_url_to_local_url(r["url"]),
"yanked": r.get("yanked", False),
}
Expand Down Expand Up @@ -263,7 +268,10 @@ def sync_index_page(
simple_json_path = simple_dir / "index.v1_json"

simple_json: dict[str, Any] = {
"meta": {"_last-serial": serial, "api-version": "1.0"},
"meta": {
"_last-serial": serial,
"api-version": self.pypi_simple_api_version,
},
"projects": [],
}

Expand Down
8 changes: 4 additions & 4 deletions src/bandersnatch/tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
from bandersnatch.storage import Storage
from bandersnatch.tests.test_simple_fixtures import (
EXPECTED_SIMPLE_GLOBAL_JSON_PRETTY,
EXPECTED_SIMPLE_SIXTYNINE_JSON,
EXPECTED_SIMPLE_SIXTYNINE_JSON_PRETTY,
EXPECTED_SIMPLE_SIXTYNINE_JSON_1_1,
EXPECTED_SIMPLE_SIXTYNINE_JSON_PRETTY_1_1,
SIXTYNINE_METADATA,
)
from bandersnatch_storage_plugins.filesystem import FilesystemStorage
Expand Down Expand Up @@ -58,9 +58,9 @@ def test_json_package_page() -> None:
s = SimpleAPI(Storage(), SimpleFormat.JSON, [], SimpleDigest.SHA256, False, None)
p = Package("69")
p._metadata = SIXTYNINE_METADATA
assert EXPECTED_SIMPLE_SIXTYNINE_JSON == s.generate_json_simple_page(p)
assert EXPECTED_SIMPLE_SIXTYNINE_JSON_1_1 == s.generate_json_simple_page(p)
# Only testing pretty so it's easier for humans ...
assert EXPECTED_SIMPLE_SIXTYNINE_JSON_PRETTY == s.generate_json_simple_page(
assert EXPECTED_SIMPLE_SIXTYNINE_JSON_PRETTY_1_1 == s.generate_json_simple_page(
p, pretty=True
)

Expand Down
20 changes: 14 additions & 6 deletions src/bandersnatch/tests/test_simple_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@
"vulnerabilities": [],
}

EXPECTED_SIMPLE_SIXTYNINE_JSON = """\
{"files": [{"filename": "69-0.69.tar.gz", "hashes": {"sha256": "5c11f48399f9b1bca802751513f1f97bff6ce97e6facb576b7729e1351453c10"}, "requires-python": ">=3.6", "url": "../../packages/d3/cc/95dc5434362bd333a1fec275231775d748315b26edf1e7e568e6f8660238/69-0.69.tar.gz", "yanked": false}, {"filename": "69-6.9.tar.gz", "hashes": {"sha256": "0c8deb7c8574787283c3fc08b714ee63fd6752a38d13515a9d8508798d428597"}, "requires-python": ">=3.6", "url": "../../packages/7b/6e/7c4ce77c6ca092e94e19b78282b459e7f8270362da655cbc6a75eeb9cdd7/69-6.9.tar.gz", "yanked": false}], "meta": {"api-version": "1.0", "_last-serial": "10333928"}, "name": "69"}\
EXPECTED_SIMPLE_SIXTYNINE_JSON_1_1 = """\
{"files": [{"filename": "69-0.69.tar.gz", "hashes": {"sha256": "5c11f48399f9b1bca802751513f1f97bff6ce97e6facb576b7729e1351453c10"}, "requires-python": ">=3.6", "size": 1078, "upload-time": "2018-05-17T03:37:19.330556Z", "url": "../../packages/d3/cc/95dc5434362bd333a1fec275231775d748315b26edf1e7e568e6f8660238/69-0.69.tar.gz", "yanked": false}, {"filename": "69-6.9.tar.gz", "hashes": {"sha256": "0c8deb7c8574787283c3fc08b714ee63fd6752a38d13515a9d8508798d428597"}, "requires-python": ">=3.6", "size": 1077, "upload-time": "2018-05-17T03:47:45.953704Z", "url": "../../packages/7b/6e/7c4ce77c6ca092e94e19b78282b459e7f8270362da655cbc6a75eeb9cdd7/69-6.9.tar.gz", "yanked": false}], "meta": {"api-version": "1.1", "_last-serial": "10333928"}, "name": "69", "versions": ["0.69", "6.9"]}\
"""

EXPECTED_SIMPLE_SIXTYNINE_JSON_PRETTY = """\
EXPECTED_SIMPLE_SIXTYNINE_JSON_PRETTY_1_1 = """\
{
"files": [
{
Expand All @@ -121,6 +121,8 @@
"sha256": "5c11f48399f9b1bca802751513f1f97bff6ce97e6facb576b7729e1351453c10"
},
"requires-python": ">=3.6",
"size": 1078,
"upload-time": "2018-05-17T03:37:19.330556Z",
"url": "../../packages/d3/cc/95dc5434362bd333a1fec275231775d748315b26edf1e7e568e6f8660238/69-0.69.tar.gz",
"yanked": false
},
Expand All @@ -130,23 +132,29 @@
"sha256": "0c8deb7c8574787283c3fc08b714ee63fd6752a38d13515a9d8508798d428597"
},
"requires-python": ">=3.6",
"size": 1077,
"upload-time": "2018-05-17T03:47:45.953704Z",
"url": "../../packages/7b/6e/7c4ce77c6ca092e94e19b78282b459e7f8270362da655cbc6a75eeb9cdd7/69-6.9.tar.gz",
"yanked": false
}
],
"meta": {
"api-version": "1.0",
"api-version": "1.1",
"_last-serial": "10333928"
},
"name": "69"
"name": "69",
"versions": [
"0.69",
"6.9"
]
}\
"""

EXPECTED_SIMPLE_GLOBAL_JSON_PRETTY = """\
{
"meta": {
"_last-serial": 12345,
"api-version": "1.0"
"api-version": "1.1"
},
"projects": [
{
Expand Down

0 comments on commit 28d63fd

Please sign in to comment.