modifyClassPath.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import sys, string, os
  2. from xml.etree import ElementTree as ET
  3. from xml.dom import minidom
  4. classPathFile = sys.argv[1]
  5. pluginStr = sys.argv[2]
  6. pluginsDir = sys.argv[3]
  7. def getLibElement(pathAttr):
  8. ret = ET.Element('classpathentry')
  9. ret.set('exported', 'true')
  10. ret.set('kind', 'lib')
  11. ret.set('path', pathAttr)
  12. return ret
  13. def fomatTree(elem):
  14. root_str = ET.tostring(elem, 'UTF-8')
  15. reparse = minidom.parseString(root_str)
  16. return reparse
  17. tree = ET.parse(classPathFile)
  18. root = tree.getroot()
  19. entryNodes = root.findall('classpathentry')
  20. linkedLibs = []
  21. for node in entryNodes:
  22. entryKind = node.get('kind')
  23. entryPath = node.get('path')
  24. entryKind = entryKind.strip(' \n\r\t')
  25. entryPath = entryPath.strip(' \n\r\t')
  26. if entryKind == 'lib' and entryPath.endswith('.jar'):
  27. linkedLibs.append(entryPath)
  28. plugins = pluginStr.split(':')
  29. modified = False
  30. for pluginName in plugins:
  31. pluginAndroidDir = pluginsDir + '/' + pluginName + '/android'
  32. for fileName in os.listdir(pluginAndroidDir):
  33. if os.path.splitext(fileName)[1] == '.jar':
  34. needAdd = True
  35. for linkedJar in linkedLibs:
  36. jarName = os.path.basename(linkedJar)
  37. if fileName == jarName:
  38. needAdd = False
  39. break
  40. if needAdd:
  41. modified = True
  42. pathAttr = 'plugin-x/' + pluginName + '/android/' + fileName
  43. root.append(getLibElement(pathAttr))
  44. linkedLibs.append(pathAttr)
  45. if modified:
  46. f = open(classPathFile, 'w')
  47. fomatDom = fomatTree(root)
  48. fomatDom.writexml(f, '', '\t', '\n', 'UTF-8')
  49. f.close()