Skip to content

Commit

Permalink
Use python stdlib for date printing
Browse files Browse the repository at this point in the history
Drops the use of the heavyweight Babel library for this single task. The
result is not thread-safe, but librpm probably isn't either and
rpmautospec is usually run in its own process.

This adds a simple context manager to changelog.py that allows us to set
the LC_TIME within a context block, so we don't forget to change it
back.

Signed-off-by: Stephen Gallagher <sgallagh@redhat.com>
  • Loading branch information
sgallagher committed Dec 11, 2024
1 parent 11fa926 commit b6b99f0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
16 changes: 1 addition & 15 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ exclude = [
[tool.poetry.dependencies]
python = "^3.9"
rpmautospec_core = "^0.1.4"
babel = "^2.8"
pygit2 = [
{ version = "^1.4.0,<1.16.0", python = ">=3.9.0,<3.10.0" },
{ version = "^1.4.0", python = "^3.10.0" }
Expand Down
21 changes: 16 additions & 5 deletions rpmautospec/changelog.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import re
import locale
from enum import Enum, auto
from textwrap import TextWrapper

from babel.dates import format_datetime


class CommitLogParseState(int, Enum):
before_subject = auto()
Expand All @@ -13,6 +12,18 @@ class CommitLogParseState(int, Enum):
body = auto()


class TimeLocaleManager:
def __init__(self, localename):
self.name = localename

def __enter__(self):
self.orig = locale.setlocale(locale.LC_TIME)
locale.setlocale(locale.LC_TIME, self.name)

def __exit__(self, exc_type, exc_value, traceback): # pylint: disable=unused-argument
locale.setlocale(locale.LC_TIME, self.orig)


class ChangelogEntry(dict):
"""Dictionary holding changelog entry details."""

Expand Down Expand Up @@ -98,9 +109,9 @@ def format(self, **overrides):
# verbatim data from the changed `changelog` file
return entry_info["data"]

changelog_date = format_datetime(
entry_info["timestamp"], format="EEE MMM dd y", locale="en"
)
# WARNING: the following is NOT thread-safe
with TimeLocaleManager("en_US"):
changelog_date = entry_info["timestamp"].strftime("%a %b %d %Y")

if entry_info["epoch-version"]:
changelog_evr = f" - {entry_info['epoch-version']}"
Expand Down

0 comments on commit b6b99f0

Please sign in to comment.