2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2024-11-25 16:51:54 +00:00

capture git subprocess errors

This commit is contained in:
Philip Sargent 2021-12-30 23:27:42 +00:00
parent 21ad6ecffb
commit ab8813e389
2 changed files with 16 additions and 7 deletions

View File

@ -310,26 +310,33 @@ def editexpopage(request, path):
cwd = filepath.parent cwd = filepath.parent
filename = filepath.name filename = filepath.name
git = settings.GIT git = settings.GIT
# see also core/models/cave.py writetrogglefile()
try: try:
with open(filepath, "w") as f: with open(filepath, "w") as f:
print(f'WRITING{cwd}---{filename} ') print(f'WRITING{cwd}---{filename} ')
# as the wsgi process www-data, we have group write-access but are not owner, so cannot chmod. # as the wsgi process www-data, we have group write-access but are not owner, so cannot chmod.
# os.chmod(filepath, 0o664) # set file permissions to rw-rw-r-- # os.chmod(filepath, 0o664) # set file permissions to rw-rw-r--
# see also core/models/cave.py writetrogglefile()
f.write(result) f.write(result)
# should replace .call with .run and capture_output=True
except PermissionError: except PermissionError:
message = f'CANNOT save this file.\nPERMISSIONS incorrectly set on server for this file {filename}. Ask a nerd to fix this.' message = f'CANNOT save this file.\nPERMISSIONS incorrectly set on server for this file {filename}. Ask a nerd to fix this.'
return render(request,'errors/generic.html', {'message': message}) return render(request,'errors/generic.html', {'message': message})
try: try:
cp1 = subprocess.run([git, "add", filename], cwd=cwd, capture_output=True) cp_add = subprocess.run([git, "add", filename], cwd=cwd, capture_output=True, text=True)
cp2 = subprocess.run([git, "commit", "-m", 'Edit this page'], cwd=cwd, capture_output=True) if cp_add.returncode != 0:
message = f'Test output this file {filename}. git command output.\n' + str(cp1) + '\n\n' + str(cp2) msgdata = 'Ask a nerd to fix this.\n\n' + cp_add.stderr + '\n\n' + cp_add.stdout + '\n\nreturn code: ' + str(cp_add.returncode)
return render(request,'errors/debug.html', {'message': message}) message = f'CANNOT git on server for this file {filename}. Edits saved but not added to git.\n\n' + msgdata
return render(request,'errors/generic.html', {'message': message})
cp_commit = subprocess.run([git, "commit", "-m", 'Edit this page'], cwd=cwd, capture_output=True, text=True)
# This produces return code = 1 if it commits OK, but if the repo still needs to be pushed to origin/expoweb
if cp_commit.returncode != 0 and cp_commit.stdout != 'nothing to commit, working tree clean':
msgdata = 'Ask a nerd to fix this.\n\n' + cp_commit.stderr + '\n\n' + cp_commit.stdout + '\n\nreturn code: ' + str(cp_commit.returncode)
message = f'Eror code with git on server for this file {filename}. Edits saved, added to git but NOT committed.\n\n' + msgdata
return render(request,'errors/generic.html', {'message': message})
except subprocess.SubprocessError: except subprocess.SubprocessError:
message = f'CANNOT git on server for this file {filename}. Edits not saved.\nAsk a nerd to fix this.' message = f'CANNOT git on server for this file {filename}. Subprocess error. Edits not saved.\nAsk a nerd to fix this.'
return render(request,'errors/generic.html', {'message': message}) return render(request,'errors/generic.html', {'message': message})
return HttpResponseRedirect(reverse('expopage', args=[path])) # Redirect after POST return HttpResponseRedirect(reverse('expopage', args=[path])) # Redirect after POST

View File

@ -16,7 +16,9 @@
<font color="red"> <font color="red">
{% if message %} {% if message %}
<pre>
{{message}} {{message}}
</pre>
{% else %} {% else %}
<p>We are terribly sorry but an unknown fault has occurred. </p> <p>We are terribly sorry but an unknown fault has occurred. </p>