works, I think

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

22
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 operator import attrgetter, itemgetter, methodcaller
from pathlib import Path from pathlib import Path
from shutil import move from shutil import move
from aptdaemon.client import AptClient
from aptsources.distro import get_distro from aptsources.distro import get_distro
from aptsources.sourceslist import SourcesList from aptsources.sourceslist import SourcesList
from requests import get, RequestException
from toolz import complement, compose, count, curry, excepts, groupby from toolz import complement, compose, count, curry, excepts, groupby
from toolz.curried.operator import eq from toolz.curried.operator import eq
@@ -55,8 +56,6 @@ if count(sources_list_d.glob("*.original.save")) != count(
else: else:
# if the comment on a valid source line does not match the current OS # if the comment on a valid source line does not match the current OS
if not all(map(is_current, comments)): if not all(map(is_current, comments)):
# create an apt DBus client
client = AptClient()
# create some temp backup files of the repo .list files # create some temp backup files of the repo .list files
temp_suffix = sources.backup() temp_suffix = sources.backup()
# restore the original versions of the .list files and refresh data # restore the original versions of the .list files and refresh data
@@ -87,14 +86,19 @@ else:
sources.refresh() sources.refresh()
# test that the repo, updated for the current OS, is still valid by # 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 # an error is detected, restore the temporary backup and refresh using
# error handler defined above, otherwise leave updated .list file as-is # error handler defined above, otherwise leave updated .list file as-is
client.update_cache( try:
source.file, response = get(f"{source.uri}/dists/{source.dist}/{first(source.comps)}/binary-amd64/Release")
True, except RequestException:
error_handler=restore_backup(source.file, temp_suffix), 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 # cleanup any remaining temp backup .list files
for file in sources_list_d.glob(f"*{temp_suffix}"): for file in sources_list_d.glob(f"*{temp_suffix}"):