Advertisement
Guest User

DSR Autosave

a guest
May 24th, 2018
960
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 KB | None | 0 0
  1. from os import path, listdir, makedirs
  2. from shutil import copyfile
  3. from time import time, localtime, strftime, sleep
  4. from datetime import datetime
  5.  
  6. """
  7. automatic DSR backup file creation
  8. (1) creates backups upon start
  9. (2) creates backups every MINUTES_X minutes defined below. Default: 5
  10. note: the documents folder has to be set as FOLDER_documents below. Default: 'documents'
  11. note: auto save only for files which have been modified in the last 10 minutes
  12. note: should be compiled using PyInstaller
  13. """
  14.  
  15. # variables
  16. # insert the name of the windows documents folder
  17. # depends either on locale (by default) or user setting
  18. FOLDER_documents = "documents"
  19. MINUTES_X = 5
  20.  
  21.  
  22. # Constants
  23. PATH_UserFolder = path.expandvars("%userprofile%")
  24. PATH_DSR = path.join(PATH_UserFolder, FOLDER_documents, "NBGI", "DARK SOULS REMASTERED")
  25.  
  26. def backup_saves(init):
  27.     for file in (x for x in listdir(PATH_DSR) if path.isfile(path.join(PATH_DSR, x))):
  28.         # get name of the save
  29.         name_save, name_extension = path.splitext(file)
  30.         path_dir_backup = path.join(PATH_DSR, name_save)
  31.         path_save = path.join(PATH_DSR, file)
  32.         # get timestamps
  33.         tSave = path.getmtime(path_save)
  34.         tNow = time()
  35.         # save backup
  36.         if init or (tNow - tSave) < 600:
  37.             # create folder with save name if it doesn't exist yet
  38.             if not path.exists(path_dir_backup):
  39.                 makedirs(path_dir_backup)
  40.             # create backup file
  41.             timeStamp = strftime("%Y_%H_%M_%S", localtime(tNow))
  42.             timePrint = strftime("%H:%M", localtime(tNow))
  43.             name_backup = f"{name_save}-BACKUP_{timeStamp}{name_extension}"
  44.             path_backup = path.join(path_dir_backup, name_backup)
  45.             copyfile(path_save, path_backup)
  46.             print(f"{timePrint}   saving backup of {name_save}:")
  47.             spacing = " " * len(timePrint)
  48.             print(f"{spacing}   -> {name_backup}")
  49.  
  50. def main():
  51.     backup_saves(True)
  52.     while True:
  53.         backup_saves(False)
  54.         iSleep = 60 * MINUTES_X
  55.         print(f"\nsleeping for {MINUTES_X} minutes\n")
  56.         sleep(iSleep)
  57.  
  58. if __name__ == '__main__':
  59.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement