git hook needs more care...

This commit is contained in:
Marcell Mars 2020-09-15 04:15:39 +02:00
parent 9a190e9d95
commit 6ea76dca6b
1 changed files with 12 additions and 8 deletions

View File

@ -1,6 +1,6 @@
#! python #! python
""" """
This is an alternative to `git_hooks_post-receive.sh` This is an alternative to `git_hooks_post-receive.sh`.
The post commit hook in Gitea would include something like the following to ensure this script is called. The post commit hook in Gitea would include something like the following to ensure this script is called.
#!/bin/sh #!/bin/sh
@ -10,7 +10,7 @@ python "D:\dev\websites\sandpoints-dev\custom_syadmin\git_hooks_post-receive.py"
--website "D:\dev\websites\testing-ground\sandpoints" \ --website "D:\dev\websites\testing-ground\sandpoints" \
--website_preview "D:\dev\websites\testing-ground\sandpoints\_preview" \ --website_preview "D:\dev\websites\testing-ground\sandpoints\_preview" \
--library "D:\dev\websites\pirate-care-syllabus\_library" \ --library "D:\dev\websites\pirate-care-syllabus\_library" \
--tmp_dir "D:\tmp\tmp" --tmp_dir "D:\tmp\tmp" \
--oldrev $oldrev \ --oldrev $oldrev \
--newrev $newrev \ --newrev $newrev \
--ref $ref --ref $ref
@ -32,6 +32,9 @@ import subprocess
import shutil import shutil
import stat import stat
from datetime import datetime from datetime import datetime
import logging
logging.basicConfig(level = logging.DEBUG, filename = "/tmp/git_hooks.log")
# format for dates # format for dates
date_format = "%m/%d/%Y, %H:%M:%S" date_format = "%m/%d/%Y, %H:%M:%S"
@ -89,7 +92,7 @@ def rmrf(path, keep_dir=False):
def remove_readonly(func, path, excinfo): def remove_readonly(func, path, excinfo):
os.chmod(path, stat.S_IWRITE) os.chmod(path, stat.S_IWRITE)
func(path) func(path)
# Try removal # Try removal.
if keep_dir and os.path.exists(path) and len(os.path.realpath(path)) > 1: if keep_dir and os.path.exists(path) and len(os.path.realpath(path)) > 1:
for root, dirs, files in os.walk(path): for root, dirs, files in os.walk(path):
for f in files: for f in files:
@ -129,7 +132,8 @@ def build_site(dest, tmp, branch=None, hugo_environment='gitea'):
print("Build destination exists: ", dest) print("Build destination exists: ", dest)
except: except:
print(f"Error creating the directory: {dest}") print(f"Error creating the directory: {dest}")
lcl = f'{tmp}/last-commit-log.txt' lcl = f'{tmp}/last-commit-log.html'
clobber(lcl, '<html><head><meta charset="utf-8"></head><body><pre>')
# overriding hugo config for development environments # overriding hugo config for development environments
env = os.environ.copy() env = os.environ.copy()
if BASE_URL: if BASE_URL:
@ -137,10 +141,11 @@ def build_site(dest, tmp, branch=None, hugo_environment='gitea'):
if GIT_URL: if GIT_URL:
env["HUGO_PARAMS_GITURL"] = GIT_URL env["HUGO_PARAMS_GITURL"] = GIT_URL
# run the hugo command # run the hugo command
hugo_output = cmd([HUGO_PATH, '-e', hugo_environment, '-d', dest], cwd=tmp, env=env) hugo_output = cmd([HUGO_PATH, '--templateMetrics', '-e', hugo_environment, '-d', dest], cwd=tmp, env=env)
clobber(lcl, hugo_output) clobber(lcl, hugo_output, append=True)
now_time = datetime.now().strftime(date_format) now_time = datetime.now().strftime(date_format)
clobber(lcl, f"\n{start_time}\n{now_time}", append=True) clobber(lcl, f"\n{start_time}\n{now_time}", append=True)
clobber(lcl, f"</pre></body></html>", append=True)
shutil.move(lcl, dest) shutil.move(lcl, dest)
@ -149,6 +154,7 @@ parser = argparse.ArgumentParser()
for v in vars: for v in vars:
parser.add_argument(f"--{v.lower()}") parser.add_argument(f"--{v.lower()}")
args = parser.parse_args() args = parser.parse_args()
logging.info(args)
# Load all variables from command line arguments or environment variables # Load all variables from command line arguments or environment variables
for v in vars: for v in vars:
@ -186,5 +192,3 @@ build_site(WEBSITE_PREVIEW, TMP_WEBSITE_PREVIEW, branch=PREVIEW_BRANCH, hugo_env
# Clean up temp directories # Clean up temp directories
rmrf(TMP_WEBSITE) rmrf(TMP_WEBSITE)
rmrf(TMP_WEBSITE_PREVIEW) rmrf(TMP_WEBSITE_PREVIEW)
# @TODO: link the library