]>
Commit | Line | Data |
---|---|---|
11774a5d AF |
1 | #!/usr/bin/python |
2 | ||
c143293b AF |
3 | debug = False |
4 | ||
89cef9e8 | 5 | import os, os.path, re, sys, locale, StringIO |
11774a5d AF |
6 | |
7 | langs={ | |
8 | 'bg':'windows-1251', | |
9 | 'br':'iso8859-1', | |
10 | 'ca':'iso8859-1', | |
11 | 'cs':'iso8859-2', | |
12 | 'da':'iso8859-1', | |
13 | 'de':'iso8859-1', | |
14 | 'en':'iso8859-1', | |
15 | 'eo':'iso8859-3', | |
16 | 'es':'iso8859-1', | |
17 | 'fi':'iso8859-1', | |
18 | 'fo':'iso8859-1', | |
19 | 'fr':'iso8859-1', | |
20 | 'gl':'iso8859-1', | |
21 | 'he':'iso8859-8', | |
22 | 'id':'iso8859-1', | |
23 | 'is':'iso8859-1', | |
24 | 'it':'iso8859-1', | |
25 | 'ja':'euc-jp', | |
26 | 'ko':'euc-kr', | |
27 | 'nb':'iso8859-1', | |
28 | 'nl':'iso8859-1', | |
29 | 'pl':'iso8859-2', | |
30 | 'pt':'iso8859-1', | |
31 | 'pt_BR':'iso8859-1', | |
32 | 'ro':'iso8859-2', | |
e4fe63e5 | 33 | 'ru':'KOI8-R', |
11774a5d AF |
34 | 'se':'UTF-8', |
35 | 'sk':'iso8859-2', | |
36 | 'sl':'iso8859-2', | |
37 | 'sv':'iso8859-1', | |
38 | 'tr':'iso8859-9', | |
39 | 'uk':'KOI8-U', | |
40 | 'wa':'iso8859-1', | |
41 | 'zh_CN':'GB2312', | |
42 | 'zh_HK':'BIG5-HKSCS', | |
43 | 'zh_TW':'BIG5', | |
44 | 0:0} | |
45 | ||
fb3b78fc AF |
46 | def find_encoding(lang): |
47 | r = re.match("^([^.]+)(\.[^@]+)?$", lang) | |
48 | pure_lang = r.group(1) | |
49 | if r.group(2) == None: | |
50 | try: | |
51 | enc = langs[lang] | |
52 | except KeyError: | |
53 | enc = None | |
54 | else: | |
55 | # strip dot | |
56 | enc = r.group(2)[1:] | |
57 | return (enc, pure_lang) | |
58 | ||
11774a5d | 59 | def parse_spec(infile, outfile): |
a5acbd63 | 60 | success = True |
11774a5d | 61 | re_summary = re.compile("^Summary\(([^\)]+)\):\t+(.*)$") |
fb3b78fc | 62 | re_utf = re.compile("^utf-8$", re.I) |
11774a5d AF |
63 | re_desc = re.compile("^(%description.*\s)-l\s+([\S]+)($|\s.*$)") |
64 | re_proc = re.compile("^%") | |
c143293b | 65 | re_changelog = re.compile("^%changelog") |
11774a5d | 66 | in_desc = False |
c143293b | 67 | in_changelog = False |
11774a5d AF |
68 | |
69 | for l in infile: | |
c143293b AF |
70 | outline = l |
71 | if debug: outfile.write("%s, %s, %s" % (in_desc, in_changelog, l)) | |
72 | ||
73 | # %description start | |
74 | r = re_desc.match(l) | |
75 | if r: | |
96b557de AF |
76 | lang = r.group(2) |
77 | (enc, pure_lang) = find_encoding(lang) | |
c143293b | 78 | if enc == None: |
96b557de | 79 | outfile.write("#spec_utf8: unknown lang code in %%description -l %s\n" % (lang)) |
a5acbd63 | 80 | success = False |
c143293b AF |
81 | elif not re_utf.search(enc): |
82 | in_desc = True | |
83 | outline = "%s-l %s.UTF-8%s\n" % (r.group(1), pure_lang, r.group(3)) | |
84 | elif in_desc: | |
85 | if re_proc.search(l): | |
86 | in_desc = False | |
87 | else: | |
88 | # %description continues | |
89 | if not re_utf.search(enc): | |
90 | try: | |
91 | outline = unicode(l, enc).encode("UTF-8") | |
92 | except UnicodeDecodeError: | |
96b557de | 93 | outfile.write("#spec_utf8: transcoding error %%description -l %s\n" % (pure_lang)) |
a5acbd63 | 94 | success = False |
c143293b AF |
95 | elif in_changelog: |
96 | try: | |
97 | outline = unicode(l, "UTF-8").encode("UTF-8") | |
98 | except UnicodeDecodeError: | |
99 | try: | |
100 | outline = unicode(l, "ISO-8859-2").encode("UTF-8") | |
101 | except UnicodeDecodeError: | |
102 | outfile.write("#spec_utf8: transcoding next line from Latin2 failed\n") | |
a5acbd63 | 103 | success = False |
c143293b AF |
104 | else: |
105 | # Summary | |
11774a5d AF |
106 | r = re_summary.match(l) |
107 | if r: | |
96b557de AF |
108 | lang = r.group(1) |
109 | (enc, pure_lang) = find_encoding(lang) | |
fb3b78fc | 110 | if enc == None: |
96b557de | 111 | outfile.write("#spec_utf8: unknow lang code Summary(%s)\n" % (lang)) |
a5acbd63 | 112 | success = False |
fb3b78fc | 113 | elif not re_utf.search(enc): |
11774a5d | 114 | try: |
fb3b78fc | 115 | desc = unicode(r.group(2), enc).encode("UTF-8") |
c143293b | 116 | outline = "Summary(%s.UTF-8): %s\n" % (pure_lang, desc) |
11774a5d | 117 | except UnicodeDecodeError: |
96b557de | 118 | outfile.write("#spec_utf8: transcoding error Summary(%s)\n" % (lang)) |
a5acbd63 | 119 | success = False |
c143293b AF |
120 | elif re_changelog.match(l): |
121 | # %changelog start | |
122 | in_changelog = True | |
123 | ||
124 | ||
125 | if debug: outfile.write("%s, %s\n"% (in_desc, in_changelog)) | |
126 | outfile.write("%s"% (outline, )) | |
8b4cb13b | 127 | return success |
11774a5d | 128 | |
89cef9e8 | 129 | def main(argv): |
8bb5df68 | 130 | print "Converting %s ..." % argv[1] |
89cef9e8 AM |
131 | f = open(argv[1], 'r') |
132 | sio = StringIO.StringIO() | |
133 | sio.write(f.read()) | |
134 | f.close() | |
135 | sio.seek(0) | |
136 | f = open(argv[1] + '.tmp', 'w') | |
a5acbd63 AM |
137 | if not parse_spec(sio, f): |
138 | sys.stderr.write(" Problems while converting %s.\n" % argv[1]) | |
89cef9e8 AM |
139 | f.close() |
140 | os.rename(argv[1] + '.tmp', argv[1]) | |
11774a5d AF |
141 | |
142 | if __name__ == "__main__": | |
89cef9e8 AM |
143 | if len(sys.argv) != 2: |
144 | sys.stderr.write("Usage: %s <spec file>\n" % sys.argv[0]) | |
145 | sys.exit(1) | |
146 | main(sys.argv) | |
11774a5d | 147 |