Tags: #python3 #permissions #chmod
"""
note if you just need to ensure that you keep the original permissions
then using the horrifically named `shutil.copy2` will work for you.
just replace the last two lines (`os.stat` and `os.chmod`) with:
`shutil.copy2(src_file, dst_file)`
"""
src_file = f"{src}{path}/{file}"
dst_file = f"{dst}/{file}"
shutil.copyfile(src_file, dst_file)
# we lose the execute permissions when copying a file with Python,
# where as it seems that's not a problem when we were using Bash.
#
# if we don't add execute perms to the copied file, then when the docker
# container is run we would get an "OCI runtime create failed" error.
#
# our code uses os.stat() to get the current file permissions, then uses
# the pipe operator | to do a bitwise OR operation which ensures we get
# both the current perms + the new perms (i.e. +x execute).
#
# Bitwise OR reference:
# https://en.wikipedia.org/wiki/Logical_disjunction
st = os.stat(src_file)
os.chmod(dst_file, st.st_mode | stat.S_IEXEC)