check.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. from __future__ import annotations
  18. import functools
  19. import importlib
  20. from importlib import metadata
  21. from packaging.version import Version
  22. from airflow.exceptions import AirflowOptionalProviderFeatureException
  23. def require_provider_version(provider_name: str, provider_min_version: str):
  24. """
  25. Enforce minimum version requirement for a specific provider.
  26. Some providers, do not explicitly require other provider packages but may offer optional features
  27. that depend on it. These features are generally available starting from a specific version of such
  28. provider. This decorator helps ensure compatibility, preventing import errors and providing clear
  29. logs about version requirements.
  30. Args:
  31. provider_name: Name of the provider e.g., apache-airflow-providers-openlineage
  32. provider_min_version: Optional minimum version requirement e.g., 1.0.1
  33. Raises:
  34. ValueError: If neither `provider_name` nor `provider_min_version` is provided.
  35. ValueError: If full provider name (e.g., apache-airflow-providers-openlineage) is not provided.
  36. TypeError: If the decorator is used without parentheses (e.g., `@require_provider_version`).
  37. """
  38. err_msg = (
  39. "`require_provider_version` decorator must be used with two arguments: "
  40. "'provider_name' and 'provider_min_version', "
  41. 'e.g., @require_provider_version(provider_name="apache-airflow-providers-openlineage", '
  42. 'provider_min_version="1.0.0")'
  43. )
  44. # Detect if decorator is mistakenly used without arguments
  45. if callable(provider_name) and not provider_min_version:
  46. raise TypeError(err_msg)
  47. # Ensure both arguments are provided and not empty
  48. if not provider_name or not provider_min_version:
  49. raise ValueError(err_msg)
  50. # Ensure full provider name is passed
  51. if not provider_name.startswith("apache-airflow-providers-"):
  52. raise ValueError(
  53. f"Full `provider_name` must be provided starting with 'apache-airflow-providers-', "
  54. f"got `{provider_name}`."
  55. )
  56. def decorator(func):
  57. @functools.wraps(func)
  58. def wrapper(*args, **kwargs):
  59. try:
  60. provider_version: str = metadata.version(provider_name)
  61. except metadata.PackageNotFoundError:
  62. try:
  63. # Try dynamically importing the provider module based on the provider name
  64. import_provider_name = provider_name.replace("apache-airflow-providers-", "").replace(
  65. "-", "."
  66. )
  67. provider_module = importlib.import_module(f"airflow.providers.{import_provider_name}")
  68. provider_version = getattr(provider_module, "__version__")
  69. except (ImportError, AttributeError, ModuleNotFoundError):
  70. raise AirflowOptionalProviderFeatureException(
  71. f"Provider `{provider_name}` not found or has no version, "
  72. f"skipping function `{func.__name__}` execution"
  73. )
  74. if provider_version and Version(provider_version) < Version(provider_min_version):
  75. raise AirflowOptionalProviderFeatureException(
  76. f"Provider's `{provider_name}` version `{provider_version}` is lower than required "
  77. f"`{provider_min_version}`, skipping function `{func.__name__}` execution"
  78. )
  79. return func(*args, **kwargs)
  80. return wrapper
  81. return decorator