connectors_misnumbered.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # usage:
  2. # copper1fzp.py -d [fzp folder]
  3. # adds a copper1 layer if there isn't one found already.
  4. import getopt, sys, os, os.path, re, xml.dom.minidom, xml.dom
  5. def usage():
  6. print """
  7. usage:
  8. connectors_misnumbered.py -d [fzp folder]
  9. checks that connectors with integer names are correctly mapped to connector numbers
  10. """
  11. def main():
  12. try:
  13. opts, args = getopt.getopt(sys.argv[1:], "hd:", ["help", "directory"])
  14. except getopt.GetoptError, err:
  15. # print help information and exit:
  16. print str(err) # will print something like "option -a not recognized"
  17. usage()
  18. return
  19. dir = None
  20. for o, a in opts:
  21. #print o
  22. #print a
  23. if o in ("-d", "--directory"):
  24. dir = a
  25. elif o in ("-h", "--help"):
  26. usage()
  27. return
  28. else:
  29. assert False, "unhandled option"
  30. if(not(dir)):
  31. usage()
  32. return
  33. pattern = r'(\d+)'
  34. numberFinder = re.compile(pattern, re.IGNORECASE)
  35. for root, dirs, files in os.walk(dir, topdown=False):
  36. for filename in files:
  37. if not filename.endswith(".fzp"):
  38. continue
  39. fzpFilename = os.path.join(root, filename)
  40. try:
  41. dom = xml.dom.minidom.parse(fzpFilename)
  42. except xml.parsers.expat.ExpatError, err:
  43. print str(err), fzpFilename
  44. continue
  45. fzp = dom.documentElement
  46. connectors = fzp.getElementsByTagName("connector")
  47. gotInt = False
  48. for connector in connectors:
  49. try:
  50. intname = int(connector.getAttribute("name"))
  51. gotInt = True
  52. except:
  53. continue
  54. if not gotInt:
  55. continue
  56. idZero = False
  57. for connector in connectors:
  58. try:
  59. id = connector.getAttribute("id")
  60. match = numberFinder.search(id)
  61. if match == None:
  62. continue
  63. if match.group(1) == '0':
  64. idZero = True
  65. break
  66. except:
  67. continue
  68. nameZero = False
  69. for connector in connectors:
  70. if connector.getAttribute("name") == "0":
  71. nameZero = True
  72. break
  73. mismatches = []
  74. for connector in connectors:
  75. idInt = 0
  76. nameInt = 0
  77. try:
  78. id = connector.getAttribute("id")
  79. match = numberFinder.search(id)
  80. if match == None:
  81. continue
  82. idInt = int(match.group(1))
  83. nameInt = int(connector.getAttribute("name"))
  84. except:
  85. continue
  86. mismatch = False
  87. if nameZero and idZero:
  88. mismatch = (idInt != nameInt)
  89. elif nameZero:
  90. mismatch = (idInt != nameInt + 1)
  91. elif idZero:
  92. mismatch = (idInt + 1 != nameInt)
  93. else:
  94. mismatch = (idInt != nameInt)
  95. if mismatch:
  96. mismatches.append(connector)
  97. if len(mismatches) > 0:
  98. print fzpFilename, nameZero, idZero
  99. for connector in mismatches:
  100. strings = connector.toxml().split("\n")
  101. print strings[0]
  102. print
  103. if __name__ == "__main__":
  104. main()