def search(pattern): db = get_db() files = {} patches = set() for file_id in get_db().execute("SELECT id FROM blobs"): for patch_id in patches_for(file_id[0]): files.setdefault(patch_id, set()) files[patch_id].update([file_id[0]]) patches.update([os.path.join(config["BASE_PATH"], binascii.hexlify(patch_id).decode("utf-8"))]) grep = subprocess.Popen(["xzgrep", "-F", "-l", pattern]+list(patches), stdout=subprocess.PIPE) matches, _ = grep.communicate() if grep.returncode > 1: raise Exception("xzgrep failed with %s" % grep.returncode) matches = matches.splitlines() possible_matches = set() for match in map(lambda s: binascii.unhexlify(os.path.basename(s.decode("utf-8"))), matches): possible_matches.update(files[match]) matches = set() for file in possible_matches: for line in rebuild(file): if line.decode("utf-8").find(pattern) >= 0: matches.update([file]) break return matches
def editcat(): url = flask.request.form["url"] file_id = flask.request.form["id"] name = flask.request.form["title"] print("Editing cat: " + file_id) db = get_db() user = verify() if user == None: flask.abort(403) try: rev = decode_rev(url[3::]) except: flask.abort(404) c = get_db().execute("SELECT revision, name FROM files WHERE substr(revision, 1, ?) = ?", (len(rev), sqlite3.Binary(rev))) if c is None: flask.abort(404) rev = c.fetchone()[0] plaincat = b"".join(rebuild(rev)).decode("utf-8") lexers = sorted(map(lambda x: (x[0], x[1][0]), pygments.lexers.get_all_lexers())) return flask.render_template("editcat.html", plaincat=plaincat, lexers=lexers, file_id=file_id, name=name)