67 lines
2.1 KiB
Python
67 lines
2.1 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Python script to parse and clone a repo
|
|
in order for asciidoctor to be able to access included files
|
|
|
|
Doesn't overwrite folder if it already exists.
|
|
|
|
Cache asciidoctor result for performance.
|
|
|
|
Parameters :
|
|
in : a relative path as given by GITEA_PREFIX_SRC (https://docs.gitea.io/en-us/config-cheat-sheet/#markup-markup)
|
|
return : The name of the folder containing the cloned repo
|
|
"""
|
|
|
|
import sys
|
|
import subprocess
|
|
import os
|
|
import zlib
|
|
import shutil
|
|
|
|
if(len(sys.argv) != 4):
|
|
sys.exit("wrong number of args <source repo> <prefix> <cache path>") # wrong number of args
|
|
|
|
# Prefix for the path where repository are saved, can be an url
|
|
PATH_PREFIX = sys.argv[2]
|
|
# Prefix for where we will clone the repos
|
|
CACHE_FOLDER_PATH = sys.argv[3]
|
|
# Filename for the input buffer
|
|
INPUT_BUFFER_FILENAME = "input_buff"
|
|
|
|
# get repo name (/username/repo)
|
|
repo_path = sys.argv[1].split('/',3)
|
|
if(len(repo_path)<2):
|
|
sys.exit("Wrong path format passed")
|
|
|
|
repo_name = "/".join([CACHE_FOLDER_PATH,repo_path[2]])
|
|
repo_path = "/".join(repo_path[:3])
|
|
repo_path = PATH_PREFIX + repo_path
|
|
repo_path = repo_path.lower()
|
|
|
|
if not os.path.isdir(CACHE_FOLDER_PATH):
|
|
os.makedirs(CACHE_FOLDER_PATH)
|
|
|
|
if not os.path.isdir(repo_name):
|
|
subprocess.run(["git", "clone", repo_path, repo_name], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
|
|
|
|
inputBufferPath = "/".join([repo_name,INPUT_BUFFER_FILENAME])
|
|
|
|
# get input hash and save stdin to file
|
|
inHash = 0
|
|
b = bytearray(128*1024)
|
|
mv = memoryview(b)
|
|
with open(inputBufferPath,'wb') as out:
|
|
while n := sys.stdin.buffer.readinto(mv):
|
|
inHash = zlib.adler32(mv[:n],inHash)
|
|
out.write(mv[:n])
|
|
|
|
cachedOutPath = "/".join([repo_name,str(inHash)])
|
|
|
|
# If not already rendered
|
|
if not os.path.isfile(cachedOutPath):
|
|
subprocess.run(["asciidoctor", "-s", "--safe-mode", "server", "-a", "showtitle", "-a", "source-highlighter=rouge", "-a", "rouge-css=style", "-a", "rouge-style=base16.monokai.dark", f"--out-file={str(inHash)}", f"--base-dir={repo_name}", inputBufferPath])
|
|
|
|
# return rendered(or cached) output to stdout
|
|
with open(cachedOutPath, "rb") as f:
|
|
shutil.copyfileobj(f, sys.stdout.buffer)
|