You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "hussein-awala (via GitHub)" <gi...@apache.org> on 2023/08/08 23:17:54 UTC

[GitHub] [airflow] hussein-awala commented on a diff in pull request #33240: Add check if google-re2 is declared as dependency

hussein-awala commented on code in PR #33240:
URL: https://github.com/apache/airflow/pull/33240#discussion_r1287776048


##########
scripts/ci/pre_commit/pre_commit_check_google_re2_imports.py:
##########
@@ -0,0 +1,92 @@
+#!/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 ast
+import sys
+from pathlib import Path
+from typing import NamedTuple
+
+from rich.console import Console
+
+console = Console(color_system="standard", width=200)
+
+
+class ImportTuple(NamedTuple):
+    module: list[str]
+    name: list[str]
+    alias: str
+
+
+def get_imports(path: str):
+    with open(path) as fh:
+        root = ast.parse(fh.read(), path)
+
+    for node in ast.iter_child_nodes(root):
+        if isinstance(node, ast.Import):
+            module: list[str] = node.names[0].name.split(".") if node.names else []
+        elif isinstance(node, ast.ImportFrom) and node.module:
+            module = node.module.split(".")
+        else:
+            continue
+
+        for n in node.names:  # type: ignore[attr-defined]
+            yield ImportTuple(module=module, name=n.name.split("."), alias=n.asname)
+
+
+def find_provider_yaml(path: str) -> Path | None:
+    cur_path = Path(path)
+    while cur_path.parent != cur_path:
+        if (cur_path / "provider.yaml").exists():
+            return cur_path / "provider.yaml"
+        cur_path = cur_path.parent
+    return None
+
+
+errors: list[str] = []
+
+
+def main() -> int:
+    for path in sys.argv[1:]:
+        for imp in get_imports(path):
+            if imp.module[0] == "re2":
+                if path.startswith("airflow/providers/"):
+                    provider_yaml = find_provider_yaml(path)
+                    if not provider_yaml:
+                        console.print(f"[yellow]{path}:[/] Not found provider.yaml")
+                    else:
+                        if "google-re2" not in provider_yaml.read_text():
+                            console.print(f"[yellow]{path}:[/] Not found google-re2 in {provider_yaml}")
+                            errors.append(f"{path}:{provider_yaml}")
+    if errors:
+        console.print(
+            "[red]Some {roviders use 're2' module but do not declare 'google-re2' as dependency.[/]\n"

Review Comment:
   ```suggestion
               "[red]Some providers use 're2' module but do not declare 'google-re2' as dependency.[/]\n"
   ```



##########
scripts/ci/pre_commit/pre_commit_check_google_re2_imports.py:
##########
@@ -0,0 +1,92 @@
+#!/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 ast
+import sys
+from pathlib import Path
+from typing import NamedTuple
+
+from rich.console import Console
+
+console = Console(color_system="standard", width=200)
+
+
+class ImportTuple(NamedTuple):
+    module: list[str]
+    name: list[str]
+    alias: str
+
+
+def get_imports(path: str):
+    with open(path) as fh:
+        root = ast.parse(fh.read(), path)
+
+    for node in ast.iter_child_nodes(root):
+        if isinstance(node, ast.Import):
+            module: list[str] = node.names[0].name.split(".") if node.names else []
+        elif isinstance(node, ast.ImportFrom) and node.module:
+            module = node.module.split(".")
+        else:
+            continue
+
+        for n in node.names:  # type: ignore[attr-defined]
+            yield ImportTuple(module=module, name=n.name.split("."), alias=n.asname)
+
+
+def find_provider_yaml(path: str) -> Path | None:
+    cur_path = Path(path)
+    while cur_path.parent != cur_path:
+        if (cur_path / "provider.yaml").exists():
+            return cur_path / "provider.yaml"
+        cur_path = cur_path.parent
+    return None
+
+
+errors: list[str] = []
+
+
+def main() -> int:
+    for path in sys.argv[1:]:
+        for imp in get_imports(path):
+            if imp.module[0] == "re2":
+                if path.startswith("airflow/providers/"):

Review Comment:
   if you move this condition and put it after `for path in sys.argv[1:]:`, we could avoid call `get_imports` on no providers files (it will be faster)



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org