From 2d93e8fe3a45288c258c2194cf043dc961ebcc1f Mon Sep 17 00:00:00 2001 From: Erik Danielsson Date: Mon, 28 Jun 2021 14:02:57 +0200 Subject: [PATCH 1/4] Added support for 'modules.json' in remove commmand --- nf_core/modules/remove.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/nf_core/modules/remove.py b/nf_core/modules/remove.py index 3313343a8b..506eb6cd60 100644 --- a/nf_core/modules/remove.py +++ b/nf_core/modules/remove.py @@ -1,5 +1,6 @@ import os import sys +import json import questionary import logging @@ -43,7 +44,6 @@ def remove(self, module): # Set the install folder based on the repository name install_folder = ["nf-core", "software"] - print(self.modules_repo.name) if not self.modules_repo.name == "nf-core/modules": install_folder = ["external"] @@ -58,5 +58,27 @@ def remove(self, module): log.info("Removing {}".format(module)) + # Remove entry from modules.json + if not self.remove_modules_json_entry(module): + return False + # Remove the module return self.clear_module_dir(module_name=module, module_dir=module_dir) + + def remove_modules_json_entry(self, module): + # Load 'modules.json' + modules_json_path = os.path.join(self.dir, "modules.json") + with open(modules_json_path, "r") as fh: + modules_json = json.load(fh) + if module in modules_json.get("modules", {}): + modules_json["modules"].pop(module) + else: + # If the module is missing from modules.json we can still clear the directory + # in which the untracked module resides + log.error(f"Module '{module}' is missing from 'modules.json' file.") + return questionary.confirm( + f"Do you wish to continue removing module '{module}'?", default=False + ).unsafe_ask() + with open(modules_json_path, "w") as fh: + json.dump(modules_json, fh, indent=4) + return True From 2f68087e900b4c0fc8da6bce9d5fccdcad5ba1b1 Mon Sep 17 00:00:00 2001 From: Erik Danielsson Date: Mon, 28 Jun 2021 14:06:01 +0200 Subject: [PATCH 2/4] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2317c19e12..effb6b63d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ * Use Biocontainers API instead of quayi.io API for `nf-core modules create` [[#875](https://github.com/nf-core/tools/issues/875)] * Update `nf-core modules install` to handle different versions of modules [#1116](https://github.com/nf-core/tools/pull/1116) * Refactored `nf-core modules` command into one file per command [#1124](https://github.com/nf-core/tools/pull/1124) +* Updated `nf-core modules remove` to also remove entry in `modules.json` file ([#1115](https://github.com/nf-core/tools/issues/1115)) #### Sync From d878a26d29ec9f222e4566fff363250b6f4d5fd2 Mon Sep 17 00:00:00 2001 From: Erik Danielsson Date: Mon, 28 Jun 2021 15:00:28 +0200 Subject: [PATCH 3/4] Apply suggestions from code review --- nf_core/modules/remove.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/nf_core/modules/remove.py b/nf_core/modules/remove.py index 506eb6cd60..c217098593 100644 --- a/nf_core/modules/remove.py +++ b/nf_core/modules/remove.py @@ -68,17 +68,19 @@ def remove(self, module): def remove_modules_json_entry(self, module): # Load 'modules.json' modules_json_path = os.path.join(self.dir, "modules.json") - with open(modules_json_path, "r") as fh: - modules_json = json.load(fh) + try: + with open(modules_json_path, "r") as fh: + modules_json = json.load(fh) + except FileNotFoundError: + log.error("File 'modules.json' is missing") + return False if module in modules_json.get("modules", {}): modules_json["modules"].pop(module) else: - # If the module is missing from modules.json we can still clear the directory - # in which the untracked module resides log.error(f"Module '{module}' is missing from 'modules.json' file.") - return questionary.confirm( - f"Do you wish to continue removing module '{module}'?", default=False - ).unsafe_ask() + return False + with open(modules_json_path, "w") as fh: json.dump(modules_json, fh, indent=4) + return True From bb1741322a02412acf00cc9fd1a5351a9c65558e Mon Sep 17 00:00:00 2001 From: Erik Danielsson Date: Mon, 28 Jun 2021 15:02:41 +0200 Subject: [PATCH 4/4] Remove module even though 'modules.json' entry is missing --- nf_core/modules/remove.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/nf_core/modules/remove.py b/nf_core/modules/remove.py index c217098593..cfc0cdb940 100644 --- a/nf_core/modules/remove.py +++ b/nf_core/modules/remove.py @@ -59,8 +59,7 @@ def remove(self, module): log.info("Removing {}".format(module)) # Remove entry from modules.json - if not self.remove_modules_json_entry(module): - return False + self.remove_modules_json_entry(module) # Remove the module return self.clear_module_dir(module_name=module, module_dir=module_dir)