From a5e3e22c51f00800f2b729d510ad4f0785558b6b Mon Sep 17 00:00:00 2001 From: mirpedrol Date: Mon, 10 Oct 2022 15:21:47 +0200 Subject: [PATCH 1/3] update subworkflows install so it installs also imported modules and subworkflows --- nf_core/modules/install.py | 26 ++++++++++----- nf_core/subworkflows/install.py | 58 ++++++++++++++++++++++++++++----- 2 files changed, 66 insertions(+), 18 deletions(-) diff --git a/nf_core/modules/install.py b/nf_core/modules/install.py index b63dab0deb..ec363f0c80 100644 --- a/nf_core/modules/install.py +++ b/nf_core/modules/install.py @@ -86,17 +86,25 @@ def install(self, module): # Check that the module is not already installed if (current_version is not None and os.path.exists(module_dir)) and not self.force: + log.info("Module is already installed.") + print(f"Module {module} is already installed.") - log.error("Module is already installed.") - repo_flag = ( - "" if self.modules_repo.repo_path == NF_CORE_MODULES_NAME else f"-g {self.modules_repo.remote_url} " - ) - branch_flag = "" if self.modules_repo.branch == "master" else f"-b {self.modules_repo.branch} " + self.force = questionary.confirm( + f"Module {module} is already installed. Do you want to force the reinstallation?", + style=nf_core.utils.nfcore_question_style, + default=False, + ).unsafe_ask() - log.info( - f"To update '{module}' run 'nf-core modules {repo_flag}{branch_flag}update {module}'. To force reinstallation use '--force'" - ) - return False + if not self.force: + repo_flag = ( + "" if self.modules_repo.repo_path == NF_CORE_MODULES_NAME else f"-g {self.modules_repo.remote_url} " + ) + branch_flag = "" if self.modules_repo.branch == "master" else f"-b {self.modules_repo.branch} " + + log.info( + f"To update '{module}' run 'nf-core modules {repo_flag}{branch_flag}update {module}'. To force reinstallation use '--force'" + ) + return False if self.sha: version = self.sha diff --git a/nf_core/subworkflows/install.py b/nf_core/subworkflows/install.py index 1942a4fa7d..337453389b 100644 --- a/nf_core/subworkflows/install.py +++ b/nf_core/subworkflows/install.py @@ -1,11 +1,14 @@ import logging import os +import re import shutil +from pathlib import Path import questionary import nf_core.modules.module_utils import nf_core.utils +from nf_core.modules.install import ModuleInstall from nf_core.modules.modules_json import ModulesJson # from .modules_command import ModuleCommand @@ -96,17 +99,25 @@ def install(self, subworkflow): # Check that the subworkflow is not already installed if (current_version is not None and os.path.exists(subworkflow_dir)) and not self.force: + log.info("Subworkflow is already installed.") + print(f"Subworkflow {subworkflow} is already installed.") - log.error("Subworkflow is already installed.") - repo_flag = ( - "" if self.modules_repo.repo_path == NF_CORE_MODULES_NAME else f"-g {self.modules_repo.remote_url} " - ) - branch_flag = "" if self.modules_repo.branch == "master" else f"-b {self.modules_repo.branch} " + self.force = questionary.confirm( + f"Subworkflow {subworkflow} is already installed.\nDo you want to force the reinstallation of this subworkflow and all it's imported modules?", + style=nf_core.utils.nfcore_question_style, + default=False, + ).unsafe_ask() - log.info( - f"To update '{subworkflow}' run 'nf-core subworkflow {repo_flag}{branch_flag}update {subworkflow}'. To force reinstallation use '--force'" - ) - return False + if not self.force: + repo_flag = ( + "" if self.modules_repo.repo_path == NF_CORE_MODULES_NAME else f"-g {self.modules_repo.remote_url} " + ) + branch_flag = "" if self.modules_repo.branch == "master" else f"-b {self.modules_repo.branch} " + + log.info( + f"To update '{subworkflow}' run 'nf-core subworkflow {repo_flag}{branch_flag}update {subworkflow}'. To force reinstallation use '--force'" + ) + return False if self.sha: version = self.sha @@ -145,6 +156,14 @@ def install(self, subworkflow): if not self.install_subworkflow_files(subworkflow, version, self.modules_repo, install_folder): return False + # Install included modules and subworkflows + modules_to_install, subworkflows_to_install = self.get_modules_subworkflows_to_install(subworkflow_dir) + for s_install in subworkflows_to_install: + self.install(s_install) + for m_install in modules_to_install: + module_install = ModuleInstall(self.dir, force=self.force, prompt=self.prompt) + module_install.install(m_install) + # Print include statement subworkflow_name = subworkflow.upper() log.info( @@ -190,3 +209,24 @@ def install_subworkflow_files(self, subworkflow_name, subworkflow_version, modul (bool): Whether the operation was successful of not """ return modules_repo.install_subworkflow(subworkflow_name, install_dir, subworkflow_version) + + def get_modules_subworkflows_to_install(self, subworkflow_dir): + """ + Parse the subworkflow test main.nf file to retrieve all imported modules and subworkflows. + """ + modules = [] + subworkflows = [] + with open(Path(subworkflow_dir, "main.nf"), "r") as fh: + for line in fh: + regex = re.compile( + r"include(?: *{ *)([a-zA-Z\_0-9]*)(?: *as *)?(?:[a-zA-Z\_0-9]*)?(?: *})(?: *from *)(?:'|\")(.*)(?:'|\")" + ) + match = regex.match(line) + if match and len(match.groups()) == 2: + name, link = match.groups() + if link.startswith("../../../"): + name_split = name.lower().split("_") + modules.append("/".join(name_split)) + elif link.startswith("../"): + subworkflows.append(name.lower()) + return modules, subworkflows From 540c195f1cb3028b720fcb0794fef09a8d49ebbe Mon Sep 17 00:00:00 2001 From: mirpedrol Date: Mon, 10 Oct 2022 15:28:06 +0200 Subject: [PATCH 2/3] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b31fedd92..addfad36f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - Fix error in tagging GitPod docker images during releases - Don't remove local copy of modules repo, only update it with fetch ([#1879](https://github.com/nf-core/tools/pull/1879)) +- Update subworkflows install so it installs also imported modules and subworkflows ([#1904](https://github.com/nf-core/tools/pull/1904)) ### Modules From 1feb3905ed370e41c189e00bcb7795297b0a05bd Mon Sep 17 00:00:00 2001 From: mirpedrol Date: Mon, 10 Oct 2022 15:30:07 +0200 Subject: [PATCH 3/3] update changelog for previous PRs :D --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index addfad36f7..653c15201c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,8 @@ ### General - Fix error in tagging GitPod docker images during releases -- Don't remove local copy of modules repo, only update it with fetch ([#1879](https://github.com/nf-core/tools/pull/1879)) +- Don't remove local copy of modules repo, only update it with fetch ([#1881](https://github.com/nf-core/tools/pull/1881)) +- Add subworkflow commands create-test-yml, create and install ([#1897](https://github.com/nf-core/tools/pull/1897)) - Update subworkflows install so it installs also imported modules and subworkflows ([#1904](https://github.com/nf-core/tools/pull/1904)) ### Modules