Skip to content

Commit 6102342

Browse files
authored
If the container is deleted, the module has to be deleted too. No need to ask the user (#528)
* Only ask for confirmation once * The .version file is only in module_dir, not container_dir * When deleting containers, also delete the parent directories as long as they are empty
1 parent 4f88a3d commit 6102342

File tree

3 files changed

+49
-35
lines changed

3 files changed

+49
-35
lines changed

shpc/main/modules/base.py

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,6 @@ def substitute(self, template):
5858
template = template.replace(key, replacewith)
5959
return template
6060

61-
def _cleanup(self, module_dir):
62-
"""
63-
Given a module directory, delete up to module base
64-
"""
65-
# Exit early if the module directory for some reason was removed
66-
if not os.path.exists(module_dir):
67-
return
68-
shutil.rmtree(module_dir)
69-
70-
# If directories above it are empty, remove
71-
while module_dir != self.settings.module_base:
72-
if not utils.can_be_deleted(module_dir, [".version"]):
73-
break
74-
shutil.rmtree(module_dir)
75-
module_dir = os.path.dirname(module_dir)
76-
7761
@property
7862
def container_base(self):
7963
"""
@@ -106,25 +90,22 @@ def uninstall(self, name, force=False):
10690
# Podman needs image deletion
10791
self.container.delete(name)
10892

109-
if container_dir != module_dir:
110-
self._uninstall(container_dir, "$container_base/%s" % name, force)
111-
self._uninstall(module_dir, "$module_base/%s" % name, force)
112-
else:
113-
self._uninstall(module_dir, "$module_base/%s" % name, force)
93+
if not force:
94+
msg = name + "?"
95+
if not utils.confirm_uninstall(msg, force):
96+
return
11497

115-
def _uninstall(self, module_dir, name, force=False):
116-
"""
117-
Sub function, so we can pass more than one folder from uninstall
118-
"""
119-
if os.path.exists(module_dir):
120-
if not force:
121-
msg = "%s, and all content below it? " % name
122-
if not utils.confirm_uninstall(msg, force):
123-
return
124-
self._cleanup(module_dir)
125-
logger.info("%s and all subdirectories have been removed." % name)
98+
if container_dir != module_dir:
99+
self._uninstall(
100+
container_dir, self.container_base, "$container_base/%s" % name
101+
)
102+
self._uninstall(
103+
module_dir, self.settings.module_base, "$module_base/%s" % name
104+
)
126105
else:
127-
logger.warning("%s does not exist." % name)
106+
self._uninstall(
107+
module_dir, self.settings.module_base, "$module_base/%s" % name
108+
)
128109

129110
# parent of versioned directory has module .version
130111
module_dir = os.path.dirname(module_dir)
@@ -133,6 +114,16 @@ def _uninstall(self, module_dir, name, force=False):
133114
if os.path.exists(module_dir):
134115
self.write_version_file(module_dir)
135116

117+
def _uninstall(self, path, base_path, name):
118+
"""
119+
Sub function, so we can pass more than one folder from uninstall
120+
"""
121+
if os.path.exists(path):
122+
utils.rmdir_to_base(path, base_path)
123+
logger.info("%s and all subdirectories have been removed." % name)
124+
else:
125+
logger.warning("%s does not exist." % name)
126+
136127
def _test_setup(self, tmpdir):
137128
"""
138129
Setup tests, including changes to settings or test directory
@@ -371,7 +362,7 @@ def install(self, name, tag=None, **kwargs):
371362
module_dir, container_dir, config, tag
372363
)
373364
if not container_path:
374-
self._cleanup(container_dir)
365+
utils.rmdir_to_base(container_dir, self.container_base)
375366
logger.exit("There was an issue pulling %s" % container_path)
376367

377368
# Get the template based on the module and container type
@@ -401,7 +392,7 @@ def install(self, name, tag=None, **kwargs):
401392

402393
# If the container tech does not need storage, clean up
403394
if not os.listdir(container_dir):
404-
self._cleanup(container_dir)
395+
utils.rmdir_to_base(container_dir, self.container_base)
405396

406397
# Write the environment file to be bound to the container
407398
self.container.add_environment(

shpc/utils/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
get_tmpfile,
1616
mkdir_p,
1717
mkdirp,
18+
rmdir_to_base,
1819
print_json,
1920
read_file,
2021
read_json,

shpc/utils/fileio.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,28 @@ def mkdir_p(path):
6161
logger.exit("Error creating path %s, exiting." % path)
6262

6363

64+
def rmdir_to_base(path, base_path):
65+
"""
66+
Delete the tree under $path and all the parents
67+
up to $base_path as long as they are empty
68+
"""
69+
if not os.path.isdir(base_path):
70+
logger.exit("Error: %s is not a directory" % base_path)
71+
if not path.startswith(base_path):
72+
logger.exit("Error: %s is not a parent of %s" % (base_path, path))
73+
74+
if os.path.exists(path):
75+
shutil.rmtree(path)
76+
77+
# If directories above it are empty, remove
78+
while path != base_path:
79+
if os.path.exists(path):
80+
if not can_be_deleted(path, [".version"]):
81+
break
82+
shutil.rmtree(path)
83+
path = os.path.dirname(path)
84+
85+
6486
def get_tmpfile(tmpdir=None, prefix=""):
6587
"""
6688
Get a temporary file with an optional prefix.

0 commit comments

Comments
 (0)