Skip to content

Commit 9d9ad80

Browse files
committed
pip: Cache PyPI network calls
1 parent 3f2844f commit 9d9ad80

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

pip/flatpak-pip-generator.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@
168168
except ImportError:
169169
sys.exit("Please install the 'PyYAML' module")
170170

171+
_PYPI_CACHE: dict[tuple[str, str], list[dict]] = {}
172+
171173

172174
def get_flatpak_runtime_scope(runtime: str) -> str:
173175
for scope in ("--user", "--system"):
@@ -324,10 +326,15 @@ def resolve_package_sources(
324326
pypi_files: list[dict] | None = None
325327

326328
def fetch_pypi_files(name: str, version: str) -> list[dict]:
329+
key = (name, version)
330+
if key in _PYPI_CACHE:
331+
return _PYPI_CACHE[key]
327332
url = f"https://pypi.org/pypi/{name}/{version}/json"
328333
print(f"Fetching PyPI metadata for {name}=={version}")
329334
with urllib.request.urlopen(url) as response: # noqa: S310
330-
return json.loads(response.read().decode("utf-8"))["urls"]
335+
data = json.loads(response.read().decode("utf-8"))["urls"]
336+
_PYPI_CACHE[key] = data
337+
return data
331338

332339
def get_pypi_files() -> list[dict]:
333340
nonlocal pypi_files

0 commit comments

Comments
 (0)