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

  1. import ctypes
  2. import os
  3. import os.path as osp
  4. import datetime
  5. from util import no_parents,get_drives
  6. from config import config
  7. FORCE_ADMIN = not config.getboolean('global','no_admin',fallback=False)
  8. def is_admin():
  9. return ctypes.windll.shell32.IsUserAnAdmin() != 0
  10. EXE_PATH = os.path.expandvars(r"%userprofile%\portables"
  11. r"\foldertimeupdate"
  12. r"\FolderTimeUpdate.exe"
  13. )
  14. def update_mtimes(starts):
  15. if not is_admin() and FORCE_ADMIN:
  16. raise Exception("Process must be admin")
  17. for start in starts:
  18. subprocess.check_call([EXE_PATH,'/stext','null','/BaseFolder',start])
  19. def build(after,starts):
  20. if isinstance(after,datetime.datetime):
  21. after = after.timestamp()
  22. var_unsubbed = map(osp.expandvars,starts)
  23. abs_paths = map(osp.abspath,var_unsubbed)
  24. starts = list(no_parents(abs_paths))
  25. ret = []
  26. for path in starts:
  27. for root,dirs,files in os.walk(path,topdown=True):
  28. try:
  29. dirs[:] = list(filter(
  30. lambda p: osp.getmtime(osp.join(root,p)) > after,
  31. dirs
  32. ))
  33. ret += [osp.join(root,file) for file in files]
  34. except:
  35. print('Error',root,sep= ': ')
  36. return ret
  37. def build_ext(ext,paths):
  38. ret = []
  39. for path in paths:
  40. if path.endswith(ext):
  41. name = osp.basename(path)
  42. try:
  43. ret[name].append(path)
  44. except KeyError:
  45. ret.append([name,path])
  46. return ret
  47. def update(after,*starts,update_mtimes = False):
  48. if update_mtimes:
  49. update_mtimes(starts)
  50. return build(after,starts)