Jack Jansen | ac4d869 | 1996-09-05 15:22:16 +0000 | [diff] [blame] | 1 | """Parse sys/errno.h and Errors.h and create Estr resource""" |
| 2 | |
Jack Jansen | be614ee | 2001-01-02 22:02:02 +0000 | [diff] [blame] | 3 | import re |
Guido van Rossum | 3c3eda2 | 1995-02-05 17:01:45 +0000 | [diff] [blame] | 4 | import string |
Jack Jansen | 5a6fdcd | 2001-08-25 12:15:04 +0000 | [diff] [blame] | 5 | from Carbon import Res |
Jack Jansen | ac4d869 | 1996-09-05 15:22:16 +0000 | [diff] [blame] | 6 | import os |
Guido van Rossum | 3c3eda2 | 1995-02-05 17:01:45 +0000 | [diff] [blame] | 7 | |
| 8 | READ = 1 |
| 9 | WRITE = 2 |
| 10 | smAllScripts = -3 |
| 11 | |
Jack Jansen | ac4d869 | 1996-09-05 15:22:16 +0000 | [diff] [blame] | 12 | ERRNO_PROG="#define[ \t]+" \ |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 13 | "([A-Z0-9a-z_]+)" \ |
| 14 | "[ \t]+" \ |
| 15 | "([0-9]+)" \ |
| 16 | "[ \t]*/\*[ \t]*" \ |
| 17 | "(.*)" \ |
| 18 | "[ \t]*\*/" |
| 19 | |
Jack Jansen | ac4d869 | 1996-09-05 15:22:16 +0000 | [diff] [blame] | 20 | ERRORS_PROG="[ \t]*" \ |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 21 | "([A-Z0-9a-z_]+)" \ |
| 22 | "[ \t]*=[ \t]*" \ |
| 23 | "([-0-9]+)" \ |
| 24 | "[, \t]*/\*[ \t]*" \ |
| 25 | "(.*)" \ |
| 26 | "[ \t]*\*/" |
| 27 | |
Jack Jansen | 955a2f5 | 1998-04-21 15:27:45 +0000 | [diff] [blame] | 28 | ERRORS_PROG_2="[ \t]*" \ |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 29 | "([A-Z0-9a-z_]+)" \ |
| 30 | "[ \t]*=[ \t]*" \ |
| 31 | "([-0-9]+)" \ |
| 32 | "[, \t]*" |
Jack Jansen | ac4d869 | 1996-09-05 15:22:16 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | 3c3eda2 | 1995-02-05 17:01:45 +0000 | [diff] [blame] | 34 | def Pstring(str): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 35 | if len(str) > 255: |
| 36 | raise ValueError, 'String too large' |
| 37 | return chr(len(str))+str |
| 38 | |
Guido van Rossum | 3c3eda2 | 1995-02-05 17:01:45 +0000 | [diff] [blame] | 39 | def writeestr(dst, edict): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 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 | |
Jack Jansen | ac4d869 | 1996-09-05 15:22:16 +0000 | [diff] [blame] | 52 | def writepython(fp, dict): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 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 | |
Jack Jansen | ac4d869 | 1996-09-05 15:22:16 +0000 | [diff] [blame] | 58 | |
| 59 | def parse_errno_h(fp, dict): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 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 | |
Jack Jansen | ac4d869 | 1996-09-05 15:22:16 +0000 | [diff] [blame] | 75 | def parse_errors_h(fp, dict): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 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 | |
Jack Jansen | ac4d869 | 1996-09-05 15:22:16 +0000 | [diff] [blame] | 105 | def main(): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 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() |
Jack Jansen | ac4d869 | 1996-09-05 15:22:16 +0000 | [diff] [blame] | 112 | |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 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() |
Jack Jansen | ac4d869 | 1996-09-05 15:22:16 +0000 | [diff] [blame] | 118 | |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 3c3eda2 | 1995-02-05 17:01:45 +0000 | [diff] [blame] | 156 | if __name__ == '__main__': |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 157 | main() |