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 <felix.moessbauer@siemens.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
This commit is contained in:
Felix Moessbauer 2023-05-18 09:15:08 +02:00 committed by Jan Kiszka
parent d3963ff494
commit 5e4da5020b

View File

@ -65,6 +65,12 @@ class PatchMappingError(KasUserError):
pass pass
class PatchApplyError(KasUserError):
"""
The provided patch file could not be applied
"""
class Repo: class Repo:
""" """
Represents a repository in the kas configuration. Represents a repository in the kas configuration.
@ -326,13 +332,10 @@ class RepoImpl(Repo):
None) None)
if not other_repo: if not other_repo:
logging.error('Could not find referenced repo. ' raise PatchMappingError(
'(missing repo: %s, repo: %s, ' 'Could not find referenced repo. '
'patch entry: %s)', '(missing repo: {}, repo: {}, patch entry: {})'
patch['repo'], .format(patch['repo'], self.name, patch['id']))
self.name,
patch['id'])
return 1
path = os.path.join(other_repo.path, patch['path']) path = os.path.join(other_repo.path, patch['path'])
cmd = [] cmd = []
@ -351,42 +354,41 @@ class RepoImpl(Repo):
else: else:
raise PatchFileNotFound(p) raise PatchFileNotFound(p)
else: else:
logging.error('Could not find patch. ' raise PatchFileNotFound(
'(patch path: %s, repo: %s, patch entry: %s)', 'Could not find patch. '
path, '(patch path: {}, repo: {}, patch entry: {})'
self.name, .format(path, self.name, patch['id']))
patch['id'])
return 1
for (path, patch_id) in my_patches: for (path, patch_id) in my_patches:
cmd = self.apply_patches_file_cmd(path) 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: if retc:
logging.error('Could not apply patch. Please fix repos and ' raise PatchApplyError(
'patches. (patch path: %s, repo: %s, patch ' 'Could not apply patch. Please fix repos and '
'entry: %s, vcs output: %s)', 'patches. (patch path: {}, repo: {}, patch '
path, self.name, patch_id, output) 'entry: {}, vcs output: {})'
return 1 .format(path, self.name, patch_id, output))
else:
logging.info('Patch applied. ' logging.info('Patch applied. '
'(patch path: %s, repo: %s, patch entry: %s)', '(patch path: %s, repo: %s, patch entry: %s)',
path, self.name, patch_id) path, self.name, patch_id)
cmd = self.add_cmd() 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: if retc:
logging.error('Could not add patched files. ' raise PatchApplyError(
'repo: %s, vcs output: %s)', 'Could not add patched files. repo: {}, vcs output: {})'
self.name, output) .format(self.name, output))
return 1
cmd = self.commit_cmd() 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: if retc:
logging.error('Could not commit patch changes. ' raise PatchApplyError(
'repo: %s, vcs output: %s)', 'Could not commit patch changes. repo: {}, vcs output: {})'
self.name, output) .format(self.name, output))
return 1
return 0 return 0