blob: faa203beb3107563ca7114762f29a26c53f8ece8 [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]+" \
Jack Jansenbe614ee2001-01-02 22:02:02 +000013 "([A-Z0-9a-z_]+)" \
Jack Jansenac4d8691996-09-05 15:22:16 +000014 "[ \t]+" \
Jack Jansenbe614ee2001-01-02 22:02:02 +000015 "([0-9]+)" \
Jack Jansenac4d8691996-09-05 15:22:16 +000016 "[ \t]*/\*[ \t]*" \
Jack Jansenbe614ee2001-01-02 22:02:02 +000017 "(.*)" \
Jack Jansenac4d8691996-09-05 15:22:16 +000018 "[ \t]*\*/"
19
20ERRORS_PROG="[ \t]*" \
Jack Jansenbe614ee2001-01-02 22:02:02 +000021 "([A-Z0-9a-z_]+)" \
Jack Jansenac4d8691996-09-05 15:22:16 +000022 "[ \t]*=[ \t]*" \
Jack Jansenbe614ee2001-01-02 22:02:02 +000023 "([-0-9]+)" \
Jack Jansenac4d8691996-09-05 15:22:16 +000024 "[, \t]*/\*[ \t]*" \
Jack Jansenbe614ee2001-01-02 22:02:02 +000025 "(.*)" \
Jack Jansenac4d8691996-09-05 15:22:16 +000026 "[ \t]*\*/"
Jack Jansen955a2f51998-04-21 15:27:45 +000027
28ERRORS_PROG_2="[ \t]*" \
Jack Jansenbe614ee2001-01-02 22:02:02 +000029 "([A-Z0-9a-z_]+)" \
Jack Jansen955a2f51998-04-21 15:27:45 +000030 "[ \t]*=[ \t]*" \
Jack Jansenbe614ee2001-01-02 22:02:02 +000031 "([-0-9]+)" \
Jack Jansen955a2f51998-04-21 15:27:45 +000032 "[, \t]*"
Jack Jansenac4d8691996-09-05 15:22:16 +000033
Guido van Rossum3c3eda21995-02-05 17:01:45 +000034def Pstring(str):
35 if len(str) > 255:
36 raise ValueError, 'String too large'
37 return chr(len(str))+str
38
39def writeestr(dst, edict):
40 """Create Estr resource file given a dictionary of errors."""
41
Jack Jansenac4d8691996-09-05 15:22:16 +000042 os.unlink(dst.as_pathname())
43 Res.FSpCreateResFile(dst, 'RSED', 'rsrc', smAllScripts)
44 output = Res.FSpOpenResFile(dst, WRITE)
45 Res.UseResFile(output)
Guido van Rossum3c3eda21995-02-05 17:01:45 +000046 for num in edict.keys():
Jack Jansenac4d8691996-09-05 15:22:16 +000047 res = Res.Resource(Pstring(edict[num][0]))
Guido van Rossum3c3eda21995-02-05 17:01:45 +000048 res.AddResource('Estr', num, '')
49 res.WriteResource()
Jack Jansenac4d8691996-09-05 15:22:16 +000050 Res.CloseResFile(output)
Guido van Rossum3c3eda21995-02-05 17:01:45 +000051
Jack Jansenac4d8691996-09-05 15:22:16 +000052def writepython(fp, dict):
53 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
58
59def parse_errno_h(fp, dict):
Jack Jansenbe614ee2001-01-02 22:02:02 +000060 errno_prog = re.compile(ERRNO_PROG)
Jack Jansenac4d8691996-09-05 15:22:16 +000061 for line in fp.readlines():
Jack Jansenbe614ee2001-01-02 22:02:02 +000062 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))
Guido van Rossum3c3eda21995-02-05 17:01:45 +000067
Jack Jansenac4d8691996-09-05 15:22:16 +000068 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
75def parse_errors_h(fp, dict):
Jack Jansenbe614ee2001-01-02 22:02:02 +000076 errno_prog = re.compile(ERRORS_PROG)
77 errno_prog_2 = re.compile(ERRORS_PROG_2)
Jack Jansenac4d8691996-09-05 15:22:16 +000078 for line in fp.readlines():
Jack Jansen955a2f51998-04-21 15:27:45 +000079 match = 0
Jack Jansenbe614ee2001-01-02 22:02:02 +000080 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))
Jack Jansen955a2f51998-04-21 15:27:45 +000086 match=1
Jack Jansenbe614ee2001-01-02 22:02:02 +000087 elif m2:
88 number = string.atoi(m2.group(2))
89 name = m2.group(1)
Jack Jansen955a2f51998-04-21 15:27:45 +000090 desc = name
91 match=1
92 if match:
Jack Jansenac4d8691996-09-05 15:22:16 +000093 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)
Jack Jansen955a2f51998-04-21 15:27:45 +0000101 if len(desc) > len(dict[number][0]):
102 print 'Pick second one'
103 dict[number] = desc, name
Jack Jansenac4d8691996-09-05 15:22:16 +0000104
105def main():
106 dict = {}
Jack Jansen5a793292003-02-06 22:57:44 +0000107 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
Jack Jansen5a793292003-02-06 22:57:44 +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 Jansenb83b46d1997-05-15 11:18:13 +0000118
Jack Jansen5a793292003-02-06 22:57:44 +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()
Jack Jansenac4d8691996-09-05 15:22:16 +0000124
Jack Jansen5a793292003-02-06 22:57:44 +0000125 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()
Jack Jansen117dbdf2002-04-22 11:44:26 +0000130
Jack Jansenac4d8691996-09-05 15:22:16 +0000131 if not dict:
132 return
133
Jack Jansen5a793292003-02-06 22:57:44 +0000134 pathname = EasyDialogs.AskFileForSave(message="Resource output file?", savedFileName="errors.rsrc")
135 if pathname:
Jack Jansenac4d8691996-09-05 15:22:16 +0000136 writeestr(fss, dict)
137
Jack Jansen5a793292003-02-06 22:57:44 +0000138 pathname = EasyDialogs.AskFileForSave(message="Python output file?", savedFileName="macerrors.py")
139 if pathname:
140 fp = open(pathname, "w")
Jack Jansenac4d8691996-09-05 15:22:16 +0000141 writepython(fp, dict)
142 fp.close()
143 fss.SetCreatorType('Pyth', 'TEXT')
144
Jack Jansen5a793292003-02-06 22:57:44 +0000145 pathname = EasyDialogs.AskFileForSave(message="Text output file?", savedFileName="errors.txt")
146 if pathname:
147 fp = open(pathname, "w")
Jack Jansenac4d8691996-09-05 15:22:16 +0000148
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
Guido van Rossum3c3eda21995-02-05 17:01:45 +0000155
156if __name__ == '__main__':
Jack Jansenac4d8691996-09-05 15:22:16 +0000157 main()
158