-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from jaimergp/feedstock-outputs
add api for feedstock outputs
- Loading branch information
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from functools import lru_cache | ||
|
||
import requests | ||
|
||
|
||
@lru_cache(maxsize=1) | ||
def feedstock_outputs_config(): | ||
ref = "main" | ||
req = requests.get( | ||
"https://raw.githubusercontent.com/conda-forge/feedstock-outputs/" | ||
f"{ref}/config.json" | ||
) | ||
req.raise_for_status() | ||
return req.json() | ||
|
||
|
||
def sharded_path(name) -> str: | ||
""" | ||
Get the path to the sharded JSON path in the feedstock_outputs repository. | ||
Parameters | ||
---------- | ||
name : str | ||
The name of the package. | ||
Returns | ||
------- | ||
path : str | ||
The path to the sharded JSON file. Leading slash is omitted. | ||
""" | ||
# See https://github.com/conda-forge/feedstock-outputs/ | ||
# blob/c35451f2fb8b7/scripts/shard_repo.py | ||
# for sharding details. | ||
config = feedstock_outputs_config() | ||
outputs_path = config["outputs_path"] | ||
shard_level = config["shard_level"] | ||
shard_fill = config["shard_fill"] | ||
|
||
name = name.lower() | ||
chars = [c for c in name if c.isalnum()][:shard_level] | ||
while len(chars) < shard_level: | ||
chars.append(shard_fill) | ||
|
||
return f"{outputs_path}/{'/'.join(chars)}/{name}.json" | ||
|
||
|
||
@lru_cache(maxsize=1024) | ||
def package_to_feedstock(name, **request_kwargs): | ||
"""Map a package name to the feedstock name(s). | ||
Parameters | ||
---------- | ||
package : str | ||
The name of the package. | ||
Returns | ||
------- | ||
feedstock : str | ||
The name of the feedstock, without the ``-feedstock`` suffix. | ||
request_kwargs : dict | ||
Keyword arguments to pass to ``requests.get``. | ||
""" | ||
assert name, "name must not be empty" | ||
|
||
ref = "main" | ||
path = sharded_path(name) | ||
req = requests.get( | ||
f"https://raw.githubusercontent.com/conda-forge/feedstock-outputs/{ref}/{path}", | ||
**request_kwargs, | ||
) | ||
req.raise_for_status() | ||
return req.json()["feedstocks"] | ||
|
||
|
||
if __name__ == "__main__": | ||
import sys | ||
|
||
if len(sys.argv) < 2: | ||
print("Usage: python -m conda_forge_metadata.feedstock_outputs <package>") | ||
sys.exit(1) | ||
|
||
for name in sys.argv[1:]: | ||
print(name, "->", package_to_feedstock(name)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from conda_forge_metadata.feedstock_outputs import package_to_feedstock | ||
|
||
|
||
def test_feedstock_outputs(): | ||
assert package_to_feedstock("conda-forge-pinning") == ["conda-forge-pinning"] | ||
assert package_to_feedstock("tk") == ["tk"] | ||
assert "python" in package_to_feedstock("python") |