webdist/app/webdist.py

102 lines
2.9 KiB
Python
Raw Normal View History

"""
Webdist Flask backend
"""
2022-10-02 18:29:41 +02:00
import logging
import subprocess
import sqlite3
from datetime import datetime
from flask import Flask, render_template
2022-10-02 18:29:41 +02:00
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
def create_app(config):
""" creates and returns the Flask app """
_app = Flask(__name__)
_app.config.from_pyfile(config)
2022-10-02 18:29:41 +02:00
# initialize moment on the app within create_app()
return _app
2022-10-02 18:29:41 +02:00
app = create_app("/etc/webdist/main.cfg")
def cursor_row_to_dict(cursor, row):
""" Converts a sqlite3 cursor row into a dict """
2022-10-02 18:29:41 +02:00
items = {}
for idx, key in enumerate(cursor.description):
if key[0] in ['buildtime']:
items[key[0]] = datetime.fromtimestamp(row[idx]).isoformat()
2022-10-02 18:29:41 +02:00
else:
items[key[0]] = row[idx]
return items
def latest():
""" Returns a dict of latest packages """
2022-10-02 18:29:41 +02:00
response = {}
for repo in app.config['REPOS']:
2022-10-02 18:29:41 +02:00
response[repo] = []
REPO_SOURCES_DB = "file:" + app.config['REPOS'][repo]['path'] + \
f"{app.config['REPOS'][repo]['dbname']}-sources.db?mode=ro"
logger.info(REPO_SOURCES_DB)
2022-10-02 18:29:41 +02:00
conn = sqlite3.connect(REPO_SOURCES_DB, uri=True)
cursor = conn.cursor()
cursor.execute(
'SELECT * FROM sources ORDER BY buildtime DESC limit 100')
2022-10-02 18:29:41 +02:00
for row in cursor:
response[repo].append(cursor_row_to_dict(cursor, row))
2022-10-02 18:29:41 +02:00
return response
def query(querystring):
""" Performs a query of a package """
logger.info("Query of %s", querystring)
2022-10-02 18:29:41 +02:00
response = {}
for repo in app.config['REPOS']:
2022-10-02 18:29:41 +02:00
response[repo] = []
REPO_SOURCES_DB = "file:" + app.config['REPOS'][repo]['path'] + \
f"{app.config['REPOS'][repo]['dbname']}-sources.db?mode=ro"
2022-10-02 18:29:41 +02:00
conn = sqlite3.connect(REPO_SOURCES_DB, uri=True)
cursor = conn.cursor()
cursor.execute(
f'SELECT * FROM sources where name like "%%{querystring}%%" '
'or summary like "%%{querystring}%%" '
'ORDER BY name="{querystring}" DESC, name like "{qs}%%" DESC, '
'name like "%%{querystring}%%" DESC limit 10')
2022-10-02 18:29:41 +02:00
for row in cursor:
response[repo].append(cursor_row_to_dict(cursor, row))
2022-10-02 18:29:41 +02:00
return response
@app.route("/")
def _search():
return render_template("index.html")
@app.route("/latest")
def _latest():
data = latest()
return render_template("latest.html", data=data)
@app.route("/button/<name>")
def _button(name):
script = app.config['BUTTONS'][name]['script']
output = subprocess.check_output([script])
return render_template("button.html",output=output.decode())
@app.route("/query/<querystring>")
def _query(querystring):
data = query(querystring)
2022-10-02 18:29:41 +02:00
return render_template("query.html", data=data)
@app.route("/api/v1/latest")
def _hello():
data = latest()
return {'result': data }