diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-11 08:28:00 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-11 08:28:00 +0000 |
commit | 3565071f226432336a54d0193d729fa4508a3394 (patch) | |
tree | 4cde13f078f84c0a7785d234fd52edce7c90546a /debian/lib/python/debian_linux/utils.py | |
parent | Adding upstream version 6.6.15. (diff) | |
download | linux-3565071f226432336a54d0193d729fa4508a3394.tar.xz linux-3565071f226432336a54d0193d729fa4508a3394.zip |
Adding debian version 6.6.15-2.debian/6.6.15-2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | debian/lib/python/debian_linux/utils.py | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/debian/lib/python/debian_linux/utils.py b/debian/lib/python/debian_linux/utils.py new file mode 100644 index 0000000000..0c6569b5a0 --- /dev/null +++ b/debian/lib/python/debian_linux/utils.py @@ -0,0 +1,76 @@ +import io +import os +import re +import textwrap +import typing + +import jinja2 + +from .debian import SourcePackage, BinaryPackage, TestsControl + + +class Templates(object): + dirs: list[str] + _cache: dict[str, str] + _jinja2: jinja2.Environment + + def __init__(self, dirs: list[str] = ["debian/templates"]) -> None: + self.dirs = dirs + + self._cache = {} + self._jinja2 = jinja2.Environment( + # autoescape uses HTML safe escaping, which does not help us + autoescape=False, + keep_trailing_newline=True, + trim_blocks=True, + undefined=jinja2.StrictUndefined, + ) + + def _read(self, name: str) -> typing.Any: + pkgid, name = name.rsplit('.', 1) + + for suffix in ['.j2', '.in', '']: + for dir in self.dirs: + filename = "%s/%s.%s%s" % (dir, pkgid, name, suffix) + if os.path.exists(filename): + with open(filename, 'r', encoding='utf-8') as f: + return (f.read(), suffix) + + raise KeyError(name) + + def _get(self, key: str) -> typing.Any: + try: + return self._cache[key] + except KeyError: + self._cache[key] = value = self._read(key) + return value + + def get(self, key: str, context: dict[str, str] = {}) -> str: + value = self._get(key) + suffix = value[1] + + if context: + if suffix == '.in': + def subst(match): + return context[match.group(1)] + return re.sub(r'@([-_a-z0-9]+)@', subst, str(value[0])) + + elif suffix == '.j2': + return self._jinja2.from_string(value[0]).render(context) + + return value[0] + + def get_control(self, key: str, context: dict[str, str] = {}) -> BinaryPackage: + return BinaryPackage.read_rfc822(io.StringIO(self.get(key, context))) + + def get_source_control(self, key: str, context: dict[str, str] = {}) -> SourcePackage: + return SourcePackage.read_rfc822(io.StringIO(self.get(key, context))) + + def get_tests_control(self, key: str, context: dict[str, str] = {}) -> TestsControl: + return TestsControl.read_rfc822(io.StringIO(self.get(key, context))) + + +class TextWrapper(textwrap.TextWrapper): + wordsep_re = re.compile( + r'(\s+|' # any whitespace + r'(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))') # em-dash |