Skip to content

Commit 1c19f7d

Browse files
authored
Merge pull request #31 from gardenlinux/debsource
Add debsource parser from GLVD
2 parents 9ed8d31 + 4c837e7 commit 1c19f7d

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed

src/python_gardenlinux_lib/apt/__init__.py

Whitespace-only changes.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# SPDX-License-Identifier: MIT
2+
3+
# Based on code from glvd https://github.com/gardenlinux/glvd/blob/7ca2ff54e01da5e9eae61d1cd565eaf75f3c62ce/src/glvd/data/debsrc.py#L1
4+
5+
from __future__ import annotations
6+
7+
import re
8+
from typing import TextIO
9+
10+
11+
class Debsrc:
12+
def __init__(self, deb_source, deb_version):
13+
self.deb_source = deb_source
14+
self.deb_version = deb_version
15+
16+
deb_source: str
17+
deb_version: str
18+
19+
def __repr__(self) -> str:
20+
return f"{self.deb_source} {self.deb_version}"
21+
22+
23+
class DebsrcFile(dict[str, Debsrc]):
24+
__re = re.compile(
25+
r"""
26+
^(?:
27+
Package:\s*(?P<source>[a-z0-9.-]+)
28+
|
29+
Version:\s*(?P<version>[A-Za-z0-9.+~:-]+)
30+
|
31+
Extra-Source-Only:\s*(?P<eso>yes)
32+
|
33+
(?P<eoe>)
34+
|
35+
# All other fields
36+
[A-Za-z0-9-]+:.*
37+
|
38+
# Continuation field
39+
\s+.*
40+
)$
41+
""",
42+
re.VERBOSE,
43+
)
44+
45+
def _read_source(self, source: str, version: str) -> None:
46+
self[source] = Debsrc(
47+
deb_source=source,
48+
deb_version=version,
49+
)
50+
51+
def read(self, f: TextIO) -> None:
52+
current_source = current_version = None
53+
54+
def finish():
55+
if current_source and current_version:
56+
self._read_source(current_source, current_version)
57+
58+
for line in f.readlines():
59+
if match := self.__re.match(line):
60+
if i := match["source"]:
61+
current_source = i
62+
elif i := match["version"]:
63+
current_version = i
64+
elif match["eso"]:
65+
current_source = current_version = None
66+
elif match["eoe"] is not None:
67+
finish()
68+
current_source = current_version = None
69+
else:
70+
raise RuntimeError(f"Unable to read line: {line}")
71+
72+
finish()

