forked from bazel-contrib/rules_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpypi_cache.bzl
More file actions
55 lines (42 loc) · 1.77 KB
/
pypi_cache.bzl
File metadata and controls
55 lines (42 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""A cache for the PyPI index contents evaluation.
This is design to work as the following:
- in-memory cache for results of PyPI index queries, so that we are not calling PyPI multiple times
for the same package for different hub repos.
In the future the same will be used to:
- Store PyPI index query results as facts in the MODULE.bazel.lock file
"""
def pypi_cache(store = None):
"""The cache for PyPI index queries.
Currently the key is of the following structure:
(url, real_url)
"""
# buildifier: disable=uninitialized
self = struct(
_store = store or {},
setdefault = lambda key, parsed_result: _pypi_cache_setdefault(self, key, parsed_result),
get = lambda key: _pypi_cache_get(self, key),
)
# buildifier: enable=uninitialized
return self
def _pypi_cache_setdefault(self, key, parsed_result):
"""Store the value if not yet cached.
Args:
self: {type}`struct` The self of this implementation.
key: {type}`str` The cache key, can be any string.
parsed_result: {type}`struct` The result of `parse_simpleapi_html` function.
index_url and distribution is used to write to the MODULE.bazel.lock file as facts
real_index_url and distribution is used to write to in-memory cache to ensure that there are
no duplicate calls to the PyPI indexes
Returns:
The `parse_result`.
"""
return self._store.setdefault(key, parsed_result)
def _pypi_cache_get(self, key):
"""Return the parsed result from the cache.
Args:
self: {type}`struct` The self of this implementation.
key: {type}`str` The cache key, can be any string.
Returns:
The {type}`struct` or `None` based on if the result is in the cache or not.
"""
return self._store.get(key)