Skip to content

Commit

Permalink
Implement Specfile.bump_release() method (#399)
Browse files Browse the repository at this point in the history
Implement `Specfile.bump_release()` method

Fixes #364.
RELEASE NOTES BEGIN
It is now possible to bump a release in a manner similar to rpmdev-bumpspec using Specfile.bump_release() method.
RELEASE NOTES END

Reviewed-by: Miroslav Suchý <[email protected]>
  • Loading branch information
softwarefactory-project-zuul[bot] authored Jul 18, 2024
2 parents 2f17aef + ebe2f24 commit a1f476e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
47 changes: 47 additions & 0 deletions specfile/specfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -989,3 +989,50 @@ def handle_prerelease(version):
return version[:base_end] + "~" + version[suffix_start:]

self.update_tag("Version", handle_prerelease(version))

@staticmethod
def _bump_release_string(release_string: str) -> str:
"""
Bumps release string. Follows the logic of `rpmdev-bumpspec`.
Args:
release_string: Release string to be bumped.
Returns:
Bumped release string.
"""
m = re.match(
r"^(?P<func>%release_func\s+)?(?P<pre>0\.)?(?P<rel>\d+)(?P<post>.*)$",
release_string,
)
if m and (
m.group("pre")
or all(x not in m.group("post") for x in ["alpha", "beta", "rc"])
):
return (
(m.group("func") or "")
+ (m.group("pre") or "")
+ str(int(m.group("rel")) + 1)
+ m.group("post")
)
m = re.match(r"^(?P<pre>.+\.)(?P<rel>\d+)$", release_string)
if m:
return m.group("pre") + str(int(m.group("rel")) + 1)
return release_string + ".1"

def bump_release(self) -> None:
"""
Tries to bump release. Follows the logic of `rpmdev-bumpspec`, first trying to update
macro definitions that seem to define a release, then trying to update value
of the Release tag.
"""
if self.has_autorelease:
return

with self.macro_definitions() as macro_definitions:
for md in macro_definitions:
if md.name.lower() in ["release", "baserelease"]:
md.body = self._bump_release_string(md.body)
return

self.raw_release = self._bump_release_string(self.raw_release)
28 changes: 28 additions & 0 deletions tests/unit/test_specfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,31 @@ def test_split_raw_release(raw_release, release, dist, minorbump):
)
def test_get_updated_release(raw_release, release, result):
assert Specfile._get_updated_release(raw_release, release) == result


@pytest.mark.parametrize(
"release, bumped_release",
[
("1%{?dist}", "2%{?dist}"),
("0.1%{?dist}", "0.2%{?dist}"),
("%release_func 26", "%release_func 27"),
("0.24.rc1%{?dist}", "0.25.rc1%{?dist}"),
("0.2.%{prerel}%{?dist}", "0.3.%{prerel}%{?dist}"),
(
"0.8.%{commitdate}%{shortcommit}%{?dist}",
"0.9.%{commitdate}%{shortcommit}%{?dist}",
),
(
"3.%{git_date}git%{git_commit_short}%{?dist}",
"4.%{git_date}git%{git_commit_short}%{?dist}",
),
("1%{?rcrel}%{?dist}.1", "1%{?rcrel}%{?dist}.2"),
(
"%{?beta_ver:0.}%{fedora_rel}%{?beta_ver:.%beta_ver}%{?dist}%{flagrel}%{?extrarel}",
"%{?beta_ver:0.}%{fedora_rel}%{?beta_ver:.%beta_ver}%{?dist}%{flagrel}%{?extrarel}.1",
),
("4.rc2%{?dist}", "4.rc2%{?dist}.1"),
],
)
def test_bump_release_string(release, bumped_release):
assert Specfile._bump_release_string(release) == bumped_release

0 comments on commit a1f476e

Please sign in to comment.