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 23, 2024
1 parent 7217fcc commit d3c45d2
Showing 1 changed file with 80 additions and 14 deletions.
94 changes: 80 additions & 14 deletions salt/modules/aptpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@
except ImportError:
HAS_APT = False

HAS_DEB822 = False

if HAS_APT:
try:
from aptsources.sourceslist import Deb822SourceEntry, _deb822

HAS_DEB822 = True
except ImportError:
pass

try:
import apt_pkg

Expand Down Expand Up @@ -1907,28 +1917,52 @@ def list_repos(**kwargs):
salt '*' pkg.list_repos disabled=True
"""
repos = {}
sources = SourcesList()
for source in sources.list:
if HAS_DEB822:
sources = SourcesList(deb822=True)
else:
sources = SourcesList()
for source in sources:
if _skip_source(source):
continue
if not HAS_APT:
signedby = source.signedby
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_comps = getattr(source, "comps", [])
repo_dists = source.dist.split(" ")
repo["comps"] = repo_comps
repo["disabled"] = source.disabled
repo["enabled"] = not repo[
"disabled"
] # This is for compatibility with the other modules
repo["dist"] = source.dist
repo["dist"] = repo_dists.pop(0)
repo["type"] = source.type
repo["uri"] = source.uri
repo["line"] = source.line.strip()
if "Types: " in source.line and "\n" in source.line:
repo["line"] = (
f"{source.type} {source.uri} {repo['dist']} {' '.join(repo_comps)}"
)
else:
repo["line"] = source.line.strip()
repo["architectures"] = getattr(source, "architectures", [])
repo["signedby"] = signedby
repos.setdefault(source.uri, []).append(repo)
if len(repo_dists):
for dist in repo_dists:
repo_copy = repo.copy()
repo_copy["dist"] = dist
if "Types: " in source.line and "\n" in source.line:
repo_copy["line"] = (
f"{source.type} {source.uri} {repo_copy['dist']} {' '.join(repo_comps)}"
)
repos[source.uri].append(repo_copy)
return repos


Expand Down Expand Up @@ -2047,7 +2081,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 +2112,9 @@ 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 +2765,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 +2785,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 @@ -2906,10 +2951,11 @@ def mod_repo(repo, saltenv="base", aptkey=True, **kwargs):
# and the resulting source line. The idea here is to ensure
# we are not returning bogus data because the source line
# has already been modified on a previous run.
apt_source_dists = apt_source.dist.split(" ")
repo_matches = (
apt_source.type == repo_type
and apt_source.uri.rstrip("/") == repo_uri.rstrip("/")
and apt_source.dist == repo_dist
and repo_dist in apt_source_dists
)
kw_matches = apt_source.dist == kw_dist and apt_source.type == kw_type

Expand All @@ -2928,7 +2974,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 +3007,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 All @@ -2962,15 +3020,20 @@ def mod_repo(repo, saltenv="base", aptkey=True, **kwargs):
else:
signedby = _get_opts(repo)["signedby"].get("value", "")

repo_source_line = mod_source.line
if "Types: " in repo_source_line and "\n" in repo_source_line:
repo_source_line = f"{mod_source.type} {mod_source.uri} {repo_dist} {' '.join(mod_source.comps)}"

return {
repo: {
"architectures": getattr(mod_source, "architectures", []),
"dist": mod_source.dist,
"comps": mod_source.comps,
"disabled": mod_source.disabled,
"file": mod_source.file,
"type": mod_source.type,
"uri": mod_source.uri,
"line": mod_source.line,
"line": repo_source_line,
"signedby": signedby,
}
}
Expand Down Expand Up @@ -3055,7 +3118,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 d3c45d2

Please sign in to comment.