from flask import Flask, render_template from datetime import datetime import logging import os import subprocess import sqlite3 # Enable logging logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) logger = logging.getLogger(__name__) def create_app(config): app = Flask(__name__) app.config.from_pyfile(config) # initialize moment on the app within create_app() return app app = create_app("/etc/webdist/main.cfg") def _cursorRowToDict(cursor, row): items = {} for idx, key in enumerate(cursor.description): if key[0] in ['buildtime']: items[key[0]] = datetime.fromtimestamp(row[idx]) else: items[key[0]] = row[idx] return items def latest(): response = {} for repo in app.config['SEARCH_REPOS']: response[repo] = [] REPO_SOURCES_DB = "file:" + app.config['REPO_BASEDIR'] + f"{repo}-sources.db?mode=ro" conn = sqlite3.connect(REPO_SOURCES_DB, uri=True) cursor = conn.cursor() cursor.execute( f'SELECT * FROM sources ORDER BY buildtime DESC limit 100') for row in cursor: response[repo].append(_cursorRowToDict(cursor, row)) return response def query(qs): logger.info(f"Query of {qs}") response = {} for repo in app.config['SEARCH_REPOS']: response[repo] = [] REPO_SOURCES_DB = "file:" + app.config['REPO_BASEDIR'] + f"{repo}-sources.db?mode=ro" conn = sqlite3.connect(REPO_SOURCES_DB, uri=True) cursor = conn.cursor() cursor.execute( f'SELECT * FROM sources where name like "%%{qs}%%" or summary like "%%{qs}%%" ' 'ORDER BY name="{qs}" DESC, name like "{qs}%%" DESC, name like "%%{qs}%%" DESC limit 10') for row in cursor: response[repo].append(_cursorRowToDict(cursor, row)) return response @app.route("/") def _search(): return render_template("index.html") @app.route("/latest") def _latest(): body = "" data = latest() return render_template("latest.html", data=data) @app.route("/query/") def _query(qs): body = "" data = query(qs) return render_template("query.html", data=data)