modifyManifest.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import sys, string, os
  2. from xml.etree import ElementTree as ET
  3. manifestFile = sys.argv[1]
  4. pluginStr = sys.argv[2]
  5. pluginsDir = sys.argv[3]
  6. androidNS = 'http://schemas.android.com/apk/res/android'
  7. sourceCfgFile = '/android/ForManifest.xml'
  8. def doModify(sourceFile, root):
  9. bRet = False
  10. sourceTree = ET.parse(sourceFile)
  11. sourceRoot = sourceTree.getroot()
  12. # get target content
  13. f = open(manifestFile)
  14. targetContent = f.read()
  15. f.close()
  16. # check config for application
  17. appCfgNode = sourceRoot.find('applicationCfg')
  18. if appCfgNode is not None and len(appCfgNode) > 0:
  19. appKeyWord = appCfgNode.get('keyword')
  20. if appKeyWord != None and len(appKeyWord) > 0:
  21. keyIndex = targetContent.find(appKeyWord)
  22. if -1 == keyIndex:
  23. bRet = True
  24. for node in list(appCfgNode):
  25. root.find('application').append(node)
  26. # check permission config
  27. perCfgNode = sourceRoot.find('permissionCfg')
  28. if perCfgNode is not None and len(perCfgNode) > 0:
  29. for oneNode in list(perCfgNode):
  30. key = '{' + androidNS + '}name'
  31. perAttr = oneNode.get(key)
  32. if perAttr != None and len(perAttr) > 0:
  33. attrIndex = targetContent.find(perAttr)
  34. if -1 == attrIndex:
  35. bRet = True
  36. root.append(oneNode)
  37. return bRet
  38. # parse file AndroidManifest.xml of game project
  39. ET.register_namespace("android", androidNS)
  40. targetTree = ET.parse(manifestFile)
  41. targetRoot = targetTree.getroot()
  42. # traverse all plugins
  43. plugins = pluginStr.split(':')
  44. for pluginName in plugins:
  45. # find the file 'ForManifest.xml'
  46. sourceXml = pluginsDir + '/' + pluginName + sourceCfgFile
  47. if not os.path.exists(sourceXml):
  48. continue
  49. # check & modify target xml
  50. haveChanged = doModify(sourceXml, targetRoot)
  51. if haveChanged:
  52. print 'Modify AndroidManifest.xml for plugin ' + pluginName
  53. targetTree.write(manifestFile, 'UTF-8')