amaechler commented on code in PR #20736: URL: https://github.com/apache/druid/pull/20736#discussion_r4390834908
########## distribution/bin/refresh-license-versions.py: ########## @@ -0,0 +1,198 @@ +#!/usr/bin/env python3 + +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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 contextlib +import importlib.util +import os +import re +import sys +import types + +import yaml + +spec = importlib.util.spec_from_file_location( + "check_licenses", os.path.join(os.path.dirname(os.path.abspath(__file__)), "check-licenses.py") +) +check_licenses = importlib.util.module_from_spec(spec) +spec.loader.exec_module(check_licenses) + +VERSION_LINE = re.compile(r"^version:[ \t]*(.*?)[ \t]*(\r?\n)?$") + + +def split_documents(text): + # Returns the licenses.yaml lines grouped into YAML documents; each group after the first starts with its '---' line. + documents = [[]] + for line in text.splitlines(keepends=True): + if line.rstrip() == "---": + documents.append([]) + documents[-1].append(line) + return documents + + +def find_version_line(lines): + for index, line in enumerate(lines): + match = VERSION_LINE.match(line) + if match: + return index, match + return None, None + + +def unquote(raw): + if len(raw) >= 2 and raw[0] == raw[-1] and raw[0] in "\"'": + return raw[1:-1], raw[0] + return raw, None + + +def format_version(version, quote): + # Quote when the old value was quoted, or when YAML would not read the new value back as a string (2.22 is a float). + if quote is None and not isinstance(yaml.safe_load(version), str): + quote = '"' + return version if quote is None else quote + version + quote + + +def index_reports(reported_modules): + # (group_id, artifact_id, version) -> modules becomes (group_id, artifact_id) -> module -> versions. + versions = {} + for (group_id, artifact_id, version), modules in reported_modules.items(): + for module in modules: + versions.setdefault((group_id, artifact_id), {}).setdefault(module, set()).add(version) + return versions + + +def refresh_versions(text, reported_modules): + # Updates the version of every licenses.yaml entry whose libraries all moved to one new reported version. + # Returns the new text, the updated entries as (name, module, old, new) and the entries that need a person, + # as (name, module, reason). Only version lines change, so comments and notices stay as they are. + reported = index_reports(reported_modules) + report_modules = set().union(*reported_modules.values()) + documents = split_documents(text) + + entries = [] + # A reported version that another entry already registers belongs to that entry, not to a stale one. + registered = {} + for lines in documents: + parsed = yaml.safe_load("".join(lines)) + line_index, match = find_version_line(lines) + if not isinstance(parsed, dict) or "libraries" not in parsed or match is None or not match.group(1): + continue + version, quote = unquote(match.group(1)) + libraries = [library for item in parsed["libraries"] for library in item.items()] + for library in libraries: + registered.setdefault(library, {}).setdefault(parsed.get("module"), set()).add(version) + entries.append(types.SimpleNamespace( + lines=lines, line_index=line_index, newline=match.group(2) or "", name=parsed.get("name"), + module=parsed.get("module"), skip=parsed.get("skip_dependency_report_check"), version=version, + quote=quote, libraries=libraries, + )) + + proposals = [] + flagged = [] + for entry in entries: + if entry.skip or entry.module not in report_modules: + continue + + current = [] + unreported = [] + moved_to = set() + problems = [] + for library in entry.libraries: + by_module = reported.get(library) + if not by_module: + unreported.append("{}:{}".format(*library)) + continue + # Prefer the entry's own module report; only entries of that module compete for its versions. + if entry.module in by_module: + pool, taken = by_module[entry.module], registered[library][entry.module] + else: + pool, taken = set().union(*by_module.values()), set().union(*registered[library].values()) + if entry.version in pool: + current.append(library) + continue + candidates = pool - taken Review Comment: Fixed in de1478f405. A library whose reported versions all belong to other entries now blocks the update, and the entry is listed for a manual edit instead. `test_flags_moved_entries_with_libraries_another_entry_registers` covers it and fails without the fix. The output on master, on #20737's `licenses.yaml` and in the three replays from the description is unchanged. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
