From 27058667f792f21a985a1702bedbface74e0a198 Mon Sep 17 00:00:00 2001 From: Ben Souchet Date: Wed, 11 Sep 2024 13:47:39 +0200 Subject: [PATCH] Deadline: Improve get_publish_plugin_paths to properly get paths for the specified host --- openpype/modules/deadline/deadline_module.py | 69 ++++++++++++++++++-- 1 file changed, 65 insertions(+), 4 deletions(-) diff --git a/openpype/modules/deadline/deadline_module.py b/openpype/modules/deadline/deadline_module.py index 9855f8c1b10..20c136ce0ac 100644 --- a/openpype/modules/deadline/deadline_module.py +++ b/openpype/modules/deadline/deadline_module.py @@ -1,7 +1,10 @@ +import ast +import re import os import requests import six import sys +from pathlib import Path from openpype.lib import requests_get, Logger from openpype.modules import OpenPypeModule, IPluginPaths @@ -15,9 +18,17 @@ class DeadlineWebserviceError(Exception): class DeadlineModule(OpenPypeModule, IPluginPaths): name = "deadline" + _valid_plugin_types = ["publish"] def __init__(self, manager, settings): self.deadline_urls = {} + + self.plugin_paths = {} + for valid_plugin_type in self._valid_plugin_types: + self.plugin_paths[valid_plugin_type] = { + 'all': [] + } + super(DeadlineModule, self).__init__(manager, settings) def initialize(self, modules_settings): @@ -36,12 +47,62 @@ def initialize(self, modules_settings): "not specified. Disabling module.")) return + # Retrieve plugin paths and properly sort them + hosts_regex = re.compile(r'hosts = (?P\[[^\]]+\])') + plugin_dir = Path(__file__).joinpath("plugins").resolve() + + for valid_plugin_type in self._valid_plugin_types: + search_dir = plugin_dir.joinpath(valid_plugin_type) + + if not search_dir.exists(): + continue + + dir_plugin_paths = list(search_dir.glob('*.py')) + + for plugin_path_str in dir_plugin_paths: + plugin_path = Path(plugin_path_str).resolve() + plugin_path_resolved = str(plugin_path) + + # Opening the file, reading the content and extracting + # the list of hosts for the plugin path + with open(plugin_path) as f: + content = f.read() + match = hosts_regex.search(content) + + if not match or not match.lastgroup: + # The regex didn't match, maybe an __init__.py file + continue + + hosts_str = re.sub(r'\s+', '', match.group(match.lastgroup)) + hosts_list = ast.literal_eval(hosts_str) + + # Adding the plugin path to the "all" array + self.plugin_paths[valid_plugin_type]['all'].append(plugin_path_resolved) + + # Adding the plugin path to the correct host array(s) + for host in hosts_list: + if host in self.plugin_paths[valid_plugin_type]: + self.plugin_paths[valid_plugin_type][host].append(plugin_path_resolved) + else: + self.plugin_paths[valid_plugin_type][host] = [plugin_path_resolved] + def get_plugin_paths(self): """Deadline plugin paths.""" - current_dir = os.path.dirname(os.path.abspath(__file__)) - return { - "publish": [os.path.join(current_dir, "plugins", "publish")] - } + all_plugin_paths = {} + + for valid_plugin_type in self._valid_plugin_types: + all_plugin_paths[valid_plugin_type] = self.plugin_paths[valid_plugin_type]['all'] + + return all_plugin_paths + + def get_publish_plugin_paths(self, host_name): + """Only get the plugin paths related to the host specified""" + if not host_name or host_name not in self.plugin_paths['publish']: + # If we cannot determine the host, or it's not specified, + # then we will return all the registered plugin paths + host_name = "all" + + return self.plugin_paths['publish'][host_name] @staticmethod def get_deadline_pools(webservice, log=None):