1 #! /usr/bin/env python3
2 """Rescan filesystem and update database
4 Rescan images directories; remove unknown images from DB; add new images.
6 This file is a part of XSetBg.
13 from sqlobject import SQLObjectNotFound
14 from sqlobject.sqlbuilder import Insert, Update, Delete
15 from m_lib.defenc import default_encoding
17 from xsetbg_conf import xsetbg_dir, xsetbg_conf
18 from xsetbg_db import recreate_db, SQLiteMassInsert
20 if xsetbg_conf.has_option("images", "directory") or \
21 xsetbg_conf.has_option("images", "directory0") or \
22 xsetbg_conf.has_option("images", "directory1"):
24 if xsetbg_conf.has_option("images", "directory"):
25 image_dirs.append(xsetbg_conf.get("images", "directory"))
26 if xsetbg_conf.has_option("images", "directory0"):
27 image_dirs.append(xsetbg_conf.get("images", "directory0"))
28 if xsetbg_conf.has_option("images", "directory1"):
29 image_dirs.append(xsetbg_conf.get("images", "directory1"))
32 option = "directory%d" % i
33 if xsetbg_conf.has_option("images", option):
34 image_dirs.append(xsetbg_conf.get("images", option))
39 image_dirs = ["images"]
42 os.path.join(xsetbg_dir, os.path.expandvars(os.path.expanduser(dirname)))
43 for dirname in image_dirs
46 fs_encoding = xsetbg_conf.get("images", "fs_encoding")
47 xsetbg_db = recreate_db()
48 count_new = count_old = count_del = 0
50 NULL = open(os.devnull, 'w')
53 def is_image(full_path):
54 # Run `identify` from ImageMagic; convert retcode to bool
55 return not subprocess.call(['identify', full_path.encode(fs_encoding)],
56 stdout=NULL, stderr=subprocess.STDOUT)
59 with SQLiteMassInsert() as txn:
60 for image_dir in image_dirs:
61 # List images in all subdirectories
62 for dirpath, dirs, files in os.walk(image_dir):
64 if default_encoding != fs_encoding:
65 file = file.encode().decode(fs_encoding)
66 full_name = os.path.join(dirpath, file)
68 row = xsetbg_db.byFull_name(full_name)
69 except SQLObjectNotFound:
70 values = {'full_name': full_name,
71 'is_image': is_image(full_name),
74 query = txn.sqlrepr(Insert(xsetbg_db.sqlmeta.table,
79 if row.is_image is None:
80 row.is_image = is_image(full_name)
83 count_del = xsetbg_db.select('flag IS NULL').count()
84 query = txn.sqlrepr(Delete(xsetbg_db.sqlmeta.table, 'flag IS NULL'))
86 query = txn.sqlrepr(Update(xsetbg_db.sqlmeta.table, {'flag': None}))
90 print("New images:", count_new)
91 print("Existing images:", count_old)
92 print("Removed images:", count_del)