Guest User

Untitled

a guest
Dec 14th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. #!c:\conda\python.exe
  2. # This is a small script for backing up directories on windows. Just drop a directory in the explorer
  3. # on this script and you get a file with the directory name which includes a time stamp.
  4. #
  5. # Requirements:
  6. #
  7. # 1) 7z must be installed with the installer
  8. # 2) A registration of python. This is default for CPython
  9. #
  10. # Usage: Drag and Drop a file or directory on this script
  11. #
  12.  
  13. import sys
  14. import winreg
  15. import time
  16. import subprocess as sp
  17. from pathlib import Path
  18.  
  19. regkey_zip = r"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\7-Zip"
  20.  
  21. msg_missing7z = """Error:\n7z must be installed with the installer or\nI can't find the path to the executable"""
  22. msg_missingarg = """Error:\nYou must provide exact one argument!"""
  23. msg_argfile = """Error:\nThe argument must be an readable directory or a file!"""
  24. msg_startup = """Make a backup"""
  25.  
  26. def regkey_value(path, name="", start_key = None):
  27. if isinstance(path, str):
  28. path = path.split("\\")
  29. if start_key is None:
  30. start_key = getattr(winreg, path[0])
  31. return regkey_value(path[1:], name, start_key)
  32. else:
  33. subkey = path.pop(0)
  34. with winreg.OpenKey(start_key, subkey) as handle:
  35. assert handle
  36. if path:
  37. return regkey_value(path, name, handle)
  38. else:
  39. desc, i = None, 0
  40. while not desc or desc[0] != name:
  41. desc = winreg.EnumValue(handle, i)
  42. i += 1
  43. return desc[1]
  44.  
  45. def msg(text):
  46. lines = text.split('\n')
  47. linelen = max(len(s) for s in lines)
  48.  
  49. print('+'+'-'*linelen+'+')
  50. for line in lines:
  51. print('|{:{length}}|'.format(line,length=linelen))
  52. print('+' + '-' * linelen + '+')
  53.  
  54.  
  55. if len(sys.argv)!=2:
  56. msg(msg_missingarg)
  57. _ = input('Press a key')
  58. quit(-1)
  59.  
  60. try:
  61. value = regkey_value(regkey_zip,'InstallLocation')
  62. except:
  63. msg(msg_missing7z)
  64. _ = input('Press a key')
  65. quit(-1)
  66.  
  67. zipper = Path(value) / '7z.exe'
  68.  
  69. try:
  70. archive_src = Path(sys.argv[1])
  71. if not archive_src.exists():
  72. raise FileNotFoundError
  73. except:
  74. msg(msg_argfile)
  75. _ = input('Press a key')
  76. quit(-1)
  77.  
  78. archive_dst = archive_src.parent / Path(str(archive_src.name) + ' ' + time.strftime('%Y%m%d-%H%M%S'))
  79.  
  80. args = ['{}'.format(str(zipper)), 'a', str(archive_dst), str(archive_src)]
  81.  
  82. msg(msg_startup)
  83. process = sp.Popen(args)
  84. process.wait()
  85.  
  86. msg('Ready! ')
  87. _ = input('Press a key')
Add Comment
Please, Sign In to add comment