syncconnectors.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import getopt, sys, os, os.path, re
  2. import xml.etree.ElementTree as ETree
  3. import traceback
  4. def usage():
  5. print """
  6. usage:
  7. syncconnectors.py -o { old fzp file } -n { new fzp file } -c { output csv file } -a
  8. adds 'replacedby' attribute to old connectors to point to new ones, when a partial name match is found
  9. overwrites the old fzp file, and saves record of changes to the csv file.
  10. to append to the csv file rather than overwrite it, use the -a option
  11. """
  12. IndexFinder = None
  13. def getIndex(element):
  14. global IndexFinder
  15. id = element.attrib.get("id")
  16. if id == None:
  17. return -1
  18. match = IndexFinder.search(id)
  19. if match == None:
  20. return -1
  21. return int(match.group(1))
  22. def main():
  23. global IndexFinder
  24. try:
  25. opts, args = getopt.getopt(sys.argv[1:], "ho:n:c:a", ["help", "old", "new","csv","append"])
  26. except getopt.GetoptError, err:
  27. # print help information and exit:
  28. print str(err) # will print something like "option -a not recognized"
  29. usage()
  30. return
  31. oldfn = None
  32. newfn = None
  33. csvfn = None
  34. append = False
  35. for o, a in opts:
  36. #print o
  37. #print a
  38. if o in ("-o", "--old"):
  39. oldfn = a
  40. elif o in ("-n", "--new"):
  41. newfn = a
  42. elif o in ("-c", "--csv"):
  43. csvfn = a
  44. elif o in ("-a", "--append"):
  45. append = True
  46. elif o in ("-h", "--help"):
  47. usage()
  48. return
  49. else:
  50. assert False, "unhandled option"
  51. if oldfn == None:
  52. print "'old' argument missing"
  53. usage()
  54. return
  55. if newfn == None:
  56. print "'new' argument missing"
  57. usage()
  58. return
  59. if csvfn == None:
  60. print "'csv' argument missing"
  61. usage()
  62. return
  63. if not append:
  64. try:
  65. os.remove(csvfn)
  66. except:
  67. pass
  68. csvfile = None
  69. try:
  70. flags = "w"
  71. if append:
  72. flags = "a"
  73. csvfile = open(csvfn, flags)
  74. except:
  75. pass
  76. if csvfile == None:
  77. print "unable to open", csvfn
  78. usage()
  79. return
  80. oldroot = None
  81. newroot = None
  82. try:
  83. oldroot = ETree.parse(oldfn)
  84. newroot = ETree.parse(newfn)
  85. except :
  86. info = sys.exc_info()
  87. traceback.print_exception(info[0], info[1], info[2])
  88. return
  89. pattern = r'(\d+)'
  90. IndexFinder = re.compile(pattern, re.IGNORECASE)
  91. newelements = newroot.findall(".//connector")
  92. newelements.sort(key=getIndex)
  93. newelementmarkers = [None] * len(newelements)
  94. oldelements = oldroot.findall(".//connector")
  95. oldelements.sort(key=getIndex)
  96. oldelementmarkers = [None] * len(oldelements)
  97. for oldix in range(0, len(oldelements)):
  98. oldelement = oldelements[oldix]
  99. oldname = oldelement.attrib.get("name")
  100. for newix in range(0, len(newelements)):
  101. if newelementmarkers[newix] != None:
  102. continue
  103. newelement = newelements[newix]
  104. if newelement.attrib.get("name") == oldname:
  105. oldelementmarkers[oldix] = newelements[newix]
  106. newelementmarkers[newix] = oldelements[oldix]
  107. break
  108. for oldix in range(0, len(oldelements)):
  109. if oldelementmarkers[oldix] != None:
  110. continue
  111. oldelement = oldelements[oldix]
  112. oldname = oldelement.attrib.get("name").lower()
  113. for newix in range(0, len(newelements)):
  114. if newelementmarkers[newix] != None:
  115. continue
  116. newelement = newelements[newix]
  117. newname = newelement.attrib.get("name").lower()
  118. if (newname in oldname) or (oldname in newname):
  119. oldelementmarkers[oldix] = newelements[newix]
  120. newelementmarkers[newix] = oldelements[oldix]
  121. oldelement.attrib['replacedby'] = newelements[newix].attrib["name"]
  122. break
  123. for oldix in range(0, len(oldelements)):
  124. if oldelementmarkers[oldix] != None:
  125. continue
  126. oldelement = oldelements[oldix]
  127. oldname = oldelement.attrib.get("name").lower()
  128. for newix in range(0, len(newelements)):
  129. if newelementmarkers[newix] != None:
  130. continue
  131. newelement = newelements[newix]
  132. newname = newelement.attrib.get("name").lower()
  133. if (oldname == "rst" and newname == "reset") or ("d0" in oldname and "d0" in newname) or ("d1" in oldname and "d1" in newname):
  134. oldelementmarkers[oldix] = newelements[newix]
  135. newelementmarkers[newix] = oldelements[oldix]
  136. oldelement.attrib['replacedby'] = newelements[newix].attrib["name"]
  137. break
  138. formatstring = ",{oldid},{oldname},{action},{newid},{newname}\n"
  139. if append:
  140. csvfile.write("\n")
  141. csvfile.write("{0} => {1},,,,,\n".format(os.path.basename(oldfn), os.path.basename(newfn)))
  142. for oldix in range(0, len(oldelements)):
  143. oldelement = oldelements[oldix]
  144. oldmarker = oldelementmarkers[oldix]
  145. oldid = oldelement.attrib.get("id")
  146. oldname = oldelement.attrib.get("name")
  147. if oldmarker == None:
  148. csvfile.write(formatstring.format(oldid=oldid, oldname=oldname, action="not found", newid="", newname=""))
  149. continue
  150. newid = oldmarker.attrib.get("id")
  151. newname = oldmarker.attrib.get("name")
  152. if oldname.lower() == newname.lower():
  153. csvfile.write(formatstring.format(oldid=oldid, oldname=oldname, action="same", newid=newid, newname=newname))
  154. continue
  155. csvfile.write(formatstring.format(oldid=oldid, oldname=oldname, action="replacedby", newid=newid, newname=newname))
  156. for newix in range(0, len(newelements)):
  157. newelement = newelements[newix]
  158. newmarker = newelementmarkers[newix]
  159. newid = newelement.attrib.get("id")
  160. newname = newelement.attrib.get("name")
  161. if newmarker == None:
  162. csvfile.write(formatstring.format(oldid="", oldname="", action="not found", newid=newid, newname=newname))
  163. csvfile.close()
  164. oldroot.write(oldfn)
  165. if __name__ == "__main__":
  166. main()