amoghrajesh commented on code in PR #76575: URL: https://github.com/apache/airflow/pull/76575#discussion_r4665719185
########## dev/registry/fill_durable_versions.py: ########## @@ -0,0 +1,172 @@ +#!/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. +"""Date each durable module in modules.json against the provider tags that first shipped it. + +Runs on the host rather than in Breeze with the rest of the module extraction, because it reads +the ``providers-<id>/<version>`` git tags and Breeze does not mount ``.git``. + +Usage: + python dev/registry/fill_durable_versions.py --modules dev/registry/modules.json +""" + +from __future__ import annotations + +import argparse +import functools +import json +import subprocess +import sys +from pathlib import Path + +from packaging.version import InvalidVersion, Version + +AIRFLOW_ROOT = Path(__file__).parent.parent.parent + +# Inputs to the dating, not part of the published /api/modules.json contract. +EXTRACTION_ONLY_FIELDS = ("durable_mechanism", "durable_gating_class") + +# Patterns matching the mechanism, never its registry declaration, and earliest match wins. +# KubernetesPodOperator reconnected via task_state_store in 10.20.0 but only declared +# `__supports_durable_execution` in 10.21.0; AgentOperator took `durable` in common-ai 0.1.0 +# but only moved to task_state_store in 0.7.0. +DURABLE_TOKENS = { + "marker": r"task_state_store|durable: bool", + "resumable_mixin": "execute_resumable", +} + + +class GitCommandError(RuntimeError): + """A git call failed for a reason other than one the caller expected.""" + + +def run_git(*args: str, allowed_returncodes: tuple[int, ...] = (0,)) -> list[str]: + try: + result = subprocess.run(["git", *args], capture_output=True, text=True, cwd=AIRFLOW_ROOT, check=False) + except OSError as exc: + raise GitCommandError(f"Could not run git {' '.join(args)}: {exc}") from exc + if result.returncode not in allowed_returncodes: + raise GitCommandError( + f"git {' '.join(args)} failed with exit code {result.returncode}: {result.stderr.strip()}" + ) + return [line for line in result.stdout.splitlines() if line] + + [email protected] +def get_released_tags(provider_id: str) -> tuple[str, ...]: + tags = [] + for tag in run_git("tag", "--list", f"providers-{provider_id}/*"): + try: + version = Version(tag.rsplit("/", 1)[-1]) + except InvalidVersion: + continue + if not version.is_prerelease: + tags.append(tag) + return tuple(tags) + + +def build_source_path(provider_id: str, qualified_class: str) -> str: + module_path = qualified_class.rsplit(".", 1)[0].replace(".", "/") + return f"providers/{provider_id.replace('-', '/')}/src/{module_path}.py" + + +def find_first_releases(provider_id: str, token: str, paths: set[str]) -> dict[str, str]: + """Map each path to the earliest released version of `provider_id` whose copy holds `token`. + + One `git grep` searches every release tag at once, so this costs a single call per provider + rather than one per version, and it reports every match rather than assuming the capability + was added once and kept. + """ + tags = get_released_tags(provider_id) + if not tags: + return {} + # git grep exits 1 when nothing matches, which is an ordinary answer here. + matches = run_git("grep", "-lE", token, *tags, "--", *sorted(paths), allowed_returncodes=(0, 1)) Review Comment: Thanks! The token is now searched inside the gating class body: `git grep -l` still picks the candidate tags per file, then each class is read with `git show` + `ast` from the oldest candidate up. Added a test with two classes in one file adopting it in 7.17.0 and 7.25.0. Against real tags, `DatabricksNotebookOperator` and `DatabricksTaskOperator` now get no version (7.25.0 is still an RC) instead of 7.17.0. -- 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]
