From 8d06ac2e9a75bdba891b1866397ea2ece79b7e0e Mon Sep 17 00:00:00 2001 From: Youen Date: Sat, 13 Aug 2022 12:38:25 +0200 Subject: [PATCH] pulling before building --- src/api/document.py | 10 +++++++--- src/data/document.py | 28 ++++++++++------------------ src/web_utils/run.py | 14 ++++++++++++++ test-document-clone.sh | 16 ++++++++++++++++ 4 files changed, 47 insertions(+), 21 deletions(-) create mode 100644 src/web_utils/run.py create mode 100755 test-document-clone.sh diff --git a/src/api/document.py b/src/api/document.py index dc52911..bd39ccc 100644 --- a/src/api/document.py +++ b/src/api/document.py @@ -10,9 +10,13 @@ bp = Blueprint('document', __name__, url_prefix='/api/doc') def build(): doc_name = get_arg('doc') branch = get_arg('branch', 'master') - doc = Document(doc_name, branch) - output = doc.build() + + output = "" + output += "\n# Pulling source...\n" + output += doc.pull() + + output += "\n# Compiling...\n" + output += doc.build() return output.replace('\n', '
') - diff --git a/src/data/document.py b/src/data/document.py index d4ada63..9a8a71d 100644 --- a/src/data/document.py +++ b/src/data/document.py @@ -1,5 +1,5 @@ import os -from subprocess import Popen, PIPE, STDOUT +from web_utils.run import run document_root = None @@ -8,30 +8,22 @@ class Document: 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) + raise Exception("Invalid document path for "+doc_name+"@"+branch) + + if not os.path.isdir(doc_path + "/repo/.git"): + raise Exception("This document does not exist: "+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 + cmd = "sphinx-build -M html \""+self.doc_path + "/repo/source\" \""+self.doc_path+"/build\"" + return run(cmd) + + def pull(self): + return run("cd \"" + self.doc_path + "/repo\" && git pull") def set_document_root(dir): global document_root diff --git a/src/web_utils/run.py b/src/web_utils/run.py new file mode 100644 index 0000000..b941b38 --- /dev/null +++ b/src/web_utils/run.py @@ -0,0 +1,14 @@ +from subprocess import Popen, PIPE, STDOUT + +def run(cmd): + 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("Command failed ("+str(p.returncode)+")\n"+cmd+"\n"+outputStr) + + return outputStr diff --git a/test-document-clone.sh b/test-document-clone.sh new file mode 100755 index 0000000..be148ce --- /dev/null +++ b/test-document-clone.sh @@ -0,0 +1,16 @@ +TARGET_DIR=data/doc/test/master/repo +REPO_ORIGIN=https://git.vhelio.org/vhelio/vheliotech-guide-de-montage.git +REPO_BRANCH=main +REPO_SOURCE_DIR=source + +[ -e $TARGET_DIR ] && rm -rf $TARGET_DIR + +mkdir -p $TARGET_DIR +cd $TARGET_DIR + +git init --initial-branch=$REPO_BRANCH +git remote add -f origin $REPO_ORIGIN +git sparse-checkout init +git sparse-checkout set "$REPO_SOURCE_DIR" +git pull origin $REPO_BRANCH +git branch --set-upstream-to=origin/$REPO_BRANCH $REPO_BRANCH