Null commit with -f option to force an uprev and put HEADs firmly on the trunk.
[python/dscho.git] / Mac / scripts / mkestrres.py
blob3ed3b34f6df89ac9d6f848effe5870d9c75812b2
1 """Parse sys/errno.h and Errors.h and create Estr resource"""
3 import re
4 import macfs
5 import string
6 import Res
7 import os
9 READ = 1
10 WRITE = 2
11 smAllScripts = -3
13 ERRNO_PROG="#define[ \t]+" \
14 "([A-Z0-9a-z_]+)" \
15 "[ \t]+" \
16 "([0-9]+)" \
17 "[ \t]*/\*[ \t]*" \
18 "(.*)" \
19 "[ \t]*\*/"
21 ERRORS_PROG="[ \t]*" \
22 "([A-Z0-9a-z_]+)" \
23 "[ \t]*=[ \t]*" \
24 "([-0-9]+)" \
25 "[, \t]*/\*[ \t]*" \
26 "(.*)" \
27 "[ \t]*\*/"
29 ERRORS_PROG_2="[ \t]*" \
30 "([A-Z0-9a-z_]+)" \
31 "[ \t]*=[ \t]*" \
32 "([-0-9]+)" \
33 "[, \t]*"
35 def Pstring(str):
36 if len(str) > 255:
37 raise ValueError, 'String too large'
38 return chr(len(str))+str
40 def writeestr(dst, edict):
41 """Create Estr resource file given a dictionary of errors."""
43 os.unlink(dst.as_pathname())
44 Res.FSpCreateResFile(dst, 'RSED', 'rsrc', smAllScripts)
45 output = Res.FSpOpenResFile(dst, WRITE)
46 Res.UseResFile(output)
47 for num in edict.keys():
48 res = Res.Resource(Pstring(edict[num][0]))
49 res.AddResource('Estr', num, '')
50 res.WriteResource()
51 Res.CloseResFile(output)
53 def writepython(fp, dict):
54 k = dict.keys()
55 k.sort()
56 for i in k:
57 fp.write("%s\t=\t%d\t#%s\n"%(dict[i][1], i, dict[i][0]))
60 def parse_errno_h(fp, dict):
61 errno_prog = re.compile(ERRNO_PROG)
62 for line in fp.readlines():
63 m = errno_prog.match(line)
64 if m:
65 number = string.atoi(m.group(2))
66 name = m.group(1)
67 desc = string.strip(m.group(3))
69 if not dict.has_key(number):
70 dict[number] = desc, name
71 else:
72 print 'DUPLICATE', number
73 print '\t', dict[number]
74 print '\t', (desc, name)
76 def parse_errors_h(fp, dict):
77 errno_prog = re.compile(ERRORS_PROG)
78 errno_prog_2 = re.compile(ERRORS_PROG_2)
79 for line in fp.readlines():
80 match = 0
81 m = errno_prog.match(line)
82 m2 = errno_prog_2.match(line)
83 if m:
84 number = string.atoi(m.group(2))
85 name = m.group(1)
86 desc = string.strip(m.group(3))
87 match=1
88 elif m2:
89 number = string.atoi(m2.group(2))
90 name = m2.group(1)
91 desc = name
92 match=1
93 if match:
94 if number > 0: continue
96 if not dict.has_key(number):
97 dict[number] = desc, name
98 else:
99 print 'DUPLICATE', number
100 print '\t', dict[number]
101 print '\t', (desc, name)
102 if len(desc) > len(dict[number][0]):
103 print 'Pick second one'
104 dict[number] = desc, name
106 def main():
107 dict = {}
108 fss, ok = macfs.PromptGetFile("Where is GUSI sys/errno.h?")
109 if not ok: return
110 fp = open(fss.as_pathname())
111 parse_errno_h(fp, dict)
112 fp.close()
114 fss, ok = macfs.PromptGetFile("Select 2nd errno.h (MSL) or cancel")
115 if not ok: return
116 fp = open(fss.as_pathname())
117 parse_errno_h(fp, dict)
118 fp.close()
120 fss, ok = macfs.PromptGetFile("Where is Errors.h?")
121 if not ok: return
122 fp = open(fss.as_pathname())
123 parse_errors_h(fp, dict)
124 fp.close()
126 if not dict:
127 return
129 fss, ok = macfs.StandardPutFile("Resource output file?", "errors.rsrc")
130 if ok:
131 writeestr(fss, dict)
133 fss, ok = macfs.StandardPutFile("Python output file?", "macerrors.py")
134 if ok:
135 fp = open(fss.as_pathname(), "w")
136 writepython(fp, dict)
137 fp.close()
138 fss.SetCreatorType('Pyth', 'TEXT')
140 fss, ok = macfs.StandardPutFile("Text output file?", "errors.txt")
141 if ok:
142 fp = open(fss.as_pathname(), "w")
144 k = dict.keys()
145 k.sort()
146 for i in k:
147 fp.write("%d\t%s\t%s\n"%(i, dict[i][1], dict[i][0]))
148 fp.close()
151 if __name__ == '__main__':
152 main()