'Change a message\'s labels to `labels`.
Note that this function does not check against dryrun settings,
so you need to ensure that it is never called in a dryrun mode.'

def savemessagelabels(self, uid, labels, ignorelabels=set()):

    filename = self.messagelist[uid]['filename']
    filepath = os.path.join(self.getfullname(), filename)
    file = open(filepath, 'rt')
    content = file.read()
    file.close()
    oldlabels = set()
    for hstr in self.getmessageheaderlist(content, self.labelsheader):
        oldlabels.update(imaputil.labels_from_header(self.labelsheader, hstr))
    labels = (labels - ignorelabels)
    ignoredlabels = (oldlabels & ignorelabels)
    oldlabels = (oldlabels - ignorelabels)
    if (labels == oldlabels):
        return
    labels_str = imaputil.format_labels_string(self.labelsheader, sorted((labels | ignoredlabels)))
    content = self.deletemessageheaders(content, self.labelsheader)
    content = self.addmessageheader(content, '\n', self.labelsheader, labels_str)
    mtime = int(os.stat(filepath).st_mtime)
    messagename = self.new_message_filename(uid, set())
    tmpname = self.save_to_tmp_file(messagename, content)
    tmppath = os.path.join(self.getfullname(), tmpname)
    try:
        os.rename(tmppath, filepath)
    except OSError as e:
        six.reraise(OfflineImapError, OfflineImapError(("Can't    rename    file    '%s'    to    '%s':    %s" % (tmppath, filepath, e[1])), OfflineImapError.ERROR.FOLDER), exc_info()[2])
    if (self._utime_from_header and mtime):
        os.utime(filepath, (mtime, mtime))
    self.messagelist[uid]['mtime'] = int(os.stat(filepath).st_mtime)
    self.messagelist[uid]['labels'] = labels
