]> git.phdru.name Git - xsetbg.git/blob - rescan_fs.py
Version 5.0.2
[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 __author__ = "Oleg Broytman <phd@phdru.name>"
11 __copyright__ = "Copyright (C) 2015 PhiloSoft Design"
12 __license__ = "GNU GPL"
13
14 import os
15 from sqlobject import SQLObjectNotFound
16 from sqlobject.sqlbuilder import Insert, Update, Delete
17 from xsetbg_conf import xsetbg_dir, xsetbg_conf
18 from xsetbg_db import recreate_db, SQLiteMassInsert
19
20 if xsetbg_conf.has_option("images", "directory") or \
21         xsetbg_conf.has_option("images", "directory0") or \
22         xsetbg_conf.has_option("images", "directory1"):
23     image_dirs = []
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"))
30     i = 2
31     while True:
32         option = "directory%d" % i
33         if xsetbg_conf.has_option("images", option):
34             image_dirs.append(xsetbg_conf.get("images", option))
35             i += 1
36         else:
37             break
38 else:
39     image_dirs = ["images"]
40
41 image_dirs = [
42     os.path.join(xsetbg_dir, os.path.expandvars(os.path.expanduser(dirname)))
43     for dirname in image_dirs
44 ]
45
46 fs_encoding = xsetbg_conf.get("images", "fs_encoding")
47 xsetbg_db = recreate_db()
48 count_new = count_old = count_del = 0
49
50 with SQLiteMassInsert() as txn:
51     for image_dir in image_dirs:
52         # List images in all subdirectories
53         for dirpath, dirs, files in os.walk(image_dir):
54             for file in files:
55                 full_name = os.path.join(dirpath, file).decode(fs_encoding)
56                 try:
57                     row = xsetbg_db.byFull_name(full_name)
58                 except SQLObjectNotFound:
59                     values = {'full_name': full_name.encode('utf-8'),
60                               'flag': True}
61                     query = txn.sqlrepr(Insert(xsetbg_db.sqlmeta.table,
62                                                values=values))
63                     txn.query(query)
64                     count_new += 1
65                 else:
66                     row.flag = True
67                     count_old += 1
68     count_del = xsetbg_db.select('flag IS NULL').count()
69     query = txn.sqlrepr(Delete(xsetbg_db.sqlmeta.table, 'flag IS NULL'))
70     txn.query(query)
71     query = txn.sqlrepr(Update(xsetbg_db.sqlmeta.table, {'flag': None}))
72     txn.query(query)
73
74 print "New images:", count_new
75 print "Existing images:", count_old
76 print "Removed images:", count_del