-
Notifications
You must be signed in to change notification settings - Fork 92
/
angrysearch_update_database.py
352 lines (273 loc) · 9.77 KB
/
angrysearch_update_database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Index the drives and create new database replacing the old one.
It respects ignored directories.
Use crontab to run this update periodically, 2 times a day sounds about right
crontab example that executes at noon and at midnight
00 00,12 * * * /usr/share/angrysearch/angrysearch_update_database.py
"""
# pylama:ignore=D103 ## Hide docstring warnings
import os
import sqlite3
import subprocess
import sys
from datetime import datetime
from PyQt5.QtCore import QSettings, QStandardPaths
TEMP_PATH = QStandardPaths.standardLocations(QStandardPaths.TempLocation)[0]
CACHE_PATH = QStandardPaths.standardLocations(QStandardPaths.CacheLocation)[0]
DATABASE_PATH = CACHE_PATH + '/angrysearch/angry_database.db'
# CHECK SCANDIR AVAILABILITY
try:
import scandir
SCANDIR_AVAILABLE = True
except ImportError:
SCANDIR_AVAILABLE = False
# CHECK IF NOTIFICATIONS CAN BE MADE
try:
from gi import require_version
require_version('Gtk', '3.0')
require_version('Notify', '0.7')
from gi.repository import Notify, GdkPixbuf
NOTIFY_AVAILABLE = True
except ImportError:
NOTIFY_AVAILABLE = False
# FIX FOR CRONTAB
if 'DISPLAY' not in os.environ:
os.environ['DISPLAY'] = ':0'
# MORE GLOBAL VARIABLES
LITE = True
PREP_EXCLUDED = []
MOUNTS_NEEDED = []
NOTIFICATIONS_ENABLED = True
START_TIME = datetime.now()
def load_settings():
global LITE
global PREP_EXCLUDED
global MOUNTS_NEEDED
global NOTIFICATIONS_ENABLED
settings = QSettings('angrysearch', 'angrysearch')
if settings.value('angrysearch_lite'):
q = settings.value('angrysearch_lite')
if q.lower() in ['false', 'no', '0', 'n', 'none', 'nope']:
LITE = False
if settings.value('directories_excluded'):
dirs_excluded = settings.value('directories_excluded').strip().split()
for x in dirs_excluded:
y = [k.encode() for k in x.split('/') if k]
z = ''
# IF FULL PATH
if x.startswith('/'):
up = b'/' + b'/'.join(y[:-1])
z = {'case': 1, 'ign': y[-1], 'up': up}
# IF ONLY SINGLE DIRECTORY NAME
elif len(y) == 1:
z = {'case': 2, 'ign': y[-1], 'up': ''}
# IF PARENT/TARGET
elif len(y) == 2:
z = {'case': 3, 'ign': y[-1], 'up': y[-2]}
if z:
PREP_EXCLUDED.append(z)
if settings.value('conditional_mounts_for_autoupdate'):
q = settings.value('conditional_mounts_for_autoupdate').strip().split()
MOUNTS_NEEDED = [x for x in q]
if settings.value('notifications'):
q = settings.value('notifications')
if q.lower() in ['false', 'no', '0', 'n', 'none', 'nope']:
NOTIFICATIONS_ENABLED = False
def test_conditional_mounts_for_autoupdate():
missing_mount = False
missing_mounts_list = []
for x in MOUNTS_NEEDED:
if not os.path.ismount(x):
missing_mount = True
missing_mounts_list.append(x)
if missing_mount is True:
notify_text = 'aborting automatic update'
for x in missing_mounts_list:
notify_text = notify_text + '\n<b>{}</b> missing'.format(x)
show_notification(notify_text)
print('angrysearch: ' + ', '.join(notify_text.split('\n')))
sys.exit(0)
def show_notification(text):
global NOTIFY_AVAILABLE
global NOTIFICATIONS_ENABLED
if NOTIFY_AVAILABLE is False or NOTIFICATIONS_ENABLED is False:
print('angrysearch: Desktop notifications disabled or unavailable')
return
Notify.init('angrysearch')
n = Notify.Notification.new('ANGRYsearch:', text)
possible_image_locations = [
'angrysearch.svg',
'/usr/share/pixmaps/angrysearch.svg',
'/usr/share/angrysearch/angrysearch.svg',
'/opt/angrysearch/angrysearch.svg'
]
for x in possible_image_locations:
if os.path.exists(x):
icon = GdkPixbuf.Pixbuf.new_from_file(x)
n.set_image_from_pixbuf(icon)
break
else:
n.set_property('icon-name', 'drive-harddisk')
n.show()
def crawling_drives():
def error(err):
print(err)
root_dir = b'/'
table = []
dir_list = []
file_list = []
if SCANDIR_AVAILABLE:
ror = scandir
else:
ror = os
for root, dirs, files in ror.walk(root_dir, onerror=error):
dirs.sort()
files.sort()
if root == b'/' and b'proc' in dirs:
dirs.remove(b'proc')
dirs[:] = remove_excluded_dirs(dirs, root, PREP_EXCLUDED)
for dname in dirs:
path = os.path.join(root, dname)
utf_path = path.decode(encoding='utf-8', errors='ignore')
try:
stats = os.lstat(path)
epoch_time = stats.st_mtime.__trunc__()
except:
print('Can\'t access: ' + str(path))
epoch_time = 0
dir_list.append(('1', utf_path, '', epoch_time))
for fname in files:
path = os.path.join(root, fname)
utf_path = path.decode(encoding='utf-8', errors='ignore')
try:
stats = os.lstat(path)
size = stats.st_size
epoch_time = stats.st_mtime.__trunc__()
except:
print('Can\'t access: ' + str(path))
size = 0
epoch_time = 0
file_list.append(('0', utf_path, size, epoch_time))
table = dir_list + file_list
new_database(table)
def crawling_drives_lite():
def error(err):
print(err)
root_dir = b'/'
table = []
dir_list = []
file_list = []
if SCANDIR_AVAILABLE:
ror = scandir
else:
ror = os
for root, dirs, files in ror.walk(root_dir, onerror=error):
dirs.sort()
files.sort()
if root == b'/' and b'proc' in dirs:
dirs.remove(b'proc')
dirs[:] = remove_excluded_dirs(dirs, root, PREP_EXCLUDED)
for dname in dirs:
dir_list.append(('1', os.path.join(root, dname).decode(
encoding='UTF-8', errors='ignore')))
for fname in files:
file_list.append(('0', os.path.join(root, fname).decode(
encoding='UTF-8', errors='ignore')))
table = dir_list + file_list
new_database_lite(table)
def remove_excluded_dirs(dirs, root, to_ignore):
after_exclusion = []
for x in dirs:
for z in to_ignore:
if x == z['ign']:
if z['case'] == 1:
if root == z['up']:
break
elif z['case'] == 2:
break
elif z['case'] == 3:
y = [k for k in root.split(b'/') if k]
if y[-1] == z['up']:
break
else:
after_exclusion.append(x)
return after_exclusion
def new_database(table):
temp_db_path = TEMP_PATH + '/angry_database.db'
if os.path.exists(temp_db_path):
os.remove(temp_db_path)
con = sqlite3.connect(temp_db_path, check_same_thread=False)
cur = con.cursor()
if fts5_pragma_check():
cur.execute('''CREATE VIRTUAL TABLE angry_table
USING fts5(directory, path, size, date)''')
cur.execute('''PRAGMA user_version = 4;''')
else:
cur.execute('''CREATE VIRTUAL TABLE angry_table
USING fts4(directory, path, size, date)''')
cur.execute('''PRAGMA user_version = 3;''')
cur.executemany('''INSERT INTO angry_table VALUES (?, ?, ?, ?)''', table)
con.commit()
replace_old_db_with_new()
def new_database_lite(table):
temp_db_path = TEMP_PATH + '/angry_database.db'
if os.path.exists(temp_db_path):
os.remove(temp_db_path)
con = sqlite3.connect(temp_db_path, check_same_thread=False)
cur = con.cursor()
if fts5_pragma_check():
cur.execute('''CREATE VIRTUAL TABLE angry_table
USING fts5(directory, path)''')
cur.execute('''PRAGMA user_version = 4;''')
else:
cur.execute('''CREATE VIRTUAL TABLE angry_table
USING fts4(directory, path)''')
cur.execute('''PRAGMA user_version = 3;''')
cur.executemany('''INSERT INTO angry_table VALUES (?, ?)''', table)
con.commit()
replace_old_db_with_new()
def replace_old_db_with_new():
global DATABASE_PATH
temp_db_path = TEMP_PATH + '/angry_database.db'
dir_path = os.path.dirname(DATABASE_PATH)
if not os.path.exists(temp_db_path):
return
if not os.path.exists(dir_path):
cmd = ['install', '-d', dir_path]
p = subprocess.Popen(cmd)
p.wait()
cmd = ['mv', '-f', temp_db_path, DATABASE_PATH]
p = subprocess.Popen(cmd, stderr=subprocess.PIPE)
p.wait()
def time_difference(nseconds):
mins, secs = divmod(nseconds, 60)
return '{:0>2d}:{:0>2d}'.format(mins, secs)
def fts5_pragma_check():
with sqlite3.connect(':memory:') as conn:
cur = conn.cursor()
cur.execute('pragma compile_options;')
available_pragmas = cur.fetchall()
if ('ENABLE_FTS5', ) in available_pragmas:
return True
else:
return False
if __name__ == '__main__':
load_settings()
test_conditional_mounts_for_autoupdate()
if LITE is True:
crawling_drives_lite()
else:
crawling_drives()
total_time = datetime.now() - START_TIME
noti_text = '{} | database updated'.format(
time_difference(total_time.seconds))
try:
show_notification(noti_text)
except Exception as err:
print(err)
log_path = TEMP_PATH + '/angrysearch_cron.log'
with open(log_path, 'a') as log:
t = '{:%Y-%b-%d | %H:%M | } '.format(datetime.now())
log.write(t + str(err) + os.linesep)