From: Christian Heller <c.heller@plomlompom.de>
Date: Sun, 15 Dec 2024 08:05:09 +0000 (+0100)
Subject: Slightly reorganize sync script.
X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/static/%7B%7B%20web_path%20%7D%7D/%7B%7Bprefix%7D%7D/day?a=commitdiff_plain;h=52eccb14d9d5ab078b65e4bbb5dafb2f152d66d9;p=ytplom

Slightly reorganize sync script.
---

diff --git a/src/sync.py b/src/sync.py
index 63108b7..7de9fe1 100755
--- a/src/sync.py
+++ b/src/sync.py
@@ -69,13 +69,8 @@ def sync_relations(host_names: tuple[str, str],
         yt_result.save_to_query(dbs[1], q.id_)
 
 
-def main():
-    """Connect to remote, sync local+remote DBs, + downloads where missing."""
-    config = Config()
-    ssh = SSHClient()
-    ssh.load_system_host_keys()
-    ssh.connect(config.remote)
-    scp = SCPClient(ssh.get_transport())
+def sync_dbs(scp: SCPClient) -> None:
+    """Download remote DB, run sync_(objects|relations), put remote DB back."""
     scp.get(PATH_DB, PATH_DB_REMOTE)
     with DbConn(PATH_DB) as db_local, DbConn(PATH_DB_REMOTE) as db_remote:
         for cls in (QuotaLog, YoutubeQuery, YoutubeVideo, VideoFile):
@@ -87,6 +82,10 @@ def main():
         db_local.commit()
     scp.put(PATH_DB_REMOTE, PATH_DB)
     PATH_DB_REMOTE.unlink()
+
+
+def fill_missing(scp: SCPClient, config: Config) -> None:
+    """Between config.host and .remote, fill files listed in as missing."""
     missings = []
     for host, port in ((config.remote, config.port_remote),
                        (config.host, config.port)):
@@ -101,7 +100,17 @@ def main():
             full_path = PATH_DOWNLOADS.joinpath(path)
             print(f'SYNC: sending {direction} file {path}')
             mover(full_path, full_path)
-    scp.close()
+
+
+def main():
+    """Connect to remote, sync local+remote DBs, + downloads where missing."""
+    config = Config()
+    ssh = SSHClient()
+    ssh.load_system_host_keys()
+    ssh.connect(config.remote)
+    with SCPClient(ssh.get_transport()) as scp:
+        sync_dbs(scp)
+        fill_missing(scp, config)
 
 
 if __name__ == '__main__':