| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 | 2 |
| 3 # Copyright (c) 2012 Google Inc. All rights reserved. | 3 # Copyright (c) 2012 Google Inc. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 """Make the format of a vcproj really pretty. | 7 """Make the format of a vcproj really pretty. |
| 8 | 8 |
| 9 This script normalize and sort an xml. It also fetches all the properties | 9 This script normalize and sort an xml. It also fetches all the properties |
| 10 inside linked vsprops and include them explicitly in the vcproj. | 10 inside linked vsprops and include them explicitly in the vcproj. |
| 11 | 11 |
| 12 It outputs the resulting xml to stdout. | 12 It outputs the resulting xml to stdout. |
| 13 """ | 13 """ |
| 14 | 14 |
| 15 from __future__ import print_function |
| 16 |
| 15 __author__ = 'nsylvain (Nicolas Sylvain)' | 17 __author__ = 'nsylvain (Nicolas Sylvain)' |
| 16 | 18 |
| 17 import os | 19 import os |
| 18 import sys | 20 import sys |
| 19 | 21 |
| 20 from xml.dom.minidom import parse | 22 from xml.dom.minidom import parse |
| 21 from xml.dom.minidom import Node | 23 from xml.dom.minidom import Node |
| 22 | 24 |
| 23 REPLACEMENTS = dict() | 25 REPLACEMENTS = dict() |
| 24 ARGUMENTS = None | 26 ARGUMENTS = None |
| 25 | 27 |
| 26 | 28 |
| 27 class CmpTuple(object): | 29 def GetNodeString(x): |
| 28 """Compare function between 2 tuple.""" | 30 node_string = "node" |
| 29 def __call__(self, x, y): | 31 node_string += node.nodeName |
| 30 return cmp(x[0], y[0]) | 32 if node.nodeValue: |
| 33 node_string += node.nodeValue |
| 31 | 34 |
| 35 if node.attributes: |
| 36 # We first sort by name, if present. |
| 37 node_string += node.getAttribute("Name") |
| 32 | 38 |
| 33 class CmpNode(object): | 39 all_nodes = [] |
| 34 """Compare function between 2 xml nodes.""" | 40 for (name, value) in node.attributes.items(): |
| 41 all_nodes.append((name, value)) |
| 35 | 42 |
| 36 def __call__(self, x, y): | 43 all_nodes.sort(key=(lambda node: node[0])) |
| 37 def get_string(node): | 44 for (name, value) in all_nodes: |
| 38 node_string = "node" | 45 node_string += name |
| 39 node_string += node.nodeName | 46 node_string += value |
| 40 if node.nodeValue: | |
| 41 node_string += node.nodeValue | |
| 42 | 47 |
| 43 if node.attributes: | 48 return node_string |
| 44 # We first sort by name, if present. | |
| 45 node_string += node.getAttribute("Name") | |
| 46 | |
| 47 all_nodes = [] | |
| 48 for (name, value) in node.attributes.items(): | |
| 49 all_nodes.append((name, value)) | |
| 50 | |
| 51 all_nodes.sort(CmpTuple()) | |
| 52 for (name, value) in all_nodes: | |
| 53 node_string += name | |
| 54 node_string += value | |
| 55 | |
| 56 return node_string | |
| 57 | |
| 58 return cmp(get_string(x), get_string(y)) | |
| 59 | |
| 60 | 49 |
| 61 def PrettyPrintNode(node, indent=0): | 50 def PrettyPrintNode(node, indent=0): |
| 62 if node.nodeType == Node.TEXT_NODE: | 51 if node.nodeType == Node.TEXT_NODE: |
| 63 if node.data.strip(): | 52 if node.data.strip(): |
| 64 print '%s%s' % (' '*indent, node.data.strip()) | 53 print('%s%s' % (' '*indent, node.data.strip())) |
| 65 return | 54 return |
| 66 | 55 |
| 67 if node.childNodes: | 56 if node.childNodes: |
| 68 node.normalize() | 57 node.normalize() |
| 69 # Get the number of attributes | 58 # Get the number of attributes |
| 70 attr_count = 0 | 59 attr_count = 0 |
| 71 if node.attributes: | 60 if node.attributes: |
| 72 attr_count = node.attributes.length | 61 attr_count = node.attributes.length |
| 73 | 62 |
| 74 # Print the main tag | 63 # Print the main tag |
| 75 if attr_count == 0: | 64 if attr_count == 0: |
| 76 print '%s<%s>' % (' '*indent, node.nodeName) | 65 print('%s<%s>' % (' '*indent, node.nodeName)) |
| 77 else: | 66 else: |
| 78 print '%s<%s' % (' '*indent, node.nodeName) | 67 print('%s<%s' % (' '*indent, node.nodeName)) |
| 79 | 68 |
| 80 all_attributes = [] | 69 all_attributes = [] |
| 81 for (name, value) in node.attributes.items(): | 70 for (name, value) in node.attributes.items(): |
| 82 all_attributes.append((name, value)) | 71 all_attributes.append((name, value)) |
| 83 all_attributes.sort(CmpTuple()) | 72 all_attributes.sort(key=(lambda attr: attr[0])) |
| 84 for (name, value) in all_attributes: | 73 for (name, value) in all_attributes: |
| 85 print '%s %s="%s"' % (' '*indent, name, value) | 74 print('%s %s="%s"' % (' '*indent, name, value)) |
| 86 print '%s>' % (' '*indent) | 75 print('%s>' % (' '*indent)) |
| 87 if node.nodeValue: | 76 if node.nodeValue: |
| 88 print '%s %s' % (' '*indent, node.nodeValue) | 77 print('%s %s' % (' '*indent, node.nodeValue)) |
| 89 | 78 |
| 90 for sub_node in node.childNodes: | 79 for sub_node in node.childNodes: |
| 91 PrettyPrintNode(sub_node, indent=indent+2) | 80 PrettyPrintNode(sub_node, indent=indent+2) |
| 92 print '%s</%s>' % (' '*indent, node.nodeName) | 81 print('%s</%s>' % (' '*indent, node.nodeName)) |
| 93 | 82 |
| 94 | 83 |
| 95 def FlattenFilter(node): | 84 def FlattenFilter(node): |
| 96 """Returns a list of all the node and sub nodes.""" | 85 """Returns a list of all the node and sub nodes.""" |
| 97 node_list = [] | 86 node_list = [] |
| 98 | 87 |
| 99 if (node.attributes and | 88 if (node.attributes and |
| 100 node.getAttribute('Name') == '_excluded_files'): | 89 node.getAttribute('Name') == '_excluded_files'): |
| 101 # We don't add the "_excluded_files" filter. | 90 # We don't add the "_excluded_files" filter. |
| 102 return [] | 91 return [] |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 | 168 |
| 180 # If the child is a filter, we want to append all its children | 169 # If the child is a filter, we want to append all its children |
| 181 # to this same list. | 170 # to this same list. |
| 182 if current.nodeName == 'Filter': | 171 if current.nodeName == 'Filter': |
| 183 node_array.extend(FlattenFilter(current)) | 172 node_array.extend(FlattenFilter(current)) |
| 184 else: | 173 else: |
| 185 node_array.append(current) | 174 node_array.append(current) |
| 186 | 175 |
| 187 | 176 |
| 188 # Sort the list. | 177 # Sort the list. |
| 189 node_array.sort(CmpNode()) | 178 node_array.sort(key=GetNodeString) |
| 190 | 179 |
| 191 # Insert the nodes in the correct order. | 180 # Insert the nodes in the correct order. |
| 192 for new_node in node_array: | 181 for new_node in node_array: |
| 193 # But don't append empty tool node. | 182 # But don't append empty tool node. |
| 194 if new_node.nodeName == 'Tool': | 183 if new_node.nodeName == 'Tool': |
| 195 if new_node.attributes and new_node.attributes.length == 1: | 184 if new_node.attributes and new_node.attributes.length == 1: |
| 196 # This one was empty. | 185 # This one was empty. |
| 197 continue | 186 continue |
| 198 if new_node.nodeName == 'UserMacro': | 187 if new_node.nodeName == 'UserMacro': |
| 199 continue | 188 continue |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 | 309 |
| 321 # Finally, we use the prett xml function to print the vcproj back to the | 310 # Finally, we use the prett xml function to print the vcproj back to the |
| 322 # user. | 311 # user. |
| 323 #print dom.toprettyxml(newl="\n") | 312 #print dom.toprettyxml(newl="\n") |
| 324 PrettyPrintNode(dom.documentElement) | 313 PrettyPrintNode(dom.documentElement) |
| 325 return 0 | 314 return 0 |
| 326 | 315 |
| 327 | 316 |
| 328 if __name__ == '__main__': | 317 if __name__ == '__main__': |
| 329 sys.exit(main(sys.argv)) | 318 sys.exit(main(sys.argv)) |
| OLD | NEW |