From 5e4da5020bec0a7238f1b1a937a8efa9531fff00 Mon Sep 17 00:00:00 2001 From: Felix Moessbauer Date: Thu, 18 May 2023 09:15:08 +0200 Subject: [PATCH] repos: restore error handling of patch apply In ac4373088 the error handling was made more strict. By that, failures during patch apply instantly bailed out instead of returning and providing the more useful error message. This patch restores the error handling by marking the commands as not-fail and encapsulating the error into a more specific exception with a useful error message. Signed-off-by: Felix Moessbauer Signed-off-by: Jan Kiszka --- kas/repos.py | 68 +++++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/kas/repos.py b/kas/repos.py index 08e903a..31c74c2 100644 --- a/kas/repos.py +++ b/kas/repos.py @@ -65,6 +65,12 @@ class PatchMappingError(KasUserError): pass +class PatchApplyError(KasUserError): + """ + The provided patch file could not be applied + """ + + class Repo: """ Represents a repository in the kas configuration. @@ -326,13 +332,10 @@ class RepoImpl(Repo): None) if not other_repo: - logging.error('Could not find referenced repo. ' - '(missing repo: %s, repo: %s, ' - 'patch entry: %s)', - patch['repo'], - self.name, - patch['id']) - return 1 + raise PatchMappingError( + 'Could not find referenced repo. ' + '(missing repo: {}, repo: {}, patch entry: {})' + .format(patch['repo'], self.name, patch['id'])) path = os.path.join(other_repo.path, patch['path']) cmd = [] @@ -351,42 +354,41 @@ class RepoImpl(Repo): else: raise PatchFileNotFound(p) else: - logging.error('Could not find patch. ' - '(patch path: %s, repo: %s, patch entry: %s)', - path, - self.name, - patch['id']) - return 1 + raise PatchFileNotFound( + 'Could not find patch. ' + '(patch path: {}, repo: {}, patch entry: {})' + .format(path, self.name, patch['id'])) for (path, patch_id) in my_patches: cmd = self.apply_patches_file_cmd(path) - (retc, output) = await run_cmd_async(cmd, cwd=self.path) + (retc, output) = await run_cmd_async( + cmd, cwd=self.path, fail=False) if retc: - logging.error('Could not apply patch. Please fix repos and ' - 'patches. (patch path: %s, repo: %s, patch ' - 'entry: %s, vcs output: %s)', - path, self.name, patch_id, output) - return 1 - else: - logging.info('Patch applied. ' - '(patch path: %s, repo: %s, patch entry: %s)', - path, self.name, patch_id) + raise PatchApplyError( + 'Could not apply patch. Please fix repos and ' + 'patches. (patch path: {}, repo: {}, patch ' + 'entry: {}, vcs output: {})' + .format(path, self.name, patch_id, output)) + + logging.info('Patch applied. ' + '(patch path: %s, repo: %s, patch entry: %s)', + path, self.name, patch_id) cmd = self.add_cmd() - (retc, output) = await run_cmd_async(cmd, cwd=self.path) + (retc, output) = await run_cmd_async( + cmd, cwd=self.path, fail=False) if retc: - logging.error('Could not add patched files. ' - 'repo: %s, vcs output: %s)', - self.name, output) - return 1 + raise PatchApplyError( + 'Could not add patched files. repo: {}, vcs output: {})' + .format(self.name, output)) cmd = self.commit_cmd() - (retc, output) = await run_cmd_async(cmd, cwd=self.path) + (retc, output) = await run_cmd_async( + cmd, cwd=self.path, fail=False) if retc: - logging.error('Could not commit patch changes. ' - 'repo: %s, vcs output: %s)', - self.name, output) - return 1 + raise PatchApplyError( + 'Could not commit patch changes. repo: {}, vcs output: {})' + .format(self.name, output)) return 0