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.
12 from sqlobject import SQLObjectNotFound
13 from sqlobject.sqlbuilder import Insert, Update, Delete
14 from xsetbg_conf import xsetbg_dir, xsetbg_conf
15 from xsetbg_db import recreate_db, SQLiteMassInsert
17 if xsetbg_conf.has_option("images", "directory") or \
18 xsetbg_conf.has_option("images", "directory0") or \
19 xsetbg_conf.has_option("images", "directory1"):
21 if xsetbg_conf.has_option("images", "directory"):
22 image_dirs.append(xsetbg_conf.get("images", "directory"))
23 if xsetbg_conf.has_option("images", "directory0"):
24 image_dirs.append(xsetbg_conf.get("images", "directory0"))
25 if xsetbg_conf.has_option("images", "directory1"):
26 image_dirs.append(xsetbg_conf.get("images", "directory1"))
29 option = "directory%d" % i
30 if xsetbg_conf.has_option("images", option):
31 image_dirs.append(xsetbg_conf.get("images", option))
36 image_dirs = ["images"]
39 os.path.join(xsetbg_dir, os.path.expandvars(os.path.expanduser(dirname)))
40 for dirname in image_dirs
43 fs_encoding = xsetbg_conf.get("images", "fs_encoding")
44 xsetbg_db = recreate_db()
45 count_new = count_old = count_del = 0
47 NULL = open(os.devnull, 'w')
50 def is_image(full_path):
51 # Run `identify` from ImageMagic; convert retcode to bool
52 return not subprocess.call(['identify', full_path],
53 stdout=NULL, stderr=subprocess.STDOUT)
56 with SQLiteMassInsert() as txn:
57 for image_dir in image_dirs:
58 # List images in all subdirectories
59 for dirpath, dirs, files in os.walk(image_dir):
61 full_name = os.path.join(dirpath, file).decode(fs_encoding)
63 row = xsetbg_db.byFull_name(full_name)
64 except SQLObjectNotFound:
65 values = {'full_name': full_name.encode('utf-8'),
66 'is_image': is_image(full_name),
69 query = txn.sqlrepr(Insert(xsetbg_db.sqlmeta.table,
74 if row.is_image is None:
75 row.is_image = is_image(full_name)
78 count_del = xsetbg_db.select('flag IS NULL').count()
79 query = txn.sqlrepr(Delete(xsetbg_db.sqlmeta.table, 'flag IS NULL'))
81 query = txn.sqlrepr(Update(xsetbg_db.sqlmeta.table, {'flag': None}))
85 print("New images:", count_new)
86 print("Existing images:", count_old)
87 print("Removed images:", count_del)