From 096c3be4e51c121af9486d6c9a2667d029d20f12 Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Tue, 11 Feb 2025 17:40:07 +0000 Subject: copies uploaded photos to expofiles too (as originals) --- core/utils.py | 74 +++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 30 deletions(-) (limited to 'core/utils.py') 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: -- cgit v1.2.3