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.
 
 

57 lines
1.7 KiB

import ctypes
import os
import os.path as osp
import datetime
from util import no_parents,get_drives
from config import config
import subprocess
FORCE_ADMIN = not config.getboolean('global','no_admin',fallback=False)
def is_admin():
return ctypes.windll.shell32.IsUserAnAdmin() != 0
EXE_PATH = os.path.expandvars(r"%userprofile%\portables"
r"\foldertimeupdate"
r"\FolderTimeUpdate.exe"
)
def update_mtimes(starts):
if not is_admin() and FORCE_ADMIN:
raise Exception("Process must be admin")
for start in starts:
subprocess.check_call([EXE_PATH,'/stext','nul','/BaseFolder',start])
def build(after,starts):
if isinstance(after,datetime.datetime):
after = after.timestamp()
var_unsubbed = map(osp.expandvars,starts)
abs_paths = map(osp.abspath,var_unsubbed)
starts = list(no_parents(abs_paths))
ret = []
for path in starts:
for root,dirs,files in os.walk(path,topdown=True):
try:
dirs[:] = list(filter(
lambda p: osp.getmtime(osp.join(root,p)) > after,
dirs
))
ret += [osp.join(root,file) for file in files]
except:
print('Error',root,sep= ': ')
return ret
def build_ext(ext,paths):
ret = []
for path in paths:
if path.endswith(ext):
name = osp.basename(path)
ret.append([name,path])
return ret
def update(ext,starts,after,do_update_mtimes = False):
if do_update_mtimes:
update_mtimes(starts)
files = build(after,starts)
return build_ext(ext,files)