From ea9dc8d60c648448c42f765fcab265c33cd55c0a Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Fri, 23 Sep 2011 16:43:25 +0200 Subject: [PATCH 1/3] handle errors with exceptions instead of function Signed-off-by: Steven Armstrong --- bin/cdist | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/bin/cdist b/bin/cdist index fe3c17fd..e3d63458 100755 --- a/bin/cdist +++ b/bin/cdist @@ -81,9 +81,9 @@ def file_to_list(filename): return lines -def exit_error(*args): - log.error(*args) - sys.exit(1) +class CdistError(Exception): + """Base exception class for this project""" + pass class Cdist: """Cdist main class to hold arbitrary data""" @@ -185,9 +185,9 @@ class Cdist: print(script_fd.read()) script_fd.close() - exit_error("Command failed (shell): " + " ".join(*args)) + raise CdistError("Command failed (shell): " + " ".join(*args)) except OSError as error: - exit_error(" ".join(*args) + ": " + error.args[1]) + raise CdistError(" ".join(*args) + ": " + error.args[1]) def run_or_fail(self, *args, **kargs): if "remote" in kargs: @@ -200,9 +200,9 @@ class Cdist: try: subprocess.check_call(*args, **kargs) except subprocess.CalledProcessError: - exit_error("Command failed: " + " ".join(*args)) + raise CdistError("Command failed: " + " ".join(*args)) except OSError as error: - exit_error(" ".join(*args) + ": " + error.args[1]) + raise CdistError(" ".join(*args) + ": " + error.args[1]) def remove_remote_dir(self, destination): @@ -368,7 +368,7 @@ class Cdist: """Run global explorers""" explorers = self.list_global_explorers() if(len(explorers) == 0): - exit_error("No explorers found in", self.global_explorer_dir) + raise CdistError("No explorers found in", self.global_explorer_dir) self.transfer_global_explorers() for explorer in explorers: @@ -674,7 +674,7 @@ def emulator(): try: os.makedirs(param_out_dir, exist_ok=True) except OSError as error: - exit_error(param_out_dir + ": " + error.args[1]) + raise CdistError(param_out_dir + ": " + error.args[1]) # Record parameter params = vars(args) @@ -790,3 +790,6 @@ if __name__ == "__main__": commandline() except KeyboardInterrupt: sys.exit(0) + except CdistError as e: + log.error(e) + sys.exit(1) From a2f3246758cf954e1eecf682c16992cbf9a6026d Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Fri, 23 Sep 2011 17:03:00 +0200 Subject: [PATCH 2/3] fix typo /nun_or_fail/run_or_fail/ Signed-off-by: Steven Armstrong --- bin/cdist | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/cdist b/bin/cdist index 0aa661a3..fde7a251 100755 --- a/bin/cdist +++ b/bin/cdist @@ -153,7 +153,7 @@ class Cdist: def remote_mkdir(self, directory): """Create directory on remote side""" - self.nun_or_fail(["mkdir", "-p", directory], remote=True) + self.run_or_fail(["mkdir", "-p", directory], remote=True) def remote_cat(filename): """Use cat on the remote side for output""" From 91022c3f7e7102833a4416cb45ae171f6e8d4b1e Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Fri, 23 Sep 2011 17:19:45 +0200 Subject: [PATCH 3/3] handle ioerror if script does not exist Signed-off-by: Steven Armstrong --- bin/cdist | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/bin/cdist b/bin/cdist index fde7a251..d9f88b48 100755 --- a/bin/cdist +++ b/bin/cdist @@ -181,9 +181,12 @@ class Cdist: if remote: remote_cat(script) else: - script_fd = open(script) - print(script_fd.read()) - script_fd.close() + try: + script_fd = open(script) + print(script_fd.read()) + script_fd.close() + except IOError as error: + raise CdistError(str(error)) raise CdistError("Command failed (shell): " + " ".join(*args)) except OSError as error: