Extend documention and use some more pythonier loops

This commit is contained in:
Stephan Kulow 2022-11-02 13:29:18 +01:00
parent 05a5e6aea7
commit a1ead29734

View File

@ -1,5 +1,8 @@
from __future__ import annotations
import logging import logging
from hashlib import md5 from hashlib import md5
from typing import Optional
from lib.db import DB from lib.db import DB
from lib.request import Request from lib.request import Request
@ -204,18 +207,24 @@ class DBRevision:
self._files.sort(key=lambda x: x["name"]) self._files.sort(key=lambda x: x["name"])
return self._files return self._files
def calc_delta(self, db: DB, current_rev=None): def calc_delta(self, db: DB, current_rev: Optional[DBRevision]):
"""Calculate the list of files to download and to delete""" """Calculate the list of files to download and to delete.
old_files = dict() Param current_rev is the revision that's currently checked out.
If it's None, the repository is empty.
"""
to_download = [] to_download = []
to_delete = [] to_delete = []
if current_rev: if current_rev:
for entry in current_rev.files_list(db): old_files = {
old_files[entry["name"]] = f"{entry['md5']}-{entry['size']}" e["name"]: f"{entry['md5']}-{entry['size']}"
for e in current_rev.files_list(db)
}
else:
old_files = dict()
for entry in self.files_list(db): for entry in self.files_list(db):
if old_files.get(entry["name"], "") != f"{entry['md5']}-{entry['size']}": if old_files.get(entry["name"]) != f"{entry['md5']}-{entry['size']}":
logging.debug(f"Download {entry['name']}") logging.debug(f"Download {entry['name']}")
to_download.append(entry["name"]) to_download.append((entry["name"], entry["md5"]))
old_files.pop(entry["name"], None) old_files.pop(entry["name"], None)
for entry in old_files.keys(): for entry in old_files.keys():
logging.debug(f"Delete {entry}") logging.debug(f"Delete {entry}")