def update_safe(filename: str, **kw: Any) -> Generator[IO, None, None]:
    """Rewrite a file atomically.

    Clients are allowed to delete the tmpfile to signal that they don't
    want to have it updated.

    """
    with tempfile.NamedTemporaryFile(
        dir=os.path.dirname(filename),
        delete=False,
        prefix=f"{os.path.basename(filename)}.",
        **kw,
    ) as tf:
        if os.path.exists(filename):
            os.chmod(tf.name, os.stat(filename).st_mode & 0o7777)
        tf.has_changed = False  # type: ignore
        yield tf
        if not os.path.exists(tf.name):
            return
        filename_tmp = tf.name
    if os.path.exists(filename) and filecmp.cmp(filename, filename_tmp, shallow=False):
        os.unlink(filename_tmp)
    else:
        os.rename(filename_tmp, filename)
        tf.has_changed = True
def update_safe ( filename : str , ** kw : Any ) -> Generator [ IO , None , None ] : with tempfile . NamedTemporaryFile ( dir = os . path . dirname ( filename ) , delete = False , prefix = f"{os.path.basename(filename)}." , ** kw , ) as tf : if os . path . exists ( filename ) : os . chmod ( tf . name , os . stat ( filename ) . st_mode & 0o7777 ) tf . has_changed = False yield tf if not os . path . exists ( tf . name ) : return filename_tmp = tf . name if os . path . exists ( filename ) and filecmp . cmp ( filename , filename_tmp , shallow = False ) : os . unlink ( filename_tmp ) else : os . rename ( filename_tmp , filename ) tf . has_changed = True
Rewrite a file atomically.

    Clients are allowed to delete the tmpfile to signal that they don't
    want to have it updated.