/Mac/scripts/mkestrres.py

http://unladen-swallow.googlecode.com/ · Python · 157 lines · 130 code · 25 blank · 2 comment · 26 complexity · 6a7fa8170fecb7a94906935567884fe4 MD5 · raw file

  1. """Parse sys/errno.h and Errors.h and create Estr resource"""
  2. import re
  3. import string
  4. from Carbon import Res
  5. import os
  6. READ = 1
  7. WRITE = 2
  8. smAllScripts = -3
  9. ERRNO_PROG="#define[ \t]+" \
  10. "([A-Z0-9a-z_]+)" \
  11. "[ \t]+" \
  12. "([0-9]+)" \
  13. "[ \t]*/\*[ \t]*" \
  14. "(.*)" \
  15. "[ \t]*\*/"
  16. ERRORS_PROG="[ \t]*" \
  17. "([A-Z0-9a-z_]+)" \
  18. "[ \t]*=[ \t]*" \
  19. "([-0-9]+)" \
  20. "[, \t]*/\*[ \t]*" \
  21. "(.*)" \
  22. "[ \t]*\*/"
  23. ERRORS_PROG_2="[ \t]*" \
  24. "([A-Z0-9a-z_]+)" \
  25. "[ \t]*=[ \t]*" \
  26. "([-0-9]+)" \
  27. "[, \t]*"
  28. def Pstring(str):
  29. if len(str) > 255:
  30. raise ValueError, 'String too large'
  31. return chr(len(str))+str
  32. def writeestr(dst, edict):
  33. """Create Estr resource file given a dictionary of errors."""
  34. os.unlink(dst.as_pathname())
  35. Res.FSpCreateResFile(dst, 'RSED', 'rsrc', smAllScripts)
  36. output = Res.FSpOpenResFile(dst, WRITE)
  37. Res.UseResFile(output)
  38. for num in edict.keys():
  39. res = Res.Resource(Pstring(edict[num][0]))
  40. res.AddResource('Estr', num, '')
  41. res.WriteResource()
  42. Res.CloseResFile(output)
  43. def writepython(fp, dict):
  44. k = dict.keys()
  45. k.sort()
  46. for i in k:
  47. fp.write("%s\t=\t%d\t#%s\n"%(dict[i][1], i, dict[i][0]))
  48. def parse_errno_h(fp, dict):
  49. errno_prog = re.compile(ERRNO_PROG)
  50. for line in fp.readlines():
  51. m = errno_prog.match(line)
  52. if m:
  53. number = string.atoi(m.group(2))
  54. name = m.group(1)
  55. desc = string.strip(m.group(3))
  56. if not dict.has_key(number):
  57. dict[number] = desc, name
  58. else:
  59. print 'DUPLICATE', number
  60. print '\t', dict[number]
  61. print '\t', (desc, name)
  62. def parse_errors_h(fp, dict):
  63. errno_prog = re.compile(ERRORS_PROG)
  64. errno_prog_2 = re.compile(ERRORS_PROG_2)
  65. for line in fp.readlines():
  66. match = 0
  67. m = errno_prog.match(line)
  68. m2 = errno_prog_2.match(line)
  69. if m:
  70. number = string.atoi(m.group(2))
  71. name = m.group(1)
  72. desc = string.strip(m.group(3))
  73. match=1
  74. elif m2:
  75. number = string.atoi(m2.group(2))
  76. name = m2.group(1)
  77. desc = name
  78. match=1
  79. if match:
  80. if number > 0: continue
  81. if not dict.has_key(number):
  82. dict[number] = desc, name
  83. else:
  84. print 'DUPLICATE', number
  85. print '\t', dict[number]
  86. print '\t', (desc, name)
  87. if len(desc) > len(dict[number][0]):
  88. print 'Pick second one'
  89. dict[number] = desc, name
  90. def main():
  91. dict = {}
  92. pathname = EasyDialogs.AskFileForOpen(message="Where is GUSI sys/errno.h?")
  93. if pathname:
  94. fp = open(pathname)
  95. parse_errno_h(fp, dict)
  96. fp.close()
  97. pathname = EasyDialogs.AskFileForOpen(message="Select cerrno (MSL) or cancel")
  98. if pathname:
  99. fp = open(pathname)
  100. parse_errno_h(fp, dict)
  101. fp.close()
  102. pathname = EasyDialogs.AskFileForOpen(message="Where is MacErrors.h?")
  103. if pathname:
  104. fp = open(pathname)
  105. parse_errors_h(fp, dict)
  106. fp.close()
  107. pathname = EasyDialogs.AskFileForOpen(message="Where is mkestrres-MacErrors.h?")
  108. if pathname:
  109. fp = open(pathname)
  110. parse_errors_h(fp, dict)
  111. fp.close()
  112. if not dict:
  113. return
  114. pathname = EasyDialogs.AskFileForSave(message="Resource output file?", savedFileName="errors.rsrc")
  115. if pathname:
  116. writeestr(fss, dict)
  117. pathname = EasyDialogs.AskFileForSave(message="Python output file?", savedFileName="macerrors.py")
  118. if pathname:
  119. fp = open(pathname, "w")
  120. writepython(fp, dict)
  121. fp.close()
  122. fss.SetCreatorType('Pyth', 'TEXT')
  123. pathname = EasyDialogs.AskFileForSave(message="Text output file?", savedFileName="errors.txt")
  124. if pathname:
  125. fp = open(pathname, "w")
  126. k = dict.keys()
  127. k.sort()
  128. for i in k:
  129. fp.write("%d\t%s\t%s\n"%(i, dict[i][1], dict[i][0]))
  130. fp.close()
  131. if __name__ == '__main__':
  132. main()