Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge multiple if statements #1120

Closed
wants to merge 4 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 85 additions & 47 deletions mock/py/mockbuild/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,56 +95,94 @@ def clean(self):

@traceLog()
def scrub(self, scrub_opts):
"""clean out chroot and/or cache dirs with extreme prejudice :)"""
statestr = "scrub %s" % scrub_opts
"""Clean out chroot and/or cache dirs with extreme prejudice :)"""
statestr = f"scrub {scrub_opts}"
self.state.start(statestr)

try:
try:
self.plugins.call_hooks('clean')
self.plugins.call_hooks('clean')
if self.bootstrap_buildroot is not None:
self.bootstrap_buildroot.plugins.call_hooks('clean')

for scrub in scrub_opts:
self.plugins.call_hooks('scrub', scrub)
if self.bootstrap_buildroot is not None:
self.bootstrap_buildroot.plugins.call_hooks('clean')

for scrub in scrub_opts:
self.plugins.call_hooks('scrub', scrub)
if self.bootstrap_buildroot is not None:
self.bootstrap_buildroot.plugins.call_hooks('scrub', scrub)

if scrub == 'all':
self.buildroot.root_log.info("scrubbing everything for %s", self.config_name)
self.buildroot.delete()
file_util.rmtree(self.buildroot.cachedir, selinux=self.buildroot.selinux)
if self.bootstrap_buildroot is not None:
self.bootstrap_buildroot.delete()
file_util.rmtree(self.bootstrap_buildroot.cachedir,
selinux=self.bootstrap_buildroot.selinux)
elif scrub == 'chroot':
self.buildroot.root_log.info("scrubbing chroot for %s", self.config_name)
self.buildroot.delete()
elif scrub == 'cache':
self.buildroot.root_log.info("scrubbing cache for %s", self.config_name)
file_util.rmtree(self.buildroot.cachedir, selinux=self.buildroot.selinux)
elif scrub == 'c-cache':
self.buildroot.root_log.info("scrubbing c-cache for %s", self.config_name)
file_util.rmtree(os.path.join(self.buildroot.cachedir, 'ccache'),
selinux=self.buildroot.selinux)
elif scrub == 'root-cache':
self.buildroot.root_log.info("scrubbing root-cache for %s", self.config_name)
file_util.rmtree(os.path.join(self.buildroot.cachedir, 'root_cache'),
selinux=self.buildroot.selinux)
elif scrub in ['yum-cache', 'dnf-cache']:
self.buildroot.root_log.info("scrubbing yum-cache and dnf-cache for %s", self.config_name)
file_util.rmtree(os.path.join(self.buildroot.cachedir, 'yum_cache'),
selinux=self.buildroot.selinux)
file_util.rmtree(os.path.join(self.buildroot.cachedir, 'dnf_cache'),
selinux=self.buildroot.selinux)
elif scrub == 'bootstrap' and self.bootstrap_buildroot is not None:
self.buildroot.root_log.info("scrubbing bootstrap for %s", self.config_name)
self.bootstrap_buildroot.delete()
file_util.rmtree(self.bootstrap_buildroot.cachedir, selinux=self.bootstrap_buildroot.selinux)

except IOError as e:
getLog().warning("parts of chroot do not exist: %s", e)
raise
self.bootstrap_buildroot.plugins.call_hooks('scrub', scrub)

scrub_actions = {
'all': {
'msg': f"scrubbing everything for {self.config_name}",
'actions': [
self.buildroot.delete,
lambda: file_util.rmtree(self.buildroot.cachedir,
selinux=self.buildroot.selinux),
self.bootstrap_buildroot.delete,
(lambda: file_util.rmtree(self.bootstrap_buildroot.cachedir,
selinux=self.bootstrap_buildroot.selinux)
if self.bootstrap_buildroot is not None else None),
]
},
'chroot': {
'msg': f"scrubbing chroot for {self.config_name}",
'actions': [self.buildroot.delete]
},
'cache': {
'msg': f"scrubbing cache for {self.config_name}",
'actions': [lambda: file_util.rmtree(self.buildroot.cachedir,
selinux=self.buildroot.selinux)]
},
'c-cache': {
'msg': f"scrubbing c-cache for {self.config_name}",
'actions': [lambda: file_util.rmtree(os.path.join(self.buildroot.cachedir, 'ccache'),
selinux=self.buildroot.selinux)]
},
'root-cache': {
'msg': f"scrubbing root-cache for {self.config_name}",
'actions': [lambda: file_util.rmtree(os.path.join(self.buildroot.cachedir, 'root_cache'),
selinux=self.buildroot.selinux)]
},
'yum-cache': {
'msg': f"scrubbing yum-cache and dnf-cache for {self.config_name}",
'actions': [
lambda: file_util.rmtree(os.path.join(self.buildroot.cachedir, 'yum_cache'),
selinux=self.buildroot.selinux),
lambda: file_util.rmtree(os.path.join(self.buildroot.cachedir, 'dnf_cache'),
selinux=self.buildroot.selinux)
]
},
'dnf-cache': {
'msg': f"scrubbing yum-cache and dnf-cache for {self.config_name}",
'actions': [
lambda: file_util.rmtree(os.path.join(self.buildroot.cachedir, 'yum_cache'),
selinux=self.buildroot.selinux),
lambda: file_util.rmtree(os.path.join(self.buildroot.cachedir, 'dnf_cache'),
selinux=self.buildroot.selinux)
]
},
'bootstrap': {
'msg': f"scrubbing bootstrap for {self.config_name}",
'actions': [
self.bootstrap_buildroot.delete,
(lambda: file_util.rmtree(self.bootstrap_buildroot.cachedir,
selinux=self.bootstrap_buildroot.selinux)
if self.bootstrap_buildroot is not None else None)
]
}
}

try:
scrub_action = scrub_actions[scrub]
except KeyError as exc:
raise RuntimeError(f"Unknown scrub option '{scrub}'") from exc

self.buildroot.root_log.info(f"scrubbing {scrub_action['msg']} for {self.config_name}")
for action in scrub_action['actions']:
if action is not None:
action()

except IOError as e:
self.log_manager.warning("parts of chroot do not exist: %s", e)
Fixed Show fixed Hide fixed
raise
finally:
self.state.finish(statestr)

Expand Down