pretty-vcproj-manx.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/python2.6
  2. # Copyright (c) 2009 Google Inc. All rights reserved.
  3. # Copyright (C) 2012 Sony Computer Entertainment Inc. All rights reserved.
  4. # Use of this source code is governed by a BSD-style license that can be
  5. # found in the LICENSE file.
  6. """Make the format of a vcproj really pretty.
  7. It outputs the resulting xml to stdout.
  8. """
  9. import os
  10. import sys
  11. from xml.dom.minidom import parse
  12. from xml.dom.minidom import Node
  13. class CmpTuple:
  14. """Compare function between 2 tuple."""
  15. def __call__(self, x, y):
  16. (key1, value1) = x
  17. (key2, value2) = y
  18. return cmp(key1, key2)
  19. class CmpNode:
  20. """Compare function between 2 xml nodes."""
  21. def get_string(self, node):
  22. node_string = "node"
  23. node_string += node.nodeName
  24. if node.nodeValue:
  25. node_string += node.nodeValue
  26. if node.attributes:
  27. # We first sort by name, if present.
  28. node_string += node.getAttribute("Name")
  29. all_nodes = []
  30. for (name, value) in node.attributes.items():
  31. all_nodes.append((name, value))
  32. all_nodes.sort(CmpTuple())
  33. for (name, value) in all_nodes:
  34. node_string += name
  35. node_string += value
  36. return node_string
  37. def __call__(self, x, y):
  38. return cmp(self.get_string(x), self.get_string(y))
  39. def _write_data(writer, data):
  40. "Writes datachars to writer."
  41. data = data.replace("&", "&amp;").replace("<", "&lt;")
  42. data = data.replace("\"", "&quot;").replace(">", "&gt;")
  43. writer.write(data)
  44. def writexml(node, writer, indent="", addindent="", newl=""):
  45. # indent = current indentation
  46. # addindent = indentation to add to higher levels
  47. # newl = newline string
  48. writer.write(indent+"<" + node.tagName)
  49. attrs = node._get_attributes()
  50. a_names = attrs.keys()
  51. for a_name in a_names:
  52. writer.write("\n%s%s" % (indent,addindent))
  53. writer.write("%s=\"" % a_name)
  54. _write_data(writer, attrs[a_name].value)
  55. writer.write("\"")
  56. if node.childNodes:
  57. writer.write("\n%s%s" % (indent,addindent))
  58. writer.write(">%s"%(newl))
  59. for c in node.childNodes:
  60. writexml(c,writer,indent+addindent,addindent,newl)
  61. writer.write("%s</%s>%s" % (indent,node.tagName,newl))
  62. else:
  63. writer.write("\n%s" % (indent))
  64. writer.write("/>%s"%(newl))
  65. def CleanupVcproj(node):
  66. # For each sub node, we call recursively this function.
  67. for sub_node in node.childNodes:
  68. CleanupVcproj(sub_node)
  69. # Normalize the node, and remove all extranous whitespaces.
  70. for sub_node in node.childNodes:
  71. if sub_node.nodeType == Node.TEXT_NODE:
  72. sub_node.data = sub_node.data.replace("\r", "")
  73. sub_node.data = sub_node.data.replace("\n", "")
  74. sub_node.data = sub_node.data.rstrip()
  75. if node.childNodes:
  76. node.normalize()
  77. # For each node, take a copy, and remove it from the list.
  78. node_array = []
  79. filters = []
  80. files = []
  81. while node.childNodes and node.childNodes[0]:
  82. # Take a copy of the node and remove it from the list.
  83. current = node.childNodes[0]
  84. if current.nodeName == 'Filter':
  85. filters.append(current)
  86. elif current.nodeName == 'File':
  87. files.append(current)
  88. else:
  89. node_array.append(current)
  90. node.removeChild(current)
  91. # Sort the list.
  92. filters.sort(CmpNode())
  93. files.sort(CmpNode())
  94. for new_node in node_array + filters + files:
  95. node.appendChild(new_node)
  96. def main(argv):
  97. """Main function of this vcproj prettifier."""
  98. # check if we have exactly 1 parameter.
  99. if len(argv) < 1:
  100. print ('Usage: %s <vcproj.vcproj>' % argv[0])
  101. return
  102. # Open the vcproj and parse the xml.
  103. dom = parse(argv[1])
  104. CleanupVcproj(dom.documentElement)
  105. sys.stdout.write('<?xml version="1.0" encoding="Windows-1252"?>\n')
  106. writexml(dom.documentElement, sys.stdout, "", "\t", "\n")
  107. if __name__ == '__main__':
  108. main(sys.argv)