code: def safe_copyfile(src, dest):
      
      fd, tmpname = tempfile.mkstemp(dir=os.path.dirname(dest))
      shutil.copyfileobj(open(src, 'rb'), os.fdopen(fd, 'wb'))
      shutil.copystat(src, tmpname)
      os.rename(tmpname, dest)
docs: safely copy src to dest using a temporary intermediate and then renaming
      to dest