human
Complete this python program:
def WriteOnDiff(filename):
assistant
#Here is the Python code you requested.

    """Write to a file only if the new contents differ.
    Arguments:
    filename: name of the file to potentially write to.
    Returns:
    A file like object which will write to temporary file and only overwrite
    the target if it differs (on close).
    """
    class Writer(object, ):
        'Wrapper    around    file    which    only    covers    the    target    if    it    differs.'
        def __init__(self):
            (tmp_fd, self.tmp_path) = tempfile.mkstemp(suffix='.tmp', prefix=(os.path.split(filename)[1] + '.gyp.'), dir=os.path.split(filename)[0])
            try:
                self.tmp_file = os.fdopen(tmp_fd, 'wb')
            except Exception:
                os.unlink(self.tmp_path)
                raise
        def __getattr__(self, attrname):
            return getattr(self.tmp_file, attrname)
        def close(self):
            try:
                self.tmp_file.close()
                same = False
                try:
                    same = filecmp.cmp(self.tmp_path, filename, False)
                except OSError as e:
                    if (e.errno != errno.ENOENT):
                        raise
                if same:
                    os.unlink(self.tmp_path)
                else:
                    umask = os.umask(63)
                    os.umask(umask)
                    os.chmod(self.tmp_path, (438 & (~ umask)))
                    if ((sys.platform == 'win32') and os.path.exists(filename)):
                        os.remove(filename)
                    os.rename(self.tmp_path, filename)
            except Exception:
                os.unlink(self.tmp_path)
                raise
    return Writer()