]> git.phdru.name Git - xsetbg.git/blob - rescan_fs.py
Fix(DB): Fix column encoding
[xsetbg.git] / rescan_fs.py
1 #! /usr/bin/env python
2 """Rescan filesystem and update database
3
4 Rescan images directories; remove unknown images from DB; add new images.
5
6 This file is a part of XSetBg.
7
8 """
9
10 import os
11 import subprocess
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
16
17 if xsetbg_conf.has_option("images", "directory") or \
18         xsetbg_conf.has_option("images", "directory0") or \
19         xsetbg_conf.has_option("images", "directory1"):
20     image_dirs = []
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"))
27     i = 2
28     while True:
29         option = "directory%d" % i
30         if xsetbg_conf.has_option("images", option):
31             image_dirs.append(xsetbg_conf.get("images", option))
32             i += 1
33         else:
34             break
35 else:
36     image_dirs = ["images"]
37
38 image_dirs = [
39     os.path.join(xsetbg_dir, os.path.expandvars(os.path.expanduser(dirname)))
40     for dirname in image_dirs
41 ]
42
43 fs_encoding = xsetbg_conf.get("images", "fs_encoding")
44 xsetbg_db = recreate_db()
45 count_new = count_old = count_del = 0
46
47 NULL = open(os.devnull, 'w')
48
49
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)
54
55
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):
60             for file in files:
61                 full_name = os.path.join(dirpath, file).decode(fs_encoding)
62                 try:
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),
67                               'flag': True,
68                               }
69                     query = txn.sqlrepr(Insert(xsetbg_db.sqlmeta.table,
70                                                values=values))
71                     txn.query(query)
72                     count_new += 1
73                 else:
74                     if row.is_image is None:
75                         row.is_image = is_image(full_name)
76                     row.flag = True
77                     count_old += 1
78     count_del = xsetbg_db.select('flag IS NULL').count()
79     query = txn.sqlrepr(Delete(xsetbg_db.sqlmeta.table, 'flag IS NULL'))
80     txn.query(query)
81     query = txn.sqlrepr(Update(xsetbg_db.sqlmeta.table, {'flag': None}))
82     txn.query(query)
83
84 NULL.close()
85 print "New images:", count_new
86 print "Existing images:", count_old
87 print "Removed images:", count_del