5 import os, os.path, re, sys, locale
46 def find_encoding(lang):
47 r = re.match("^([^.]+)(\.[^@]+)?$", lang)
48 pure_lang = r.group(1)
49 if r.group(2) == None:
57 return (enc, pure_lang)
59 def parse_spec(infile, outfile):
60 re_summary = re.compile("^Summary\(([^\)]+)\):\t+(.*)$")
61 re_utf = re.compile("^utf-8$", re.I)
62 re_desc = re.compile("^(%description.*\s)-l\s+([\S]+)($|\s.*$)")
63 re_proc = re.compile("^%")
64 re_changelog = re.compile("^%changelog")
70 if debug: outfile.write("%s, %s, %s" % (in_desc, in_changelog, l))
75 (enc, pure_lang) = find_encoding(r.group(2))
77 outfile.write("#spec_utf8: unknown lang code in %%description -l %s\n" % (lang))
78 elif not re_utf.search(enc):
80 outline = "%s-l %s.UTF-8%s\n" % (r.group(1), pure_lang, r.group(3))
85 # %description continues
86 if not re_utf.search(enc):
88 outline = unicode(l, enc).encode("UTF-8")
89 except UnicodeDecodeError:
90 outfile.write("#spec_utf8: transcoding error %%description -l %s\n" % (lang))
93 outline = unicode(l, "UTF-8").encode("UTF-8")
94 except UnicodeDecodeError:
96 outline = unicode(l, "ISO-8859-2").encode("UTF-8")
97 except UnicodeDecodeError:
98 outfile.write("#spec_utf8: transcoding next line from Latin2 failed\n")
101 r = re_summary.match(l)
103 (enc, pure_lang) = find_encoding(r.group(1))
105 outfile.write("#spec_utf8: unknow lang code Summary(%s)\n" % (lang))
106 elif not re_utf.search(enc):
108 desc = unicode(r.group(2), enc).encode("UTF-8")
109 outline = "Summary(%s.UTF-8): %s\n" % (pure_lang, desc)
110 except UnicodeDecodeError:
111 outfile.write("#spec_utf8: ranscoding error Summary(%s)\n" % (lang))
112 elif re_changelog.match(l):
117 if debug: outfile.write("%s, %s\n"% (in_desc, in_changelog))
118 outfile.write("%s"% (outline, ))
121 parse_spec(sys.stdin, sys.stdout)
123 if __name__ == "__main__":