From a1ead29734ad77f619bb2887fdee95c3813f3c05 Mon Sep 17 00:00:00 2001 From: Stephan Kulow Date: Wed, 2 Nov 2022 13:29:18 +0100 Subject: [PATCH] Extend documention and use some more pythonier loops --- lib/db_revision.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/db_revision.py b/lib/db_revision.py index bea541d..4458fe7 100644 --- a/lib/db_revision.py +++ b/lib/db_revision.py @@ -1,5 +1,8 @@ +from __future__ import annotations + import logging from hashlib import md5 +from typing import Optional from lib.db import DB from lib.request import Request @@ -204,18 +207,24 @@ class DBRevision: self._files.sort(key=lambda x: x["name"]) return self._files - def calc_delta(self, db: DB, current_rev=None): - """Calculate the list of files to download and to delete""" - old_files = dict() + def calc_delta(self, db: DB, current_rev: Optional[DBRevision]): + """Calculate the list of files to download and to delete. + Param current_rev is the revision that's currently checked out. + If it's None, the repository is empty. + """ to_download = [] to_delete = [] if current_rev: - for entry in current_rev.files_list(db): - old_files[entry["name"]] = f"{entry['md5']}-{entry['size']}" + old_files = { + 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): - 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']}") - to_download.append(entry["name"]) + to_download.append((entry["name"], entry["md5"])) old_files.pop(entry["name"], None) for entry in old_files.keys(): logging.debug(f"Delete {entry}")