5 import os, os.path, re, sys, locale, StringIO, filecmp
47 def find_encoding(lang):
48 r = re.match("^([^.]+)(\.[^@]+)?$", lang)
49 pure_lang = r.group(1)
50 if r.group(2) == None:
58 return (enc, pure_lang)
60 def parse_spec(infile, outfile):
62 re_summary = re.compile("^Summary\(([^\)]+)\):[ \t]+(.*)$")
63 re_utf = re.compile("^utf-8$", re.I)
64 re_desc = re.compile("^(%description.*\s)-l\s+([\S]+)($|\s.*$)")
65 re_proc = re.compile("^%[^{]")
66 re_ignore_proc = re.compile("^%(if|endif)")
67 re_changelog = re.compile("^%changelog")
73 if debug: outfile.write("%s, %s, %s" % (in_desc, in_changelog, l))
79 (enc, pure_lang) = find_encoding(lang)
81 outfile.write("#spec_utf8: unknown lang code in %%description -l %s\n" % (lang))
83 elif not re_utf.search(enc):
85 outline = "%s-l %s.UTF-8%s\n" % (r.group(1), pure_lang, r.group(3))
87 if re_proc.search(l) and not re_ignore_proc.search(l):
90 # %description continues
91 if not re_utf.search(enc):
93 outline = unicode(l, enc).encode("UTF-8")
94 except UnicodeDecodeError:
95 outfile.write("#spec_utf8: transcoding error %%description -l %s\n" % (pure_lang))
99 outline = unicode(l, "UTF-8").encode("UTF-8")
100 except UnicodeDecodeError:
102 outline = unicode(l, "ISO-8859-2").encode("UTF-8")
103 except UnicodeDecodeError:
104 outfile.write("#spec_utf8: transcoding next line from Latin2 failed\n")
108 r = re_summary.match(l)
111 (enc, pure_lang) = find_encoding(lang)
113 outfile.write("#spec_utf8: unknown lang code in Summary(%s)\n" % (lang))
115 elif not re_utf.search(enc):
117 desc = unicode(r.group(2), enc).encode("UTF-8")
118 outline = "Summary(%s.UTF-8):\t%s\n" % (pure_lang, desc)
119 except UnicodeDecodeError:
120 outfile.write("#spec_utf8: transcoding error Summary(%s)\n" % (lang))
122 elif re_changelog.match(l):
127 if debug: outfile.write("%s, %s\n"% (in_desc, in_changelog))
128 outfile.write("%s"% (outline, ))
133 print "Converting %s ..." % spec
135 sio = StringIO.StringIO()
140 if not parse_spec(sio, f):
141 sys.stderr.write(" Problems while converting %s.\n" % spec)
143 if filecmp.cmp(spec, tmp, False):
148 if __name__ == "__main__":
149 if len(sys.argv) == 1:
150 sys.stderr.write("Usage: %s <spec-files>\n" % sys.argv[0])
152 for spec in sys.argv[1:]: