| 1 |
#!/usr/bin/env python3 |
| 2 |
# SPDX-FileCopyrightText: 2023 Sandro Knauß <[email protected]> |
| 3 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
|
| 5 |
import contextlib |
| 6 |
import json |
| 7 |
import logging |
| 8 |
import pathlib |
| 9 |
import re |
| 10 |
import subprocess |
| 11 |
|
| 12 |
from debian import deb822, substvars |
| 13 |
|
| 14 |
logging.basicConfig(format='%(levelname).1s: pkgkde ' |
| 15 |
'%(module)s:%(lineno)d: %(message)s') |
| 16 |
log = logging.getLogger('pkgkde') |
| 17 |
|
| 18 |
PATHS = [pathlib.Path(__file__).parent.with_name("datalib"), |
| 19 |
pathlib.Path('/usr/share/pkg-kde-tools/lib') |
| 20 |
] |
| 21 |
|
| 22 |
deb822_logger = logging.getLogger('debian.deb822') |
| 23 |
REL_RAW_RE = re.compile('cannot parse package relationship ".*", returning it raw') |
| 24 |
|
| 25 |
class DebianControl: |
| 26 |
""" Simple class for debian/control file. |
| 27 |
|
| 28 |
source: with point to the source entry in contrile file |
| 29 |
packages: dict with all binary packages in control file |
| 30 |
|
| 31 |
""" |
| 32 |
def __init__(self, path: pathlib.Path) -> None: |
| 33 |
self.path = path |
| 34 |
|
| 35 |
self.read_path(path) |
| 36 |
|
| 37 |
def read_path(self, path:pathlib.Path) -> None: |
| 38 |
self.source = None |
| 39 |
self.packages = {} |
| 40 |
with path.open() as cl: |
| 41 |
for block in deb822.Deb822.iter_paragraphs(cl): |
| 42 |
if block.get("Source"): |
| 43 |
self.source = block |
| 44 |
else: |
| 45 |
self.packages[block.get("Package")] = block |
| 46 |
|
| 47 |
def isSinglePackage(self) -> bool: |
| 48 |
"""Is there only one binary package?""" |
| 49 |
return len(self.packages) == 1 |
| 50 |
|
| 51 |
|
| 52 |
def active_compat_level() -> int: |
| 53 |
output = subprocess.check_output(["dh_assistant", "active-compat-level"]) |
| 54 |
j = json.loads(output) |
| 55 |
return j["active-compat-level"] |
| 56 |
|
| 57 |
|
| 58 |
def set_substvar(path:pathlib.Path, substvar_dict: dict): |
| 59 |
""" handles a debian/substvars file. |
| 60 |
|
| 61 |
path: path to substvars file. |
| 62 |
substsvars: is a dict to set key,value in the substvars file. |
| 63 |
""" |
| 64 |
|
| 65 |
with substvars.Substvars.load_from_path(path, missing_ok=True) as svars: |
| 66 |
for var_name, values in substvar_dict.items(): |
| 67 |
for pkg in values: |
| 68 |
svars.add_dependency(var_name, pkg) |
| 69 |
|
| 70 |
|
| 71 |
def filter_rel_raw_warning(record): |
| 72 |
return record.levelno != logging.WARNING or not REL_RAW_RE.match(record.getMessage()) |
| 73 |
|
| 74 |
|
| 75 |
@contextlib.contextmanager |
| 76 |
def deb822_filter_rel_raw_warning(): |
| 77 |
deb822_logger.addFilter(filter_rel_raw_warning) |
| 78 |
try: |
| 79 |
yield |
| 80 |
finally: |
| 81 |
deb822_logger.removeFilter(filter_rel_raw_warning) |