Skip to content

Commit

Permalink
Merge pull request #192 from mineralsfree/CFE-3819
Browse files Browse the repository at this point in the history
CFE-3819: cfbs remove - prompt user before removing module dependency
  • Loading branch information
olehermanse authored May 28, 2024
2 parents 5282b8a + b3fe0ed commit 43f9dd6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ on:
jobs:
test:
runs-on: ubuntu-20.04
env:
# Temporary workaround for Python 3.5 failures - May 2024, see CFE-4395
PIP_TRUSTED_HOST: "pypi.python.org pypi.org files.pythonhosted.org"
strategy:
fail-fast: false
matrix:
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/python-validate-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ on:
jobs:
test:
runs-on: ubuntu-20.04
env:
# Temporary workaround for Python 3.5 failures - May 2024, see CFE-4395
PIP_TRUSTED_HOST: "pypi.python.org pypi.org files.pythonhosted.org"
strategy:
fail-fast: false
matrix:
Expand Down
48 changes: 38 additions & 10 deletions cfbs/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import logging as log
import json
import sys
import functools
from collections import OrderedDict
from cfbs.args import get_args

Expand Down Expand Up @@ -383,6 +384,21 @@ def remove_command(to_remove: list):
)
modules = config["build"]

def _get_dependents(dependency) -> list:
if len(modules) < 2:
return []

def reduce_dependencies(a, b):
result_b = [b["name"]] if dependency in b.get("dependencies", []) else []
if type(a) is list:
return a + result_b
else:
return (
[a["name"]] if dependency in a.get("dependencies", []) else []
) + result_b

return functools.reduce(reduce_dependencies, modules)

def _get_module_by_name(name) -> dict:
if not name.startswith("./") and name.endswith(".cf") and os.path.exists(name):
name = "./" + name
Expand All @@ -392,6 +408,21 @@ def _get_module_by_name(name) -> dict:
return module
return None

def _remove_module_user_prompt(module):
dependents = _get_dependents(module["name"])
return prompt_user(
config.non_interactive,
"Do you wish to remove '%s'?" % module["name"]
+ (
" (The module is a dependency of the following module%s: %s)"
% ("s" if len(dependents) > 1 else "", ", ".join(dependents))
if dependents
else ""
),
choices=YES_NO_CHOICES,
default="yes",
)

def _get_modules_by_url(name) -> list:
r = []
for module in modules:
Expand All @@ -408,12 +439,7 @@ def _get_modules_by_url(name) -> list:
if not matches:
user_error("Could not find module with URL '%s'" % name)
for module in matches:
answer = prompt_user(
config.non_interactive,
"Do you wish to remove '%s'?" % module["name"],
choices=YES_NO_CHOICES,
default="yes",
)
answer = _remove_module_user_prompt(module)
if answer.lower() in ("yes", "y"):
print("Removing module '%s'" % module["name"])
modules.remove(module)
Expand All @@ -422,10 +448,12 @@ def _get_modules_by_url(name) -> list:
else:
module = _get_module_by_name(name)
if module:
print("Removing module '%s'" % name)
modules.remove(module)
msg += "\n - Removed module '%s'" % module["name"]
num_removed += 1
answer = _remove_module_user_prompt(module)
if answer.lower() in ("yes", "y"):
print("Removing module '%s'" % name)
modules.remove(module)
msg += "\n - Removed module '%s'" % module["name"]
num_removed += 1
else:
print("Module '%s' not found" % name)
input_path = os.path.join(".", name, "input.json")
Expand Down

0 comments on commit 43f9dd6

Please sign in to comment.