Advertisement
Guest User

Untitled

a guest
Jun 11th, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. from xml.etree import ElementTree as ET
  2. from xml.dom import minidom
  3. import re, os
  4.  
  5. projectFile = "../.project"
  6. classpathFile = "../.classpath"
  7.  
  8. linksToAdd = [
  9. ["DWCMResources", "2", "PROJECT_LOC/dwcm/resources"],
  10. ["DWCMSrc", "2", "PROJECT_LOC/dwcm/src"],
  11.  
  12. ["Archangel", "2", "PROJECT_LOC/dwcm/lib/archangel/Java"],
  13. ["ArchangelResources", "2", "PROJECT_LOC/dwcm/lib/archangel/resources"],
  14.  
  15. ["ModDivisionCore", "2", "PROJECT_LOC/dwcm/lib/modDivisionCore/Java"],
  16. ["ModDivisionResources", "2", "PROJECT_LOC/dwcm/lib/modDivisionCore/resources"],
  17.  
  18. ### ["ColoredLightApi", "2", "PROJECT_LOC/dwcm/lib/ColoredLightAPI/src"],
  19.  
  20. ["Vanilla", "2", "PROJECT_LOC/dwcm/decompiledVanilla"]
  21. ]
  22.  
  23. classpathToAdd = [
  24. "src/main/java",
  25. "DWCMSrc", "DWCMResources",
  26. ### "DalekModSrc", "DalekModResources",
  27. "Archangel", "ArchangelResources",
  28. "ModDivisionCore", "ModDivisionResources",
  29. "Vanilla", "src/main/resources"
  30. ]
  31.  
  32. ### Because fuck you ElementTree
  33. def outputPretty(fileName, xml):
  34. text = minidom.parseString(ET.tostring(xml)).toprettyxml()
  35. # filtered = filter(lambda x: not re.match(r'^\s*$', x), s)
  36. filtered = os.linesep.join([s for s in text.splitlines() if s.strip()])
  37.  
  38. f = open(fileName, 'w')
  39. f.write(filtered)
  40. f.close()
  41.  
  42.  
  43. ### PROJECT
  44. print "Parsing",projectFile
  45. doc = ET.parse(projectFile)
  46. root = doc.getroot()
  47. linkedResources = root.find('linkedResources')
  48.  
  49. linksPresent = []
  50.  
  51. for link in linkedResources.findall('link'):
  52. linkName = link.find("name").text
  53. print "Found linked resource:",linkName
  54. linksPresent.append(linkName)
  55.  
  56. for neededLink in linksToAdd:
  57. linkName = neededLink[0]
  58. if linkName not in linksPresent:
  59. newLink = ET.Element("link")
  60. name = ET.SubElement(newLink, "name")
  61. name.text = linkName
  62. type = ET.SubElement(newLink, "type")
  63. type.text = neededLink[1]
  64. locationURI = ET.SubElement(newLink, "locationURI")
  65. locationURI.text = neededLink[2]
  66.  
  67. print "Inserting link:",linkName
  68. linkedResources.append(newLink)
  69. else:
  70. print "Skipping link insertion:",linkName
  71.  
  72. print " Writing",projectFile
  73.  
  74. outputPretty(projectFile, root)
  75.  
  76. ### CLASSPATH
  77. print "Parsing",classpathFile
  78. doc = ET.parse(classpathFile)
  79. root = doc.getroot()
  80.  
  81. newRoot = ET.Element("classpath")
  82. for newEntry in classpathToAdd:
  83. print " Adding classpath entry:",newEntry
  84. newNode = ET.Element("classpathentry")
  85. newNode.set("kind", "src")
  86. newNode.set("path", newEntry)
  87. newRoot.append(newNode)
  88.  
  89. entries = root.findall('classpathentry')
  90. for existing in entries:
  91. kind = existing.get("kind")
  92. path = existing.get("path")
  93. if kind == "src" and path in classpathToAdd:
  94. pass # Skip the ones we are already adding
  95. else:
  96. newRoot.append(existing)
  97.  
  98. print " Writing",classpathFile
  99.  
  100. outputPretty(classpathFile, newRoot)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement