From 1b184a09f9d3c9ac656f45a2bacded0125399795 Mon Sep 17 00:00:00 2001 From: "clement.hector" Date: Fri, 18 Feb 2022 10:31:09 +0100 Subject: [PATCH] add root keys and project keys --- openpype/lib/path_tools.py | 90 ++++++++++++++++++++++++++------------ 1 file changed, 61 insertions(+), 29 deletions(-) diff --git a/openpype/lib/path_tools.py b/openpype/lib/path_tools.py index c0b78c57249..181417c38c2 100644 --- a/openpype/lib/path_tools.py +++ b/openpype/lib/path_tools.py @@ -6,11 +6,12 @@ import six from openpype.settings import get_project_settings -from openpype.settings.lib import get_site_local_overrides from .anatomy import Anatomy from .profiles_filtering import filter_profiles +import avalon.api + log = logging.getLogger(__name__) @@ -130,45 +131,75 @@ def get_last_version_from_path(path_dir, filter): return None -def compute_paths(basic_paths_items, project_root): +def concatenate_splitted_paths(split_paths, anatomy): pattern_array = re.compile(r"\[.*\]") - project_root_key = "__project_root__" output = [] - for path_items in basic_paths_items: + for path_items in split_paths: clean_items = [] + if isinstance(path_items, str): + path_items = [path_items] + for path_item in path_items: - matches = re.findall(pattern_array, path_item) - if len(matches) > 0: - path_item = path_item.replace(matches[0], "") - if path_item == project_root_key: - path_item = project_root + if not re.match(r"{.+}", path_item): + path_item = re.sub(pattern_array, "", path_item) clean_items.append(path_item) + + # backward compatibility + if "__project_root__" in path_items: + for root, root_path in anatomy.roots.items(): + if not os.path.exists(str(root_path)): + log.debug("Root {} path path {} not exist on \ + computer!".format(root, root_path)) + continue + clean_items = [f"{{root[{root}]}}", "{project[name]}"] \ + + clean_items[1:] + output.append(os.path.normpath(os.path.sep.join(clean_items))) + continue + output.append(os.path.normpath(os.path.sep.join(clean_items))) + return output +def get_format_data(anatomy): + dbcon = avalon.api.AvalonMongoDB() + dbcon.Session["AVALON_PROJECT"] = anatomy.project_name + project_doc = dbcon.find_one({"type": "project"}) + project_code = project_doc["data"]["code"] + + return { + "root": anatomy.roots, + "project": { + "name": anatomy.project_name, + "code": project_code + }, + } + + +def fill_paths(path_list, anatomy): + format_data = get_format_data(anatomy) + filled_paths = [] + + for path in path_list: + new_path = path.format(**format_data) + filled_paths.append(new_path) + + return filled_paths + + def create_project_folders(basic_paths, project_name): anatomy = Anatomy(project_name) - roots_paths = [] - if isinstance(anatomy.roots, dict): - for root in anatomy.roots.values(): - roots_paths.append(root.value) - else: - roots_paths.append(anatomy.roots.value) - - for root_path in roots_paths: - project_root = os.path.join(root_path, project_name) - full_paths = compute_paths(basic_paths, project_root) - # Create folders - for path in full_paths: - full_path = path.format(project_root=project_root) - if os.path.exists(full_path): - log.debug( - "Folder already exists: {}".format(full_path) - ) - else: - log.debug("Creating folder: {}".format(full_path)) - os.makedirs(full_path) + + concat_paths = concatenate_splitted_paths(basic_paths, anatomy) + filled_paths = fill_paths(concat_paths, anatomy) + + # Create folders + for path in filled_paths: + if os.path.exists(path): + log.debug("Folder already exists: {}".format(path)) + else: + log.debug("Creating folder: {}".format(path)) + os.makedirs(path) def _list_path_items(folder_structure): @@ -267,6 +298,7 @@ class HostDirmap: on_dirmap_enabled: run host code for enabling dirmap do_dirmap: run host code to do actual remapping """ + def __init__(self, host_name, project_settings, sync_module=None): self.host_name = host_name self.project_settings = project_settings