Skip to content

Commit

Permalink
Add deb822 apt source format support
Browse files Browse the repository at this point in the history
  • Loading branch information
vzhestkov committed Dec 20, 2024
1 parent 7217fcc commit bf1b99e
Showing 1 changed file with 47 additions and 8 deletions.
55 changes: 47 additions & 8 deletions salt/modules/aptpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@
except ImportError:
HAS_APT = False

if HAS_APT:
try:
from aptsources.sourceslist import Deb822SourceEntry, _deb822
HAS_DEB822 = True
except ImportError:
HAS_DEB822 = False

try:
import apt_pkg

Expand Down Expand Up @@ -1907,7 +1914,10 @@ def list_repos(**kwargs):
salt '*' pkg.list_repos disabled=True
"""
repos = {}
sources = SourcesList()
if HAS_DEB822:
sources = SourcesList(deb822=True)
else:
sources = SourcesList()
for source in sources.list:
if _skip_source(source):
continue
Expand All @@ -1916,6 +1926,11 @@ def list_repos(**kwargs):
else:
signedby = _get_opts(line=source.line)["signedby"].get("value", "")
repo = {}
if HAS_DEB822:
try:
signedby = source.section.tags.get("Signed-By", signedby)
except AttributeError:
pass
repo["file"] = source.file
repo["comps"] = getattr(source, "comps", [])
repo["disabled"] = source.disabled
Expand Down Expand Up @@ -2047,7 +2062,10 @@ def del_repo(repo, **kwargs):
else:
repo = softwareproperties.ppa.expand_ppa_line(repo, dist)[0]

sources = SourcesList()
if HAS_DEB822:
sources = SourcesList(deb822=True)
else:
sources = SourcesList()
repos = [s for s in sources.list if not s.invalid]
if repos:
deleted_from = dict()
Expand Down Expand Up @@ -2075,7 +2093,7 @@ def del_repo(repo, **kwargs):

s_comps = set(source.comps)
r_comps = set(repo_comps)
if s_comps.intersection(r_comps):
if s_comps.intersection(r_comps) or (s_comps == set() and r_comps == set()):
deleted_from[source.file] = 0
source.comps = list(s_comps.difference(r_comps))
if not source.comps:
Expand Down Expand Up @@ -2726,7 +2744,10 @@ def mod_repo(repo, saltenv="base", aptkey=True, **kwargs):
'cannot parse "ppa:" style repo definitions: {}'.format(repo)
)

sources = SourcesList()
if HAS_DEB822:
sources = SourcesList(deb822=True)
else:
sources = SourcesList()
if kwargs.get("consolidate", False):
# attempt to de-dup and consolidate all sources
# down to entries in sources.list
Expand All @@ -2743,11 +2764,14 @@ def mod_repo(repo, saltenv="base", aptkey=True, **kwargs):

repos = []
for source in sources:
if HAS_APT:
if HAS_APT and not HAS_DEB822:
_, invalid, _, _ = _invalid(source.line)
if not invalid:
repos.append(source)
else:
if HAS_DEB822 and source.types == [""]:
# most probably invalid or comment line
continue
repos.append(source)

mod_source = None
Expand Down Expand Up @@ -2928,7 +2952,18 @@ def mod_repo(repo, saltenv="base", aptkey=True, **kwargs):
kwargs["comments"] = salt.utils.pkg.deb.combine_comments(kwargs["comments"])

if not mod_source:
mod_source = SourceEntry(repo)
if HAS_DEB822:
apt_source_file = kwargs.get("file")
section = _deb822.Section("")
section["Types"] = repo_type
section["URIs"] = repo_uri
section["Suites"] = repo_dist
section["Components"] = " ".join(repo_comps)
if kwargs.get("trusted") == True or kwargs.get("Trusted") == True:
section["Trusted"] = "yes"
mod_source = Deb822SourceEntry(section, apt_source_file)
else:
mod_source = SourceEntry(repo)
if "comments" in kwargs:
mod_source.comment = kwargs["comments"]
sources.list.append(mod_source)
Expand All @@ -2950,7 +2985,8 @@ def mod_repo(repo, saltenv="base", aptkey=True, **kwargs):

if mod_source.uri != repo_uri:
mod_source.uri = repo_uri
mod_source.line = mod_source.str()
if not HAS_DEB822:
mod_source.line = mod_source.str()

sources.save()
# on changes, explicitly refresh
Expand Down Expand Up @@ -3055,7 +3091,10 @@ def _expand_repo_def(os_name, os_codename=None, **kwargs):
if kwarg in kwargs:
setattr(source_entry, kwarg, kwargs[kwarg])

source_list = SourcesList()
if HAS_DEB822:
source_list = SourcesList(deb822=True)
else:
source_list = SourcesList()
kwargs = {}
if not HAS_APT:
signedby = source_entry.signedby
Expand Down

0 comments on commit bf1b99e

Please sign in to comment.