def copy_or_replace(src, dst):
try:<EOL><INDENT>shutil.copy(src, dst)<EOL><DEDENT>except (OSError, IOError) as e:<EOL><INDENT>import errno<EOL>if e.errno == errno.EPERM:<EOL><INDENT>import tempfile<EOL>if os.path.isdir(dst):<EOL><INDENT>dst = os.path.join(dst, os.path.basename(src))<EOL><DEDENT>dst_dir, dst_name = os.path.split(dst)<EOL>dst_temp = tempfile.mktemp(prefix=dst_name + '<STR_LIT:.>', dir=dst_dir)<EOL>shutil.copy(src, dst_temp)<EOL>if not os.path.isfile(dst_temp):<EOL><INDENT>raise RuntimeError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % dst_temp)<EOL><DEDENT>os.remove(dst)<EOL>shutil.move(dst_temp, dst)<EOL><DEDENT><DEDENT>
try to copy with mode, and if it fails, try replacing