airflow pre_commit_decorator_operator_implements_custom_name 源码

  • 2022-10-20
  • 浏览 (333)

airflow pre_commit_decorator_operator_implements_custom_name 代码

文件路径:/scripts/ci/pre_commit/pre_commit_decorator_operator_implements_custom_name.py

#!/usr/bin/env python
#
# 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.
from __future__ import annotations

import ast
import itertools
import pathlib
import sys
from typing import Iterator


def iter_decorated_operators(source: pathlib.Path) -> Iterator[ast.ClassDef]:
    mod = ast.parse(source.read_text("utf-8"), str(source))
    for node in ast.walk(mod):
        if not isinstance(node, ast.ClassDef):
            continue
        if not any(isinstance(base, ast.Name) and base.id == "DecoratedOperator" for base in node.bases):
            continue  # Not a decorated operator.
        yield node


def check_missing_custom_operator_name(klass: ast.ClassDef) -> bool:
    for node in ast.iter_child_nodes(klass):
        if isinstance(node, ast.AnnAssign):
            if isinstance(node.target, ast.Name) and node.target.id == "custom_operator_name":
                return True
        elif isinstance(node, ast.Assign):
            if any(isinstance(t, ast.Name) and t.id == "custom_operator_name" for t in node.targets):
                return True
    return False


def main(*args: str) -> int:
    classes = itertools.chain.from_iterable(iter_decorated_operators(pathlib.Path(a)) for a in args[1:])
    results = ((k.name, check_missing_custom_operator_name(k)) for k in classes)
    failures = [name for name, success in results if not success]
    for failure in failures:
        print(f"Missing custom_operator_name in class: {failure}")
    return len(failures)


if __name__ == "__main__":
    sys.exit(main(*sys.argv))

相关信息

airflow 源码目录

相关文章

airflow common_precommit_utils 源码

airflow pre_commit_base_operator_partial_arguments 源码

airflow pre_commit_boring_cyborg 源码

airflow pre_commit_breeze_cmd_line 源码

airflow pre_commit_build_providers_dependencies 源码

airflow pre_commit_changelog_duplicates 源码

airflow pre_commit_chart_schema 源码

airflow pre_commit_check_2_2_compatibility 源码

airflow pre_commit_check_lazy_logging 源码

airflow pre_commit_check_license 源码

0  赞