summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHelmut Grohne <helmut@subdivi.de>2021-12-31 13:00:29 +0100
committerHelmut Grohne <helmut@subdivi.de>2021-12-31 13:00:29 +0100
commit1631e91b116ebf04ba9bd332e12c2f165263088b (patch)
tree49495c0756f0b1191c804a85e4d0a678f869dd4e
parent0b4882ecf657d70dd3236dcf176e083bf08dccdd (diff)
downloaddebian-dedup-1631e91b116ebf04ba9bd332e12c2f165263088b.tar.gz
webapp.py: consistently close cursors using context managers
-rwxr-xr-xwebapp.py72
1 files changed, 36 insertions, 36 deletions
diff --git a/webapp.py b/webapp.py
index 0d9e3f9..162a5a4 100755
--- a/webapp.py
+++ b/webapp.py
@@ -84,6 +84,9 @@ class Application:
Rule("/source/<package>", methods=("GET",), endpoint="source"),
])
+ def cursor(self):
+ return contextlib.closing(self.db.cursor())
+
@Request.application
def __call__(self, request):
mapadapter = self.routingmap.bind_to_environ(request.environ)
@@ -112,7 +115,7 @@ class Application:
return e
def get_details(self, package):
- with contextlib.closing(self.db.cursor()) as cur:
+ with self.cursor() as cur:
cur.execute("SELECT id, version, architecture FROM package WHERE name = ?;",
(package,))
row = cur.fetchone()
@@ -132,14 +135,14 @@ class Application:
return details
def get_dependencies(self, pid):
- with contextlib.closing(self.db.cursor()) as cur:
+ with self.cursor() as cur:
cur.execute("SELECT required FROM dependency WHERE pid = ?;",
(pid,))
return set(row[0] for row in fetchiter(cur))
def cached_sharedstats(self, pid):
sharedstats = {}
- with contextlib.closing(self.db.cursor()) as cur:
+ with self.cursor() as cur:
cur.execute("SELECT pid2, package.name, f1.name, f2.name, files, size FROM sharing JOIN package ON sharing.pid2 = package.id JOIN function AS f1 ON sharing.fid1 = f1.id JOIN function AS f2 ON sharing.fid2 = f2.id WHERE pid1 = ? AND f1.eqclass = f2.eqclass;",
(pid,))
for pid2, package2, func1, func2, files, size in fetchiter(cur):
@@ -156,11 +159,10 @@ class Application:
params["dependencies"] = self.get_dependencies(params["pid"])
params["shared"] = self.cached_sharedstats(params["pid"])
params["urlroot"] = ".."
- cur = self.db.cursor()
- cur.execute("SELECT content.filename, issue.issue FROM content JOIN issue ON content.id = issue.cid WHERE content.pid = ?;",
- (params["pid"],))
- params["issues"] = dict(cur.fetchall())
- cur.close()
+ with self.cursor() as cur:
+ cur.execute("SELECT content.filename, issue.issue FROM content JOIN issue ON content.id = issue.cid WHERE content.pid = ?;",
+ (params["pid"],))
+ params["issues"] = dict(cur.fetchall())
return html_response(package_template.stream(params))
def compute_comparison(self, pid1, pid2):
@@ -173,35 +175,33 @@ class Application:
* matches: A mapping from filenames in package 2 (pid2) to a mapping
from hash function pairs to hash values.
"""
- cur = self.db.cursor()
- cur.execute("SELECT content.id, content.filename, content.size, hash.hash FROM content JOIN hash ON content.id = hash.cid JOIN duplicate ON content.id = duplicate.cid JOIN function ON hash.fid = function.id WHERE pid = ? AND function.name = 'sha512' ORDER BY size DESC;",
- (pid1,))
- cursize = -1
- files = dict()
- minmatch = 2 if pid1 == pid2 else 1
- cur2 = self.db.cursor()
- for cid, filename, size, hashvalue in fetchiter(cur):
- if cursize != size:
- for entry in files.values():
- if len(entry["matches"]) >= minmatch:
- yield entry
- files.clear()
- cursize = size
+ with self.cursor() as cur, self.cursor() as cur2:
+ cur.execute("SELECT content.id, content.filename, content.size, hash.hash FROM content JOIN hash ON content.id = hash.cid JOIN duplicate ON content.id = duplicate.cid JOIN function ON hash.fid = function.id WHERE pid = ? AND function.name = 'sha512' ORDER BY size DESC;",
+ (pid1,))
+ cursize = -1
+ files = dict()
+ minmatch = 2 if pid1 == pid2 else 1
+ cur2 = self.db.cursor()
+ for cid, filename, size, hashvalue in fetchiter(cur):
+ if cursize != size:
+ for entry in files.values():
+ if len(entry["matches"]) >= minmatch:
+ yield entry
+ files.clear()
+ cursize = size
- if hashvalue in files:
- files[hashvalue]["filenames"].add(filename)
- continue
+ if hashvalue in files:
+ files[hashvalue]["filenames"].add(filename)
+ continue
- entry = dict(filenames=set((filename,)), size=size, matches={})
- files[hashvalue] = entry
+ entry = dict(filenames=set((filename,)), size=size, matches={})
+ files[hashvalue] = entry
- cur2.execute("SELECT fa.name, ha.hash, fb.name, filename FROM hash AS ha JOIN hash AS hb ON ha.hash = hb.hash JOIN content ON hb.cid = content.id JOIN function AS fa ON ha.fid = fa.id JOIN function AS fb ON hb.fid = fb.id WHERE ha.cid = ? AND pid = ? AND fa.eqclass = fb.eqclass;",
- (cid, pid2))
- for func1, hashvalue, func2, filename in fetchiter(cur2):
- entry["matches"].setdefault(filename, {})[func1, func2] = \
- hashvalue
- cur2.close()
- cur.close()
+ cur2.execute("SELECT fa.name, ha.hash, fb.name, filename FROM hash AS ha JOIN hash AS hb ON ha.hash = hb.hash JOIN content ON hb.cid = content.id JOIN function AS fa ON ha.fid = fa.id JOIN function AS fb ON hb.fid = fb.id WHERE ha.cid = ? AND pid = ? AND fa.eqclass = fb.eqclass;",
+ (cid, pid2))
+ for func1, hashvalue, func2, filename in fetchiter(cur2):
+ entry["matches"].setdefault(filename, {})[func1, func2] = \
+ hashvalue
for entry in files.values():
if len(entry["matches"]) >= minmatch:
@@ -221,7 +221,7 @@ class Application:
return html_response(detail_template.stream(params))
def show_hash(self, function, hashvalue):
- with contextlib.closing(self.db.cursor()) as cur:
+ with self.cursor() as cur:
cur.execute("SELECT package.name, content.filename, content.size, f2.name FROM hash JOIN content ON hash.cid = content.id JOIN package ON content.pid = package.id JOIN function AS f2 ON hash.fid = f2.id JOIN function AS f1 ON f2.eqclass = f1.eqclass WHERE f1.name = ? AND hash = ?;",
(function, hashvalue,))
entries = [dict(package=package, filename=filename, size=size,
@@ -242,7 +242,7 @@ class Application:
return html_response(hash_template.stream(params))
def show_source(self, package):
- with contextlib.closing(self.db.cursor()) as cur:
+ with self.cursor() as cur:
cur.execute("SELECT name FROM package WHERE source = ?;",
(package,))
binpkgs = dict.fromkeys(pkg for pkg, in fetchiter(cur))