Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSON encoder #6

Merged
merged 7 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ The motivating use case for this project is
[`pre-commit-hooks`](https://github.com/rapidsai/pre-commit-hooks), but other
projects may certainly use it too.

This package can also output the metadata in JSON form for consumption by
external programs, such as shell scripts and `jq`. To get JSON output, run:

```
rapids-metadata-json
```

This will print the metadata for the RAPIDS version specified by the `VERSION`
file in the current directory or above. If you wish to get metadata for all
RAPIDS versions instead, run:

```
rapids-metadata-json --all-versions
```

## Justification

`pre-commit-hooks` has to know things about the structure of the RAPIDS project
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ classifiers = [
]
requires-python = ">=3.9"

[project.scripts]
rapids-metadata-json = "rapids_metadata.json:main"

[build-system]
build-backend = "setuptools.build_meta"
requires = [
Expand Down
3 changes: 1 addition & 2 deletions src/rapids_metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.

from .metadata import (
PseudoRepository,
RAPIDSMetadata,
RAPIDSPackage,
RAPIDSRepository,
Expand All @@ -28,7 +27,7 @@

rapids_metadata.versions["24.08"] = RAPIDSVersion(
repositories={
PseudoRepository.NVIDIA: RAPIDSRepository(
"_nvidia": RAPIDSRepository(
packages={
"cubinlinker": RAPIDSPackage(),
}
Expand Down
64 changes: 64 additions & 0 deletions src/rapids_metadata/json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# 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 argparse
import dataclasses
import json
import os
import sys
from typing import Any, Union

from . import rapids_metadata
from .metadata import (
RAPIDSMetadata,
RAPIDSPackage,
RAPIDSRepository,
RAPIDSVersion,
)
from .rapids_version import get_rapids_version


__all__ = [
"main",
]


class _RAPIDSMetadataEncoder(json.JSONEncoder):
def default(
self, o: Union[RAPIDSMetadata, RAPIDSPackage, RAPIDSRepository, RAPIDSVersion]
) -> dict[str, Any]:
return dataclasses.asdict(o)


def main():
parser = argparse.ArgumentParser()
parser.add_argument("--all-versions", action="store_true")

parsed = parser.parse_args()
metadata = (
rapids_metadata
if parsed.all_versions
else RAPIDSMetadata(
versions={
get_rapids_version(os.getcwd()): rapids_metadata.get_current_version(
os.getcwd()
)
}
)
)
json.dump(metadata, sys.stdout, cls=_RAPIDSMetadataEncoder)


if __name__ == "__main__":
main()
14 changes: 1 addition & 13 deletions src/rapids_metadata/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,19 @@
# limitations under the License.

from dataclasses import dataclass, field
from enum import Enum
from os import PathLike
from typing import Union

from packaging.version import Version
from .rapids_version import get_rapids_version

__all__ = [
"PseudoRepository",
"RAPIDSMetadata",
"RAPIDSPackage",
"RAPIDSRepository",
"RAPIDSVersion",
]


class PseudoRepository(Enum):
NVIDIA = "nvidia"

def __str__(self):
return f"_{self.value}"


@dataclass
class RAPIDSPackage:
has_alpha_spec: bool = field(default=True)
Expand All @@ -49,9 +39,7 @@ class RAPIDSRepository:

@dataclass
class RAPIDSVersion:
repositories: dict[Union[str, PseudoRepository], RAPIDSRepository] = field(
default_factory=dict
)
repositories: dict[str, RAPIDSRepository] = field(default_factory=dict)

@property
def all_packages(self) -> set[str]:
Expand Down
Loading