From: Oleg Broytman Date: Sat, 25 Jul 2015 16:50:01 +0000 (+0300) Subject: Add rescan_fs.py: scan images directories and update DB X-Git-Tag: v5.0.0~6 X-Git-Url: https://git.phdru.name/?p=xsetbg.git;a=commitdiff_plain;h=d972f7582e458cbfbbf6a0eca72533ada9ad9324 Add rescan_fs.py: scan images directories and update DB --- diff --git a/rescan_fs.py b/rescan_fs.py new file mode 100755 index 0000000..82dbcf8 --- /dev/null +++ b/rescan_fs.py @@ -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 " +__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