]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - spec_utf8
try to transcode %changelog from Laint2
[packages/rpm-build-tools.git] / spec_utf8
1 #!/usr/bin/python
2
3 debug = False
4
5 import os, os.path, re, sys, locale
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',
33     'ru':'KOI8-R',
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
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
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")
65   in_desc = False
66   in_changelog = False
67
68   for l in infile:
69     outline = l
70     if debug: outfile.write("%s, %s, %s" % (in_desc, in_changelog, l))
71
72     # %description start
73     r = re_desc.match(l)
74     if r:
75       (enc, pure_lang) = find_encoding(r.group(2))
76       if enc == None:
77         outfile.write("#spec_utf8: unknown lang code in %%description -l %s\n" % (lang))
78       elif not re_utf.search(enc):
79         in_desc = True
80         outline = "%s-l %s.UTF-8%s\n" % (r.group(1), pure_lang, r.group(3))
81     elif in_desc:
82       if re_proc.search(l):
83         in_desc = False
84       else:
85         # %description continues
86         if not re_utf.search(enc):
87           try:
88             outline = unicode(l, enc).encode("UTF-8")
89           except UnicodeDecodeError:
90             outfile.write("#spec_utf8: transcoding error %%description -l %s\n" % (lang))
91     elif in_changelog:
92       try:
93         outline = unicode(l, "UTF-8").encode("UTF-8")
94       except UnicodeDecodeError:
95         try:
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")
99     else: 
100       # Summary
101       r = re_summary.match(l)
102       if r:
103         (enc, pure_lang) = find_encoding(r.group(1))
104         if enc == None:
105           outfile.write("#spec_utf8: unknow lang code Summary(%s)\n" % (lang))
106         elif not re_utf.search(enc):
107           try:
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):
113         # %changelog start
114         in_changelog = True
115     
116     
117     if debug: outfile.write("%s, %s\n"% (in_desc, in_changelog))
118     outfile.write("%s"% (outline, ))
119
120 def main():
121   parse_spec(sys.stdin, sys.stdout)
122
123 if __name__ == "__main__":
124   main()
125
This page took 0.067262 seconds and 4 git commands to generate.