File: debsigs.py

package info (click to toggle)
debsigs 0.2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 420 kB
  • sloc: python: 1,160; perl: 728; makefile: 12; sh: 9
file content (57 lines) | stat: -rw-r--r-- 1,614 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# SPDX-FileCopyrightText: Peter Pentchev <roam@ringlet.net>
# SPDX-License-Identifier: GPL-2.0-or-later
"""Parse the human-readable output of `debsigs --list`."""

from __future__ import annotations

import typing

import pyparsing as pyp

from testsigs import defs


if typing.TYPE_CHECKING:
    from typing import Final


_p_first = pyp.Literal("GPG signatures in ") + pyp.CharsNotIn("\n") + pyp.Char("\n")

_p_sig_type = pyp.Regex("[A-Za-z0-9_]+")

_p_hex = pyp.Word("0123456789ABCDEF")

_p_sig = (
    _p_sig_type
    + pyp.Literal(": signed by ").suppress()
    + _p_hex
    + pyp.Literal(" on ").suppress()
    + pyp.CharsNotIn("\n").suppress()
    + pyp.Char("\n").suppress()
)


@_p_sig.set_parse_action
def _parse_sig(tokens: pyp.ParseResults) -> defs.DebSig:
    """Parse a signature line."""
    match tokens.as_list():
        case [sig_type, key_id] if isinstance(sig_type, str) and isinstance(key_id, str):
            return defs.DebSig(sig_type=sig_type, key_id=key_id)

        case _:
            raise RuntimeError(repr(tokens))


_p_last = pyp.Literal(" *** NO ATTEMPT HAS BEEN MADE") + pyp.CharsNotIn("\n") + pyp.Char("\n")

_p_list = _p_first.suppress() + pyp.ZeroOrMore(_p_sig) + _p_last.suppress()

_p_list_complete = _p_list.leave_whitespace().parse_with_tabs()


def parse_list(output: str) -> list[defs.DebSig]:
    """Parse the output of `debsigs --list`, extract the info we care about."""
    res: Final = _p_list_complete.parse_string(output, parse_all=True).as_list()
    if not all(isinstance(sig, defs.DebSig) for sig in res):
        raise RuntimeError(repr(res))
    return res