aif-site/app/views.py

67 lines
1.7 KiB
Python
Raw Normal View History

2019-11-12 01:39:34 -05:00
import os
import subprocess
2019-11-12 02:43:56 -05:00
# import tempfile
2019-11-12 01:39:34 -05:00
##
import git
from flask import render_template, make_response, request, Response
##
from app import app
2019-11-12 02:43:56 -05:00
# statfile = tempfile.mkstemp(prefix = '.aif-site.')[1]
2019-11-12 01:39:34 -05:00
repo_path = '/srv/python/aif_ng'
repo_uri = '/opt/git/repositories/aif-ng.git'
branch = 'v2_rewrite'
update_hours = 1
if not os.path.isdir(repo_path):
repo = git.Repo.clone_from(repo_uri, repo_path)
else:
repo = git.Repo(repo_path)
def chkbranch(ref_param = None):
oldhead = repo.head.ref
2019-11-12 02:43:56 -05:00
repo.remotes.origin.fetch()
2019-11-12 01:39:34 -05:00
if not ref_param:
ref = branch
else:
ref = ref_param
if not ref_param:
if repo.active_branch.name != branch:
repo.git.checkout(branch)
else:
repo.git.checkout(ref)
return(oldhead)
@app.route('/', methods = ['GET'])
def index():
oldref = chkbranch(ref_param = request.args.get('ref'))
docsdir = os.path.join(repo_path, 'docs')
2019-11-12 02:43:56 -05:00
docspath = os.path.join(docsdir, 'MANUAL.adoc')
2019-11-12 01:39:34 -05:00
for fname in ('MANUAL', 'README'):
fpath = os.path.join(docsdir, '{0}.adoc'.format(fname))
if os.path.isfile(fpath):
docspath = fpath
break
cmd = subprocess.run(['asciidoctor',
docspath,
'-o', '-'],
stdout = subprocess.PIPE)
2019-11-12 02:43:56 -05:00
oldref.checkout()
2019-11-12 01:39:34 -05:00
return(cmd.stdout.decode('utf-8'))
@app.route('/aif.xsd', methods = ['GET'])
def xsd():
oldref = chkbranch(ref_param = request.args.get('ref'))
with open(os.path.join(repo_path, 'aif.xsd'), 'r') as fh:
xsd_raw = fh.read()
resp = Response(xsd_raw)
resp.headers['content-type'] = 'text/xml'
2019-11-12 02:43:56 -05:00
oldref.checkout()
2019-11-12 01:39:34 -05:00
return(resp)