X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=rescan_fs.py;h=9688de941cbb4831d35947261f03b238783800ea;hb=8c29b392cb63442b52c6252c3a8418ff1677ffb3;hp=07d3fa31c07d6188353941416c4d69e709eb7c28;hpb=cdffc4b36468a4d72497c25ec4a3bf1f54fe1b3f;p=xsetbg.git diff --git a/rescan_fs.py b/rescan_fs.py index 07d3fa3..9688de9 100755 --- a/rescan_fs.py +++ b/rescan_fs.py @@ -1,4 +1,4 @@ -#! /usr/bin/env python +#! /usr/bin/env python3 """Rescan filesystem and update database Rescan images directories; remove unknown images from DB; add new images. @@ -8,8 +8,12 @@ This file is a part of XSetBg. """ import os +import subprocess + from sqlobject import SQLObjectNotFound from sqlobject.sqlbuilder import Insert, Update, Delete +from m_lib.defenc import default_encoding + from xsetbg_conf import xsetbg_dir, xsetbg_conf from xsetbg_db import recreate_db, SQLiteMassInsert @@ -43,22 +47,37 @@ fs_encoding = xsetbg_conf.get("images", "fs_encoding") xsetbg_db = recreate_db() count_new = count_old = count_del = 0 +NULL = open(os.devnull, 'w') + + +def is_image(full_path): + # Run `identify` from ImageMagic; convert retcode to bool + return not subprocess.call(['identify', full_path.encode(fs_encoding)], + stdout=NULL, stderr=subprocess.STDOUT) + + with SQLiteMassInsert() as txn: for image_dir in image_dirs: # List images in all subdirectories for dirpath, dirs, files in os.walk(image_dir): for file in files: - full_name = os.path.join(dirpath, file).decode(fs_encoding) + if default_encoding != fs_encoding: + file = file.encode().decode(fs_encoding) + full_name = os.path.join(dirpath, file) try: row = xsetbg_db.byFull_name(full_name) except SQLObjectNotFound: values = {'full_name': full_name.encode('utf-8'), - 'flag': True} + 'is_image': is_image(full_name), + 'flag': True, + } query = txn.sqlrepr(Insert(xsetbg_db.sqlmeta.table, values=values)) txn.query(query) count_new += 1 else: + if row.is_image is None: + row.is_image = is_image(full_name) row.flag = True count_old += 1 count_del = xsetbg_db.select('flag IS NULL').count() @@ -67,6 +86,7 @@ with SQLiteMassInsert() as txn: query = txn.sqlrepr(Update(xsetbg_db.sqlmeta.table, {'flag': None})) txn.query(query) -print "New images:", count_new -print "Existing images:", count_old -print "Removed images:", count_del +NULL.close() +print("New images:", count_new) +print("Existing images:", count_old) +print("Removed images:", count_del)