potiuk commented on code in PR #44686:
URL: https://github.com/apache/airflow/pull/44686#discussion_r1872631012


##########
scripts/ci/pre_commit/update_providers_init.py:
##########
@@ -0,0 +1,80 @@
+#!/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 subprocess
+import sys
+from pathlib import Path
+
+sys.path.insert(0, str(Path(__file__).parent.resolve()))
+from common_precommit_utils import console, initialize_breeze_precommit
+
+initialize_breeze_precommit(__name__, __file__)
+
+providers: set[str] = set()
+
+file_list = sys.argv[1:]
+console.print(f"[bright_blue]Determining providers to regenerate from: 
{file_list}\n")
+
+# get all folders from arguments
+for examined_file in file_list:
+    if not examined_file.startswith("providers/src"):
+        continue
+    console.print(f"[bright_blue]Looking at {examined_file} for provider.yaml")
+    # find the folder where provider.yaml is
+    for parent in Path(examined_file).parents:
+        console.print(f"[bright_blue]Checking {parent}")
+        if (parent / "provider.yaml").exists():
+            provider_folder = parent
+            break
+    else:
+        console.print(f"[yellow]\nCould not find `provider.yaml` in any parent 
of {examined_file}[/]")
+        continue
+    # find base for the provider sources
+    for parent in provider_folder.parents:
+        if parent.name == "providers":
+            base_folder = parent
+            console.print(f"[bright_blue]Found base folder {base_folder}")
+            break
+    else:
+        console.print(f"[red]\nCould not find base folder for 
{provider_folder}")
+        sys.exit(1)
+    provider_name = 
".".join(provider_folder.relative_to(base_folder).as_posix().split("/"))
+    providers.add(provider_name)
+
+console.print(f"[bright_blue]Regenerating providers __init__ files for 
providers: {providers}[/]")
+
+if not providers:
+    console.print("[red]\nThe found providers list cannot be empty[/]")
+    sys.exit(1)
+
+res = subprocess.run(

Review Comment:
   Good question @gopidesupavan  It does not trigger comparision but it 
triggers regeneration of `__init__.py` files - this is generally standard 
approach of pre-commits - in order to simplify and have auto-fixing capability 
in pre-commit, usually they work in the way that when they are triggered and it 
is supposed to produce some generated files it **always** regenerates them.  
That is standard convention used in many precommits -  including ours. 
   
   Files are filtered in pre-commit by files/excludes - and it will only run if 
any of the affected files changed - and it will pass the files as args to the 
pre-commit scripts (by default), so this is rather efficient - pre-commits are 
simply skipped unless you modify any of the affect files in your PR.
   
   This has nice effects:
   
   * Pre-commit will automatically "fail" when such generated files are 
different than the ones alrady in the repository and add the end of pre-commit 
session it will produce diff of produced files when run with 
`--show-diff-on-failure` which we do in our CI. 
   
   * When you run it locally, the modified files remain in your local folders 
in repo - which means that they are changes you can immediately commit - this 
is basically "auto-fixing" functionality of pre-commit. 
   
   In this case `PROVIDER__INIT__PY_TEMPLATE.py.jinja2` is the "template 
source" and all the `__init__.py` files should be generated from it, so 
regeneration of those files means that any change you made locally to those 
files will be overridden by the content that **should** be there (i.e. 
generated from the template).
   
   So we do not "require" changes from the template - instead we "require" that 
all the `__init__.py` files are generated from the template and not modified 
manually after (which was the root cause why we had 
#https://github.com/apache/airflow/issues/44024).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to