You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
104 lines
3.1 KiB
104 lines
3.1 KiB
from config import config
|
|
from util import no_parents,get_drives,is_admin
|
|
import argparse
|
|
import datetime
|
|
import os
|
|
import os.path as osp
|
|
import sqlite3
|
|
import subprocess
|
|
import time
|
|
import traceback
|
|
|
|
FORCE_ADMIN = not config.getboolean('global','no_admin',fallback=False)
|
|
TEST = True
|
|
DB_PATH = config.getpath('global','db_path')
|
|
ext_database = sqlite3.connect(DB_PATH)
|
|
|
|
|
|
EXE_PATH = config.getpath('global','modified_time_updater')
|
|
|
|
def update_mtimes(starts):
|
|
if not is_admin() and FORCE_ADMIN:
|
|
raise Exception("Process must be admin")
|
|
for start in starts:
|
|
EXE_PATH = shlex.split(EXE_PATH)
|
|
EXE_PATH.append(start)
|
|
subprocess.check_call(EXE_PATH)
|
|
|
|
def build(after,starts):
|
|
if isinstance(after,datetime.datetime):
|
|
after = after.timestamp()
|
|
starts = map(lambda item: item.replace('"','').replace("'",''),starts)
|
|
for path in starts:
|
|
for root,dirs,files in os.walk(path,topdown=True):
|
|
try:
|
|
for file in files:
|
|
yield os.path.join(root,file)
|
|
dirs[:] = list(filter(lambda p: osp.getmtime(osp.join(root,p)) > after,dirs))
|
|
except:
|
|
print('Error',root,sep= ': ')
|
|
|
|
def _update(ext,starts,after,do_update_mtimes = False):
|
|
if do_update_mtimes:
|
|
update_mtimes(starts)
|
|
files = build(after,starts)
|
|
for file in files:
|
|
if file.endswith(ext):
|
|
name = osp.basename(file)
|
|
yield [name,file]
|
|
|
|
def set_updating(ext,con,updating=True):
|
|
cur = con.cursor()
|
|
if updating:
|
|
status = 1
|
|
else:
|
|
status = 0
|
|
cur.execute('UPDATE update_status SET status=? WHERE ext=?;',[status,ext])
|
|
con.commit()
|
|
|
|
def get_updating(ext,cur):
|
|
cur.execute('SELECT status FROM update_status WHERE ext=?;',[ext])
|
|
return cur.fetchone()[0] == 1
|
|
|
|
def update(ext,do_update_mtimes):
|
|
set_updating(ext,ext_database)
|
|
cur = ext_database.cursor()
|
|
c = config.getint('global','commit_interval',fallback = 30)
|
|
try:
|
|
cur.execute('SELECT MAX(update_time) FROM updates WHERE ext=?;',[ext])
|
|
last_update = cur.fetchone()[0]
|
|
_time = int(time.time())
|
|
|
|
params = (
|
|
'.'+ext,
|
|
config.getpaths(ext,'paths'),
|
|
last_update,
|
|
do_update_mtimes
|
|
)
|
|
|
|
files = _update(*params)
|
|
count = 0
|
|
|
|
for count,row in enumerate(files):
|
|
row.append(_time)
|
|
cur.execute('INSERT OR IGNORE INTO {} VALUES (?,?,?);'.format(ext),row)
|
|
count += 1
|
|
if count % c == 0:
|
|
ext_database.commit()
|
|
|
|
cur.execute('INSERT INTO updates VALUES (?, ?, ?);',[ext,_time,count+1])
|
|
ext_database.commit()
|
|
set_updating(ext,ext_database,False)
|
|
|
|
except Exception as e:
|
|
set_updating(ext,ext_database,False)
|
|
traceback.print_exc()
|
|
input()
|
|
raise e
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('ext')
|
|
parser.add_argument('-m','--update-modified-time',action='store_true')
|
|
args = parser.parse_args()
|
|
update(args.ext,args.update_modified_time)
|