kaxil commented on a change in pull request #7644: [AIRFLOW-7003] Lazy load all 
plguins
URL: https://github.com/apache/airflow/pull/7644#discussion_r389821560
 
 

 ##########
 File path: airflow/plugins_manager.py
 ##########
 @@ -84,89 +107,82 @@ def on_load(cls, *args, **kwargs):
         """
 
 
-def load_entrypoint_plugins(entry_points, airflow_plugins):
-    """
-    Load AirflowPlugin subclasses from the entrypoints
-    provided. The entry_point group should be 'airflow.plugins'.
-
-    :param entry_points: A collection of entrypoints to search for plugins
-    :type entry_points: Generator[setuptools.EntryPoint, None, None]
-    :param airflow_plugins: A collection of existing airflow plugins to
-        ensure we don't load duplicates
-    :type airflow_plugins: list[type[airflow.plugins_manager.AirflowPlugin]]
-    :rtype: list[airflow.plugins_manager.AirflowPlugin]
-    """
-    for entry_point in entry_points:
-        log.debug('Importing entry_point plugin %s', entry_point.name)
-        plugin_obj = entry_point.load()
-        if is_valid_plugin(plugin_obj, airflow_plugins):
-            if callable(getattr(plugin_obj, 'on_load', None)):
-                plugin_obj.on_load()
-                airflow_plugins.append(plugin_obj)
-    return airflow_plugins
-
-
-def is_valid_plugin(plugin_obj, existing_plugins):
+def is_valid_plugin(plugin_obj):
     """
     Check whether a potential object is a subclass of
     the AirflowPlugin class.
 
     :param plugin_obj: potential subclass of AirflowPlugin
-    :param existing_plugins: Existing list of AirflowPlugin subclasses
     :return: Whether or not the obj is a valid subclass of
         AirflowPlugin
     """
+    global plugins  # pylint: disable=global-statement
+
     if (
         inspect.isclass(plugin_obj) and
         issubclass(plugin_obj, AirflowPlugin) and
         (plugin_obj is not AirflowPlugin)
     ):
         plugin_obj.validate()
-        return plugin_obj not in existing_plugins
+        return plugin_obj not in plugins
     return False
 
 
-plugins = []  # type: List[AirflowPlugin]
-
-norm_pattern = re.compile(r'[/|.]')
-
-if not settings.PLUGINS_FOLDER:
-    raise ValueError("Plugins folder is not set")
-
-# Crawl through the plugins folder to find AirflowPlugin derivatives
-for root, dirs, files in os.walk(settings.PLUGINS_FOLDER, followlinks=True):
-    for f in files:
-        filepath = os.path.join(root, f)
-        try:
-            if not os.path.isfile(filepath):
-                continue
-            mod_name, file_ext = os.path.splitext(
-                os.path.split(filepath)[-1])
-            if file_ext != '.py':
-                continue
-
-            log.debug('Importing plugin module %s', filepath)
-            # normalize root path as namespace
-            namespace = '_'.join([re.sub(norm_pattern, '__', root), mod_name])
-
-            loader = importlib.machinery.SourceFileLoader(mod_name, filepath)
-            spec = importlib.util.spec_from_loader(mod_name, loader)
-            m = importlib.util.module_from_spec(spec)
-            sys.modules[spec.name] = m
-            loader.exec_module(m)
-            for obj in list(m.__dict__.values()):
-                if is_valid_plugin(obj, plugins):
-                    plugins.append(obj)
-        except Exception as e:  # pylint: disable=broad-except
-            log.exception(e)
-            path = filepath or str(f)
-            log.error('Failed to import plugin %s', path)
-            import_errors[path] = str(e)
-
-plugins = load_entrypoint_plugins(
-    pkg_resources.iter_entry_points('airflow.plugins'),
-    plugins
-)
+def load_entrypoint_plugins():
+    """
+    Load and register plugins AirflowPlugin subclasses from the entrypoints.
+    The entry_point group should be 'airflow.plugins'.
+    """
+    global plugins  # pylint: disable=global-statement
+
+    entry_points = pkg_resources.iter_entry_points('airflow.plugins')
+
+    log.debug("Loading plugins from entrypoints")
+
+    for entry_point in entry_points:
+        log.debug('Importing entry_point plugin %s', entry_point.name)
+        plugin_obj = entry_point.load()
+        if is_valid_plugin(plugin_obj):
+            if callable(getattr(plugin_obj, 'on_load', None)):
+                plugin_obj.on_load()
+                plugins.append(plugin_obj)
+
+
+def load_plugins_from_plugin_directory():
+    """
+    Load and register Airflow Plugin from plugin directory
 
 Review comment:
   ```suggestion
       Load and register Airflow Plugins from plugins directory
   ```

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to