checkcase.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import sys, os, os.path, re, xml.dom.minidom, xml.dom, optparse
  2. def usage():
  3. print """
  4. usage:
  5. checkcase.py -f [fzp folder] -s [svg folder]
  6. ensure all fzp files case-sensitively match svg file names
  7. """
  8. def main():
  9. parser = optparse.OptionParser()
  10. parser.add_option('-f', '--fzp', dest="fzpdir" )
  11. parser.add_option('-s', '--svg', dest="svgdir" )
  12. (options, args) = parser.parse_args()
  13. if not options.fzpdir:
  14. usage()
  15. parser.error("fzp dir argument not given")
  16. return
  17. if not options.svgdir:
  18. usage()
  19. parser.error("svg dir argument not given")
  20. return
  21. allsvgs = []
  22. lowersvgs = {}
  23. for root, dirs, files in os.walk(options.svgdir, topdown=False):
  24. for filename in files:
  25. if not filename.endswith(".svg"):
  26. continue
  27. path = os.path.join(root, filename)
  28. allsvgs.append(path)
  29. lowersvgs[path.lower()] = filename
  30. for root, dirs, files in os.walk(options.fzpdir, topdown=False):
  31. for filename in files:
  32. if not filename.endswith(".fzp"):
  33. continue
  34. fzpFilename = os.path.join(root, filename)
  35. try:
  36. dom = xml.dom.minidom.parse(fzpFilename)
  37. except xml.parsers.expat.ExpatError, err:
  38. print str(err), fzpFilename
  39. continue
  40. doUpdate = False
  41. fzp = dom.documentElement
  42. layerss = fzp.getElementsByTagName("layers")
  43. for layers in layerss:
  44. image = layers.getAttribute("image").replace("/", "\\")
  45. if ("dip_" in image) and ("mil_" in image):
  46. continue
  47. if ("sip_" in image) and ("mil_" in image):
  48. continue
  49. if ("jumper_" in image) and ("mil_" in image):
  50. continue
  51. if ("screw_terminal_" in image):
  52. continue
  53. if ("jumper" in image):
  54. continue
  55. if ("mystery_" in image):
  56. continue
  57. if ("LED-" in image):
  58. continue
  59. if ("axial_lay" in image):
  60. continue
  61. if ("resistor_" in image):
  62. continue
  63. if ("generic" in image) and ("header" in image):
  64. continue
  65. path1 = os.path.join(options.svgdir, "core", image)
  66. path2 = os.path.join(options.svgdir, "contrib", image)
  67. path3 = os.path.join(options.svgdir, "obsolete", image)
  68. if os.path.isfile(path1) or os.path.isfile(path2) or os.path.isfile(path3):
  69. for path in [path1, path2, path3]:
  70. try:
  71. handle = open(path)
  72. if not path in allsvgs:
  73. print "mismatch", fzpFilename
  74. print "\t", path
  75. print
  76. thing = layers.getAttribute("image").split("/")
  77. thing[1] = lowersvgs[path.lower()]
  78. layers.setAttribute("image", "/".join(thing))
  79. doUpdate = True
  80. except:
  81. pass
  82. else:
  83. print "missing", fzpFilename, image
  84. if doUpdate:
  85. outfile = open(fzpFilename, 'wb')
  86. s = dom.toxml("UTF-8")
  87. outfile.write(s)
  88. outfile.close()
  89. if __name__ == "__main__":
  90. main()