2019-10-09 14:33:42 +02:00
|
|
|
#!/usr/bin/python3
|
2014-08-07 13:56:08 +02:00
|
|
|
|
|
|
|
from flask import Flask
|
|
|
|
from flask import request
|
|
|
|
from flask import make_response
|
|
|
|
|
|
|
|
import re
|
|
|
|
import os
|
2019-10-09 14:33:42 +02:00
|
|
|
from urllib.parse import urlparse
|
2014-08-07 13:56:08 +02:00
|
|
|
|
2018-01-17 16:40:55 -06:00
|
|
|
digits_re = re.compile('^[0-9.]+$')
|
2014-08-07 13:56:08 +02:00
|
|
|
|
2017-04-18 22:04:59 -05:00
|
|
|
BASE_DIR = '/var/lib'
|
2014-08-07 13:56:08 +02:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
2022-02-18 17:15:48 +01:00
|
|
|
|
2017-04-18 22:04:59 -05:00
|
|
|
def get_dir(url):
|
|
|
|
return os.path.join(BASE_DIR, urlparse(url).path.lstrip('/'))
|
|
|
|
|
2022-02-18 17:15:48 +01:00
|
|
|
|
2014-08-07 13:56:08 +02:00
|
|
|
@app.route('/')
|
|
|
|
def list():
|
2017-04-18 22:04:59 -05:00
|
|
|
_dir = get_dir(request.url_root)
|
2014-08-07 13:56:08 +02:00
|
|
|
fn = os.path.join(_dir, 'current')
|
|
|
|
current = None
|
|
|
|
if os.path.exists(fn):
|
|
|
|
current = os.readlink(fn)
|
|
|
|
|
|
|
|
ret = ''
|
|
|
|
for i in sorted(os.listdir(_dir), reverse=True):
|
|
|
|
if not digits_re.match(i):
|
|
|
|
continue
|
2019-11-27 11:08:06 +01:00
|
|
|
ret = ret + '<a href="diff/%s">%s</a>' % (i, i)
|
2014-08-07 13:56:08 +02:00
|
|
|
if i == current:
|
|
|
|
ret = ret + " <--"
|
|
|
|
ret = ret + '<br/>'
|
|
|
|
return ret
|
|
|
|
|
2022-02-18 17:15:48 +01:00
|
|
|
|
2014-08-07 13:56:08 +02:00
|
|
|
@app.route('/current', methods=['GET', 'POST'])
|
|
|
|
def current():
|
2017-04-18 22:04:59 -05:00
|
|
|
_dir = get_dir(request.url_root)
|
2014-08-07 13:56:08 +02:00
|
|
|
fn = os.path.join(_dir, 'current')
|
|
|
|
if request.method == 'POST':
|
2021-09-21 14:20:08 +02:00
|
|
|
if 'version' not in request.form:
|
2014-08-07 13:56:08 +02:00
|
|
|
return "missing version", 400
|
|
|
|
version = request.form['version']
|
|
|
|
if not digits_re.match(version):
|
|
|
|
return "malformed version", 400
|
|
|
|
if not os.path.exists(os.path.join(_dir, version)):
|
|
|
|
return "invalid version", 400
|
2019-11-27 11:08:06 +01:00
|
|
|
tmpfn = os.path.join(_dir, '.' + version)
|
2014-08-07 13:56:08 +02:00
|
|
|
app.logger.debug(tmpfn)
|
|
|
|
if os.path.exists(tmpfn):
|
|
|
|
os.unlink(tmpfn)
|
|
|
|
os.symlink(version, tmpfn)
|
|
|
|
os.rename(tmpfn, fn)
|
|
|
|
return "ok"
|
|
|
|
else:
|
|
|
|
if not os.path.exists(fn):
|
|
|
|
return "", 404
|
|
|
|
return os.readlink(fn)
|
|
|
|
|
2022-02-18 17:15:48 +01:00
|
|
|
|
2017-05-01 20:57:27 -05:00
|
|
|
@app.route('/diff/<version>')
|
2014-08-07 13:56:08 +02:00
|
|
|
def diff(version):
|
2017-04-18 22:04:59 -05:00
|
|
|
_dir = get_dir(request.url_root)
|
2014-08-07 13:56:08 +02:00
|
|
|
fn = os.path.join(_dir, 'current')
|
|
|
|
if not os.path.exists(fn):
|
|
|
|
return "current version doesn't exist", 404
|
|
|
|
if not os.path.exists(os.path.join(_dir, version)):
|
|
|
|
return "invalid version", 400
|
|
|
|
import subprocess
|
2019-12-10 12:41:11 +01:00
|
|
|
cmd = [os.path.dirname(os.path.abspath(__file__)) + '/factory-package-news.py',
|
2022-02-18 13:50:43 +01:00
|
|
|
'diff', '--dir', _dir, "current", version]
|
2014-08-07 13:56:08 +02:00
|
|
|
app.logger.debug(cmd)
|
|
|
|
response = make_response(subprocess.check_output(cmd))
|
|
|
|
response.content_type = "text/plain"
|
|
|
|
return response
|
|
|
|
|
2022-02-18 17:11:46 +01:00
|
|
|
|
2014-08-07 13:56:08 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
from optparse import OptionParser
|
|
|
|
parser = OptionParser()
|
|
|
|
parser.add_option("--debug", action="store_true", help="debug output")
|
|
|
|
parser.add_option("--host", metavar="IP", help="ip to listen to")
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
app.run(debug=options.debug, host=options.host)
|
|
|
|
|
2014-08-07 14:12:59 +02:00
|
|
|
application = app
|