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