blob: 715b8c62f6780982c367df2908ada24b1d22c758 [file] [log] [blame]
Jack Jansenac4d8691996-09-05 15:22:16 +00001"""Parse sys/errno.h and Errors.h and create Estr resource"""
2
Jack Jansenbe614ee2001-01-02 22:02:02 +00003import re
Guido van Rossum3c3eda21995-02-05 17:01:45 +00004import string
Jack Jansen5a6fdcd2001-08-25 12:15:04 +00005from Carbon import Res
Jack Jansenac4d8691996-09-05 15:22:16 +00006import os
Guido van Rossum3c3eda21995-02-05 17:01:45 +00007
8READ = 1
9WRITE = 2
10smAllScripts = -3
11
Jack Jansenac4d8691996-09-05 15:22:16 +000012ERRNO_PROG="#define[ \t]+" \
Tim Peters182b5ac2004-07-18 06:16:08 +000013 "([A-Z0-9a-z_]+)" \
14 "[ \t]+" \
15 "([0-9]+)" \
16 "[ \t]*/\*[ \t]*" \
17 "(.*)" \
18 "[ \t]*\*/"
19
Jack Jansenac4d8691996-09-05 15:22:16 +000020ERRORS_PROG="[ \t]*" \
Tim Peters182b5ac2004-07-18 06:16:08 +000021 "([A-Z0-9a-z_]+)" \
22 "[ \t]*=[ \t]*" \
23 "([-0-9]+)" \
24 "[, \t]*/\*[ \t]*" \
25 "(.*)" \
26 "[ \t]*\*/"
27
Jack Jansen955a2f51998-04-21 15:27:45 +000028ERRORS_PROG_2="[ \t]*" \
Tim Peters182b5ac2004-07-18 06:16:08 +000029 "([A-Z0-9a-z_]+)" \
30 "[ \t]*=[ \t]*" \
31 "([-0-9]+)" \
32 "[, \t]*"
Jack Jansenac4d8691996-09-05 15:22:16 +000033
Guido van Rossum3c3eda21995-02-05 17:01:45 +000034def Pstring(str):
Tim Peters182b5ac2004-07-18 06:16:08 +000035 if len(str) > 255:
36 raise ValueError, 'String too large'
37 return chr(len(str))+str
38
Guido van Rossum3c3eda21995-02-05 17:01:45 +000039def writeestr(dst, edict):
Tim Peters182b5ac2004-07-18 06:16:08 +000040 """Create Estr resource file given a dictionary of errors."""
41
42 os.unlink(dst.as_pathname())
43 Res.FSpCreateResFile(dst, 'RSED', 'rsrc', smAllScripts)
44 output = Res.FSpOpenResFile(dst, WRITE)
45 Res.UseResFile(output)
46 for num in edict.keys():
47 res = Res.Resource(Pstring(edict[num][0]))
48 res.AddResource('Estr', num, '')
49 res.WriteResource()
50 Res.CloseResFile(output)
51
Jack Jansenac4d8691996-09-05 15:22:16 +000052def writepython(fp, dict):
Tim Peters182b5ac2004-07-18 06:16:08 +000053 k = dict.keys()
54 k.sort()
55 for i in k:
56 fp.write("%s\t=\t%d\t#%s\n"%(dict[i][1], i, dict[i][0]))
57
Jack Jansenac4d8691996-09-05 15:22:16 +000058
59def parse_errno_h(fp, dict):
Tim Peters182b5ac2004-07-18 06:16:08 +000060 errno_prog = re.compile(ERRNO_PROG)
61 for line in fp.readlines():
62 m = errno_prog.match(line)
63 if m:
64 number = string.atoi(m.group(2))
65 name = m.group(1)
66 desc = string.strip(m.group(3))
67
68 if not dict.has_key(number):
69 dict[number] = desc, name
70 else:
71 print 'DUPLICATE', number
72 print '\t', dict[number]
73 print '\t', (desc, name)
74
Jack Jansenac4d8691996-09-05 15:22:16 +000075def parse_errors_h(fp, dict):
Tim Peters182b5ac2004-07-18 06:16:08 +000076 errno_prog = re.compile(ERRORS_PROG)
77 errno_prog_2 = re.compile(ERRORS_PROG_2)
78 for line in fp.readlines():
79 match = 0
80 m = errno_prog.match(line)
81 m2 = errno_prog_2.match(line)
82 if m:
83 number = string.atoi(m.group(2))
84 name = m.group(1)
85 desc = string.strip(m.group(3))
86 match=1
87 elif m2:
88 number = string.atoi(m2.group(2))
89 name = m2.group(1)
90 desc = name
91 match=1
92 if match:
93 if number > 0: continue
94
95 if not dict.has_key(number):
96 dict[number] = desc, name
97 else:
98 print 'DUPLICATE', number
99 print '\t', dict[number]
100 print '\t', (desc, name)
101 if len(desc) > len(dict[number][0]):
102 print 'Pick second one'
103 dict[number] = desc, name
104
Jack Jansenac4d8691996-09-05 15:22:16 +0000105def main():
Tim Peters182b5ac2004-07-18 06:16:08 +0000106 dict = {}
107 pathname = EasyDialogs.AskFileForOpen(message="Where is GUSI sys/errno.h?")
108 if pathname:
109 fp = open(pathname)
110 parse_errno_h(fp, dict)
111 fp.close()
Jack Jansenac4d8691996-09-05 15:22:16 +0000112
Tim Peters182b5ac2004-07-18 06:16:08 +0000113 pathname = EasyDialogs.AskFileForOpen(message="Select cerrno (MSL) or cancel")
114 if pathname:
115 fp = open(pathname)
116 parse_errno_h(fp, dict)
117 fp.close()
Jack Jansenac4d8691996-09-05 15:22:16 +0000118
Tim Peters182b5ac2004-07-18 06:16:08 +0000119 pathname = EasyDialogs.AskFileForOpen(message="Where is MacErrors.h?")
120 if pathname:
121 fp = open(pathname)
122 parse_errors_h(fp, dict)
123 fp.close()
124
125 pathname = EasyDialogs.AskFileForOpen(message="Where is mkestrres-MacErrors.h?")
126 if pathname:
127 fp = open(pathname)
128 parse_errors_h(fp, dict)
129 fp.close()
130
131 if not dict:
132 return
133
134 pathname = EasyDialogs.AskFileForSave(message="Resource output file?", savedFileName="errors.rsrc")
135 if pathname:
136 writeestr(fss, dict)
137
138 pathname = EasyDialogs.AskFileForSave(message="Python output file?", savedFileName="macerrors.py")
139 if pathname:
140 fp = open(pathname, "w")
141 writepython(fp, dict)
142 fp.close()
143 fss.SetCreatorType('Pyth', 'TEXT')
144
145 pathname = EasyDialogs.AskFileForSave(message="Text output file?", savedFileName="errors.txt")
146 if pathname:
147 fp = open(pathname, "w")
148
149 k = dict.keys()
150 k.sort()
151 for i in k:
152 fp.write("%d\t%s\t%s\n"%(i, dict[i][1], dict[i][0]))
153 fp.close()
154
155
Guido van Rossum3c3eda21995-02-05 17:01:45 +0000156if __name__ == '__main__':
Tim Peters182b5ac2004-07-18 06:16:08 +0000157 main()