Skip to content

Commit 2c0d003

Browse files
Jakub RuzickaGerrit Code Review
authored andcommitted
refactor: merge legacy rdopkg.utils.exception
In the ancient times, rdopkg.utils was shared between other projects using `git subtree` so it had relative imports and own set of exceptions. However, that is no longer the case so we can have all the exceptions in exception.py. Yay! Change-Id: I09ed85fe3dbcec1bae37d067e63d5828e7e27cfe
1 parent eda5364 commit 2c0d003

File tree

5 files changed

+80
-95
lines changed

5 files changed

+80
-95
lines changed

rdopkg/actionmods/cbsbuild.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import os
88
import subprocess
99

10-
from rdopkg.utils.exception import ModuleNotAvailable, RpmModuleNotAvailable
10+
from rdopkg.exception import ModuleNotAvailable, RpmModuleNotAvailable
1111
from rdopkg.utils.specfile import spec_fn, Spec
1212
from rdopkg.utils.log import log
1313
from rdopkg import exception, guess

rdopkg/exception.py

Lines changed: 74 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,77 @@
1-
from rdopkg.utils.exception import RdopkgException
2-
# TODO before 1.0: merge utils.exception.* here.
3-
# Legacy reasons to have them separate no longer apply.
4-
from rdopkg.utils.exception import BranchNotFound # NOQA
5-
from rdopkg.utils.exception import BuildArchSanityCheckFailed # NOQA
6-
from rdopkg.utils.exception import CommandFailed # NOQA
7-
from rdopkg.utils.exception import CommandNotFound # NOQA
8-
from rdopkg.utils.exception import DuplicatePatchesBaseError # NOQA
9-
from rdopkg.utils.exception import IncompleteChangelog # NOQA
10-
from rdopkg.utils.exception import InvalidAction # NOQA
11-
from rdopkg.utils.exception import ModuleNotAvailable # NOQA
12-
from rdopkg.utils.exception import MultipleSpecFilesFound # NOQA
13-
from rdopkg.utils.exception import RpmModuleNotAvailable # NOQA
14-
from rdopkg.utils.exception import SpecFileNotFound # NOQA
15-
from rdopkg.utils.exception import SpecFileParseError # NOQA
1+
class RdopkgException(Exception):
2+
msg_fmt = "An unknown error occurred"
3+
exit_code = 1
4+
5+
def __init__(self, msg=None, exit_code=None, **kwargs):
6+
self.kwargs = kwargs
7+
if not msg:
8+
try:
9+
msg = self.msg_fmt % kwargs
10+
except Exception:
11+
# kwargs doesn't match those in message.
12+
# Returning this is still better than nothing.
13+
msg = self.msg_fmt
14+
if exit_code is not None:
15+
self.exit_code = exit_code
16+
super(RdopkgException, self).__init__(msg)
17+
18+
19+
class CommandFailed(RdopkgException):
20+
msg_fmt = "Command failed: %(cmd)s"
21+
22+
23+
class CommandNotFound(RdopkgException):
24+
msg_fmt = "Command not found: %(cmd)s"
25+
26+
27+
class InvalidRemoteBranch(RdopkgException):
28+
msg_fmt = "Git remote not found for remote branch: %(branch)s"
29+
30+
31+
class BranchNotFound(RdopkgException):
32+
msg_fmt = "Expected git branch not found: %(branch)s"
33+
34+
35+
class SpecFileNotFound(RdopkgException):
36+
msg_fmt = "No .spec files found."
37+
38+
39+
class IncompleteChangelog(RdopkgException):
40+
msg_fmt = "Description of changes is missing in %%changelog."
41+
42+
43+
class MultipleSpecFilesFound(RdopkgException):
44+
msg_fmt = "Multiple .spec files found. Expected only one."
45+
46+
47+
class SpecFileParseError(RdopkgException):
48+
msg_fmt = "Error parsing .spec file '%(spec_fn)s': %(error)s"
49+
50+
51+
class ModuleNotAvailable(RdopkgException):
52+
msg_fmt = "Module %(module)s is not available. Unable to continue."
53+
54+
55+
class RpmModuleNotAvailable(ModuleNotAvailable):
56+
msg_fmt = ("Module rpm is not available. It is required to parse .spec "
57+
"files. Pro tip: `yum install rpm-python`")
58+
59+
60+
class InvalidAction(RdopkgException):
61+
msg_fmt = "Invalid action: %(action)s"
62+
63+
64+
class BuildArchSanityCheckFailed(RdopkgException):
65+
msg_fmt = ("Due to mysterious ways of rpm, BuildArch needs to be placed "
66+
"AFTER SourceX and PatchX lines in .spec file, "
67+
"otherwise %%{patches} macro will be empty "
68+
"and both %%autosetup and `git am %%{patches}` will fail.\n\n"
69+
"Please move BuildArch AFTER SourceX and PatchX.")
70+
71+
72+
class DuplicatePatchesBaseError(RdopkgException):
73+
msg_fmt = ("Please make sure to only have one "
74+
"# patches_base= entry in .spec")
1675

1776

1877
class UserAbort(RdopkgException):

rdopkg/utils/exception.py

Lines changed: 0 additions & 74 deletions
This file was deleted.

tests/test_exception.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from rdopkg.utils.exception import CommandFailed
2-
from rdopkg.utils.exception import RdopkgException
1+
from rdopkg.exception import CommandFailed
2+
from rdopkg.exception import RdopkgException
33

44

55
def test_exception_default():

tests/test_update_patches.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from rdopkg.actions.distgit.actions import update_patches
55
from rdopkg.utils.cmd import git
66
from rdopkg.utils import specfile
7-
import rdopkg.utils.exception
7+
import rdopkg.exception
88

99
import test_common as common
1010

@@ -146,7 +146,7 @@ def test_update_git_am_buildarch_fail(tmpdir):
146146
common.prep_patches_branch()
147147
commit_before = git('rev-parse', 'HEAD')
148148
common.add_patches(extra=True)
149-
with pytest.raises(rdopkg.utils.exception.BuildArchSanityCheckFailed):
149+
with pytest.raises(rdopkg.exception.BuildArchSanityCheckFailed):
150150
update_patches('master',
151151
local_patches_branch='master-patches',
152152
version='1.2.3')
@@ -237,7 +237,7 @@ def test_update_double_patches_base(tmpdir):
237237
with dist_path.as_cwd():
238238
common.prep_patches_branch()
239239
commit_before = git('rev-parse', 'HEAD')
240-
with pytest.raises(rdopkg.utils.exception.DuplicatePatchesBaseError):
240+
with pytest.raises(rdopkg.exception.DuplicatePatchesBaseError):
241241
update_patches('master',
242242
local_patches_branch='master-patches',
243243
version='1.2.3')

0 commit comments

Comments
 (0)