From 5ae6aa0ca3fecd2065e86d884b01a0d5e840f278 Mon Sep 17 00:00:00 2001 From: Sean Dockray Date: Tue, 8 Sep 2020 22:57:50 +1000 Subject: [PATCH] better deleting dirs where perms not helping --- custom_syadmin/git_hooks_post-receive.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/custom_syadmin/git_hooks_post-receive.py b/custom_syadmin/git_hooks_post-receive.py index ca59075..3eb0a5b 100644 --- a/custom_syadmin/git_hooks_post-receive.py +++ b/custom_syadmin/git_hooks_post-receive.py @@ -22,7 +22,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 +84,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) @@ -115,9 +121,13 @@ def build_site(dest, tmp, branch=None, hugo_environment='gitea'): else: cmd([GIT_PATH, 'clone', '.', tmp], cwd=GIT_REPO) rmrf(os.path.join(tmp, '.git')) - rmrf(dest) + 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'