diff options
author | Jack Jansen <jack.jansen@cwi.nl> | 1996-09-05 15:22:16 (GMT) |
---|---|---|
committer | Jack Jansen <jack.jansen@cwi.nl> | 1996-09-05 15:22:16 (GMT) |
commit | ac4d869433290482f827ccb12a368a242357ac5b (patch) | |
tree | 10c85eff078441934d5ab7c7c51d4cb8047f1abb /Mac/scripts/mkestrres.py | |
parent | 6c3d35aeb9d835fe13626bc6bf08818554949e4a (diff) | |
download | cpython-ac4d869433290482f827ccb12a368a242357ac5b.zip cpython-ac4d869433290482f827ccb12a368a242357ac5b.tar.gz cpython-ac4d869433290482f827ccb12a368a242357ac5b.tar.bz2 |
Completely redone. mkestrres now parses errno.h and Errors.h files
(which have the descriptions of the errors in comments) and
generates errors.txt, macerrors.py and errors.rsrc
Diffstat (limited to 'Mac/scripts/mkestrres.py')
-rw-r--r-- | Mac/scripts/mkestrres.py | 148 |
1 files changed, 106 insertions, 42 deletions
diff --git a/Mac/scripts/mkestrres.py b/Mac/scripts/mkestrres.py index ae9114b..56fec98 100644 --- a/Mac/scripts/mkestrres.py +++ b/Mac/scripts/mkestrres.py @@ -1,15 +1,31 @@ -# -# Create 'Estr' resource from error dictionary -from Res import * -import Res -from Resources import * -import MacOS +"""Parse sys/errno.h and Errors.h and create Estr resource""" + +import regex +import macfs import string +import Res +import os READ = 1 WRITE = 2 smAllScripts = -3 +ERRNO_PROG="#define[ \t]+" \ + "\([A-Z0-9a-z_]+\)" \ + "[ \t]+" \ + "\([0-9]+\)" \ + "[ \t]*/\*[ \t]*" \ + "\(.*\)" \ + "[ \t]*\*/" + +ERRORS_PROG="[ \t]*" \ + "\([A-Z0-9a-z_]+\)" \ + "[ \t]*=[ \t]*" \ + "\([-0-9]+\)" \ + "[, \t]*/\*[ \t]*" \ + "\(.*\)" \ + "[ \t]*\*/" + def Pstring(str): if len(str) > 255: raise ValueError, 'String too large' @@ -18,45 +34,93 @@ def Pstring(str): def writeestr(dst, edict): """Create Estr resource file given a dictionary of errors.""" - - FSpCreateResFile(dst, 'RSED', 'rsrc', smAllScripts) - output = FSpOpenResFile(dst, WRITE) - UseResFile(output) + os.unlink(dst.as_pathname()) + Res.FSpCreateResFile(dst, 'RSED', 'rsrc', smAllScripts) + output = Res.FSpOpenResFile(dst, WRITE) + Res.UseResFile(output) for num in edict.keys(): - res = Resource(Pstring(edict[num])) + res = Res.Resource(Pstring(edict[num][0])) res.AddResource('Estr', num, '') res.WriteResource() - CloseResFile(output) - -def parsefile(src): - fp = open(src) - lines = [] - while 1: - x = fp.readline() - if not x: - break - x = x[:-1] - words = string.split(x) - if x[0] in (' ', '\t'): - # continuation line - x = string.join(words) - lines[-1] = lines[-1] + ' ' + x - else: - x = string.join(words) - lines.append(x) - dict = {} - for line in lines: - words = string.split(line) - index = eval(words[0]) - if dict.has_key(index): - print '** Duplicate key:', index - x = string.join(words[2:]) - if not x: - x = words[1] - dict[index] = x - return dict + Res.CloseResFile(output) + +def writepython(fp, dict): + k = dict.keys() + k.sort() + for i in k: + fp.write("%s\t=\t%d\t#%s\n"%(dict[i][1], i, dict[i][0])) + + +def parse_errno_h(fp, dict): + errno_prog = regex.compile(ERRNO_PROG) + for line in fp.readlines(): + if errno_prog.match(line) > 0: + number = string.atoi(errno_prog.group(2)) + name = errno_prog.group(1) + desc = string.strip(errno_prog.group(3)) + if not dict.has_key(number): + dict[number] = desc, name + else: + print 'DUPLICATE', number + print '\t', dict[number] + print '\t', (desc, name) + +def parse_errors_h(fp, dict): + errno_prog = regex.compile(ERRORS_PROG) + for line in fp.readlines(): + if errno_prog.match(line) > 0: + number = string.atoi(errno_prog.group(2)) + name = errno_prog.group(1) + desc = string.strip(errno_prog.group(3)) + if number > 0: continue + + if not dict.has_key(number): + dict[number] = desc, name + else: + print 'DUPLICATE', number + print '\t', dict[number] + print '\t', (desc, name) + +def main(): + dict = {} + fss, ok = macfs.PromptGetFile("Where is errno.h?") + if not ok: return + fp = open(fss.as_pathname()) + parse_errno_h(fp, dict) + fp.close() + + fss, ok = macfs.PromptGetFile("Where is Errors.h?") + if not ok: return + fp = open(fss.as_pathname()) + parse_errors_h(fp, dict) + fp.close() + + if not dict: + return + + fss, ok = macfs.StandardPutFile("Resource output file?", "errors.rsrc") + if ok: + writeestr(fss, dict) + + fss, ok = macfs.StandardPutFile("Python output file?", "macerrors.py") + if ok: + fp = open(fss.as_pathname(), "w") + writepython(fp, dict) + fp.close() + fss.SetCreatorType('Pyth', 'TEXT') + + fss, ok = macfs.StandardPutFile("Text output file?", "errors.txt") + if ok: + fp = open(fss.as_pathname(), "w") + + k = dict.keys() + k.sort() + for i in k: + fp.write("%d\t%s\t%s\n"%(i, dict[i][1], dict[i][0])) + fp.close() + if __name__ == '__main__': - dict = parsefile('errors.txt') - writeestr('errors.rsrc', dict) + main() + |