|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import dataclasses |
| 6 | +import glob |
| 7 | +import io |
| 8 | +import json |
| 9 | +import pathlib |
| 10 | +import typing |
| 11 | +import unittest |
| 12 | + |
| 13 | +import yaml |
| 14 | + |
| 15 | +import package_versions_selftestdata |
| 16 | + |
| 17 | +"""Generates the workbench software listings for https://access.redhat.com/articles/rhoai-supported-configs |
| 18 | +using the Markdown variant described at https://access.redhat.com/articles/7056942""" |
| 19 | + |
| 20 | +""" |
| 21 | +TODO: |
| 22 | + * separate reading data and printing output |
| 23 | + so that output can be properly sorted (by opendatahub.io/notebook-image-order probably) |
| 24 | + * don't repeat image name when printing multiple tags for it |
| 25 | + * run this in red-hat-data-services repo so we also have (or not have) Habana image |
| 26 | + * diff it with what's in the knowledge base now, to check if outputs match |
| 27 | +""" |
| 28 | + |
| 29 | +ROOT_DIR = pathlib.Path(__file__).parent.parent |
| 30 | + |
| 31 | + |
| 32 | +# unused for now |
| 33 | +@dataclasses.dataclass |
| 34 | +class Manifest: |
| 35 | + _data: any |
| 36 | + |
| 37 | + @property |
| 38 | + def name(self) -> str: |
| 39 | + return self._data['metadata']['annotations']['opendatahub.io/notebook-image-name'] |
| 40 | + |
| 41 | + @property |
| 42 | + def order(self) -> int: |
| 43 | + return int(self._data['metadata']['annotations']['opendatahub.io/notebook-image-order']) |
| 44 | + |
| 45 | + @property |
| 46 | + def tags(self) -> list[Tag]: |
| 47 | + return [Tag(tag) for tag in self._data['spec']['tags']] |
| 48 | + |
| 49 | + |
| 50 | +@dataclasses.dataclass() |
| 51 | +class Tag: |
| 52 | + _data: any |
| 53 | + |
| 54 | + @property |
| 55 | + def name(self) -> str: |
| 56 | + return self._data['name'] |
| 57 | + |
| 58 | + @property |
| 59 | + def recommended(self) -> bool: |
| 60 | + if 'opendatahub.io/workbench-image-recommended' not in self._data['annotations']: |
| 61 | + return False |
| 62 | + return self._data['annotations']['opendatahub.io/workbench-image-recommended'] == 'true' |
| 63 | + |
| 64 | + @property |
| 65 | + def outdated(self) -> bool: |
| 66 | + if 'opendatahub.io/image-tag-outdated' not in self._data['annotations']: |
| 67 | + return False |
| 68 | + return self._data['annotations']['opendatahub.io/image-tag-outdated'] == 'true' |
| 69 | + |
| 70 | + @property |
| 71 | + def sw_general(self) -> list[typing.TypedDict("Software", {"name": str, "version": str})]: |
| 72 | + return json.loads(self._data['annotations']['opendatahub.io/notebook-software']) |
| 73 | + |
| 74 | + @property |
| 75 | + def sw_python(self) -> list[typing.TypedDict("Software", {"name": str, "version": str})]: |
| 76 | + return json.loads(self._data['annotations']['opendatahub.io/notebook-python-dependencies']) |
| 77 | + |
| 78 | + |
| 79 | +def main(): |
| 80 | + pathname = 'manifests/base/*.yaml' |
| 81 | + # pathname = 'manifests/overlays/additional/*.yaml' |
| 82 | + imagestreams: list[Manifest] = [] |
| 83 | + for fn in glob.glob(pathname, root_dir=ROOT_DIR): |
| 84 | + # there may be more than one yaml document in a file (e.g. rstudio buildconfigs) |
| 85 | + with (open(ROOT_DIR / fn, 'rt') as fp): |
| 86 | + for data in yaml.safe_load_all(fp): |
| 87 | + if 'kind' not in data or data['kind'] != 'ImageStream': |
| 88 | + continue |
| 89 | + if 'labels' not in data['metadata']: |
| 90 | + continue |
| 91 | + if ('opendatahub.io/notebook-image' not in data['metadata']['labels'] or |
| 92 | + data['metadata']['labels']['opendatahub.io/notebook-image'] != 'true'): |
| 93 | + continue |
| 94 | + imagestream = Manifest(data) |
| 95 | + imagestreams.append(imagestream) |
| 96 | + |
| 97 | + tabular_data: list[tuple[str, str, str]] = [] |
| 98 | + |
| 99 | + # todo(jdanek): maybe we want to change to sorting by `imagestream.order` |
| 100 | + # for imagestream in sorted(imagestreams, key=lambda imagestream: imagestream.order): |
| 101 | + for imagestream in sorted(imagestreams, key=lambda imagestream: imagestream.name): |
| 102 | + name = imagestream.name |
| 103 | + |
| 104 | + prev_tag = None |
| 105 | + for tag in imagestream.tags: |
| 106 | + if tag.outdated: |
| 107 | + continue |
| 108 | + |
| 109 | + tag_name = tag.name |
| 110 | + recommended = tag.recommended |
| 111 | + |
| 112 | + sw_general = tag.sw_general |
| 113 | + sw_python = tag.sw_python |
| 114 | + |
| 115 | + software: list[str] = [] |
| 116 | + for item in sw_general: |
| 117 | + sw_name: str |
| 118 | + sw_version: str |
| 119 | + sw_name, sw_version = item['name'], item['version'] |
| 120 | + sw_version = sw_version.lstrip("v") |
| 121 | + |
| 122 | + # do not allow duplicates when general and python lists both contain e.g. TensorFlow |
| 123 | + if sw_name in set(item['name'] for item in sw_python): |
| 124 | + continue |
| 125 | + software.append(f"{sw_name} {sw_version}") |
| 126 | + for item in sw_python: |
| 127 | + sw_name: str |
| 128 | + sw_version: str |
| 129 | + sw_name, sw_version = item['name'], item['version'] |
| 130 | + sw_version = sw_version.lstrip("v") |
| 131 | + software.append(f"{sw_name}: {sw_version}") |
| 132 | + |
| 133 | + maybe_techpreview = "" if name not in ('code-server',) else " (Technology Preview)" |
| 134 | + maybe_recommended = "" if not recommended or len(imagestream.tags) == 1 else ' (Recommended)' |
| 135 | + |
| 136 | + tabular_data.append(( |
| 137 | + f'{name}{maybe_techpreview}' if not prev_tag else '', |
| 138 | + f'{tag_name}{maybe_recommended}', |
| 139 | + ', '.join(software) |
| 140 | + )) |
| 141 | + |
| 142 | + prev_tag = tag |
| 143 | + |
| 144 | + print('| Image name | Image version | Preinstalled packages |') |
| 145 | + print('|------------|---------------|-----------------------|') |
| 146 | + for row in tabular_data: |
| 147 | + print(f'| {row[0]} | {row[1]} | {row[2]} |') |
| 148 | + |
| 149 | + print() |
| 150 | + |
| 151 | + print('## Source') |
| 152 | + print() |
| 153 | + print('_mouse hover reveals copy button in top right corner of the box_') |
| 154 | + print() |
| 155 | + print('```markdown') |
| 156 | + print('Image name | Image version | Preinstalled packages') |
| 157 | + print('--------- | ---------') |
| 158 | + for row in tabular_data: |
| 159 | + print(f'{row[0]} | {row[1]} | {row[2]}') |
| 160 | + print('```') |
| 161 | + |
| 162 | +class TestManifest(unittest.TestCase): |
| 163 | + _data = yaml.safe_load(io.StringIO(package_versions_selftestdata.imagestream)) |
| 164 | + manifest = Manifest(_data) |
| 165 | + |
| 166 | + def test_name(self): |
| 167 | + assert self.manifest.name == "Minimal Python" |
| 168 | + |
| 169 | + def test_order(self): |
| 170 | + assert self.manifest.order == 1 |
| 171 | + |
| 172 | + def test_tag_name(self): |
| 173 | + assert self.manifest.tags[0].name == "2024.2" |
| 174 | + |
| 175 | + def test_tag_recommended(self): |
| 176 | + assert self.manifest.tags[0].recommended is True |
| 177 | + |
| 178 | + def test_tag_sw_general(self): |
| 179 | + assert self.manifest.tags[0].sw_general == [{'name': 'Python', 'version': 'v3.11'}] |
| 180 | + |
| 181 | + def test_tag_sw_python(self): |
| 182 | + assert self.manifest.tags[0].sw_python == [{'name': 'JupyterLab', 'version': '4.2'}] |
| 183 | + |
| 184 | + |
| 185 | +if __name__ == '__main__': |
| 186 | + main() |
0 commit comments