diff --git a/gestion/darcs_send_changes.py b/gestion/darcs_send_changes.py index 6e9d16bf..a13b56f6 100755 --- a/gestion/darcs_send_changes.py +++ b/gestion/darcs_send_changes.py @@ -35,12 +35,28 @@ except: sys.exit(1) sys.path.append("/usr/scripts/gestion") -from affich_tools import cprint +from affich_tools import cprint, encoding from unicode2ascii import unicode2ascii +def to_utf8(str): + """ Decode un str ou un unicode vers un str en UTF-8. """ + if isinstance(str, unicode): + return str.encode("UTF-8") + else: + try: + # Si c'est une chaine brute, on commend par essayer + # de la décoder comme une chaine en UTF-8 + str.decode("UTF-8") + return str + except: + try: + return str.decode(encoding).encode("UTF-8") + except: + return str + def darcs(args): """ Invoque darcs et renvoie sa sortie. """ - return commands.getoutput("env DARCS_DONT_ESCAPE_8BIT=1 darcs " + args) + return to_utf8(commands.getoutput("env DARCS_DONT_ESCAPE_8BIT=1 darcs " + args)) def get_patch_properties(): """ Récupère les informations a propos du dernier patch. """ @@ -54,8 +70,8 @@ def get_patch_properties(): 'shortrepo': os.path.basename(cwd), 'date': prop.attrib['local_date'], 'hash': prop.attrib['hash'], - 'name': prop.findtext('name').encode("UTF-8"), - 'comment': prop.findtext('comment').encode("UTF-8"), + 'name': prop.findtext('name'), + 'comment': prop.findtext('comment'), 'diff': diff, 'changes': darcs("changes --match='hash %s' --summary" % hash) } @@ -86,16 +102,26 @@ peut contenir les variables suivantes: si template est None, DEFAULT_TEMPLATE est utilisé. """ patch_props["recipient"] = ", ".join(recipient) - mail = email.message_from_string((template or DEFAULT_TEMPLATE) % patch_props) - if not mail['Content-Transfer-Encoding']: - mail['Content-Transfer-Encoding'] = 'quoted-printable' - if not mail.get_charset(): - mail.set_charset("UTF-8") + # On met toutes valeurs en string, en UTF-8 + for key, val in patch_props.items(): + patch_props[key] = to_utf8(val) + template = to_utf8(template or DEFAULT_TEMPLATE) + mail = email.message_from_string(template % patch_props) # On met le titre en ascii sinon c'est atroce pour le filtrage # automatique - subject = unicode2ascii(mail['Subject'].decode("UTF-8")) - mail.replace_header('Subject', subject) + subject = mail['Subject'] + if subject: + subject = unicode2ascii(subject.decode("UTF-8")) + mail.replace_header('Subject', subject) + if not mail['Content-Transfer-Encoding']: + mail['Content-Transfer-Encoding'] = 'quoted-printable' + charset = mail.get_charset() + if not charset: + charset = "UTF-8" + mail.set_charset("UTF-8") rawmail = mail.as_string() + if charset != "UTF-8": + rawmail = rawmail.decode("UTF-8").encode(charset) for to in recipient: smtp.sendmail(patch_props['author'], to, rawmail)