def safe_concurrent_rename(src, dst):
  """Rename src to dst, ignoring errors due to dst already existing.

  Useful when concurrent processes may attempt to create dst, and it doesn't matter who wins.
  """
  # Delete dst, in case it existed (with old content) even before any concurrent processes
  # attempted this write. This ensures that at least one process writes the new content.
  if os.path.isdir(src):  # Note that dst may not exist, so we test for the type of src.
    safe_rmtree(dst)
  else:
    safe_delete(dst)
  try:
    shutil.move(src, dst)
  except IOError as e:
    if e.errno != errno.EEXIST:
      raise
Rename src to dst, ignoring errors due to dst already existing.

  Useful when concurrent processes may attempt to create dst, and it doesn't matter who wins.