-
Notifications
You must be signed in to change notification settings - Fork 209
Expand file tree
/
Copy pathrestore_backup.py
More file actions
75 lines (52 loc) · 2.32 KB
/
restore_backup.py
File metadata and controls
75 lines (52 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
## File by _baku89 on twitter
## Source: https://gist.github.com/baku89/615a15228e5747d4e83fa579796009fe
## Modified & Added to toolset by: Paul Ambrosiussen
import hou
import glob, os, re
from stat import ST_MTIME
from datetime import datetime
def recoverFile():
# Get a filename of the most recent opened file
userPrefDir = hou.getenv('HOUDINI_USER_PREF_DIR')
historyPath = os.path.join(userPrefDir, 'file.history')
destPath = None
filename = None
if not os.path.exists(historyPath):
return False
with open(historyPath, 'r') as file:
txt = file.read()
result = re.search(r'^HIP\n{\n((?:[^\n]*(\n+))+?)}', txt)
if not result:
return False
recentFiles = [path for path in result.groups()[0].split('\n') if path]
if len(recentFiles) == 0:
return False
destPath = recentFiles[-1]
filename = os.path.basename(destPath)
# Get the backup file
tempDir = hou.getenv('HOUDINI_TEMP_DIR')
os.chdir(tempDir)
bakPattern = r"^crash\.%s\.(.*)\%s" % os.path.splitext(filename)
entries = [name for name in glob.glob('*.hip*') if re.match(bakPattern, name)]
entries = [(os.stat(path)[ST_MTIME], path) for path in entries]
if len(entries) == 0:
hou.ui.displayMessage('No backup file for %s has found' % filename)
return False
bakDate, bakFile = sorted(entries, reverse=True)[0]
bakPath = os.path.join(tempDir, bakFile)
bakDate = datetime.fromtimestamp(int(bakDate))
bakDate = bakDate.strftime("%m/%d/%Y, %H:%M:%S")
# Ask whether restore or not
msg = "The backup of the most recent opened file has found. Do you want to restore it?"
details = (
"Location: " + os.path.dirname(destPath) + "\n"
"Original: " + filename + "\n"
"Backup: " + bakFile + " (" + bakDate + ")")
options = ('Yes', 'Cancel')
if hou.ui.displayMessage(msg, options, close_choice=1, details=details, details_expanded=True) == 1:
return False
prefix, ext = os.path.splitext(destPath)
newDest = prefix + '_recovered' + ext
os.rename(bakPath, newDest)
if hou.ui.displayMessage('Do you want to open the file?', ('Yes', 'Cancel'), close_choice=1) == 0:
hou.hipFile.load(newDest)