From c3a54858d5bd253186eb3d318a58a240137f8a10 Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Thu, 30 Dec 2021 20:03:34 +0000 Subject: [PATCH] chmod with context handler --- core/models/caves.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/models/caves.py b/core/models/caves.py index 313935a..4ecbfdb 100644 --- a/core/models/caves.py +++ b/core/models/caves.py @@ -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)