works, I think

This commit is contained in:
Brennen Raimer
2021-10-11 21:33:35 -04:00
parent c4b0d2be07
commit 3e1e820079

24
restore_apt_repos.py Normal file → Executable file
View File

@@ -1,10 +1,11 @@
#!/usr/bin/env python3
from operator import attrgetter, itemgetter, methodcaller
from pathlib import Path
from shutil import move
from aptdaemon.client import AptClient
from aptsources.distro import get_distro
from aptsources.sourceslist import SourcesList
from requests import get, RequestException
from toolz import complement, compose, count, curry, excepts, groupby
from toolz.curried.operator import eq
@@ -55,8 +56,6 @@ if count(sources_list_d.glob("*.original.save")) != count(
else:
# if the comment on a valid source line does not match the current OS
if not all(map(is_current, comments)):
# create an apt DBus client
client = AptClient()
# create some temp backup files of the repo .list files
temp_suffix = sources.backup()
# restore the original versions of the .list files and refresh data
@@ -67,7 +66,7 @@ else:
for source in filter(is_valid, sources):
# get the release and codename which are the last 2 words in the comment
orig_release, orig_codename = source.comment.split(" ")[-2:]
# if they don't match the current OS, update them, save, and refresh
if (RELEASE != orig_release) or (CODENAME != orig_codename):
if source.dist == orig_release:
@@ -87,14 +86,19 @@ else:
sources.refresh()
# test that the repo, updated for the current OS, is still valid by
# refreshing the cache. An error indicates that it is not valid. If
# getting a specific URL. An error indicates that it is not valid. If
# an error is detected, restore the temporary backup and refresh using
# error handler defined above, otherwise leave updated .list file as-is
client.update_cache(
source.file,
True,
error_handler=restore_backup(source.file, temp_suffix),
)
try:
response = get(f"{source.uri}/dists/{source.dist}/{first(source.comps)}/binary-amd64/Release")
except RequestException:
move(source.file+temp_suffix, source.file)
else:
if source.status_code >= 400:
move(source.file+temp_suffix, source.file)
finally:
sources.refresh()
# cleanup any remaining temp backup .list files
for file in sources_list_d.glob(f"*{temp_suffix}"):