You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2020/11/16 20:13:06 UTC

[GitHub] [airflow] ashb commented on a change in pull request #12383: Adds mechanism for provider package discovery.

ashb commented on a change in pull request #12383:
URL: https://github.com/apache/airflow/pull/12383#discussion_r524535879



##########
File path: airflow/providers_manager.py
##########
@@ -0,0 +1,78 @@
+#
+# 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.
+"""Manages all providers."""
+import importlib
+import logging
+import pkgutil
+import traceback
+
+import yaml
+
+try:
+    import importlib.resources as pkg_resources
+except ImportError:
+    # Try backported to PY<37 `importlib_resources`.
+    import importlib_resources as pkg_resources
+
+
+log = logging.getLogger(__name__)
+
+
+class ProvidersManager:

Review comment:
       How about moving this in to `airflow/providers/manager.py` -- with the namespace packages this won't cause any problems to installing other providers.

##########
File path: airflow/providers_manager.py
##########
@@ -0,0 +1,78 @@
+#
+# 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.
+"""Manages all providers."""
+import importlib
+import logging
+import pkgutil
+import traceback
+
+import yaml
+
+try:
+    import importlib.resources as pkg_resources
+except ImportError:
+    # Try backported to PY<37 `importlib_resources`.
+    import importlib_resources as pkg_resources
+
+
+log = logging.getLogger(__name__)
+
+
+class ProvidersManager:
+    """Manages all provider packages."""
+
+    def __find_all_providers(self, paths: str):
+        def onerror(_):
+            exception_string = traceback.format_exc()
+            log.warning(exception_string)
+
+        for module_info in pkgutil.walk_packages(paths, prefix="airflow.providers.", onerror=onerror):

Review comment:
       So I think this is my only real issue with this approach: this only works for packages installed in/under airflow.providers, which won't always be the case with third party providers (i.e. https://github.com/great-expectations/airflow-provider-great-expectations)
   
   That said, this approach is "Good Enough" for 2.0, so happy to discuss this later.

##########
File path: airflow/providers_manager.py
##########
@@ -0,0 +1,78 @@
+#
+# 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.
+"""Manages all providers."""
+import importlib
+import logging
+import pkgutil
+import traceback
+
+import yaml
+
+try:
+    import importlib.resources as pkg_resources
+except ImportError:
+    # Try backported to PY<37 `importlib_resources`.
+    import importlib_resources as pkg_resources
+
+
+log = logging.getLogger(__name__)
+
+
+class ProvidersManager:
+    """Manages all provider packages."""
+
+    def __find_all_providers(self, paths: str):
+        def onerror(_):
+            exception_string = traceback.format_exc()
+            log.warning(exception_string)
+
+        for module_info in pkgutil.walk_packages(paths, prefix="airflow.providers.", onerror=onerror):
+            try:
+                imported_module = importlib.import_module(module_info.name)
+            except Exception as e:  # noqa pylint: disable=broad-except
+                log.warning("Error when importing %s:%s", module_info.name, e)
+                continue
+            try:
+                provider_info = yaml.safe_load(pkg_resources.read_text(imported_module, 'provider.yaml'))
+                # TODO(potiuk): map to a class maybe? Or we might stick to a dictionary
+                self._provider_directory[provider_info['package-name']] = provider_info
+            except FileNotFoundError:
+                # This is OK - this is not a provider package
+                pass
+            except TypeError as e:
+                if "is not a package" not in str(e):
+                    log.warning("Error when loading 'provider.yaml' file from %s:%s}", module_info.name, e)
+                # Otherwise this is OK - this is likely a module
+            except Exception as e:  # noqa pylint: disable=broad-except
+                log.warning("Error when loading 'provider.yaml' file from %s:%s", module_info.name, e)
+
+    def __init__(self):
+        self._provider_directory = {}
+        try:
+            from airflow import providers
+        except ImportError as e:
+            log.warning("No providers are present or error when importing them! :%s", e)
+            return
+        self.__find_all_providers(providers.__path__)
+
+    # TODO(potiuk) - add refresh mechanism to re-import providers and update the dictionary

Review comment:
       I don't think we really need this -- with multiple webserver worker processes any refresh would lead to confusing state (some on new list, some on old, and no way to tell which is which) unless we have some way to make _all_ workers perform it.

##########
File path: airflow/providers_manager.py
##########
@@ -0,0 +1,78 @@
+#
+# 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.
+"""Manages all providers."""
+import importlib
+import logging
+import pkgutil
+import traceback
+
+import yaml
+
+try:
+    import importlib.resources as pkg_resources
+except ImportError:
+    # Try backported to PY<37 `importlib_resources`.
+    import importlib_resources as pkg_resources

Review comment:
       ```suggestion
       import importlib.resources as importlib_resources
   except ImportError:
       # Try backported to PY<37 `importlib_resources`.
       import importlib_resources as importlib_resources
   ```
   
   Let's _not_ call it pkg_resources as that is the old quasi-deprecated name.




----------------------------------------------------------------
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.

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