From d304449cb1d0c27affb136734a10143f7898cf84 Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Wed, 16 Jun 2021 10:57:55 +1000 Subject: [PATCH] Adding helper method to remove text files that are not in the index --- backend/store.py | 16 ++++++++++++++++ changedetection.py | 11 ++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) mode change 100644 => 100755 changedetection.py diff --git a/backend/store.py b/backend/store.py index d022f2da..60aed844 100644 --- a/backend/store.py +++ b/backend/store.py @@ -347,3 +347,19 @@ class ChangeDetectionStore: self.sync_to_json() time.sleep(3) + # Go through the datastore path and remove any snapshots that are not mentioned in the index + # This usually is not used, but can be handy. + def remove_unused_snapshots(self): + print ("Removing snapshots from datastore that are not in the index..") + + index=[] + for uuid in self.data['watching']: + for id in self.data['watching'][uuid]['history']: + index.append(self.data['watching'][uuid]['history'][str(id)]) + + import pathlib + # Only in the sub-directories + for item in pathlib.Path(self.datastore_path).rglob("*/*txt"): + if not str(item) in index: + print ("Removing",item) + os.unlink(item) diff --git a/changedetection.py b/changedetection.py old mode 100644 new mode 100755 index e5cdb280..2e349eff --- a/changedetection.py +++ b/changedetection.py @@ -35,12 +35,13 @@ def init_app_secret(datastore_path): def main(argv): ssl_mode = False port = 5000 + do_cleanup = False # Must be absolute so that send_from_directory doesnt try to make it relative to backend/ datastore_path = os.path.join(os.getcwd(), "datastore") try: - opts, args = getopt.getopt(argv, "sd:p:", "purge") + opts, args = getopt.getopt(argv, "csd:p:", "port") except getopt.GetoptError: print('backend.py -s SSL enable -p [port] -d [datastore path]') sys.exit(2) @@ -60,12 +61,20 @@ def main(argv): if opt == '-d': datastore_path = arg + # Cleanup (remove text files that arent in the index) + if opt == '-c': + do_cleanup = True + # isnt there some @thingy to attach to each route to tell it, that this route needs a datastore app_config = {'datastore_path': datastore_path} datastore = store.ChangeDetectionStore(datastore_path=app_config['datastore_path']) app = backend.changedetection_app(app_config, datastore) + # Go into cleanup mode + if do_cleanup: + datastore.remove_unused_snapshots() + app.config['datastore_path'] = datastore_path app.secret_key = init_app_secret(app_config['datastore_path'])