tests/test_parse_debsource.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
from python_gardenlinux_lib.apt.parse_debsource import DebsrcFile
2+
import io
3+
4+
test_data = """Package: vim
5+
Source: vim (2:9.1.0496-1)
6+
Version: 2:9.1.0496-1+b1
7+
Architecture: amd64
8+
Maintainer: Debian Vim Maintainers <[email protected]>
9+
Installed-Size: 3788
10+
Depends: vim-common (= 2:9.1.0496-1), vim-runtime (= 2:9.1.0496-1), libacl1 (>= 2.2.23), libc6 (>= 2.38), libgpm2 (>= 1.20.7), libselinux1 (>= 3.1~), libsodium23 (>= 1.0.14), libtinfo6 (>= 6)
11+
Suggests: ctags, vim-doc, vim-scripts
12+
Provides: editor
13+
Filename: pool/d4e0296c516812e2038ff3574f99af7c1a4810612598c3360cb99de61e047011/vim_2%3a9.1.0496-1+b1_amd64.deb
14+
Size: 1619492
15+
MD5sum: 65a0123a1fc76cf7695d90030f5e80d2
16+
SHA1: b0aebce641b3c708235d940f50b1627b5fd42f99
17+
SHA256: d4e0296c516812e2038ff3574f99af7c1a4810612598c3360cb99de61e047011
18+
Section: editors
19+
Priority: optional
20+
Homepage: https://www.vim.org/
21+
Description: Vi IMproved - enhanced vi editor
22+
This is a sample description.
23+
.
24+
It can have multiple lines.
25+
.
26+
27+
Package: vim-common
28+
Source: vim
29+
Version: 2:9.1.0496-1
30+
Architecture: all
31+
Maintainer: Debian Vim Maintainers <[email protected]>
32+
Installed-Size: 1850
33+
Recommends: xxd, vim | vim-gtk3 | vim-motif | vim-nox | vim-tiny
34+
Breaks: vim-runtime (<< 2:9.0.1658-1~)
35+
Replaces: vim-runtime (<< 2:9.0.1658-1~)
36+
Filename: pool/3eb851c9789f075d9e1dbdd721d40e5c6d0e835776518f66fd4335e0bb6f1f61/vim-common_2%3a9.1.0496-1_all.deb
37+
Size: 411852
38+
MD5sum: 259c2baa838b5325829c88dbf583bf2b
39+
SHA1: 2b7252742fb40dff8ce3abbc6c54730a3e519b2f
40+
SHA256: 3eb851c9789f075d9e1dbdd721d40e5c6d0e835776518f66fd4335e0bb6f1f61
41+
Section: editors
42+
Priority: important
43+
Multi-Arch: foreign
44+
Homepage: https://www.vim.org/
45+
Description: Vi IMproved - Common files
46+
47+
Package: vim-runtime
48+
Source: vim
49+
Version: 2:9.1.0496-1
50+
Architecture: all
51+
Maintainer: Debian Vim Maintainers <[email protected]>
52+
Installed-Size: 36945
53+
Recommends: vim | vim-gtk3 | vim-motif | vim-nox | vim-tiny
54+
Enhances: vim-tiny
55+
Breaks: vim-tiny (<< 2:9.1.0496-1)
56+
Filename: pool/488a128a939e430f681230ef9fd83b371585f0e0d3cf7649f135270133b5b162/vim-runtime_2%3a9.1.0496-1_all.deb
57+
Size: 7116680
58+
MD5sum: a2e9e7a7907644444b7555eb6b23ce59
59+
SHA1: 330bb657c0249504a3114f56cd70848bee5e7270
60+
SHA256: 488a128a939e430f681230ef9fd83b371585f0e0d3cf7649f135270133b5b162
61+
Section: editors
62+
Priority: optional
63+
Multi-Arch: foreign
64+
Homepage: https://www.vim.org/
65+
Description: Vi IMproved - Runtime files
66+
67+
Package: vim-tiny
68+
Source: vim (2:9.1.0496-1)
69+
Version: 2:9.1.0496-1+b1
70+
Architecture: amd64
71+
Maintainer: Debian Vim Maintainers <[email protected]>
72+
Installed-Size: 1750
73+
Depends: vim-common (= 2:9.1.0496-1), libacl1 (>= 2.2.23), libc6 (>= 2.34), libselinux1 (>= 3.1~), libtinfo6 (>= 6)
74+
Suggests: indent
75+
Provides: editor
76+
Filename: pool/a44d841ab0ba4dcea6da764126fbfa351642a913920ad15a10781239e4297e27/vim-tiny_2%3a9.1.0496-1+b1_amd64.deb
77+
Size: 743340
78+
MD5sum: 518f5bb267de52be4613a9eaac5325fb
79+
SHA1: 5bc3956b165fd36a7da185ed3962afc7d0957d53
80+
SHA256: a44d841ab0ba4dcea6da764126fbfa351642a913920ad15a10781239e4297e27
81+
Section: editors
82+
Priority: important
83+
Homepage: https://www.vim.org/
84+
Description: Vi IMproved - enhanced vi editor - compact version
85+
"""
86+
87+
88+
def test_parse_debsource_file():
89+
expected = sorted(
90+
[
91+
"vim-common 2:9.1.0496-1",
92+
"vim-runtime 2:9.1.0496-1",
93+
"vim-tiny 2:9.1.0496-1+b1",
94+
"vim 2:9.1.0496-1+b1",
95+
]
96+
)
97+
unit_under_test = DebsrcFile()
98+
99+
unit_under_test.read(io.StringIO(test_data))
100+
101+
# Explicitly sort list for comparison because the order is not deterministic
102+
actual = sorted([f"{f'{package!r}'}" for package in unit_under_test.values()])
103+
assert expected == actual

0 commit comments

Comments
 (0)