summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorSoniEx2 <endermoneymod@gmail.com>2021-09-20 11:46:23 -0300
committerSoniEx2 <endermoneymod@gmail.com>2021-09-20 11:46:23 -0300
commit3077ca112d587b84bfdd64599cedc2ed91715281 (patch)
tree54eb34a668d138932307080eca117d01a4ca96f9
parentbfb1518052235e21f52c7bf0f6d0015569765801 (diff)
Initial soupault support
-rwxr-xr-xgit-hooks/post-receive88
1 files changed, 87 insertions, 1 deletions
diff --git a/git-hooks/post-receive b/git-hooks/post-receive
index 53b5cab..c95ca41 100755
--- a/git-hooks/post-receive
+++ b/git-hooks/post-receive
@@ -77,19 +77,76 @@ def find_lexer(text, meta):
         pass
     return None
 
+def check_soupault_version(soupault):
+    """Checks if the given soupault command provides the correct version."""
+    # e.g. soupault 3.1.1
+    # versions up to 3.1.0 have a major security flaw which makes them
+    # unsuitable for use with this program
+    version = subprocess.run([
+        soupault,
+        "--version"
+    ], stdout=subprocess.PIPE, check=True).stdout.splitlines()[0].decode()
+    if not version.startswith("soupault 3."):
+        print("please use soupault 3.1.1 or newer")
+        exit()
+    if version.startswith("soupault 3.0."):
+        print("please use soupault 3.1.1 or newer")
+        exit()
+    # semver doesn't allow leading 0 on any numeric fields, so this is safe
+    if version.startswith("soupault 3.1.0"):
+        print("please use soupault 3.1.1 or newer")
+        exit()
+
+def find_soupault_config(dirs):
+    for d in dirs:
+        path = pathlib.Path(d) / "soupault.toml"
+        try:
+            f = path.open()
+            print("using {} as soupault config".format(path))
+            return f
+        except OSError as e:
+            pass
+    print("couldn't find soupault config. tried paths:")
+    for d in dirs:
+        path = pathlib.Path(d) / "soupault.toml"
+        print(path)
+    exit()
+
 CACHE_HOME = os.environ.get('XDG_CACHE_HOME', '')
 if not CACHE_HOME:
     CACHE_HOME = os.environ['HOME'] + '/.cache'
 CACHE_HOME = CACHE_HOME + "/htmlgdump"
 
+CONFIG_HOME = os.environ.get('XDG_CONFIG_HOME', '')
+if not CONFIG_HOME:
+    CONFIG_HOME = os.environ['HOME'] + '/.config'
+CONFIG_HOME = CONFIG_HOME + "/htmlgdump"
+
+CONFIG_DIRS = os.environ.get('XDG_CONFIG_DIRS', '')
+if not CONFIG_DIRS:
+    CONFIG_DIRS = '/etc/xdg'
+# TODO check if this is correct
+CONFIG_DIRS = [config_dir + "/htmlgdump" for config_dir in CONFIG_DIRS.split(':')]
+
+soupault_config = find_soupault_config([CONFIG_HOME] + CONFIG_DIRS)
+
 # post-receive runs on $GIT_DIR
 repo = pygit2.Repository(os.getcwd())
 
 try:
     name = pathlib.Path.cwd().relative_to(repo.config["htmlgdump.base"])
 except (KeyError, ValueError):
+    print("please set htmlgdump.base")
     exit()
 
+soupault = "soupault"
+try:
+    soupault = repo.config["htmlgdump.soupault"]
+except (KeyError, ValueError):
+    pass
+
+check_soupault_version(soupault)
+
 changes = [GitChange(*l.rstrip("\n").split(" ", 2)) for l in sys.stdin]
 
 gen_dir = pathlib.Path(CACHE_HOME) / name / "gen"
@@ -269,6 +326,35 @@ with path.open("w") as f:
         f.write("<li><a href=\"./{}\">{}</a></li>".format(quoted, quoted))
     f.write("</ul></body></html>")
 
+print("preparing build")
+
+# CANNOT use shutil.copytree - it is broken.
+# also need to be aware of copying into a directory, so we just always make it
+# a directory.
+build_dir = pathlib.Path(CACHE_HOME) / name / "build"
+build_dir.mkdir(parents=True,exist_ok=True)
+subprocess.run(["cp", "-R", "-P", *gen_dir.glob("*"), build_dir], check=True)
+
+print("running soupault")
+
+# run soupault on it. note that soupault currently ignores symlinks.
+subprocess.run(
+    [
+        soupault,
+        "--site-dir",
+        gen_dir,
+        "--build-dir",
+        build_dir,
+    ],
+    cwd=pathlib.Path(CONFIG_HOME),
+    env={
+        **os.environ,
+        'SOUPAULT_CONFIG': '/dev/fd/{}'.format(soupault_config.fileno())
+    },
+    check=True,
+    pass_fds=[soupault_config.fileno()]
+)
+
 print("copying to output")
 
 # CANNOT use shutil.copytree - it is broken.
@@ -276,7 +362,7 @@ print("copying to output")
 # a directory.
 browse = pathlib.Path.cwd() / "browse"
 browse.mkdir(parents=True,exist_ok=True)
-subprocess.run(["cp", "-R", "-P", *gen_dir.glob("*"), browse], check=True)
+subprocess.run(["cp", "-R", "-P", *build_dir.glob("*"), browse], check=True)
 
 # └── gen
 #     ├── blobs