some fixes to python builder

This commit is contained in:
Sean Dockray 2020-09-09 00:38:07 +10:00
parent 1ce60da9cf
commit f112e8fa52
1 changed files with 24 additions and 9 deletions

View File

@ -9,7 +9,6 @@ python "D:\dev\websites\sandpoints-dev\custom_syadmin\git_hooks_post-receive.py"
--git_repo "D:\dev\websites\pirate-care-syllabus\gitea\gitea-repositories\gitea\pirate-care-syllabus.git" \
--website "D:\dev\websites\testing-ground\sandpoints" \
--website_preview "D:\dev\websites\testing-ground\sandpoints\_preview" \
--hugo_preview_url http://localhost:8000/_preview/ \
--library "D:\dev\websites\pirate-care-syllabus\_library" \
--tmp_dir "D:\tmp\tmp"
--oldrev $oldrev \
@ -22,7 +21,7 @@ python "D:\dev\websites\sandpoints-dev\custom_syadmin\git_hooks_post-receive.py"
Just a mental note to read refs from the post-receive hook like this:
oldrev,newrev,refname = sys.stdin.readline().strip().split( )
oldrev,newrev,refname = sys.stdin.readline().strip().split(' ')
"""
import os
@ -84,14 +83,20 @@ def cmd(parts, cwd=None, env=None):
return subprocess.check_output(parts, cwd=cwd, env=env, universal_newlines=True).strip()
def rmrf(path):
def rmrf(path, keep_dir=False):
""" Use safe-rm for recursive removal """
""" @TODO: make it safer """
def remove_readonly(func, path, excinfo):
os.chmod(path, stat.S_IWRITE)
func(path)
# Try removal
if 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 f in files:
os.unlink(os.path.join(root, f))
for d in dirs:
shutil.rmtree(os.path.join(root, d), onerror=remove_readonly)
elif os.path.exists(path) and len(os.path.realpath(path)) > 1:
shutil.rmtree(path, onerror=remove_readonly)
else:
print("Either the path doesn't exist or you are trying to delete the root directory(?!):", path)
@ -109,12 +114,19 @@ def build_site(dest, tmp, branch=None, hugo_environment='gitea'):
global GIT_PATH, GIT_REPO, HUGO_PATH
if branch:
print("Cloning branch: ", branch)
cmd([GIT_PATH, 'clone', '--single-branch', '--branch', branch, '.', tmp], cwd=GIT_REPO)
cmd([GIT_PATH, 'clone', '--branch', branch, '.', tmp], cwd=GIT_REPO)
#print(' '.join([GIT_PATH, 'archive', '--format', 'tar', branch, '|', 'tar', '-x', '-C', tmp]))
#subprocess.call(' '.join([GIT_PATH, 'archive', '--format', 'tar', branch, '|', 'tar', '-x', '-C', tmp]), shell=True, cwd=GIT_REPO)
else:
cmd([GIT_PATH, 'clone', '.', tmp], cwd=GIT_REPO)
rmrf(dest)
rmrf(os.path.join(tmp, '.git'))
rmrf(dest, keep_dir=True)
try:
os.makedirs(dest, exist_ok=True)
if not os.path.exists(dest):
os.makedirs(dest)
print("Build destination created: ", dest)
else:
print("Build destination exists: ", dest)
except:
print(f"Error creating the directory: {dest}")
lcl = f'{tmp}/last-commit-log.txt'
@ -130,7 +142,6 @@ def build_site(dest, tmp, branch=None, hugo_environment='gitea'):
now_time = datetime.now().strftime(date_format)
clobber(lcl, f"\n{start_time}\n{now_time}", append=True)
shutil.move(lcl, dest)
rmrf(tmp)
# Parsing command line arguments
@ -172,4 +183,8 @@ if not published:
# create preview version
build_site(WEBSITE_PREVIEW, TMP_WEBSITE_PREVIEW, branch=PREVIEW_BRANCH, hugo_environment='preview')
# Clean up temp directories
rmrf(TMP_WEBSITE)
rmrf(TMP_WEBSITE_PREVIEW)
# @TODO: link the library