-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtmbrowse.py
235 lines (174 loc) · 5.79 KB
/
tmbrowse.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env python3
import argparse
import errno
import hashlib
import os
import re
import shutil
import subprocess
import sys
args = argparse.Namespace()
def make_sure_path_exists(path):
try:
os.makedirs(path)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
def md5(filename):
with open(filename, "rb") as f:
file_hash = hashlib.md5()
chunk = f.read(8192)
while chunk:
file_hash.update(chunk)
chunk = f.read(8192)
return file_hash.hexdigest()
def get_backup_root(filename):
"""
Walks back a directory tree until it finds a folder containing
the file 'backup.marker' .
returns that folder or False.
"""
path_parts = os.path.split(filename)
print(f"{filename=}")
if path_parts[0] == "/":
return False
if os.path.exists(os.path.join(path_parts[0], "backup.marker")):
return path_parts[0]
return get_backup_root(path_parts[0])
def get_backup_folders(path):
"""returns a list of the date/time folder names for
each backup in the archive.
"""
dates = []
dirs = os.listdir(path)
date_pattern = "(\d\d\d\d-\d\d-\d\d-\d\d\d\d\d\d)"
for file in dirs:
if re.match(date_pattern, file):
dates.append(os.path.join(path, file))
dates.sort(reverse=True)
return dates
def process_file(filename):
# print("processing file: ", filename)
rel_path = ""
orig_path, file = os.path.split(filename)
backup_root = get_backup_root(filename)
# print("BACKUP_ROOT = ", backup_root)
if not backup_root:
print("Cannot find backup.marker in this tree:", filename)
return
pattern = ".+\d\d\d\d-\d\d-\d\d-\d\d\d\d\d\d(.+)"
m = re.search(pattern, orig_path)
if m:
rel_path = m.group(1)
rel_path = rel_path.lstrip("/") # to make it relative strip leading slash
backups = get_backup_folders(backup_root)
num_backups = len(backups)
print(f"\n{num_backups} backups at root:", backup_root)
previous_md5 = ""
previous_size = ""
this_md5 = ""
if args.links_dir:
try:
make_sure_path_exists(args.links_dir)
# os.makedirs(args.links_dir, exist_ok=False)
except FileExistsError:
print("links dir exists already. No links made.")
args.links_dir = False
for b in backups:
b_date = os.path.split(b)[1]
b_rel_filepath = os.path.join(b, rel_path, file)
this_md5 = ""
this_size = ""
if os.path.exists(b_rel_filepath):
file_stats = os.stat(b_rel_filepath)
this_size = file_stats.st_size
size_changed = this_size != previous_size
if args.md5:
this_md5 = md5(b_rel_filepath)
is_changed = this_md5 != previous_md5
else:
is_changed = size_changed
if is_changed or args.all:
if args.fullpath:
print(backup_root + "/", end="")
print(b_date + "/", end="")
print(os.path.join(rel_path, file), end="")
print("\t_size=", this_size, end="")
if args.md5:
print("\t_md5=", this_md5, end="")
if args.links_dir:
try:
os.symlink(
b_rel_filepath, os.path.join(args.links_dir, b_date + "__" + file)
)
except FileExistsError:
print("file missing")
print("")
else:
pass
previous_md5 = this_md5
previous_size = this_size
previous_fullpath = b_rel_filepath
def is_windows_lnk(file):
if os.path.splitext(file)[1] == ".lnk":
return True
return False
def main():
parser = argparse.ArgumentParser(
description="List all version of a specific file in a timemachine style hardlink backup dir."
)
parser.add_argument("input", nargs="*", default="", help="input file ...")
parser.add_argument(
"-a",
"--all",
action="store_true",
help="Output every version of a file, not just unique ones.",
)
parser.add_argument(
"-f",
"--fullpath",
action="store_true",
help="output the full path to files. Default: the absolute path from backup root",
)
parser.add_argument(
"-m",
"--md5",
action="store_true",
help="compare files based on md5hash. Default: compare by size only.",
)
parser.add_argument(
"-l",
"--links-dir",
type=str,
default="",
help="Directory to story links in. Create a linked file for each unique file found. ",
)
global args
args = parser.parse_args()
# print("args==", args)
if not len(args.input):
parser.print_help()
return 0
if os.path.exists(args.links_dir):
shutil.rmtree(args.links_dir, ignore_errors=True)
if args.md5:
print("Comparing by md5sum. This could be slow if the file is large.")
for single_input in args.input:
absinput = os.path.abspath(os.path.realpath(single_input))
if not os.path.exists(single_input):
print("File not Found:", single_input)
continue
if os.path.isdir(absinput):
print(f"Directories not supported. '{single_input}'")
continue
if not (
os.path.isfile(single_input)
or os.path.islink(single_input)
or is_windows_lnk(single_input)
):
print("Not a file:", single_input)
continue
if os.path.isfile(absinput):
process_file(absinput)
if __name__ == "__main__":
sys.exit(main())