File: ConvertVcxprojToList.py

package info (click to toggle)
mygui 3.2.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 36,224 kB
  • sloc: cpp: 118,031; ansic: 30,202; xml: 15,544; cs: 12,602; tcl: 776; python: 417; makefile: 34
file content (159 lines) | stat: -rw-r--r-- 5,080 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python
# python 2.6+ required (for os.path.relpath)

# Generate *.list files with CMake SOURCE_GROUP from *.vcproj files
# goes through recursively from ../ directory


import xml.dom.minidom, os, filecmp, sys, filecmp

headers = []
sources = []
filters = {}
currentFolder = ""
currentRoot = ""

def addSourceOrHeader(fileName):
    #print line
    if fileName.endswith('.h'):
        headers.append("  " + fileName + "\n")
    else:
        sources.append("  " + fileName + "\n")

def get_a_document(name):
    return xml.dom.minidom.parse(name)

def convertRelativePath(fileName):
    fileName = os.path.join(currentRoot, fileName)
    fileName = fileName.replace('\\','/')
    fileName = os.path.relpath(fileName, currentFolder)
    fileName = fileName.replace('\\','/')
    return fileName

def parseIncludedFile(_node):
    fileName = str(_node.attributes["Include"].nodeValue)
    fileName = convertRelativePath(fileName)
    addSourceOrHeader(fileName)

def parseFilterFile(_node):
    fileName = str(_node.attributes["Include"].nodeValue)
    fileName = convertRelativePath(fileName)

    if (len(_node.getElementsByTagName("Filter")) != 0):
      filterName = _node.getElementsByTagName("Filter")[0].childNodes[0].nodeValue
      filterName = filterName.replace("\\", "\\\\")

      if not filters.has_key(filterName):
        filters[filterName] = []
      filters[filterName].append("  " + fileName + "\n")

def writeHeaders(FILE):
    headers.sort()
    FILE.writelines("set (HEADER_FILES\n")
    FILE.writelines(headers)
    FILE.writelines(")\n")
def writeSources(FILE):
    sources.sort()
    FILE.writelines("set (SOURCE_FILES\n")
    FILE.writelines(sources)
    FILE.writelines(")\n")
def writeFilters(FILE):
    for filterName in sorted(filters.keys()):
      filters[filterName].sort()
      FILE.writelines("SOURCE_GROUP(\"" + filterName + "\" FILES\n")
      FILE.writelines(filters[filterName])
      FILE.writelines(")\n")

def createFilesList(fileName, listName):
    if (not os.path.exists(listName)):
        return

    print "Converting " + fileName
    doc = get_a_document(fileName)

    for rootNode in doc.childNodes:
        for subNode in rootNode.childNodes:
            if subNode.localName == "ItemGroup":
                for subSubNode in subNode.childNodes:
                    if subSubNode.nodeType == subSubNode.ELEMENT_NODE and (subSubNode.localName == "ClInclude" or subSubNode.localName == "ClCompile" or subSubNode.localName == "ResourceCompile"):
                        parseIncludedFile(subSubNode)

    doc = get_a_document(fileName + ".filters")
    for rootNode in doc.childNodes:
        for subNode in rootNode.childNodes:
            if subNode.localName == "ItemGroup":
                for subSubNode in subNode.childNodes:
                    if subSubNode.nodeType == subSubNode.ELEMENT_NODE and (subSubNode.localName == "ClInclude" or subSubNode.localName == "ClCompile" or subSubNode.localName == "ResourceCompile" or subSubNode.localName == "CustomBuild"):
                        parseFilterFile(subSubNode)

    FILE = open("tmp.list", "w")
    writeHeaders(FILE)
    writeSources(FILE)
    writeFilters(FILE)
    FILE.close()

    del headers[:]
    del sources[:]
    filters.clear()

    if (not filecmp.cmp("tmp.list", listName)):
      if (os.path.exists(listName)):
        os.remove(listName)
      os.rename("tmp.list", listName)
    else:
      os.remove("tmp.list")
def isIgnoredProject(name):
    ignores = ["Common", "api-docs", "INSTALL", "ALL_BUILD", "ZERO_CHECK", "PACKAGE"]
    for ignore in ignores:
        if name.startswith(ignore):
            return True
    return False

def main():
  global currentRoot
  global currentFolder
  dir_src = '../'
  try:
    dir_solution = sys.argv[1]
    print "Sources directory is: " + dir_src
    print "Solution directory is: " + dir_solution
  except:
    print "Error: missing argument"
    print "Usage: parseXML <path_to_solution>"
  else:
    for root, dirs, files in os.walk(dir_solution):
      for name in files:
        if name.endswith('.vcxproj') and not isIgnoredProject(name):
            currentRoot = root
            f_src = os.path.join(root, name)
            f_src = f_src.replace('\\','/')
            currentFolder = f_src
            currentFolder = currentFolder.replace(name, "")
            currentFolder = currentFolder.replace('\\','/')

            currentFolder = os.path.join(dir_src, os.path.relpath(currentFolder, dir_solution))

            listName = f_src.replace(".vcxproj", ".list")
            listName = os.path.relpath(listName, dir_solution)
            listName = os.path.join(dir_src, listName)
            #print listName
            createFilesList(f_src, listName)

    print "Done"

if (not os.path.exists("lock")):
  try:
    FILE = open("lock", "w")
    FILE.close()

    main()

    os.remove("lock")
  except:
    if (os.path.exists("lock")):
      os.remove("lock")
    if (os.path.exists("tmp.list")):
      os.remove("tmp.list")
    raise
else:
  print "Already converting... Exiting..."