forked from zhangchuhu/serviceframe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_project2008.py
executable file
·132 lines (108 loc) · 3.93 KB
/
create_project2008.py
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
#!/usr/bin/env python
# -*- coding: gb2312 -*-
import os
import codecs
import shutil
from xml.dom import minidom
# 工程文件相对于源代码目录的路径
out_file = "./serviceframe_2008/serviceframe.vcproj"
# 源代码目录
work_dir = "./"
# 忽略的目录
igone_files = [".git", "doc", "serviceframe_2008"]
# 忽略的文件
igone = [
"tags", "noblocking_thrift_client.h", "NoBlockThriftCilent.h",
]
"""
创建VC工程文件
"""
class VSProj:
def __init__(self):
self.doc = minidom.Document()
self.root_node = self.doc.createElement("Files")
self.doc.appendChild(self.root_node)
def addDir(self, path, father_node=None):
filter_node = self.doc.createElement("Filter")
filter_node.setAttribute("Name", os.path.basename(path).decode('gb2312'))
#filter_node.setAttribute("Filter", "")
file_list = []
for f in os.listdir(path):
if f == ".svn":
continue
rel_path = os.path.join(path, f)
if os.path.isdir(rel_path) == True:
#self.addDir(rel_path, filter_node)
file_list.append(rel_path)
else:
self.addFile(rel_path, filter_node)
# 添加本目录的文件夹
file_list.sort()
for f in file_list:
self.addDir(f, filter_node)
if father_node == None:
self.root_node.appendChild(filter_node)
else:
father_node.appendChild(filter_node)
def addFile(self, path, father_node=None):
global out_file
if os.path.split(path)[-1] in igone:
return
k_rel_path = os.path.split(out_file)[0] # 工程文件所在目录
file_node = self.doc.createElement("File")
rel_path = os.path.relpath(path, k_rel_path) # 相对vip_/vip(工程文件所在目录)的路径 ../../vip_score_v2/xx
file_node.setAttribute("RelativePath", rel_path.decode('gb2312'))
if father_node == None:
self.root_node.appendChild(file_node)
else:
father_node.appendChild(file_node)
def genXml(self, out_file):
bak_file = out_file + ".bak"
shutil.copy(out_file, bak_file) # bak file
fd = open(out_file, "w")
fd2 = open(bak_file, "r")
flag = False #
while True:
line = fd2.readline()
if line == "":
break
elif line.find('<Files>') != -1:
flag = True
str = self.doc.toprettyxml(indent="\t", newl="\n", encoding="gb2312")
#print "fild start: %s" % str
pos = str.find("\n")
fd.write(str[pos+1:]) # 去掉xml头
elif line.find('</Files>') != -1:
#print "fild end: "
flag = False
elif flag:
continue
else:
fd.write(line)
fd.close()
fd2.close()
"""
f = file(file_name, "w")
writer = codecs.lookup('gb2312')[3](f)
self.doc.writexml(writer, encoding='gb2312') # 不排xml格式
writer.close()
"""
if __name__ == "__main__":
os.chdir(work_dir)
# begin gen xml
xl = VSProj()
file_list = []
for i in sorted(os.listdir("./")):
if igone_files.count(i) != 0:
continue
rel_path = os.path.join("./", i)
#print rel_path
if os.path.isdir(rel_path) == False:
xl.addFile(rel_path)
else:
file_list.append(rel_path)
# 先加文件夹,在加文件夹
file_list.sort()
for i in file_list:
xl.addDir(i)
xl.genXml(out_file)