|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +# you may not use this file except in compliance with the License. |
| 3 | +# You may obtain a copy of the License at |
| 4 | +# |
| 5 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +# See the License for the specific language governing permissions and |
| 11 | +# limitations under the License. |
| 12 | +# |
| 13 | + |
| 14 | +import os |
| 15 | + |
| 16 | +import requests |
| 17 | +import requests_cache |
| 18 | +import yaml |
| 19 | +from jinja2 import Environment, FileSystemLoader |
| 20 | + |
| 21 | +requests_cache.install_cache("adoptium_cache", expire_after=3600) |
| 22 | + |
| 23 | +# Setup the Jinja2 environment |
| 24 | +env = Environment(loader=FileSystemLoader("templates")) |
| 25 | + |
| 26 | +headers = { |
| 27 | + "User-Agent": "Adoptium Linux Specfile Updater", |
| 28 | +} |
| 29 | + |
| 30 | + |
| 31 | +def archHelper(arch): |
| 32 | + if arch == "x64": |
| 33 | + return "x86_64" |
| 34 | + else: |
| 35 | + return arch |
| 36 | + |
| 37 | + |
| 38 | +# Load the YAML configuration |
| 39 | +with open("config/temurin.yml", "r") as file: |
| 40 | + config = yaml.safe_load(file) |
| 41 | + |
| 42 | +for image_type in ["jdk", "jre"]: |
| 43 | + # Iterate through supported_distributions |
| 44 | + for distro in config["supported_distributions"]["Distros"]: |
| 45 | + for version in config["supported_distributions"]["Versions"]: |
| 46 | + |
| 47 | + # Fetch latest release for version from Adoptium API |
| 48 | + url = f"https://api.adoptium.net/v3/assets/feature_releases/{version}/ga?page=0&image_type={image_type}&os=alpine-linux&page_size=1&vendor=eclipse" |
| 49 | + response = requests.get(url, headers=headers) |
| 50 | + response.raise_for_status() |
| 51 | + data = response.json() |
| 52 | + |
| 53 | + release = response.json()[0] |
| 54 | + |
| 55 | + # Extract the version number from the release name |
| 56 | + openjdk_version = release["release_name"] |
| 57 | + |
| 58 | + # If version doesn't equal 8, get the more accurate version number |
| 59 | + if version != 8: |
| 60 | + openjdk_version = release["version_data"]["openjdk_version"] |
| 61 | + # if openjdk_version contains -LTS remove it |
| 62 | + if "-LTS" in openjdk_version: |
| 63 | + openjdk_version = openjdk_version.replace("-LTS", "") |
| 64 | + |
| 65 | + # Convert version from 11.0.24+8 to 11.0.24_p8 |
| 66 | + openjdk_version = openjdk_version.replace("jdk", "") |
| 67 | + openjdk_version = openjdk_version.replace("+", "_p") |
| 68 | + openjdk_version = openjdk_version.replace("u", ".") |
| 69 | + openjdk_version = openjdk_version.replace("-b", ".") |
| 70 | + |
| 71 | + # Generate the data for each architecture |
| 72 | + arch_data = {} |
| 73 | + |
| 74 | + for binary in release["binaries"]: |
| 75 | + arch_data[archHelper(binary["architecture"])] = { |
| 76 | + "download_url": binary["package"]["link"], |
| 77 | + "checksum": binary["package"]["checksum"], |
| 78 | + "filename": binary["package"]["name"], |
| 79 | + } |
| 80 | + |
| 81 | + # Set path to the specfiles |
| 82 | + path = f"{image_type}/{distro}/src/main/packaging/temurin/{version}" |
| 83 | + |
| 84 | + # Check that the path exists |
| 85 | + os.makedirs(path, exist_ok=True) |
| 86 | + |
| 87 | + # Load the template |
| 88 | + template = env.get_template(f"{distro}.spec.j2") |
| 89 | + |
| 90 | + # Render the template |
| 91 | + rendered = template.render( |
| 92 | + arch_data=arch_data, |
| 93 | + openjdk_version=openjdk_version, |
| 94 | + release_name=release["release_name"], |
| 95 | + version=version, |
| 96 | + image_type=image_type, |
| 97 | + ) |
| 98 | + |
| 99 | + # Set filename based on switch distro e.g APKBUILD for alpine, spec for others |
| 100 | + match distro: |
| 101 | + case "alpine": |
| 102 | + filename = "APKBUILD" |
| 103 | + case _: |
| 104 | + print(f"Unsupported distro: {distro}") |
| 105 | + exit(1) |
| 106 | + |
| 107 | + # Write the rendered template to the file |
| 108 | + with open(f"{path}/{filename}", "w") as file: |
| 109 | + file.write(rendered) |
| 110 | + print(f"Generated {path}/{filename}") |
0 commit comments