From cd57a96ac2e5e824068a2d0e60901eb31b69e157 Mon Sep 17 00:00:00 2001 From: Youen Date: Fri, 12 Aug 2022 13:37:50 +0200 Subject: [PATCH] Added web api that can build a document --- .gitignore | 1 + requirements.txt | 50 ++++++++++++++++++++++++++++++++-------- src/api/document.py | 12 +++++++++- src/app.py | 13 +++++++---- src/data/document.py | 43 ++++++++++++++++++++++++++++++++++ src/web_utils/get_arg.py | 11 +++++++++ 6 files changed, 116 insertions(+), 14 deletions(-) create mode 100644 src/data/document.py create mode 100644 src/web_utils/get_arg.py diff --git a/.gitignore b/.gitignore index 27009d8..4c2d777 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /venv __pycache__ +/data/doc diff --git a/requirements.txt b/requirements.txt index 97e4624..865daa4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,9 +1,41 @@ -click==8.1.3 -colorama==0.4.5 -Flask==2.2.2 -importlib-metadata==4.12.0 -itsdangerous==2.1.2 -Jinja2==3.1.2 -MarkupSafe==2.1.1 -Werkzeug==2.2.2 -zipp==3.8.1 +alabaster==0.7.12 +Babel==2.10.3 +certifi==2022.6.15 +charset-normalizer==2.1.0 +click==8.1.3 +colorama==0.4.5 +docutils==0.17.1 +Flask==2.2.2 +idna==3.3 +imagesize==1.4.1 +importlib-metadata==4.12.0 +itsdangerous==2.1.2 +Jinja2==3.1.2 +linkify-it-py==2.0.0 +markdown-it-py==2.1.0 +MarkupSafe==2.1.1 +mdit-py-plugins==0.3.0 +mdurl==0.1.1 +myst-parser==0.18.0 +packaging==21.3 +pkg_resources==0.0.0 +Pygments==2.12.0 +pyparsing==3.0.9 +pytz==2022.2 +PyYAML==6.0 +requests==2.28.1 +snowballstemmer==2.2.0 +Sphinx==5.1.1 +sphinx-rtd-theme==1.0.0 +sphinxcontrib-applehelp==1.0.2 +sphinxcontrib-devhelp==1.0.2 +sphinxcontrib-htmlhelp==2.0.0 +sphinxcontrib-jsmath==1.0.1 +sphinxcontrib-qthelp==1.0.3 +sphinxcontrib-serializinghtml==1.1.5 +sphinxcontrib-svg2pdfconverter==1.2.0 +typing_extensions==4.3.0 +uc-micro-py==1.0.1 +urllib3==1.26.11 +Werkzeug==2.2.2 +zipp==3.8.1 diff --git a/src/api/document.py b/src/api/document.py index 0d1dc25..dc52911 100644 --- a/src/api/document.py +++ b/src/api/document.py @@ -1,8 +1,18 @@ from flask import Blueprint, request +from markupsafe import escape +from web_utils.get_arg import get_arg + +from data.document import Document bp = Blueprint('document', __name__, url_prefix='/api/doc') @bp.route('/build') def build(): - return '

Building '+request.args.get('doc')+'@'+request.args.get('branch')+' ...

' + doc_name = get_arg('doc') + branch = get_arg('branch', 'master') + + doc = Document(doc_name, branch) + output = doc.build() + + return output.replace('\n', '
') diff --git a/src/app.py b/src/app.py index 85069a7..be16c7e 100644 --- a/src/app.py +++ b/src/app.py @@ -1,10 +1,15 @@ +import os from flask import Flask +import data.document def create_app(): - app = Flask(__name__) + app = Flask(__name__) + + src_path = os.path.dirname(os.path.realpath(__file__)) + data.document.set_document_root(os.path.realpath(src_path+'/../data/doc')) - from api import document - app.register_blueprint(document.bp) + from api import document + app.register_blueprint(document.bp) - return app + return app diff --git a/src/data/document.py b/src/data/document.py new file mode 100644 index 0000000..d4ada63 --- /dev/null +++ b/src/data/document.py @@ -0,0 +1,43 @@ +import os +from subprocess import Popen, PIPE, STDOUT + +document_root = None + +class Document: + def __init__(self, doc_name, branch = 'master'): + self.doc_name = doc_name + self.branch = branch + + #print(get_document_root()) + #print(doc_name) + #print(branch) + + doc_path = os.path.realpath(get_document_root()+'/'+doc_name+'/'+branch) + if not doc_path.startswith(get_document_root()): + raise Exception("Invalid document path for '"+doc_name+"'@'"+branch) + + self.doc_path = doc_path + + def build(self): + #venv_path = os.getenv('VIRTUAL_ENV') + cmd = "sphinx-build -M html \""+self.doc_path + "/source\" \""+self.doc_path+"/build\"" + p = Popen(cmd, stdout = PIPE, stderr = STDOUT, shell = True) + outputStr = "" + for line in p.stdout: + outputStr += line.decode() + + p.wait() + + if p.returncode != 0: + raise Exception("Build failed ("+str(p.returncode)+")\n"+outputStr) + + return outputStr + +def set_document_root(dir): + global document_root + document_root = dir + +def get_document_root(): + global document_root + return document_root + diff --git a/src/web_utils/get_arg.py b/src/web_utils/get_arg.py new file mode 100644 index 0000000..063d1cd --- /dev/null +++ b/src/web_utils/get_arg.py @@ -0,0 +1,11 @@ +from flask import request + +def get_arg(arg_name, default_value = None): + result = request.args.get(arg_name) + if result == None: + if default_value == None: + raise Exception("Missing query string parameter '"+arg_name+"'") + else: + return default_value + return result +