Skip to content

Commit

Permalink
Merge pull request #2062 from mirpedrol/remove-avid-duplicates
Browse files Browse the repository at this point in the history
Avoid trying to remove components by duplicate
  • Loading branch information
mirpedrol authored Nov 28, 2022
2 parents 0bdbf02 + 179a36d commit 57c3c1e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
28 changes: 20 additions & 8 deletions nf_core/components/remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ class ComponentRemove(ComponentCommand):
def __init__(self, component_type, pipeline_dir, remote_url=None, branch=None, no_pull=False):
super().__init__(component_type, pipeline_dir, remote_url, branch, no_pull)

def remove(self, component, removed_by=None, force=False):
def remove(self, component, removed_by=None, removed_components=None, force=False):
"""
Remove an already installed module/subworkflow
This command only works for modules/subworkflows that are installed from 'nf-core/modules'
Args:
component (str): Name of the component to remove
removed_by (str): Name of the component that is removing the current component
(a subworkflow name if the component is a dependency or "modules" or "subworkflows" if it is not a dependency)
removed_components (list[str]): list of components that have been removed during a recursive remove of subworkflows
force (bool): Force removal of component, even if there is still an include statement in a workflow file
Returns:
Expand All @@ -51,6 +54,9 @@ def remove(self, component, removed_by=None, force=False):
style=nf_core.utils.nfcore_question_style,
).unsafe_ask()

if removed_components is None:
removed_components = []

# Get the module/subworkflow directory
component_dir = Path(self.dir, self.component_type, repo_path, component)

Expand Down Expand Up @@ -85,7 +91,7 @@ def remove(self, component, removed_by=None, force=False):
include_stmts = self.check_if_in_include_stmts(str(removed_component_dir))
if include_stmts:
# print the include statements
log.warn(
log.warning(
f"The {self.component_type[:-1]} '{component}' is still included in the following workflow file{nf_core.utils.plural_s(include_stmts)}:"
)
console = Console()
Expand Down Expand Up @@ -120,14 +126,16 @@ def remove(self, component, removed_by=None, force=False):
if not ComponentInstall(self.dir, self.component_type, force=True).install(
component, silent=True
):
log.warn(
log.warning(
f"Could not install the {self.component_type[:-1]} '{component}', please install it manually with 'nf-core {component_type} install {component}'."
)
removed_components.append(component)
return removed
# Remove the component files of all entries removed from modules.json
removed = (
True if self.clear_component_dir(component, Path(self.dir, removed_component_dir)) or removed else False
)
removed_components.append(component)

if removed:
if self.component_type == "subworkflows":
Expand All @@ -136,10 +144,14 @@ def remove(self, component, removed_by=None, force=False):
self.component_type, component, self.modules_repo.remote_url, repo_path, {}
)
for component_name, component_type in dependent_components.items():
original_component_tyoe = self.component_type
if component_name in removed_components:
continue
original_component_type = self.component_type
self.component_type = component_type
dependency_removed = self.remove(component_name, removed_by=removed_by)
self.component_type = original_component_tyoe
dependency_removed = self.remove(
component_name, removed_by=removed_by, removed_components=removed_components
)
self.component_type = original_component_type
# remember removed dependencies
if dependency_removed:
removed_components.append(component_name.replace("/", "_"))
Expand All @@ -154,9 +166,9 @@ def remove(self, component, removed_by=None, force=False):
][component]["installed_by"]
if installed_by == self.component_type:
log.error(
f"Did not remove '{component}', because it was also manually installed. Only updated 'installed_by' in modules.json."
f"Did not remove '{component}', because it was also manually installed. Only updated 'installed_by' entry in modules.json."
)
log.info(
f"""Did not remove {self.component_type[:-1]} '{component}', because it was also installed by {', '.join(f"'{d}'" for d in installed_by)}."""
f"""Did not remove {self.component_type[:-1]} '{component}', because it was also installed by {', '.join(f"'{d}'" for d in installed_by)}. Only updated the 'installed_by' entry in modules.json."""
)
return removed
9 changes: 0 additions & 9 deletions nf_core/modules/modules_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,15 +705,6 @@ def remove_entry(self, component_type, name, repo_url, install_dir, removed_by=N
# write the updated modules.json file
self.dump()
return True
# write the updated modules.json file
if removed_by == component_type:
log.info(
f"""Updated the 'installed_by' list for '{name}', but it is still installed, because it is required by {", ".join(f"'{d}'" for d in repo_entry[component_type][install_dir][name]['installed_by'])}."""
)
else:
log.info(
f"Removed {removed_by} from the 'installed_by' list of {name}, but it was also installed by other modules/subworkflows."
)
self.dump()
return False
else:
Expand Down

0 comments on commit 57c3c1e

Please sign in to comment.