Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AIP-79 Generate assets for Flask application in FAB provider (#44744) #45060

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions airflow/dag_processing/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ def _serialize_dag_capturing_errors(dag: MaybeSerializedDAG, session: Session, p
else:
# Check and update DagCode
DagCode.update_source_code(dag.dag_id, dag.fileloc)

return []
except OperationalError:
raise
Expand Down
34 changes: 27 additions & 7 deletions scripts/ci/pre_commit/compile_www_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
from __future__ import annotations

import hashlib
import importlib.util
import os
import re
import shutil
import subprocess
import sys
from pathlib import Path

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

if __name__ == "__main__":
www_directory = AIRFLOW_SOURCES_PATH / "airflow" / "www"

def compile_assets(www_directory: Path, www_hash_file_name: str):
node_modules_directory = www_directory / "node_modules"
dist_directory = www_directory / "static" / "dist"
WWW_HASH_FILE.parent.mkdir(exist_ok=True, parents=True)
www_hash_file = AIRFLOW_SOURCES_PATH / ".build" / "www" / www_hash_file_name
www_hash_file.parent.mkdir(exist_ok=True, parents=True)
if node_modules_directory.exists() and dist_directory.exists():
old_hash = WWW_HASH_FILE.read_text() if WWW_HASH_FILE.exists() else ""
old_hash = www_hash_file.read_text() if www_hash_file.exists() else ""
new_hash = get_directory_hash(www_directory, skip_path_regexp=r".*node_modules.*")
if new_hash == old_hash:
print("The WWW directory has not changed! Skip regeneration.")
sys.exit(0)
return
else:
shutil.rmtree(node_modules_directory, ignore_errors=True)
shutil.rmtree(dist_directory, ignore_errors=True)
Expand All @@ -71,4 +72,23 @@ def get_directory_hash(directory: Path, skip_path_regexp: str | None = None) ->
subprocess.check_call(["yarn", "install", "--frozen-lockfile"], cwd=os.fspath(www_directory))
subprocess.check_call(["yarn", "run", "build"], cwd=os.fspath(www_directory), env=env)
new_hash = get_directory_hash(www_directory, skip_path_regexp=r".*node_modules.*")
WWW_HASH_FILE.write_text(new_hash)
www_hash_file.write_text(new_hash)


def is_fab_provider_installed() -> bool:
return (
importlib.util.find_spec("airflow") is not None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure whether this will work. because airflow and providers are not installed in pre-commit.

But as you can be sure the source tree is checked-out, how about checking if source tree is available?

Copy link
Contributor Author

@vincbeck vincbeck Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But when you install, the sources are not available, are not they? So is_fab_provider_installed will return False in this case? Although we wan do want to generate assets for FAB as well. As for the pre-commit, since Airflow is not installed, is_fab_provider_installed will return False.

I am not 100% sure I understand everything correctly here so I might be wrong :)

EDIT: what I meant is, it is okay to not generate the assets for FAB in the CI, however, when Airflow is installed, we definitely should generate them

Copy link
Contributor

@jscheffl jscheffl Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought from the point of lifecycle. The pre-commit hook will be called in CI and locally... at build time. Source tree will be there. And it needs to be called at build-time to create the static JS content as dist folder to be packaged into the wheel.

The check you are referring to is at runtime At pip install apache-airflow-providers-fab time the wheel is only un-packed to site-packages. There will no pre-commit be executed as well as you need to assume that at point of install no yarn will be available (might be the user installing is even offline w/o internet).
Otherwise - not being an expert - if the wheel install need to compile/bind/do something at point of install you need to define this in package meta data - not in a pre-commit script that is not available at install time.

You can see this also for airflow/www - the pre-commit is making the package before the sdist/wheel is built and then the dist is packed into the wheel.

So if the check now makes it more safe... I assume the dist will just not be created during build then and only the TS/TSX source tree will be packaged into the wheel.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright. Following this approach I am at the exact same point as in #45058. I dont quite get the error. Will re-try later

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bbovenzi or @pierrejeambrun can you help here? yarn is not really successfully compiling www assets. I assume the yarn lock file is somewhat "different" but I am not an expert why the compile fails. (Still I suspect that loading airflow a Py module is not correct because at CI/compile time the packahe is not installed (chicken+egg))

and importlib.util.find_spec("airflow.providers.fab") is not None
)


if __name__ == "__main__":
# Compile assets for main
main_www_directory = AIRFLOW_SOURCES_PATH / "airflow" / "www"
compile_assets(main_www_directory, "hash.txt")
# Compile assets for fab provider
fab_provider_www_directory = (
AIRFLOW_SOURCES_PATH / "providers" / "src" / "airflow" / "providers" / "fab" / "www"
)
if fab_provider_www_directory.exists():
compile_assets(fab_provider_www_directory, "hash_fab.txt")
Loading