From f87671e0a29016dd54dfbd1b8f613fd7385b8841 Mon Sep 17 00:00:00 2001 From: brent s Date: Tue, 12 Nov 2019 01:39:34 -0500 Subject: [PATCH] initial commit --- .gitignore | 21 +++++++++++++++ app/__init__.py | 7 +++++ app/views.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++++ config.py | 3 +++ run.py | 6 +++++ 5 files changed, 105 insertions(+) create mode 100644 .gitignore create mode 100644 app/__init__.py create mode 100644 app/views.py create mode 100644 config.py create mode 100755 run.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ba715fa --- /dev/null +++ b/.gitignore @@ -0,0 +1,21 @@ +*.7z +*.bak +*.deb +*.jar +*.pkg.tar.xz +*.rar +*.run +*.sig +*.tar +*.tar.bz2 +*.tar.gz +*.tar.xz +*.tbz +*.tbz2 +*.tgz +*.txz +*.zip +.*.swp +.editix +__pycache__/ +test.py diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 0000000..24fdf84 --- /dev/null +++ b/app/__init__.py @@ -0,0 +1,7 @@ +from flask import Flask + +app = Flask(__name__, instance_relative_config = True) + +from app import views + +app.config.from_object('config') diff --git a/app/views.py b/app/views.py new file mode 100644 index 0000000..204a44e --- /dev/null +++ b/app/views.py @@ -0,0 +1,68 @@ +import datetime +import os +import subprocess +## +import git +from flask import render_template, make_response, request, Response +## +from app import app + + +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 + 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) + lastcommit = repo.head.commit.authored_datetime + now = datetime.datetime.now(datetime.timezone.utc) + if (now - lastcommit) > datetime.timedelta(hours = update_hours): + print('Fetching update. Current HEAD: {0}'.format(repo.head.commit.hexsha)) + repo.remotes.origin.fetch() + return(oldhead) + + +@app.route('/', methods = ['GET']) +def index(): + oldref = chkbranch(ref_param = request.args.get('ref')) + docsdir = os.path.join(repo_path, 'docs') + oldref.checkout() + 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) + return(cmd.stdout.decode('utf-8')) + + +@app.route('/aif.xsd', methods = ['GET']) +def xsd(): + oldref = chkbranch(ref_param = request.args.get('ref')) + oldref.checkout() + 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' + return(resp) + diff --git a/config.py b/config.py new file mode 100644 index 0000000..e237be0 --- /dev/null +++ b/config.py @@ -0,0 +1,3 @@ +# Flask debugging - DISABLE FOR PRODUCTION ENVIRONMENTS +#DEBUG = True +DEBUG = False diff --git a/run.py b/run.py new file mode 100755 index 0000000..8db5665 --- /dev/null +++ b/run.py @@ -0,0 +1,6 @@ +#!/usr/bin/env python3 + +from app import app + +if __name__ == '__main__': + app.run(debug = True)