forked from Tencent/mars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrename_project.py
153 lines (107 loc) · 3.38 KB
/
rename_project.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
import os
import re
DELIMITER = "_"
PROJECT_FILE_NAME = ".project"
CPROJECT_FILE_NAME = ".cproject"
NAME_TAG_XPATH = "./name"
PROJECT_DEPEND_TAG_XPATH = "./projects/project"
CPROJECT_DEPEND_TAG_XPATH = "./storageModule/cconfiguration/storageModule/externalSettings"
CONTAINER_KEY = "containerId"
COLON = ";"
XML_DECLARATION_PATTERN = re.compile(r"^<\s*\?[\s\S]*\?\s*>")
MAIN_MENU_TEXT = "Input suffix or Press Enter to del suffix:"
SECOND_MENU_TEXT = "\nPlease input suffix:"
def listProjectFile():
folders = os.listdir(os.curdir)
fileList = []
for f in folders:
if os.path.isdir(f) and os.path.isfile(os.path.join(f, PROJECT_FILE_NAME)):
fileList.append(os.path.join(f, PROJECT_FILE_NAME))
return fileList
def listCProjectFile():
folders = os.listdir(os.curdir)
fileList = []
for f in folders:
if os.path.isdir(f) and os.path.isfile(os.path.join(f, CPROJECT_FILE_NAME)):
fileList.append(os.path.join(f, CPROJECT_FILE_NAME))
return fileList
def nameAddSuffix(_suffix):
fileList = listProjectFile()
for pf in fileList:
tree = ET.ElementTree(file = pf)
for elem in tree.iterfind(NAME_TAG_XPATH):
elem.text = elem.text + DELIMITER + _suffix
for elem in tree.iterfind(PROJECT_DEPEND_TAG_XPATH):
elem.text = elem.text + DELIMITER + _suffix
tree.write(pf);
fileList = listCProjectFile()
for pf in fileList:
tree = ET.ElementTree(file = pf)
for elem in tree.iterfind(CPROJECT_DEPEND_TAG_XPATH):
value = elem.get(CONTAINER_KEY, None)
if value == None:
continue
elem.set(CONTAINER_KEY, value[:value.find(COLON)] + DELIMITER + _suffix + COLON)
f = open(pf, "rb")
match = XML_DECLARATION_PATTERN.match(f.read())
f.close();
f = open(pf, "wb")
xml = ET.tostring(tree.getroot(), encoding="utf-8")
if match:
xml = match.group() + xml
f.write(xml)
f.close()
def nameDelSuffix():
fileList = listProjectFile()
for pf in fileList:
tree = ET.ElementTree(file = pf)
for elem in tree.iterfind(NAME_TAG_XPATH):
index = elem.text.find(DELIMITER)
if index < 0:
continue
elem.text = elem.text[:elem.text.find(DELIMITER)]
for elem in tree.iterfind(PROJECT_DEPEND_TAG_XPATH):
index = elem.text.find(DELIMITER)
if index < 0:
continue
elem.text = elem.text[:elem.text.find(DELIMITER)]
tree.write(pf, xml_declaration=True);
fileList = listCProjectFile()
for pf in fileList:
tree = ET.ElementTree(file = pf)
for elem in tree.iterfind(CPROJECT_DEPEND_TAG_XPATH):
value = elem.get(CONTAINER_KEY, None)
if value == None:
continue
elem.set(CONTAINER_KEY, value[:value.find(DELIMITER)] + COLON)
f = open(pf, "rb")
match = XML_DECLARATION_PATTERN.match(f.read())
f.close();
f = open(pf, "wb")
xml = ET.tostring(tree.getroot(), encoding="utf-8")
if match:
xml = match.group() + xml
f.write(xml)
f.close()
def displayName():
print "\nProject Name:"
fileList = listProjectFile()
for pf in fileList:
tree = ET.ElementTree(file = pf)
for elem in tree.iterfind(NAME_TAG_XPATH):
print elem.text
def main():
userInput = raw_input(MAIN_MENU_TEXT).strip()
nameDelSuffix()
if len(userInput) > 0:
nameAddSuffix(userInput)
displayName()
print("\n\nexecute end!")
raw_input("Press Enter to exit")
if __name__ == "__main__":
main()