diff --git a/bundles/apt/items.py b/bundles/apt/items.py index 00bbcca..1040470 100644 --- a/bundles/apt/items.py +++ b/bundles/apt/items.py @@ -31,15 +31,17 @@ actions = { hosts = {} -for source in node.metadata.get('apt/sources'): - host = repo.libs.apt.AptSource(source).url.hostname +for source_string in node.metadata.get('apt/sources'): + source = repo.libs.apt.AptSource(source_string) hosts\ - .setdefault(host, set())\ + .setdefault(source.url.hostname, set())\ .add(source) for host, sources in hosts.items(): files[f'/etc/apt/sources.list.d/{host}.list'] = { - 'content': '\n'.join(sorted(sources)).format( + 'content': '\n'.join( + str(source) for source in sorted(sources) + ).format( release=node.metadata.get('os_release') ), 'triggers': { diff --git a/libs/apt.py b/libs/apt.py index a673798..37bf03a 100644 --- a/libs/apt.py +++ b/libs/apt.py @@ -1,11 +1,13 @@ # https://manpages.debian.org/jessie/apt/sources.list.5.de.html from urllib.parse import urlparse -from re import match, search, sub +from re import search, sub +from functools import total_ordering +@total_ordering class AptSource(): def __init__(self, string): - if match(r'\[.*\]', string): + if search(r'\[.*\]', string): self.options = { k:v.split('=') for k,v in ( e.split('=') for e in search(r'\[(.*)\]', string)[1].split() @@ -13,13 +15,14 @@ class AptSource(): } else: self.options = {} + parts = sub(r'\[.*\]', '', string).split() self.type = parts[0] self.url = urlparse(parts[1]) self.suite = parts[2] self.components = parts[3:] - def render(self): + def __str__(self): parts = [ self.type, self.url.geturl(), @@ -42,6 +45,20 @@ class AptSource(): return ' '.join(parts) + + def __eq__(self, other): + return str(self) == str(other) + + def __lt__(self, other): + return str(self) < str(other) + + def __hash__(self): + return hash(str(self)) + + def __repr__(self): + return f"{type(self).__name__}('{str(self)}')" + + # source = AptSource('deb [arch=amd64 trusted=true] http://deb.debian.org/debian buster-backports main contrib non-free') # print(repr(source)) # print(source.type) @@ -50,4 +67,4 @@ class AptSource(): # print(source.url) # print(source.suite) # print(source.components) -# print(source.render()) +# print(str(source))