]> git.pld-linux.org Git - packages/rpm-build-tools.git/blame - spec_utf8
- fix
[packages/rpm-build-tools.git] / spec_utf8
CommitLineData
11774a5d
AF
1#!/usr/bin/python
2
c143293b
AF
3debug = False
4
89cef9e8 5import os, os.path, re, sys, locale, StringIO
11774a5d
AF
6
7langs={
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
46def 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 59def 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:
76 (enc, pure_lang) = find_encoding(r.group(2))
77 if enc == None:
8bb5df68 78 outfile.write("#spec_utf8: unknown lang code in %%description -l %s\n" % (r.group(2)))
a5acbd63 79 success = False
c143293b
AF
80 elif not re_utf.search(enc):
81 in_desc = True
82 outline = "%s-l %s.UTF-8%s\n" % (r.group(1), pure_lang, r.group(3))
83 elif in_desc:
84 if re_proc.search(l):
85 in_desc = False
86 else:
87 # %description continues
88 if not re_utf.search(enc):
89 try:
90 outline = unicode(l, enc).encode("UTF-8")
91 except UnicodeDecodeError:
8bb5df68 92 outfile.write("#spec_utf8: transcoding error %%description -l %s\n" % (r.group(2)))
a5acbd63 93 success = False
c143293b
AF
94 elif in_changelog:
95 try:
96 outline = unicode(l, "UTF-8").encode("UTF-8")
97 except UnicodeDecodeError:
98 try:
99 outline = unicode(l, "ISO-8859-2").encode("UTF-8")
100 except UnicodeDecodeError:
101 outfile.write("#spec_utf8: transcoding next line from Latin2 failed\n")
a5acbd63 102 success = False
c143293b
AF
103 else:
104 # Summary
11774a5d
AF
105 r = re_summary.match(l)
106 if r:
fb3b78fc
AF
107 (enc, pure_lang) = find_encoding(r.group(1))
108 if enc == None:
8bb5df68 109 outfile.write("#spec_utf8: unknow lang code Summary(%s)\n" % (r.group(1)))
a5acbd63 110 success = False
fb3b78fc 111 elif not re_utf.search(enc):
11774a5d 112 try:
fb3b78fc 113 desc = unicode(r.group(2), enc).encode("UTF-8")
c143293b 114 outline = "Summary(%s.UTF-8): %s\n" % (pure_lang, desc)
11774a5d 115 except UnicodeDecodeError:
8bb5df68 116 outfile.write("#spec_utf8: ranscoding error Summary(%s)\n" % (r.group(1)))
a5acbd63 117 success = False
c143293b
AF
118 elif re_changelog.match(l):
119 # %changelog start
120 in_changelog = True
121
122
123 if debug: outfile.write("%s, %s\n"% (in_desc, in_changelog))
124 outfile.write("%s"% (outline, ))
8b4cb13b 125 return success
11774a5d 126
89cef9e8 127def main(argv):
8bb5df68 128 print "Converting %s ..." % argv[1]
89cef9e8
AM
129 f = open(argv[1], 'r')
130 sio = StringIO.StringIO()
131 sio.write(f.read())
132 f.close()
133 sio.seek(0)
134 f = open(argv[1] + '.tmp', 'w')
a5acbd63
AM
135 if not parse_spec(sio, f):
136 sys.stderr.write(" Problems while converting %s.\n" % argv[1])
89cef9e8
AM
137 f.close()
138 os.rename(argv[1] + '.tmp', argv[1])
11774a5d
AF
139
140if __name__ == "__main__":
89cef9e8
AM
141 if len(sys.argv) != 2:
142 sys.stderr.write("Usage: %s <spec file>\n" % sys.argv[0])
143 sys.exit(1)
144 main(sys.argv)
11774a5d 145
This page took 0.170742 seconds and 4 git commands to generate.