Skip to content

Commit

Permalink
Merge pull request #1904 from mirpedrol/install
Browse files Browse the repository at this point in the history
update subworkflows install so it installs also imported modules and subworkflows
  • Loading branch information
mirpedrol authored Oct 10, 2022
2 parents a4033a8 + 2b0a639 commit 5bdd161
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 19 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
### 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

Expand Down
26 changes: 17 additions & 9 deletions nf_core/modules/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 49 additions & 9 deletions nf_core/subworkflows/install.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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

0 comments on commit 5bdd161

Please sign in to comment.