summaryrefslogtreecommitdiffstats
path: root/core/utils.py
diff options
context:
space:
mode:
authorPhilip Sargent <philip.sargent@gmail.com>2025-02-11 17:40:07 +0000
committerPhilip Sargent <philip.sargent@gmail.com>2025-02-11 17:40:07 +0000
commit096c3be4e51c121af9486d6c9a2667d029d20f12 (patch)
tree17682ff0f4ca640433e1d9df1a2688700759b0f7 /core/utils.py
parentb8b2d52866c7c18728357bec03928b1b80f67620 (diff)
downloadtroggle-096c3be4e51c121af9486d6c9a2667d029d20f12.tar.gz
troggle-096c3be4e51c121af9486d6c9a2667d029d20f12.tar.bz2
troggle-096c3be4e51c121af9486d6c9a2667d029d20f12.zip
copies uploaded photos to expofiles too (as originals)
Diffstat (limited to 'core/utils.py')
-rw-r--r--core/utils.py74
1 files changed, 44 insertions, 30 deletions
diff --git a/core/utils.py b/core/utils.py
index d77c474..3129ffe 100644
--- a/core/utils.py
+++ b/core/utils.py
@@ -347,16 +347,54 @@ def add_commit(fname, message, editor):
print(msg)
raise WriteAndCommitError(msg)
+def write_binary_file(filepath, content):
+ print(f"write_binary_file: {filepath}")
+ write_files([(filepath, content, "")])
+
+def write_files(files):
+ for filepath, content, encoding in files:
+ filename = filepath.name
+ if filepath.is_dir():
+ raise OSError(
+ f"CANNOT write this file {filepath} as this is an existing DIRECTORY."
+ )
+ try:
+ filepath.parent.mkdir(parents=True, exist_ok=True)
+ # os.makedirs(os.path.dirname(filepath), exist_ok = True)
+ except PermissionError as e:
+ raise PermissionError(
+ f"CANNOT make the directory.\nPERMISSIONS incorrectly set on server for this file {filepath}. Ask a nerd to fix this: {e}"
+ )
+ except Exception as e:
+ raise OSError(
+ f"CANNOT make the directory for {filepath}. Ask a nerd to fix this: {e}"
+ )
+ if encoding:
+ mode = "w"
+ kwargs = {"encoding": encoding}
+ else:
+ mode = "wb"
+ kwargs = {}
+ try:
+ with open(filepath, mode, **kwargs) as f:
+ print(f"WRITING {filepath} ")
+ f.write(content)
+ except PermissionError as e:
+ raise PermissionError(
+ f"CANNOT save this file.\nPERMISSIONS incorrectly set on server for this file {filepath}. Ask a nerd to fix this: {e}"
+ )
+ except Exception as e:
+ raise OSError(
+ f"CANNOT write this file {filepath}. Ask a nerd to fix this: {e}"
+ )
+ return
def write_and_commit(files, message, editor):
"""For each file in files, it writes the content to the filepath
and adds and commits the file to git.
filepath, content, encoding = file
If this fails, a WriteAndCommitError is raised.
-
- This needs refactoring to just write to the filesystem and then call git_add()
- for each file, and then git_commit() at the end.
-
+
message - the "-m" comment field for the git commit
editor - the "--author" field for the git commit
"""
@@ -365,36 +403,12 @@ def write_and_commit(files, message, editor):
git = settings.GIT
commands = []
editor = git_string(editor)
+ write_files(files)
try:
for filepath, content, encoding in files:
cwd = filepath.parent
filename = filepath.name
- os.makedirs(os.path.dirname(filepath), exist_ok = True)
- if filepath.is_dir():
- raise WriteAndCommitError(
- f"CANNOT write this file {filepath} as this is an existing DIRECTORY."
- )
- #return False
- if encoding:
- mode = "w"
- kwargs = {"encoding": encoding}
- else:
- mode = "wb"
- kwargs = {}
- try:
- with open(filepath, mode, **kwargs) as f:
- print(f"WRITING {cwd}/{filename} ")
- f.write(content)
- except PermissionError as e:
- raise WriteAndCommitError(
- f"CANNOT save this file.\nPERMISSIONS incorrectly set on server for this file {filepath}. Ask a nerd to fix this: {e}"
- )
- except Exception as e:
- raise WriteAndCommitError(
- f"CANNOT write this file {filepath}. Ask a nerd to fix this: {e}"
- )
- commands = git_add(filename, cwd, commands)
-
+ commands = git_add(filename, cwd, commands)
commands = git_commit(cwd, message, editor, commands)
except subprocess.SubprocessError: