From 22357fbc2be22d05b7893459cc9e89051c484b5a Mon Sep 17 00:00:00 2001 From: mirpedrol Date: Wed, 5 Oct 2022 12:29:59 +0200 Subject: [PATCH 1/4] add a function to check if patch files contain outdated paths --- nf_core/modules/modules_command.py | 27 ++++++++++++++++++++++++++- nf_core/modules/patch.py | 2 +- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/nf_core/modules/modules_command.py b/nf_core/modules/modules_command.py index 116cd1287e..ce0983eb0e 100644 --- a/nf_core/modules/modules_command.py +++ b/nf_core/modules/modules_command.py @@ -177,11 +177,36 @@ def check_modules_structure(self): # Move wrong modules to the right directory for module in wrong_location_modules: modules_dir = Path("modules").resolve() - correct_dir = Path(modules_dir, self.modules_repo.repo_path, Path(*module.parts[2:])) + module_name = str(Path(*module.parts[2:])) + correct_dir = Path(modules_dir, self.modules_repo.repo_path, Path(module_name)) wrong_dir = Path(modules_dir, module) shutil.move(wrong_dir, correct_dir) log.info(f"Moved {wrong_dir} to {correct_dir}.") + # Check if a path file exists + patch_path = correct_dir / Path(module_name + ".diff") + self.check_patch_paths(patch_path, module_name) shutil.rmtree(Path(self.dir, "modules", self.modules_repo.repo_path, "modules")) # Regenerate modules.json file modules_json = ModulesJson(self.dir) modules_json.check_up_to_date() + + def check_patch_paths(self, patch_path, module_name): + """ + Check that paths in patch files are updated to the new modules path + """ + if patch_path.exists(): + rewrite = False + with open(patch_path, "r") as fh: + lines = fh.readlines() + for line in lines: + # Check if there are old paths in the patch file and replace + if f"modules/{self.modules_repo.repo_path}/modules/{module_name}/" in line: + rewrite = True + line.replace( + f"modules/{self.modules_repo.repo_path}/modules/{module_name}/", + f"modules/{self.modules_repo.repo_path}/{module_name}/", + ) + if rewrite: + with open(patch_path, "w") as fh: + for line in lines: + fh.write(line) diff --git a/nf_core/modules/patch.py b/nf_core/modules/patch.py index ec5e05a277..565a193729 100644 --- a/nf_core/modules/patch.py +++ b/nf_core/modules/patch.py @@ -50,7 +50,7 @@ def patch(self, module=None): module_dir = [dir for dir, m in modules if m == module][0] module_fullname = str(Path("modules", module_dir, module)) - # Verify that the module has an entry is the modules.json file + # Verify that the module has an entry in the modules.json file if not self.modules_json.module_present(module, self.modules_repo.remote_url, module_dir): raise UserWarning( f"The '{module_fullname}' module does not have an entry in the 'modules.json' file. Cannot compute patch" From 90712625993dc27912d15570e84f77a09f565588 Mon Sep 17 00:00:00 2001 From: mirpedrol Date: Wed, 5 Oct 2022 16:50:58 +0200 Subject: [PATCH 2/4] fix some bugs --- nf_core/modules/modules_command.py | 14 ++++++++++++-- nf_core/modules/update.py | 4 ++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/nf_core/modules/modules_command.py b/nf_core/modules/modules_command.py index ce0983eb0e..d942a98cd2 100644 --- a/nf_core/modules/modules_command.py +++ b/nf_core/modules/modules_command.py @@ -195,18 +195,28 @@ def check_patch_paths(self, patch_path, module_name): Check that paths in patch files are updated to the new modules path """ if patch_path.exists(): + log.info(f"Modules {module_name} contains a patch file.") rewrite = False with open(patch_path, "r") as fh: lines = fh.readlines() - for line in lines: + for index, line in enumerate(lines): # Check if there are old paths in the patch file and replace if f"modules/{self.modules_repo.repo_path}/modules/{module_name}/" in line: rewrite = True - line.replace( + lines[index] = line.replace( f"modules/{self.modules_repo.repo_path}/modules/{module_name}/", f"modules/{self.modules_repo.repo_path}/{module_name}/", ) if rewrite: + log.info(f"Updating paths in {patch_path}") with open(patch_path, "w") as fh: for line in lines: fh.write(line) + # Update path in modules.json if the file is in the correct format + modules_json = ModulesJson(self.dir) + modules_json.load() + if modules_json.has_git_url_and_modules(): + modules_json.modules_json["repos"][self.modules_repo.remote_url]["modules"][ + self.modules_repo.repo_path + ][module_name]["patch"] = str(patch_path.relative_to(Path(self.dir).resolve())) + modules_json.dump() diff --git a/nf_core/modules/update.py b/nf_core/modules/update.py index cd59a4109b..4372ec8d06 100644 --- a/nf_core/modules/update.py +++ b/nf_core/modules/update.py @@ -613,6 +613,10 @@ def try_apply_patch(self, module, repo_path, patch_relpath, module_dir, module_i patch_path = Path(self.dir / patch_relpath) module_relpath = Path("modules", repo_path, module) + # Check that paths in patch file are updated + print(patch_path, module) + self.check_patch_paths(patch_path, module) + # Copy the installed files to a new temporary directory to save them for later use temp_dir = Path(tempfile.mkdtemp()) temp_module_dir = temp_dir / module From 30efa06dc36cf8bbdb0537904aa6852947958a7f Mon Sep 17 00:00:00 2001 From: mirpedrol Date: Wed, 5 Oct 2022 16:52:14 +0200 Subject: [PATCH 3/4] update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6fb6955ecb..27b8a60096 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ ### Modules +- Update patch file paths if the modules directory has the old structure ([#1878](https://github.com/nf-core/tools/pull/1878)) + ## [v2.6 - Tin Octopus](https://github.com/nf-core/tools/releases/tag/2.6) - [2022-10-04] ### Template From a89faba67d63615314e2af804b00784f4ff4e9f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlia=20Mir=20Pedrol?= Date: Wed, 5 Oct 2022 17:12:22 +0200 Subject: [PATCH 4/4] Update nf_core/modules/update.py Co-authored-by: Phil Ewels --- nf_core/modules/update.py | 1 - 1 file changed, 1 deletion(-) diff --git a/nf_core/modules/update.py b/nf_core/modules/update.py index 4372ec8d06..f40b3743f9 100644 --- a/nf_core/modules/update.py +++ b/nf_core/modules/update.py @@ -614,7 +614,6 @@ def try_apply_patch(self, module, repo_path, patch_relpath, module_dir, module_i module_relpath = Path("modules", repo_path, module) # Check that paths in patch file are updated - print(patch_path, module) self.check_patch_paths(patch_path, module) # Copy the installed files to a new temporary directory to save them for later use