From 6c571f0946a0498ee9b623455034177ee7a4c589 Mon Sep 17 00:00:00 2001 From: Zvonimir Sabljic Date: Sun, 1 Oct 2023 11:36:25 -0700 Subject: [PATCH] get_full_file_path refactoring --- pilot/helpers/Project.py | 67 ++++++++++++++++++++++++++++++++-------- 1 file changed, 54 insertions(+), 13 deletions(-) diff --git a/pilot/helpers/Project.py b/pilot/helpers/Project.py index a14cb7aef..c1285b33f 100644 --- a/pilot/helpers/Project.py +++ b/pilot/helpers/Project.py @@ -237,23 +237,64 @@ def save_file(self, data): .execute()) def get_full_file_path(self, file_path: str, file_name: str) -> Tuple[str, str]: - file_path = file_path.replace('./', '', 1) - file_path = os.path.dirname(file_path) - file_name = os.path.basename(file_name) - paths = [file_name] + # WINDOWS + are_windows_paths = '\\' in file_path or '\\' in file_name or '\\' in self.root_path + if are_windows_paths: + file_name = file_name.replace('\\', '/') + file_path = file_path.replace('\\', '/') + # END WINDOWS + + # Universal modifications + file_path = file_path.replace('~', '') + file_name = file_name.replace('~', '') + + file_path = file_path.replace(self.root_path, '') + file_name = file_name.replace(self.root_path, '') + + if '.' not in file_path and not file_path.endswith('/'): + file_path += '/' + if '.' not in file_name and not file_name.endswith('/'): + file_name += '/' + + if '/' in file_path and not file_path.startswith('/'): + file_path = '/' + file_path + if '/' in file_name and not file_name.startswith('/'): + file_name = '/' + file_name + # END Universal modifications + + head_path, tail_path = os.path.split(file_path) + head_name, tail_name = os.path.split(file_name) + + final_file_path = head_path if head_path != '' else head_name + final_file_name = tail_name if tail_name != '' else tail_path + + if head_path in head_name: + final_file_path = head_name + elif final_file_path != head_name: + if head_name not in head_path and head_path not in head_name: + if '.' in file_path: + final_file_path = head_name + head_path + else: + final_file_path = head_path + head_name - if file_path != '': - paths.insert(0, file_path) + if final_file_path == '': + final_file_path = '/' - if file_path == '/': - absolute_path = file_path + file_name - else: - if not re.match(r'^/|~|\w+:', file_path): - paths.insert(0, self.root_path) - absolute_path = '/'.join(paths) + final_absolute_path = self.root_path + final_file_path + '/' + final_file_name + + if '//' in final_absolute_path: + final_absolute_path = final_absolute_path.replace('//', '/') + if '//' in final_file_path: + final_file_path = final_file_path.replace('//', '/') + + # WINDOWS + if are_windows_paths: + final_file_path = final_file_path.replace('/', '\\') + final_absolute_path = final_absolute_path.replace('/', '\\') + # END WINDOWS - return file_path, absolute_path + return final_file_path, final_absolute_path def save_files_snapshot(self, development_step_id): files = get_files_content(self.root_path, ignore=IGNORE_FOLDERS)