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
|
#!/usr/bin/python
import datetime
import sqlite3
from wsgiref.simple_server import make_server
import jinja2
from werkzeug.exceptions import HTTPException, NotFound
from werkzeug.routing import Map, Rule, RequestRedirect
from werkzeug.wrappers import Request, Response
from dedup.utils import fetchiter
hash_functions = [
("sha512", "sha512"),
("image_sha512", "image_sha512"),
("gzip_sha512", "gzip_sha512"),
("sha512", "gzip_sha512"),
("gzip_sha512", "sha512")]
jinjaenv = jinja2.Environment(loader=jinja2.FileSystemLoader("."))
def format_size(size):
assert isinstance(size, int)
size = float(size)
fmt = "%d B"
if size >= 1024:
size /= 1024
fmt = "%.1f KB"
if size >= 1024:
size /= 1024
fmt = "%.1f MB"
if size >= 1024:
size /= 1024
fmt = "%.1f GB"
return fmt % size
def function_combination(function1, function2):
if function1 == function2:
return function1
return "%s -> %s" % (function1, function2)
jinjaenv.filters["format_size"] = format_size
base_template = jinjaenv.get_template("base.html")
package_template = jinjaenv.from_string(
"""{% extends "base.html" %}
{% block title %}duplication of {{ package|e }}{% endblock %}
{% block header %}<style type="text/css">.dependency { background-color: yellow; } </style>{% endblock %}
{% block content %}<h1>{{ package|e }}</h1>
<p>Version: {{ version|e }}</p>
<p>Architecture: {{ architecture|e }}</p>
<p>Number of files: {{ num_files }}</p>
<p>Total size: {{ total_size|format_size }}</p>
{%- if shared -%}
{%- for function, sharing in shared.items() -%}
<h3>sharing with respect to {{ function|e }}</h3>
<table border='1'><tr><th>package</th><th>files shared</th><th>data shared</th></tr>
{%- for entry in sharing|sort(attribute="savable", reverse=true) -%}
<tr><td{% if not entry.package or entry.package in dependencies %} class="dependency"{% endif %}>
{%- if entry.package %}<a href="{{ entry.package|e }}">{{ entry.package|e }}</a>{% else %}self{% endif %}
<a href="../compare/{{ package|e }}/{{ entry.package|default(package, true)|e }}">compare</a></td>
<td>{{ entry.duplicate }} ({{ (100 * entry.duplicate / num_files)|int }}%)</td>
<td>{{ entry.savable|format_size }} ({{ (100 * entry.savable / total_size)|int }}%)</td></tr>
{%- endfor -%}
</table>
{%- endfor -%}
{%- endif -%}
{% endblock %}""")
detail_template = jinjaenv.from_string(
"""{% extends "base.html" %}
{% block title %}sharing between {{ details1.package|e }} and {{ details2.package|e }}{% endblock%}
{% block content %}
<h1><a href="../../binary/{{ details1.package|e }}">{{ details1.package|e }}</a> <-> <a href="../../binary/{{ details2.package|e }}">{{ details2.package|e }}</a></h1>
<table border='1'><tr><th colspan="3">{{ details1.package|e }}</th><th colspan="3">{{ details2.package|e }}</th></tr>
<tr><th>size</th><th>filename</th><th>hash functions</th><th>size</th><th>filename</th><th>hash functions</th></tr>
{%- for entry in shared -%}
<tr><td>{{ entry.size1|format_size }}</td><td>{{ entry.filename1 }}</td><td>
{%- for funccomb, hashvalue in entry.functions.items() %}<a href="../../hash/{{ funccomb[0]|e }}/{{ hashvalue|e }}">{{ funccomb[0]|e }}</a> {% endfor %}</td>
<td>{{ entry.size2|format_size }}</td><td>{{ entry.filename2 }}</td><td>
{%- for funccomb, hashvalue in entry.functions.items() %}<a href="../../hash/{{ funccomb[1]|e }}/{{ hashvalue|e }}">{{ funccomb[1]|e }}</a> {% endfor %}</td></tr>
{%- endfor -%}
</table>
{% endblock %}""")
hash_template = jinjaenv.from_string(
"""{% extends "base.html" %}
{% block title %}information on {{ function|e }} hash {{ hashvalue|e }}{% endblock %}
{% block content %}
<h1>{{ function|e }} {{ hashvalue|e }}</h1>
<table border='1'><tr><th>package</th><th>filename</th><th>size</th><th>different function</th></tr>
{%- for entry in entries -%}
<tr><td><a href="../../binary/{{ entry.package|e }}">{{ entry.package|e }}</a></td>
<td>{{ entry.filename|e }}</td><td>{{ entry.size|format_size }}</td>
<td>{% if function != entry.function %}{{ entry.function|e }}{% endif %}</td></tr>
{%- endfor -%}
</table>
{% endblock %}""")
index_template = jinjaenv.from_string(
"""{% extends "base.html" %}
{% block title %}Debian duplication detector{% endblock %}
{% block header %}
<script type="text/javascript">
function getLinkTarget() {
var pkg = document.getElementById("pkg_name").value;
if(pkg) {
return "/binary/"+pkg;
}
return '#';
}
function processData() {
var link = document.getElementById("perma_link");
link.href = getLinkTarget();
link.text = location.href + getLinkTarget();
}
window.onload = function() {
document.getElementById('pkg_name').onkeyup = processData;
document.getElementById("pkg_form").onsubmit = function () {
location.href = getLinkTarget();
return false;
}
processData();
document.getElementById("form_div").style.display = '';
}
</script>
{% endblock %}
{% block content %}
<h1>Debian duplication detector</h1>
<ul>
<li>To inspect a particlar binary package, go to <pre>binary/<packagename></pre> Example: <a href="binary/git">binary/git</a>
<div style="display:none" id="form_div"><fieldset>
<legend>Inspect package</legend>
<noscript><b>This form is disfunctional when javascript is not enabled</b></noscript>
Enter binary package to inspect - Note: Non-existing packages will result in <b>404</b>-Errors
<form id="pkg_form">
<label for="pkg_name">Name: <input type="text" size="30" name="pkg_name" id="pkg_name">
<input type="submit" value="Go"> Permanent Link: <a id="perma_link" href="#"></a>
</form>
</fieldset></div></li>
<li>To inspect a combination of binary packages go to <pre>compare/<firstpackage>/<secondpackage></pre> Example: <a href="compare/git/git">compare/git/git</a></li>
<li>To discover package shipping a particular file go to <pre>hash/sha512/<hashvalue></pre> Example: <a href="hash/sha512/ed94df7781793f06f9426a600c1bde86397afc7b35cb3aa11b60214bd31e35ad893b53a04a2cf4676154982d7c204c4aa165d6ccdaac0170031364a05dbab3bc">hash/sha512/ed94df7781793f06f9426a600c1bde86397afc7b35cb3aa11b60214bd31e35ad893b53a04a2cf4676154982d7c204c4aa165d6ccdaac0170031364a05dbab3bc</a></li>
</ul>
{% endblock %}""")
source_template = jinjaenv.from_string(
"""{% extends "base.html" %}
{% block title %}overview of {{ source|e }}{% endblock %}
{% block content %}
<h1>overview of {{ source|e }}</h1>
<table border='1'><tr><th>binary from {{ source|e }}</th><th>savable</th><th>other package</th></tr>
{% for package, sharing in packages.items() %}
<tr><td><a href="../binary/{{ package|e }}">{{ package|e }}</td><td>
{%- if sharing -%}
{{ sharing.savable|format_size }}</td><td><a href="../binary/{{ sharing.package|e }}">{{ sharing.package|e }}</a> <a href="../compare/{{ package|e }}/{{ sharing.package|e }}">compare</a>
{%- else -%}</td><td>{%- endif -%}
</td></tr>
{% endfor %}
</table>
{% endblock %}""")
def encode_and_buffer(iterator):
buff = b""
for elem in iterator:
buff += elem.encode("utf8")
if len(buff) >= 2048:
yield buff
buff = b""
if buff:
yield buff
def html_response(unicode_iterator, max_age=24 * 60 * 60):
resp = Response(encode_and_buffer(unicode_iterator), mimetype="text/html")
resp.cache_control.max_age = max_age
resp.expires = datetime.datetime.now() + datetime.timedelta(seconds=max_age)
return resp
def generate_shared(rows):
"""internal helper from show_detail"""
entry = None
for filename1, size1, func1, filename2, size2, func2, hashvalue in rows:
funccomb = (func1, func2)
if funccomb not in hash_functions:
continue
if entry and (entry["filename1"] != filename1 or
entry["filename2"] != filename2):
yield entry
entry = None
if entry:
funcdict = entry["functions"]
else:
funcdict = dict()
entry = dict(filename1=filename1, filename2=filename2, size1=size1,
size2=size2, functions=funcdict)
funcdict[funccomb] = hashvalue
if entry:
yield entry
class Application(object):
def __init__(self, db):
self.db = db
self.routingmap = Map([
Rule("/", methods=("GET",), endpoint="index"),
Rule("/binary/<package>", methods=("GET",), endpoint="package"),
Rule("/compare/<package1>/<package2>", methods=("GET",), endpoint="detail"),
Rule("/hash/<function>/<hashvalue>", methods=("GET",), endpoint="hash"),
Rule("/source/<package>", methods=("GET",), endpoint="source"),
])
@Request.application
def __call__(self, request):
mapadapter = self.routingmap.bind_to_environ(request.environ)
try:
endpoint, args = mapadapter.match()
if endpoint == "package":
return self.show_package(args["package"])
elif endpoint == "detail":
return self.show_detail(args["package1"], args["package2"])
elif endpoint == "hash":
return self.show_hash(args["function"], args["hashvalue"])
elif endpoint == "index":
if not request.environ["PATH_INFO"]:
raise RequestRedirect(request.environ["SCRIPT_NAME"] + "/")
return html_response(index_template.stream())
elif endpoint == "source":
return self.show_source(args["package"])
raise NotFound()
except HTTPException as e:
return e
def get_details(self, package):
cur = self.db.cursor()
cur.execute("SELECT version, architecture FROM package WHERE package = ?;",
(package,))
row = cur.fetchone()
if not row:
raise NotFound()
version, architecture = row
details = dict(package=package,
version=version,
architecture=architecture)
cur.execute("SELECT count(filename), sum(size) FROM content WHERE package = ?;",
(package,))
num_files, total_size = cur.fetchone()
details.update(dict(num_files=num_files, total_size=total_size))
return details
def get_dependencies(self, package):
cur = self.db.cursor()
cur.execute("SELECT required FROM dependency WHERE package = ?;",
(package,))
return set(row[0] for row in fetchiter(cur))
def compute_sharedstats(self, package):
cur = self.db.cursor()
sharedstats = {}
for func1, func2 in hash_functions:
cur.execute("SELECT a.filename, a.hash, a.size, b.package FROM content AS a JOIN content AS b ON a.hash = b.hash WHERE a.package = ? AND a.function = ? AND b.function = ? AND (a.filename != b.filename OR b.package != ?);",
(package, func1, func2, package))
sharing = dict()
for afile, hashval, size, bpkg in fetchiter(cur):
hashdict = sharing.setdefault(bpkg, dict())
fileset = hashdict.setdefault(hashval, (size, set()))[1]
fileset.add(afile)
if sharing:
sharedstats[function_combination(func1, func2)] = curstats = []
mapping = sharing.pop(package, dict())
if mapping:
duplicate = sum(len(files) for _, files in mapping.values())
savable = sum(size * (len(files) - 1) for size, files in mapping.values())
curstats.append(dict(package=None, duplicate=duplicate, savable=savable))
for pkg, mapping in sharing.items():
duplicate = sum(len(files) for _, files in mapping.values())
savable = sum(size * len(files) for size, files in mapping.values())
curstats.append(dict(package=pkg, duplicate=duplicate, savable=savable))
return sharedstats
def cached_sharedstats(self, package):
cur = self.db.cursor()
sharedstats = {}
cur.execute("SELECT package2, func1, func2, files, size FROM sharing WHERE package1 = ?;",
(package,))
for package2, func1, func2, files, size in fetchiter(cur):
if (func1, func2) not in hash_functions:
continue
curstats = sharedstats.setdefault(
function_combination(func1, func2), list())
if package2 == package:
package2 = None
curstats.append(dict(package=package2, duplicate=files, savable=size))
return sharedstats
def show_package(self, package):
params = self.get_details(package)
params["dependencies"] = self.get_dependencies(package)
params["shared"] = self.cached_sharedstats(package)
return html_response(package_template.render(params))
def show_detail(self, package1, package2):
cur = self.db.cursor()
if package1 == package2:
details1 = details2 = self.get_details(package1)
cur.execute("SELECT a.filename, a.size, a.function, b.filename, b.size, b.function, a.hash FROM content AS a JOIN content AS b ON a.hash = b.hash WHERE a.package = ? AND b.package = ? AND a.filename != b.filename ORDER BY a.size DESC, a.filename, b.filename;",
(package1, package1))
else:
details1 = self.get_details(package1)
details2 = self.get_details(package2)
cur.execute("SELECT a.filename, a.size, a.function, b.filename, b.size, b.function, a.hash FROM content AS a JOIN content AS b ON a.hash = b.hash WHERE a.package = ? AND b.package = ? ORDER BY a.size DESC, a.filename, b.filename;",
(package1, package2))
shared = generate_shared(fetchiter(cur))
# The cursor will be in use until the template is fully rendered.
params = dict(
details1=details1,
details2=details2,
shared=shared)
return html_response(detail_template.stream(params))
def show_hash(self, function, hashvalue):
cur = self.db.cursor()
cur.execute("SELECT package, filename, size, function FROM content WHERE hash = ?;",
(hashvalue,))
entries = [dict(package=package, filename=filename, size=size,
function=otherfunc)
for package, filename, size, otherfunc in fetchiter(cur)
if (function, otherfunc) in hash_functions]
if not entries:
raise NotFound()
params = dict(function=function, hashvalue=hashvalue, entries=entries)
return html_response(hash_template.render(params))
def show_source(self, package):
cur = self.db.cursor()
cur.execute("SELECT package FROM package WHERE source = ?;",
(package,))
binpkgs = dict.fromkeys(pkg for pkg, in fetchiter(cur))
if not binpkgs:
raise NotFound
cur.execute("SELECT package.package, sharing.package2, sharing.func1, sharing.func2, sharing.files, sharing.size FROM package JOIN sharing ON package.package = sharing.package1 WHERE package.source = ?;",
(package,))
for binary, otherbin, func1, func2, files, size in fetchiter(cur):
entry = dict(package=otherbin,
funccomb=function_combination(func1, func2),
duplicate=files, savable=size)
oldentry = binpkgs.get(binary)
if not (oldentry and oldentry["savable"] >= size):
binpkgs[binary] = entry
params = dict(source=package, packages=binpkgs)
return html_response(source_template.render(params))
def main():
app = Application(sqlite3.connect("test.sqlite3"))
#app = DebuggedApplication(app, evalex=True)
make_server("0.0.0.0", 8800, app).serve_forever()
if __name__ == "__main__":
main()
|