Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

cordova_plist_to_config_xml 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/python
  2. #
  3. # Licensed to the Apache Software Foundation (ASF) under one
  4. # or more contributor license agreements. See the NOTICE file
  5. # distributed with this work for additional information
  6. # regarding copyright ownership. The ASF licenses this file
  7. # to you under the Apache License, Version 2.0 (the
  8. # "License"); you may not use this file except in compliance
  9. # with the License. You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing,
  14. # software distributed under the License is distributed on an
  15. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. # KIND, either express or implied. See the License for the
  17. # specific language governing permissions and limitations
  18. # under the License.
  19. #
  20. """
  21. Converts a project's Cordova.plist file into a config.xml one. This conversion is required for Cordova 2.3.
  22. Usage:
  23. plist2xml.py path/to/project
  24. """
  25. from __future__ import print_function
  26. import fileinput
  27. import io
  28. import os
  29. import plistlib
  30. import re
  31. import sys
  32. from xml.dom import minidom
  33. from xml.etree import ElementTree
  34. def Usage():
  35. sys.stderr.write(__doc__)
  36. sys.exit(1)
  37. def ValueToElement(node_name, key, value):
  38. if isinstance(value, bool):
  39. value = str(value).lower()
  40. return ElementTree.Element(node_name, attrib={'name':key, 'value':str(value)})
  41. def AppendDict(d, node, name, ignore=()):
  42. for key in sorted(d):
  43. if key not in ignore:
  44. node.append(ValueToElement(name, key, d[key]))
  45. def FindProjectFile(path):
  46. # Do an extra abspath here to strip off trailing / if present.
  47. path = os.path.abspath(path)
  48. if path.endswith('.pbxproj'):
  49. return path
  50. elif path.endswith('.xcodeproj'):
  51. return os.path.join(path, 'project.pbxproj')
  52. for f in os.listdir(path):
  53. if f.endswith('.xcodeproj'):
  54. return os.path.join(path, f, 'project.pbxproj')
  55. raise Exception('Invalid project path. Please provide the path to your .xcodeproj directory')
  56. def FindPlistFile(search_path):
  57. for root, unused_dirnames, file_names in os.walk(search_path):
  58. for file_name in file_names:
  59. if file_name == 'Cordova.plist':
  60. return os.path.join(root, file_name)
  61. raise Exception('Could not find a file named "Cordova.plist" within ' + search_path)
  62. def ConvertPlist(src_path, dst_path):
  63. # Run it through plutil to ensure it's not a binary plist.
  64. os.system("plutil -convert xml1 '%s'" % src_path)
  65. plist = plistlib.readPlist(src_path)
  66. root = ElementTree.Element('cordova')
  67. AppendDict(plist, root, 'preference', ignore=('Plugins', 'ExternalHosts'))
  68. plugins = ElementTree.Element('plugins')
  69. root.append(plugins)
  70. AppendDict(plist['Plugins'], plugins, 'plugin')
  71. for value in sorted(plist['ExternalHosts']):
  72. root.append(ElementTree.Element('access', attrib={'origin':value}))
  73. tree = ElementTree.ElementTree(root)
  74. with io.BytesIO() as s:
  75. tree.write(s, encoding='UTF-8')
  76. mini_dom = minidom.parseString(s.getvalue())
  77. with open(dst_path, 'wb') as out:
  78. out.write(mini_dom.toprettyxml(encoding='UTF-8'))
  79. def UpdateProjectFile(path):
  80. file_handle = fileinput.input(path, inplace=1)
  81. for line in file_handle:
  82. if 'Cordova.plist' in line:
  83. line = line.replace('Cordova.plist', 'config.xml')
  84. line = line.replace('lastKnownFileType = text.plist.xml', 'lastKnownFileType = text.xml')
  85. print(line, end=' ')
  86. file_handle.close()
  87. def main(argv):
  88. if len(argv) != 1:
  89. Usage();
  90. project_file = FindProjectFile(argv[0])
  91. plist_file = FindPlistFile(os.path.dirname(os.path.dirname(project_file)))
  92. config_file = os.path.join(os.path.dirname(plist_file), 'config.xml')
  93. sys.stdout.write('Converting %s to %s.\n' % (plist_file, config_file))
  94. ConvertPlist(plist_file, config_file)
  95. sys.stdout.write('Updating references in %s\n' % project_file)
  96. UpdateProjectFile(project_file)
  97. os.unlink(plist_file)
  98. sys.stdout.write('Updates Complete.\n')
  99. if __name__ == '__main__':
  100. main(sys.argv[1:])