Skip to content

Commit ba49469

Browse files
authored
AIP-79 Generate assets for Flask application in FAB provider (apache#44744)
1 parent fc7d983 commit ba49469

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

scripts/ci/pre_commit/compile_www_assets.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
from __future__ import annotations
1919

2020
import hashlib
21+
import importlib.util
2122
import os
2223
import re
2324
import shutil
2425
import subprocess
25-
import sys
2626
from pathlib import Path
2727

2828
# NOTE!. This script is executed from node environment created by pre-commit and this environment
@@ -52,17 +52,18 @@ def get_directory_hash(directory: Path, skip_path_regexp: str | None = None) ->
5252
f"To run this script, run the ./{__file__} command"
5353
)
5454

55-
if __name__ == "__main__":
56-
www_directory = AIRFLOW_SOURCES_PATH / "airflow" / "www"
55+
56+
def compile_assets(www_directory: Path, www_hash_file_name: str):
5757
node_modules_directory = www_directory / "node_modules"
5858
dist_directory = www_directory / "static" / "dist"
59-
WWW_HASH_FILE.parent.mkdir(exist_ok=True, parents=True)
59+
www_hash_file = AIRFLOW_SOURCES_PATH / ".build" / "www" / www_hash_file_name
60+
www_hash_file.parent.mkdir(exist_ok=True, parents=True)
6061
if node_modules_directory.exists() and dist_directory.exists():
61-
old_hash = WWW_HASH_FILE.read_text() if WWW_HASH_FILE.exists() else ""
62+
old_hash = www_hash_file.read_text() if www_hash_file.exists() else ""
6263
new_hash = get_directory_hash(www_directory, skip_path_regexp=r".*node_modules.*")
6364
if new_hash == old_hash:
6465
print("The WWW directory has not changed! Skip regeneration.")
65-
sys.exit(0)
66+
return
6667
else:
6768
shutil.rmtree(node_modules_directory, ignore_errors=True)
6869
shutil.rmtree(dist_directory, ignore_errors=True)
@@ -71,4 +72,20 @@ def get_directory_hash(directory: Path, skip_path_regexp: str | None = None) ->
7172
subprocess.check_call(["yarn", "install", "--frozen-lockfile"], cwd=os.fspath(www_directory))
7273
subprocess.check_call(["yarn", "run", "build"], cwd=os.fspath(www_directory), env=env)
7374
new_hash = get_directory_hash(www_directory, skip_path_regexp=r".*node_modules.*")
74-
WWW_HASH_FILE.write_text(new_hash)
75+
www_hash_file.write_text(new_hash)
76+
77+
78+
def is_fab_provider_installed() -> bool:
79+
return importlib.util.find_spec("airflow.providers.fab") is not None
80+
81+
82+
if __name__ == "__main__":
83+
# Compile assets for main
84+
main_www_directory = AIRFLOW_SOURCES_PATH / "airflow" / "www"
85+
compile_assets(main_www_directory, "hash.txt")
86+
if is_fab_provider_installed():
87+
# Compile assets for fab provider
88+
fab_provider_www_directory = (
89+
AIRFLOW_SOURCES_PATH / "providers" / "src" / "airflow" / "providers" / "fab" / "www"
90+
)
91+
compile_assets(fab_provider_www_directory, "hash_fab.txt")

0 commit comments

Comments
 (0)