|
| 1 | +# |
| 2 | +# ---------------------------------------------------------------------------------------------------- |
| 3 | +# |
| 4 | +# Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. |
| 5 | +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 6 | +# |
| 7 | +# This code is free software; you can redistribute it and/or modify it |
| 8 | +# under the terms of the GNU General Public License version 2 only, as |
| 9 | +# published by the Free Software Foundation. Oracle designates this |
| 10 | +# particular file as subject to the "Classpath" exception as provided |
| 11 | +# by Oracle in the LICENSE file that accompanied this code. |
| 12 | +# |
| 13 | +# This code is distributed in the hope that it will be useful, but WITHOUT |
| 14 | +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 16 | +# version 2 for more details (a copy is included in the LICENSE file that |
| 17 | +# accompanied this code). |
| 18 | +# |
| 19 | +# You should have received a copy of the GNU General Public License version |
| 20 | +# 2 along with this work; if not, write to the Free Software Foundation, |
| 21 | +# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | +# |
| 23 | +# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 24 | +# or visit www.oracle.com if you need additional information or have any |
| 25 | +# questions. |
| 26 | +# |
| 27 | +# ---------------------------------------------------------------------------------------------------- |
| 28 | + |
| 29 | +""" |
| 30 | +Updates the values for the `version` field of the labsjdk-ce-latest and labsjdk-ee-latest |
| 31 | +objects in the common.json file of the Graal repos. |
| 32 | +""" |
| 33 | + |
| 34 | +import argparse |
| 35 | +import json |
| 36 | +import difflib |
| 37 | +from pathlib import Path |
| 38 | + |
| 39 | +if __name__ == "__main__": |
| 40 | + parser = argparse.ArgumentParser(description="Updates jdks.labsjdk-ce-latest.version and jdks.labsjdk-ee-latest.version values in common.json.") |
| 41 | + parser.add_argument("labsjdk_versions", action="store", help="URL or path to file from which labsjdk versions will be read. " \ |
| 42 | + "The content must be a JSON object with ce and ee fields that specify the new values.") |
| 43 | + parser.add_argument("common_json", action="store", help="common.json file to process") |
| 44 | + |
| 45 | + args = parser.parse_args() |
| 46 | + |
| 47 | + common_json_path = Path(args.common_json) |
| 48 | + common_json_text = common_json_path.read_text() |
| 49 | + common_json = json.loads(common_json_text) |
| 50 | + ce_version = common_json["jdks"]["labsjdk-ce-latest"]["version"] |
| 51 | + ee_version = common_json["jdks"]["labsjdk-ee-latest"]["version"] |
| 52 | + |
| 53 | + if args.labsjdk_versions.startswith("http://") or args.labsjdk_versions.startswith("https://"): |
| 54 | + from urllib import request, error |
| 55 | + url = args.labsjdk_versions |
| 56 | + with request.urlopen(url, timeout=10) as response: |
| 57 | + if response.status != 200: |
| 58 | + raise SystemExit(f"Received non-200 status code {response.status} reading {url}") |
| 59 | + content_bytes = response.read() |
| 60 | + encoding = response.info().get_content_charset() or 'utf-8' |
| 61 | + new_versions = content_bytes.decode(encoding) |
| 62 | + else: |
| 63 | + new_versions = Path(args.labsjdk_versions).read_text() |
| 64 | + |
| 65 | + try: |
| 66 | + new_versions = json.loads(new_versions) |
| 67 | + except json.decoder.JSONDecodeError as e: |
| 68 | + size = len(new_versions) |
| 69 | + if size > 1024: |
| 70 | + new_versions = new_versions[0:1024] + "... (truncated)" |
| 71 | + raise SystemExit(f"Error decoding content of size {size} from {args.labsjdk_versions} as JSON: {e}\nContent:\n{new_versions}") |
| 72 | + |
| 73 | + try: |
| 74 | + new_common_json_text = common_json_text\ |
| 75 | + .replace(ce_version, new_versions["ce"])\ |
| 76 | + .replace(ee_version, new_versions["ee"]) |
| 77 | + except KeyError as e: |
| 78 | + raise SystemExit(f"Error extracting versions from JSON in {args.labsjdk_versions}: Missing value for {e}\nJSON:\n{json.dumps(new_versions, indent=2)}") |
| 79 | + |
| 80 | + if new_common_json_text == common_json_text: |
| 81 | + print(f"No change to {common_json_path}") |
| 82 | + else: |
| 83 | + patch = "\n".join((line for line in difflib.unified_diff( |
| 84 | + common_json_text.split("\n"), |
| 85 | + new_common_json_text.split("\n"), |
| 86 | + fromfile=f"a/{common_json_path.name}", |
| 87 | + tofile=f"b/{common_json_path.name}", |
| 88 | + lineterm="" |
| 89 | + ))) |
| 90 | + print(f"Updated {common_json_path} with this patch:\n{patch}") |
| 91 | + common_json_path.write_text(new_common_json_text) |
0 commit comments