home · contact · privacy
Slightly reorganize sync script. master
authorChristian Heller <c.heller@plomlompom.de>
Sun, 15 Dec 2024 08:05:09 +0000 (09:05 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Sun, 15 Dec 2024 08:05:09 +0000 (09:05 +0100)
src/sync.py

index 63108b722194f198ff4d374561b1f223df78a8b3..7de9fe1e7f08020dec604a0cfeb20a2c2cc584de 100755 (executable)
@@ -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__':