diff --git a/shadowmire.py b/shadowmire.py index 27442fe..0f5fa42 100644 --- a/shadowmire.py +++ b/shadowmire.py @@ -9,11 +9,11 @@ from pathlib import Path from html.parser import HTMLParser import logging import html -import requests import argparse import os from contextlib import contextmanager import sqlite3 +import requests logger = logging.getLogger(__name__) @@ -27,6 +27,7 @@ class LocalVersionKV: As it would have consistency issue if it's writing while downstream is downloading the database. An extra "jsonpath" is used, to store kv results when necessary. """ + def __init__(self, dbpath: Path, jsonpath: Path) -> None: self.conn = sqlite3.connect(dbpath) self.jsonpath = jsonpath @@ -54,18 +55,24 @@ class LocalVersionKV: kvs = [(k, v) for k, v in d.items()] cur.executemany(self.INSERT_SQL, kvs) self.conn.commit() - + def remove(self, key: str) -> None: cur = self.conn.cursor() cur.execute("DELETE FROM local WHERE key = ?", (key,)) self.conn.commit() - + def nuke(self, commit: bool = True) -> None: cur = self.conn.cursor() cur.execute("DELETE FROM local") if commit: self.conn.commit() + def keys(self) -> list[str]: + cur = self.conn.cursor() + res = cur.execute("SELECT key FROM local") + rows = res.fetchall() + return [row[0] for row in rows] + def dump(self) -> dict[str, int]: cur = self.conn.cursor() res = cur.execute("SELECT key, value FROM local") @@ -294,7 +301,9 @@ class Plan: class SyncBase: - def __init__(self, basedir: Path, local_db: LocalVersionKV, sync_packages: bool = False) -> None: + def __init__( + self, basedir: Path, local_db: LocalVersionKV, sync_packages: bool = False + ) -> None: self.basedir = basedir self.local_db = local_db self.simple_dir = basedir / "simple" @@ -364,7 +373,7 @@ class SyncBase: raise NotImplementedError def finalize(self) -> None: - assert self.remote + local_names = self.local_db.keys() # generate index.html at basedir index_path = self.basedir / "simple" / "index.html" # modified from bandersnatch @@ -378,17 +387,16 @@ class SyncBase: f.write("
\n") # This will either be the simple dir, or if we are using index # directory hashing, a list of subdirs to process. - for pkg in self.remote: + for pkg in local_names: # We're really trusty that this is all encoded in UTF-8. :/ f.write(f' {pkg}