]> git.phdru.name Git - xsetbg.git/commitdiff
Add rescan_fs.py: scan images directories and update DB
authorOleg Broytman <phd@phdru.name>
Sat, 25 Jul 2015 16:50:01 +0000 (19:50 +0300)
committerOleg Broytman <phd@phdru.name>
Sat, 25 Jul 2015 16:51:46 +0000 (19:51 +0300)
rescan_fs.py [new file with mode: 0755]

diff --git a/rescan_fs.py b/rescan_fs.py
new file mode 100755 (executable)
index 0000000..82dbcf8
--- /dev/null
@@ -0,0 +1,76 @@
+#! /usr/bin/env python
+"""Rescan filesystem and update database
+
+Rescan images directories; remove unknown images from DB; add new images.
+
+This file is a part of XSetBg.
+
+"""
+
+__author__ = "Oleg Broytman <phd@phdru.name>"
+__copyright__ = "Copyright (C) 2015 PhiloSoft Design"
+__license__ = "GNU GPL"
+
+import os
+from sqlobject import SQLObjectNotFound
+from sqlobject.sqlbuilder import Insert, Update, Delete
+from xsetbg_conf import xsetbg_dir, xsetbg_conf
+from xsetbg_db import recreate_db, SQLiteMassInsert
+
+if xsetbg_conf.has_option("images", "directory") or \
+        xsetbg_conf.has_option("images", "directory0") or \
+        xsetbg_conf.has_option("images", "directory1"):
+    image_dirs = []
+    if xsetbg_conf.has_option("images", "directory"):
+        image_dirs.append(xsetbg_conf.get("images", "directory"))
+    if xsetbg_conf.has_option("images", "directory0"):
+        image_dirs.append(xsetbg_conf.get("images", "directory0"))
+    if xsetbg_conf.has_option("images", "directory1"):
+        image_dirs.append(xsetbg_conf.get("images", "directory1"))
+    i = 2
+    while True:
+        option = "directory%d" % i
+        if xsetbg_conf.has_option("images", option):
+            image_dirs.append(xsetbg_conf.get("images", option))
+            i += 1
+        else:
+            break
+else:
+    image_dirs = ["images"]
+
+image_dirs = [
+    os.path.join(xsetbg_dir, os.path.expandvars(os.path.expanduser(dirname)))
+    for dirname in image_dirs
+]
+
+fs_encoding = xsetbg_conf.get("images", "fs_encoding")
+xsetbg_db = recreate_db()
+count_new = count_old = count_del = 0
+
+with SQLiteMassInsert() as txn:
+    for image_dir in image_dirs:
+        # List images in all subdirectories
+        for dirpath, dirs, files in os.walk(image_dir):
+            for file in files:
+                full_name = os.path.join(dirpath, file).decode(fs_encoding)
+                try:
+                    row = xsetbg_db.byFull_name(full_name)
+                except SQLObjectNotFound:
+                    values = {'full_name': full_name.encode('utf-8'),
+                              'flag': True}
+                    query = txn.sqlrepr(Insert(xsetbg_db.sqlmeta.table,
+                                               values=values))
+                    txn.query(query)
+                    count_new += 1
+                else:
+                    row.flag = True
+                    count_old += 1
+    count_del = xsetbg_db.select('flag IS NULL').count()
+    query = txn.sqlrepr(Delete(xsetbg_db.sqlmeta.table, 'flag IS NULL'))
+    txn.query(query)
+    query = txn.sqlrepr(Update(xsetbg_db.sqlmeta.table, {'flag': None}))
+    txn.query(query)
+
+print "New images:", count_new
+print "Existing images:", count_old
+print "Removed images:", count_del