-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_with_fdupes.py
140 lines (111 loc) · 3.88 KB
/
test_with_fdupes.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
# Undupes: Find duplicate files.
# Copyright (C) 2024 Mmanu Chaturvedi <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# SPDX-License-Identifier: AGPL-3.0
# SPDX-FileCopyrightText: 2024 Mmanu Chaturvedi <[email protected]>
import subprocess
import os
import tempfile
import json
import sys
import time
from pprint import pprint
from typing import collections
USE_FD = False
prog_name = os.getenv("PROG_NAME", "jdupes -z")
UNDUPES = os.getenv("UNDUPES", "./build/install/bin/undupes")
FIND_OPTIONS = os.getenv("FIND_OPTIONS", "")
# prog_name = "fdupes"
def get_fdupes_output(path="."):
global prog_name
fdupes_output = subprocess.check_output(
f"{prog_name} -H -r {path}", shell=True, universal_newlines=True
)
file_sets_split = fdupes_output.split("\n\n")
file_sets_linewise = [x for x in file_sets_split if not x.isspace() and len(x) != 0]
file_sets = []
for file_set in file_sets_linewise:
file_set_linewise_strip = file_set.strip()
files_list = file_set_linewise_strip.split("\n")
if files_list:
files_list.sort()
file_sets.append(files_list)
return file_sets
def get_undupes_output(path="."):
global USE_FD
global UNDUPES
global FIND_OPTIONS
if USE_FD:
files_output = subprocess.check_output(
f"fd -u . --type f --print0 {path}", shell=True
)
else:
files_output = subprocess.check_output(
f"find {path} -type f {FIND_OPTIONS}-print0", shell=True
)
undupes_output = subprocess.check_output(UNDUPES, input=files_output)
json_output = json.loads(undupes_output)
file_sets = []
if not json_output:
return file_sets
for ele in json_output:
if ele["file_list"]:
ele_list = ele["file_list"]
ele_list.sort()
file_sets.append(ele_list)
file_sets.sort()
return file_sets
def compare_file_sets(A, B):
if len(A) != len(B):
return False
return collections.Counter(A) == collections.Counter(B)
class CustomTimer:
def __init__(self, name=""):
self.name = name
pass
def __enter__(self):
self.start_time = time.time()
def __exit__(self, *args):
self.end_time = time.time()
print(f"{self.name}: Elapsed time: {(self.end_time - self.start_time):.6f} s")
def main():
global prog_name
if len(sys.argv) == 1:
path = "."
else:
path = " ".join(sys.argv[1:])
with CustomTimer(prog_name) as ct:
fdupes_sets = get_fdupes_output(path)
with CustomTimer("undupes") as ct:
undupes_sets = get_undupes_output(path)
print(f"{prog_name} set size: {len(fdupes_sets)}")
print(f"undupes set size: {len(undupes_sets)}")
not_in_fdupes, not_in_undupes = [], []
for a in undupes_sets:
if a not in fdupes_sets:
not_in_fdupes.append(a)
for a in fdupes_sets:
if a not in undupes_sets:
not_in_undupes.append(a)
if not_in_fdupes:
print(f"Not in {prog_name}:")
print(not_in_fdupes)
if not_in_undupes:
print(f"Not in undupes:")
print(not_in_undupes)
if len(not_in_fdupes) == 0 and len(not_in_undupes) == 0:
print("Perfect Match!")
if __name__ == "__main__":
main()