forked from CCBR/spacesavers2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspacesavers2_mimeo
executable file
·420 lines (390 loc) · 14.3 KB
/
spacesavers2_mimeo
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#!/usr/bin/env python3
import sys
import os
import gzip
import textwrap
import time
import shutil
import subprocess
import tqdm
MINDEPTH = 3
QUOTA_TB = 20
from src.VersionCheck import version_check
from src.VersionCheck import __version__
version_check()
from src.FileDetails import FileDetails
from src.dfUnit import dfUnit
from src.Summary import Summary
from src.Summary import pathlen
from src.utils import *
from datetime import date
import argparse
def process_hh(
uid,
hashhash,
hashhashsplits,
mindepth,
maxdepth,
uid2uname,
gid2gname,
perfolder_summaries,
perfolder_dups,
user_output,
):
for h in hashhash.keys():
# if files have the same forward and reverse hashes but different sizes then
# hashes are split into multiple hashes with <underscore><splitnumber> suffix
# being added to the bottom hash for each size
split_required = hashhash[h].compute(
hashhashsplits
) # compute if split is needed or if we have duplicates
if split_required:
continue # split is required so move on to the next hash as new hashes with <underscore><splitnumber> have been created by compute and added to hashhashsplits ... deal with them there!
# get indexes to files in the flist that belong to user with uid
# if uid is zero, then get all file indexes
uid_file_index = hashhash[h].get_user_file_index(uid)
if len(uid_file_index) == 0: # user with uid has no files in this set
continue
oldest_index = hashhash[h].oldest_index
foldest = hashhash[h].flist[oldest_index]
user_owns_original = False
if foldest.uid == uid or 0 == uid : user_owns_original = True
uid_file_index = list(filter(lambda x:x!=oldest_index,uid_file_index)) # remove oldest if present in list
inodes_already_summerized = [foldest.inode]
if user_owns_original:
fpaths = foldest.get_paths(mindepth, maxdepth)
for p in fpaths:
perfolder_summaries[p].nnondup_files += 1
perfolder_summaries[p].non_dup_Bytes.append(foldest.calculated_size)
perfolder_summaries[p].non_dup_ages.append(foldest.mtime)
# if hashhash[h].ndup_files > 0: # we have duplicates
if len(uid_file_index) > 0: # uid has copies
for i in uid_file_index:
f = hashhash[h].flist[i]
fpath = f.apath
parent = fpath.parent
fpaths = f.get_paths(mindepth, maxdepth)
if f.inode in inodes_already_summerized: # it is a hardlink
for p in fpaths:
perfolder_summaries[p].nnondup_files += 1
else:
inodes_already_summerized.append(f.inode)
if not parent in perfolder_dups:
perfolder_dups[fpath.parent] = 0
perfolder_dups[fpath.parent] += f.calculated_size
for p in fpaths:
perfolder_summaries[p].ndup_files+=1
perfolder_summaries[p].dup_Bytes.append(f.calculated_size)
perfolder_summaries[p].dup_ages.append(f.mtime)
out_index = []
out_index.append(oldest_index)
out_index.extend(uid_file_index)
if args.duplicatesonly and len(out_index)==1: continue
user_output.write(
"{}\n".format(
hashhash[h].str_with_name(uid2uname, gid2gname, out_index)
)
)
def main():
start = time.time()
scriptname = os.path.basename(__file__)
elog = textwrap.dedent(
"""\
Version:
{}
Example:
> spacesavers2_mimeo -f /output/from/spacesavers2_catalog -o /path/to/output/folder -d 7 -q 10
""".format(
__version__
)
)
parser = argparse.ArgumentParser(
description="spacesavers2_mimeo: find duplicates",
epilog=elog,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument(
"-f",
"--catalog",
dest="catalog",
required=True,
type=str,
default=sys.stdin,
help="spacesavers2_catalog output from STDIN or from catalog file",
)
parser.add_argument(
"-d",
"--maxdepth",
dest="maxdepth",
required=False,
type=int,
default=10,
help="folder max. depth upto which reports are aggregated ... absolute path is used to calculate depth (Default: 10)",
)
parser.add_argument(
"-o",
"--outdir",
dest="outdir",
required=False,
type=str,
default=os.getcwd(),
help="output folder",
)
parser.add_argument(
"-p",
"--prefix",
dest="prefix",
required=False,
type=str,
default="",
help="prefix for all output files",
)
parser.add_argument(
"-q",
"--quota",
dest="quota",
required=False,
type=float,
default=200.0,
help="total quota of the mount eg. 200 TB for /data/CCBR",
)
parser.add_argument(
"-z",
"--duplicatesonly",
dest="duplicatesonly",
required=False,
action=argparse.BooleanOptionalAction,
help="Print only duplicates to per user output file.",
)
parser.add_argument(
"-k",
"--kronaplot",
dest="kronaplot",
required=False,
action=argparse.BooleanOptionalAction,
help="Make kronaplots for duplicates.(ktImportText must be in PATH!)",
)
parser.add_argument("-v", "--version", action="version", version=__version__)
print_with_timestamp(
start=start, scriptname=scriptname, string="version: {}".format(__version__)
)
global args
args = parser.parse_args()
quota = args.quota * 1024 * 1024 * 1024 * 1024
if args.kronaplot:
ktImportText_in_path = False
if shutil.which("ktImportText") == None:
sys.stderr.write("ktImportText(from kronaTools) not found in PATH. kronaplots will not be generated.\n")
else:
ktImportText_in_path = True
uid2uname = dict()
gid2gname = dict()
hashhash = dict()
users = set() # list of all uids found
users.add(0) # 0 == all users
groups = set() # list of gids
paths = set() # set of all paths possible
path_lens = set() # set of all path depths
print_with_timestamp(
start=start, scriptname=scriptname, string="Reading in catalog file..."
)
set_complete = True
folder_info = dict()
with open(args.catalog) as catalog:
for l in tqdm.tqdm(catalog):
fd = FileDetails()
set_complete = fd.set(l)
if not set_complete:
continue
if fd.fld != "d" and fd.fld !="f": # not a file or folder
continue # ignore all symlinks
users.add(fd.uid)
groups.add(fd.gid)
path_lens.add(fd.get_depth())
for p in fd.get_paths_at_all_depths():
paths.add(p)
if fd.fld == "d":
if not fd.apath in folder_info:
folder_info[fd.apath] = fd
continue
hash = fd.xhash_top + "#" + fd.xhash_bottom
if hash == "#": # happens when file cannot be read
sys.stderr.write(
"Cannot read file listed in catalog and will be excluded from mimeo: {} \n".format(
fd.apath
)
)
continue
if not hash in hashhash:
hashhash[hash] = dfUnit(hash)
hashhash[hash].add_fd(fd)
min_path_len = min(path_lens)
max_path_len = max(path_lens)
if args.maxdepth > max_path_len:
mindepth = min_path_len
maxdepth = max_path_len
elif args.maxdepth > min_path_len:
mindepth = min_path_len
maxdepth = args.maxdepth
else:
mindepth = args.maxdepth
maxdepth = args.maxdepth
# filter paths by maxdepth
paths = list(filter(lambda x: get_folder_depth(x) <= maxdepth, paths))
paths = list(filter(lambda x: get_folder_depth(x) >= mindepth, paths))
# convert PosixPaths list to list of strings
paths = list(map(lambda x: str(x), paths))
# sort paths for outfile aesthetics
paths.sort()
# reconvert to paths
paths = list(map(lambda x: Path(x), paths))
users = list(users)
for uid in users:
uid2uname[uid] = get_username_groupname(uid)
for gid in groups:
gid2gname[gid] = get_username_groupname(gid)
print_with_timestamp(
start=start, scriptname=scriptname, string="Done reading in catalog file!"
)
print_with_timestamp(
start=start,
scriptname=scriptname,
string="Total Number of paths: %d" % len(paths),
)
print_with_timestamp(
start=start,
scriptname=scriptname,
string="Total Number of users: %d" % len(users),
)
blamematrixtsv = os.path.join(
os.path.abspath(args.outdir), args.prefix + "." + "blamematrix.tsv"
)
blamematrix = dict()
all_blamematrix_paths = set()
# users=[0] # debug only
for uid in users:
blamematrix[uid] = dict()
print_with_timestamp(
start=start,
scriptname=scriptname,
string="Gathering stats for user: %s" % (uid2uname[uid]),
)
if args.prefix != "":
outfilenameprefix = args.prefix + "." + get_username_groupname(uid)
else:
outfilenameprefix = get_username_groupname(uid)
summaryfilepath = os.path.join(
os.path.abspath(args.outdir), outfilenameprefix + ".mimeo.summary.txt"
)
useroutputpath = os.path.join(
os.path.abspath(args.outdir), outfilenameprefix + ".mimeo.files.gz"
)
if args.kronaplot:
kronatsv = os.path.join(
os.path.abspath(args.outdir), outfilenameprefix + ".mimeo.krona.tsv"
)
if ktImportText_in_path:
kronahtml = os.path.join(
os.path.abspath(args.outdir), outfilenameprefix + ".mimeo.krona.html"
)
with open(summaryfilepath, "w") as user_summary:
user_summary.write("%s\n" % (Summary.HEADER))
with gzip.open(useroutputpath, "wt") as user_output, open(
summaryfilepath, "a"
) as user_summary:
perfolder_summaries = dict()
perfolder_dups = dict()
for p in paths:
perfolder_summaries[p] = Summary(p)
if not p in folder_info:
folder_info[p] = FileDetails()
folder_info[p].initialize(p)
fd = folder_info[p]
for p2 in fd.get_paths(mindepth,maxdepth):
if not p2 in folder_info:
folder_info[p2] = FileDetails()
folder_info[p2].initialize(p2)
fd2 = folder_info[p2]
if fd2.uid == uid or uid == 0:
perfolder_summaries[p2].folder_Bytes += fd.calculated_size
hashhashsplits = dict() # dict to collect instances where the files are NOT duplicates has same hashes but different sizes (and different inodes) ... new suffix is added to bottomhash .."_iterator"
process_hh(
uid,
hashhash,
hashhashsplits,
mindepth,
maxdepth,
uid2uname,
gid2gname,
perfolder_summaries,
perfolder_dups,
user_output,
)
if len(hashhashsplits) != 0:
hashhashsplitsdummy = dict()
process_hh(
uid,
hashhashsplits,
hashhashsplitsdummy,
mindepth,
maxdepth,
uid2uname,
gid2gname,
perfolder_summaries,
perfolder_dups,
user_output,
)
del hashhashsplitsdummy
del hashhashsplits
for p in paths:
perfolder_summaries[p].update_scores(quota)
user_summary.write(f"{perfolder_summaries[p]}\n")
for p in perfolder_summaries:
p_depth = len(list(p.parents))
if p_depth == mindepth:
all_blamematrix_paths.add(p)
blamematrix[uid][p] = sum(perfolder_summaries[p].dup_Bytes)
if args.kronaplot:
print_with_timestamp(
start=start,
scriptname=scriptname,
string="Creating Kronachart for user: %s" % (uid2uname[uid]),
)
with open(kronatsv,'w') as ktsv:
for p in perfolder_dups:
path = str(p)
path = path.replace('/','\t')
path = path.replace('\t\t','\t')
if perfolder_dups[p] != 0:
ktsv.write("%d\t%s\n"%(perfolder_dups[p],path))
if ktImportText_in_path:
cmd = "ktImportText %s -o %s"%(kronatsv,kronahtml)
srun = subprocess.run(cmd,shell=True, capture_output=True, text=True)
if srun.returncode !=0:
sys.stderr.write("%s\n"%(srun.stderr))
del hashhash
print_with_timestamp(
start=start,
scriptname=scriptname,
string="Creating Blamematrix",
)
with open(blamematrixtsv,'w') as btsv:
outlist = ["path"]
uids = list(blamematrix.keys())
uids.sort()
for uid in uids:
outlist.append(uid2uname[uid])
btsv.write("\t".join(outlist)+"\n")
for p in all_blamematrix_paths:
outlist = [str(p)]
s = 0
for uid in uids:
if p in blamematrix[uid]:
s += blamematrix[uid][p]
outlist.append(str(blamematrix[uid][p]))
else:
outlist.append(str(0))
if s != 0 : btsv.write("\t".join(outlist)+"\n")
print_with_timestamp(start=start, scriptname=scriptname, string="Finished!")
if __name__ == "__main__":
main()