1"""Parse sys/errno.h and Errors.h and create Estr resource""" 2 3import re 4import string 5from Carbon import Res 6import os 7 8READ = 1 9WRITE = 2 10smAllScripts = -3 11 12ERRNO_PROG="#define[ \t]+" \ 13 "([A-Z0-9a-z_]+)" \ 14 "[ \t]+" \ 15 "([0-9]+)" \ 16 "[ \t]*/\*[ \t]*" \ 17 "(.*)" \ 18 "[ \t]*\*/" 19 20ERRORS_PROG="[ \t]*" \ 21 "([A-Z0-9a-z_]+)" \ 22 "[ \t]*=[ \t]*" \ 23 "([-0-9]+)" \ 24 "[, \t]*/\*[ \t]*" \ 25 "(.*)" \ 26 "[ \t]*\*/" 27 28ERRORS_PROG_2="[ \t]*" \ 29 "([A-Z0-9a-z_]+)" \ 30 "[ \t]*=[ \t]*" \ 31 "([-0-9]+)" \ 32 "[, \t]*" 33 34def 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 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 52def 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): 60 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 75def parse_errors_h(fp, dict): 76 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 105def main(): 106 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() 112 113 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() 118 119 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 156if __name__ == '__main__': 157 main() 158