chmod with context handler

This commit is contained in:
Philip Sargent 2021-12-30 20:03:34 +00:00
parent 0a3037f077
commit c3a54858d5

View File

@ -40,7 +40,7 @@ todo='''- Move utility function into utils.py
def writetrogglefile(filepath, filecontent):
'''Set permissions to rw-rw-r-- and commit the new saved file to git
Callers should handle exception PermissionsError explicitly
Callers to cave.writeDataFile() or entrance.writeDataFile() should handle the exception PermissionsError explicitly
'''
filepath = Path(filepath)
cwd = filepath.parent
@ -48,8 +48,10 @@ def writetrogglefile(filepath, filecontent):
git = settings.GIT
# do not trap exceptions, pass them up to the view that called this function
# if the os.chmod fails, it can zero the contents of the file as a side effect. Dangerous.
with open(filepath, "w") as f:
os.chmod(filepath, 0o664) # set file permissions to rw-rw-r--
f.chmod(0o664) # safely set file permissions to rw-rw-r--
# os.chmod(filepath, 0o664) # set file permissions to rw-rw-r--
f.write(filecontent)
print(f'WRITING{cwd}---{filename} ')
call([git, "add", filename], cwd=cwd)