forked from eborisch/zfs_versions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzfs_versions.py
176 lines (151 loc) · 5.57 KB
/
zfs_versions.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env python
# EAB 2016
"A little toy to find all saved (ZFS) versions of a file or directory."
from __future__ import print_function
import re
import sys
from os import path
from subprocess import check_output, call, check_call, STDOUT, \
CalledProcessError
if sys.platform[:5] == 'linux':
LS = 'ls -ltr --time-style=full-iso'
elif sys.platform == 'sunos5':
LS = '/usr/bin/ls -ltrE'
else:
LS = 'ls -Tltr'
# If we have colordiff, use it for diff commands
try:
check_call(('colordiff', '/dev/null', '/dev/null'), stdin=open('/dev/null', 'r'),
stdout=open('/dev/null', 'w'), stderr=STDOUT)
DIFF = 'colordiff'
except OSError as e:
DIFF = 'diff'
def find_versions(path_in, print_mode=False):
"""
Locate all zfs-snapshotted versions of path_in. Listed in time-order, with
only updated versions listed (with the exception of the current file,
which is always listed.)
"""
ls_opts = '-d' if path.isdir(path_in) else ''
orig = path.abspath(path_in).split('/')
zfs_point = len(orig)
while zfs_point > 0:
zfs_dir = '/'.join(orig[:zfs_point]) + '/.zfs'
if path.isdir(zfs_dir):
break
else:
zfs_point = zfs_point - 1
if zfs_point == 0:
raise IOError("Unable to find .zfs directory. "
"Is [{0}] on a ZFS filesystem?".format(path_in))
branch = '/'.join(orig[zfs_point:])
try:
output = check_output('{0} {1} "{2}"/snapshot/*/"{3}"'.format(LS,
ls_opts,
zfs_dir,
branch),
shell=True,
universal_newlines=True).split('\n')
except CalledProcessError:
print("Unable to find previous versions of [{0}]".format(path_in))
output = []
files = []
if print_mode is True:
sortable = re.compile('(.*zfs-auto-snap)_[a-z]+-([0-9h-]+)')
sort_lines = []
sort_last = None
for line in output:
if len(line) == 0:
continue
# This is to attempts to sort auto-snapshot names of identical
# versions chronologically. Won't work for custom snapshot names
# as we don't have direct access to their (snapshot) creation
# times.
files.append(line[line.find('/'):])
found = sortable.search(line)
if found:
if found.group(1) != sort_last:
for l in sorted(sort_lines):
print(l[1])
sort_lines[:] = []
sort_last = found.group(1)
sort_lines.append((found.group(2), line))
else:
# Put custom snapshots at bottom of identical version list
sort_lines.append(('9', line))
for l in sorted(sort_lines):
# Required to print off the last batch collected in sort_lines
print(l[1])
else: # Only print changes
last = None
for line in output:
if len(line) == 0:
continue
this = line[:line.find('/')]
if this != last:
files.append(line[line.find('/'):])
if print_mode is not None:
print(line)
last = this
# List the current (live on the filesystem) version.
if path.exists('/'.join(orig)):
# Don't call if the user is searching for a deleted file.
if print_mode is not None:
call('{0} {1} "{2}"'.format(LS, ls_opts, '/'.join(orig)),
shell=True)
files.append('/'.join(orig))
return files
def usage():
print("Usage: zfs_versions.py [-a|--all] [--diff|--idiff] \n"
" <path> [<path> ...]\n"
" -a|--all Print all versions (not just changed.)\n"
" --diff Show difference between history and current.\n"
" --idiff Show incremental differences.\n"
" -r|--recursive Recursive diffs on directories.")
if __name__ == "__main__":
print_all = False
diff = 0
FLAGS = '-u'
# Poor man's argparse to enable 2.6 compat.
while len(sys.argv) > 1 and sys.argv[1][0] is '-':
if sys.argv[1] in ('-a', '--all'):
print_all = True
elif sys.argv[1] == '--diff':
diff = 1
print_all = None
elif sys.argv[1] == '--idiff':
diff = 2
print_all = None
elif sys.argv[1] in ('-r', '--recursive'):
FLAGS = FLAGS + 'r'
else:
usage()
print("")
print("Unrecognized option: {0}".format(sys.argv[1]))
sys.exit(1)
sys.argv.pop(1)
if len(sys.argv) < 2:
usage()
sys.exit(1)
sep = False
for p in sys.argv[1:]:
if sep:
print("*" * 78)
vers = find_versions(p, print_all)
if path.isdir(vers[-1]):
# Don't echo back all the unchanged files.
f = FLAGS
else:
f = FLAGS + 's'
if diff == 1:
for n in vers[:-1]:
print("-"*78)
sys.stdout.flush()
call((DIFF, f, n, vers[-1]))
elif diff == 2:
for n in range(len(vers)-1):
print("-"*78)
sys.stdout.flush()
call((DIFF, f, vers[n], vers[n+1]))
sep = True
# vim: ts=4:sw=4:et:si:ai: