diff options
author | Martin Green <martin.speleo@gmail.com> | 2022-06-23 18:48:29 +0100 |
---|---|---|
committer | Martin Green <martin.speleo@gmail.com> | 2022-06-23 18:48:29 +0100 |
commit | f1e800d8bf4ff389cecc34f494e87604cd4fd7ac (patch) | |
tree | a6cbceae2cbfe2db3f53844c141d58882981b4f2 /lib/version_control.py | |
parent | bd8d45054245666321ea1c7360d86504aefa9865 (diff) | |
download | troggle-f1e800d8bf4ff389cecc34f494e87604cd4fd7ac.tar.gz troggle-f1e800d8bf4ff389cecc34f494e87604cd4fd7ac.tar.bz2 troggle-f1e800d8bf4ff389cecc34f494e87604cd4fd7ac.zip |
Move saving and comitting code to a seperate library
Diffstat (limited to 'lib/version_control.py')
-rw-r--r-- | lib/version_control.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/version_control.py b/lib/version_control.py new file mode 100644 index 0000000..08e6172 --- /dev/null +++ b/lib/version_control.py @@ -0,0 +1,46 @@ +import troggle.settings as settings +import subprocess + +def write_and_commit(filepath, content): + """Writes the content to the filepath and adds and commits the file to git. If this fails, a WriteAndCommitError is raised.""" + cwd = filepath.parent + filename = filepath.name + git = settings.GIT + # GIT see also core/models/cave.py writetrogglefile() + # GIT see also core/views/uploads.py dwgupload() + + try: + with open(filepath, "w", encoding="utf8") as f: + print(f'WRITING{cwd}---{filename} ') + # 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-- + f.write(content) + except PermissionError: + message = f'CANNOT save this file.\nPERMISSIONS incorrectly set on server for this file {filename}. Ask a nerd to fix this.' + raise WriteAndCommitError(message) + + try: + cp_add = subprocess.run([git, "add", filename], cwd=cwd, capture_output=True, text=True) + if cp_add.returncode != 0: + msgdata = 'Ask a nerd to fix this.\n\n' + cp_add.stderr + '\n\n' + cp_add.stdout + '\n\nreturn code: ' + str(cp_add.returncode) + message = f'CANNOT git on server for this file {filename}. Edits saved but not added to git.\n\n' + msgdata + raise WriteAndCommitError(message) + + cp_commit = subprocess.run([git, "commit", "-m", f'Troggle online: Edit this page - {filename}'], cwd=cwd, capture_output=True, text=True) + # This produces return code = 1 if it commits OK, but when 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'Error code with git on server for this file {filename}. Edits saved, added to git, but NOT committed.\n\n' + msgdata + raise WriteAndCommitError(message) + + except subprocess.SubprocessError: + message = f'CANNOT git on server for this file {filename}. Subprocess error. Edits not saved.\nAsk a nerd to fix this.' + raise WriteAndCommitError(message) + +class WriteAndCommitError(Exception): + """Exception class for errors writing files and comitting them to git""" + def __init__(self, message): + self.message = message + + def __str__(self): + return f'WriteAndCommitError: {self.message}' |