From 50ba8aadf5f7096e9079eafec9182d526956cd49 Mon Sep 17 00:00:00 2001 From: Matt Turner Date: Sat, 23 Jan 2021 21:43:47 -0500 Subject: catalyst: Clean up exception handling Use 'raise from' where sensible, but a lot of the CatalystErrors are just trading one CatalystError for another, so remove them. Signed-off-by: Matt Turner --- catalyst/base/stagebase.py | 110 ++++++++++++++------------------------ catalyst/main.py | 4 +- catalyst/support.py | 4 +- catalyst/targets/livecd_stage2.py | 4 +- catalyst/targets/netboot.py | 33 ++++-------- catalyst/targets/snapshot.py | 2 +- 6 files changed, 57 insertions(+), 100 deletions(-) diff --git a/catalyst/base/stagebase.py b/catalyst/base/stagebase.py index 28ff8fd2..46cb1fda 100644 --- a/catalyst/base/stagebase.py +++ b/catalyst/base/stagebase.py @@ -906,7 +906,7 @@ class StageBase(TargetBase, ClearBase, GenBase): fstype=fstype, options=options) cxt.mount() except Exception as e: - raise CatalystError(f"Couldn't mount: {source}, {e}") + raise CatalystError(f"Couldn't mount: {source}, {e}") from e def chroot_setup(self): self.makeconf = read_makeconf(normpath(self.settings["chroot_path"] + @@ -978,7 +978,7 @@ class StageBase(TargetBase, ClearBase, GenBase): except OSError as e: raise CatalystError('Could not write %s: %s' % ( normpath(self.settings["chroot_path"] + - self.settings["make_conf"]), e)) + self.settings["make_conf"]), e)) from e self.resume.enable("chroot_setup") def write_make_conf(self, setup=True): @@ -1206,13 +1206,11 @@ class StageBase(TargetBase, ClearBase, GenBase): # operations, so we get easy glob handling. log.notice('%s: removing %s', self.settings["spec_prefix"], x) clear_path(self.settings["stage_path"] + x) - try: - if os.path.exists(self.settings["controller_file"]): - cmd([self.settings['controller_file'], 'clean'], - env=self.env) - self.resume.enable("remove") - except: - raise + + if os.path.exists(self.settings["controller_file"]): + cmd([self.settings['controller_file'], 'clean'], + env=self.env) + self.resume.enable("remove") def preclean(self): if "autoresume" in self.settings["options"] \ @@ -1278,19 +1276,14 @@ class StageBase(TargetBase, ClearBase, GenBase): log.notice('Resume point detected, skipping run_local operation...') return - try: - if os.path.exists(self.settings["controller_file"]): - log.info('run_local() starting controller script...') - cmd([self.settings['controller_file'], 'run'], - env=self.env) - self.resume.enable("run_local") - else: - log.info('run_local() no controller_file found... %s', - self.settings['controller_file']) - - except CatalystError: - raise CatalystError("Stage build aborting due to error.", - print_traceback=False) + if os.path.exists(self.settings["controller_file"]): + log.info('run_local() starting controller script...') + cmd([self.settings['controller_file'], 'run'], + env=self.env) + self.resume.enable("run_local") + else: + log.info('run_local() no controller_file found... %s', + self.settings['controller_file']) def setup_environment(self): log.debug('setup_environment(); settings = %r', self.settings) @@ -1382,13 +1375,10 @@ class StageBase(TargetBase, ClearBase, GenBase): [self.settings[self.settings["spec_prefix"] + "/unmerge"]] # Before cleaning, unmerge stuff - try: - cmd([self.settings['controller_file'], 'unmerge'] + - self.settings[self.settings['spec_prefix'] + '/unmerge'], - env=self.env) - log.info('unmerge shell script') - except CatalystError: - raise + cmd([self.settings['controller_file'], 'unmerge'] + + self.settings[self.settings['spec_prefix'] + '/unmerge'], + env=self.env) + log.info('unmerge shell script') self.resume.enable("unmerge") def target_setup(self): @@ -1457,14 +1447,9 @@ class StageBase(TargetBase, ClearBase, GenBase): command.append(self.settings[target_pkgs]) else: command.extend(self.settings[target_pkgs]) - try: - cmd(command, env=self.env) - fileutils.touch(build_packages_resume) - self.resume.enable("build_packages") - except CatalystError: - raise CatalystError( - self.settings["spec_prefix"] + - "build aborting due to error.") + cmd(command, env=self.env) + fileutils.touch(build_packages_resume) + self.resume.enable("build_packages") def build_kernel(self): '''Build all configured kernels''' @@ -1475,19 +1460,14 @@ class StageBase(TargetBase, ClearBase, GenBase): return if "boot/kernel" in self.settings: - try: - mynames = self.settings["boot/kernel"] - if isinstance(mynames, str): - mynames = [mynames] - # Execute the script that sets up the kernel build environment - cmd([self.settings['controller_file'], 'pre-kmerge'], env=self.env) - for kname in [sanitize_name(name) for name in mynames]: - self._build_kernel(kname=kname) - self.resume.enable("build_kernel") - except CatalystError: - raise CatalystError( - "build aborting due to kernel build error.", - print_traceback=True) + mynames = self.settings["boot/kernel"] + if isinstance(mynames, str): + mynames = [mynames] + # Execute the script that sets up the kernel build environment + cmd([self.settings['controller_file'], 'pre-kmerge'], env=self.env) + for kname in [sanitize_name(name) for name in mynames]: + self._build_kernel(kname=kname) + self.resume.enable("build_kernel") def _build_kernel(self, kname): "Build a single configured kernel by name" @@ -1531,12 +1511,8 @@ class StageBase(TargetBase, ClearBase, GenBase): raise CatalystError("Can't find kernel config: %s" % self.settings[key]) - try: - shutil.copy(self.settings[key], - self.settings['chroot_path'] + '/var/tmp/' + kname + '.config') - - except IOError: - raise + shutil.copy(self.settings[key], + self.settings['chroot_path'] + '/var/tmp/' + kname + '.config') def _copy_initramfs_overlay(self, kname): key = 'boot/kernel/' + kname + '/initramfs_overlay' @@ -1560,13 +1536,10 @@ class StageBase(TargetBase, ClearBase, GenBase): 'Resume point detected, skipping bootloader operation...') return - try: - cmd([self.settings['controller_file'], 'bootloader', - self.settings['target_path'].rstrip('/')], - env=self.env) - self.resume.enable("bootloader") - except CatalystError: - raise CatalystError("Script aborting due to error.") + cmd([self.settings['controller_file'], 'bootloader', + self.settings['target_path'].rstrip('/')], + env=self.env) + self.resume.enable("bootloader") def livecd_update(self): if "autoresume" in self.settings["options"] \ @@ -1575,14 +1548,9 @@ class StageBase(TargetBase, ClearBase, GenBase): 'Resume point detected, skipping build_packages operation...') return - try: - cmd([self.settings['controller_file'], 'livecd-update'], - env=self.env) - self.resume.enable("livecd_update") - - except CatalystError: - raise CatalystError( - "build aborting due to livecd_update error.") + cmd([self.settings['controller_file'], 'livecd-update'], + env=self.env) + self.resume.enable("livecd_update") @staticmethod def _debug_pause_(): diff --git a/catalyst/main.py b/catalyst/main.py index b0d9015f..0de1040f 100644 --- a/catalyst/main.py +++ b/catalyst/main.py @@ -77,10 +77,10 @@ def build_target(addlargs): target = addlargs["target"].replace('-', '_') module = import_module(target) target = getattr(module, target)(conf_values, addlargs) - except AttributeError: + except AttributeError as e: raise CatalystError( "Target \"%s\" not available." % target, - print_traceback=True) + print_traceback=True) from e except CatalystError: return False return target.run() diff --git a/catalyst/support.py b/catalyst/support.py index fa652987..fc50fa34 100644 --- a/catalyst/support.py +++ b/catalyst/support.py @@ -140,9 +140,9 @@ def read_makeconf(mymakeconffile): if os.path.exists(mymakeconffile): try: return read_bash_dict(mymakeconffile, sourcing_command="source") - except Exception: + except Exception as e: raise CatalystError("Could not parse make.conf file " + - mymakeconffile, print_traceback=True) + mymakeconffile, print_traceback=True) from e else: makeconf = {} return makeconf diff --git a/catalyst/targets/livecd_stage2.py b/catalyst/targets/livecd_stage2.py index e90e9f53..ff4ea62a 100644 --- a/catalyst/targets/livecd_stage2.py +++ b/catalyst/targets/livecd_stage2.py @@ -79,11 +79,11 @@ class livecd_stage2(StageBase): "livecd/modblacklist"].split() for x in self.settings["livecd/modblacklist"]: myf.write("\nblacklist "+x) - except: + except Exception as e: raise CatalystError("Couldn't open " + self.settings["chroot_path"] + "/etc/modprobe.d/blacklist.conf.", - print_traceback=True) + print_traceback=True) from e def set_action_sequence(self): self.build_sequence.extend([ diff --git a/catalyst/targets/netboot.py b/catalyst/targets/netboot.py index a2a9fcb3..38d0cb45 100644 --- a/catalyst/targets/netboot.py +++ b/catalyst/targets/netboot.py @@ -30,17 +30,14 @@ class netboot(StageBase): ]) def __init__(self, spec, addlargs): - try: - if "netboot/packages" in addlargs: - if isinstance(addlargs['netboot/packages'], str): - loopy = [addlargs["netboot/packages"]] - else: - loopy = addlargs["netboot/packages"] + if "netboot/packages" in addlargs: + if isinstance(addlargs['netboot/packages'], str): + loopy = [addlargs["netboot/packages"]] + else: + loopy = addlargs["netboot/packages"] - for x in loopy: - self.valid_values |= {"netboot/packages/"+x+"/files"} - except: - raise CatalystError("configuration error in netboot/packages.") + for x in loopy: + self.valid_values |= {"netboot/packages/"+x+"/files"} StageBase.__init__(self, spec, addlargs) self.settings["merge_path"] = normpath("/tmp/image/") @@ -89,12 +86,8 @@ class netboot(StageBase): else: myfiles.append(self.settings["netboot/extra_files"]) - try: - cmd([self.settings['controller_file'], 'image'] + - myfiles, env=self.env) - except CatalystError: - raise CatalystError("Failed to copy files to image!", - print_traceback=True) + cmd([self.settings['controller_file'], 'image'] + + myfiles, env=self.env) self.resume.enable("copy_files_to_image") @@ -116,12 +109,8 @@ class netboot(StageBase): # we're done, move the kernels to builds/* # no auto resume here as we always want the # freshest images moved - try: - cmd([self.settings['controller_file'], 'final'], env=self.env) - log.notice('Netboot Build Finished!') - except CatalystError: - raise CatalystError("Failed to move kernel images!", - print_traceback=True) + cmd([self.settings['controller_file'], 'final'], env=self.env) + log.notice('Netboot Build Finished!') def remove(self): if "autoresume" in self.settings["options"] \ diff --git a/catalyst/targets/snapshot.py b/catalyst/targets/snapshot.py index 7732312c..6b727600 100644 --- a/catalyst/targets/snapshot.py +++ b/catalyst/targets/snapshot.py @@ -73,7 +73,7 @@ class snapshot(TargetBase): except subprocess.CalledProcessError as e: raise CatalystError(f'{e.cmd} failed with return code' f'{e.returncode}\n' - f'{e.output}\n') + f'{e.output}\n') from e def run(self): if self.settings['snapshot_treeish'] == 'stable': -- cgit v1.2.3-65-gdbad