|
| 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 | + |
| 19 | +import logging |
| 20 | +import os |
| 21 | +import shutil |
| 22 | +from collections.abc import Callable, Iterable |
| 23 | +from pathlib import Path |
| 24 | +from subprocess import run |
| 25 | +from typing import Any |
| 26 | + |
| 27 | +from hatchling.builders.config import BuilderConfig |
| 28 | +from hatchling.builders.plugin.interface import BuilderInterface |
| 29 | +from hatchling.plugin.manager import PluginManager |
| 30 | + |
| 31 | +log = logging.getLogger(__name__) |
| 32 | +log_level = logging.getLevelName(os.getenv("CUSTOM_AIRFLOW_BUILD_LOG_LEVEL", "INFO")) |
| 33 | +log.setLevel(log_level) |
| 34 | + |
| 35 | + |
| 36 | +class CustomBuild(BuilderInterface[BuilderConfig, PluginManager]): |
| 37 | + """Custom build class for Data Quality provider assets.""" |
| 38 | + |
| 39 | + PLUGIN_NAME = "custom" |
| 40 | + |
| 41 | + @staticmethod |
| 42 | + def clean_dir(path: Path) -> None: |
| 43 | + log.warning("Cleaning directory: %s", path) |
| 44 | + shutil.rmtree(path, ignore_errors=True) |
| 45 | + |
| 46 | + def clean(self, directory: str, versions: Iterable[str]) -> None: |
| 47 | + work_dir = Path(self.root) |
| 48 | + log.warning("Cleaning generated files in directory: %s", work_dir) |
| 49 | + dataquality_package_src = work_dir / "src" / "airflow" / "providers" / "common" / "dataquality" |
| 50 | + dataquality_ui_path = dataquality_package_src / "plugins" / "www" |
| 51 | + self.clean_dir(dataquality_ui_path / ".pnpm-store") |
| 52 | + self.clean_dir(dataquality_ui_path / "dist") |
| 53 | + self.clean_dir(dataquality_ui_path / "node_modules") |
| 54 | + (work_dir / "www-hash.txt").unlink(missing_ok=True) |
| 55 | + |
| 56 | + def get_version_api(self) -> dict[str, Callable[..., str]]: |
| 57 | + """Get custom build target for standard package preparation.""" |
| 58 | + return {"standard": self.build_standard} |
| 59 | + |
| 60 | + def build_standard(self, directory: str, artifacts: Any, **build_data: Any) -> str: |
| 61 | + work_dir = Path(self.root).parents[2].resolve() |
| 62 | + cmd = ["prek", "run", "compile-common-dataquality-provider-assets", "--all-files"] |
| 63 | + log.warning("Running command: %s", " ".join(cmd)) |
| 64 | + run(cmd, cwd=work_dir.as_posix(), check=True) |
| 65 | + dist_path = ( |
| 66 | + Path(self.root) |
| 67 | + / "src" |
| 68 | + / "airflow" |
| 69 | + / "providers" |
| 70 | + / "common" |
| 71 | + / "dataquality" |
| 72 | + / "plugins" |
| 73 | + / "www" |
| 74 | + / "dist" |
| 75 | + ) |
| 76 | + return dist_path.resolve().as_posix() |
0 commit comments