Skip to content

Commit

Permalink
Add JSON parsing and fetching from GitHub (#15)
Browse files Browse the repository at this point in the history
This is how pre-commit-hooks will get the latest version of the
metadata, since pre-commit caches the environment and doesn't update
rapids-metadata as it's updated.
  • Loading branch information
KyleFromNVIDIA authored Jun 26, 2024
1 parent fef10b8 commit 9ca0136
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions dependencies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@ dependencies:
- output_types: [conda, requirements, pyproject]
packages:
- pytest
- pytest-httpserver
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ requires = [
[project.optional-dependencies]
test = [
"pytest",
"pytest-httpserver",
] # This list was generated by `rapids-dependency-file-generator`. To make changes, edit dependencies.yaml and run `rapids-dependency-file-generator`.

[tool.setuptools]
Expand Down
33 changes: 33 additions & 0 deletions src/rapids_metadata/remote.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright (c) 2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import urllib.request

import pydantic

from .metadata import RAPIDSMetadata


__all__ = ["fetch_latest"]

_GITHUB_METADATA_URL = "https://raw.githubusercontent.com/rapidsai/rapids-metadata/main/rapids-metadata.json"


def _fetch_from_url(url: str) -> RAPIDSMetadata:
with urllib.request.urlopen(url) as f:
return pydantic.TypeAdapter(RAPIDSMetadata).validate_json(f.read())


def fetch_latest() -> RAPIDSMetadata:
return _fetch_from_url(_GITHUB_METADATA_URL)
39 changes: 39 additions & 0 deletions tests/test_remote.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright (c) 2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from unittest.mock import patch

from pytest_httpserver import HTTPServer
from pydantic import TypeAdapter

import rapids_metadata.remote as rapids_remote
from rapids_metadata import all_metadata
from rapids_metadata.metadata import RAPIDSMetadata


def test_fetch_from_url(httpserver: HTTPServer):
httpserver.expect_request("/rapids-metadata.json").respond_with_json(
TypeAdapter(RAPIDSMetadata).dump_python(all_metadata)
)
assert (
rapids_remote._fetch_from_url(httpserver.url_for("/rapids-metadata.json"))
== all_metadata
)


def test_fetch_latest():
with patch("rapids_metadata.remote._fetch_from_url") as patch_fetch_from_url:
return_value = rapids_remote.fetch_latest()
patch_fetch_from_url.assert_called_once_with(rapids_remote._GITHUB_METADATA_URL)
assert return_value == patch_fetch_from_url()

0 comments on commit 9ca0136

Please sign in to comment.