-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqklab.py
207 lines (176 loc) · 5.96 KB
/
qklab.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import os
import sys
import shutil
import logging
rootDir = os.getcwd()
logging.basicConfig(
level=logging.INFO,
format="[%(levelname)s] file: %(filename)s line: %(lineno)d\t%(message)s",
)
# Print help args
def printHelp():
print(
"Usage:\n"
+ "\t--help(-h) : list args\n"
+ "\t<prject name> : create new project\n"
+ "\t--add(-a) <prject name> : create new project\n"
+ "\t--delete(-d) <prject name> : remove existed project\n"
+ "\t--rename(-r) <old prject name> <new prject name>: rename existed project\n"
)
exit()
# Create project
def createProject(proj, cmakeFile, fileHead, fileCpp, mainCpp):
# Process exception
# 0. Create folder if not exist
if not os.path.exists(proj):
os.mkdir("src/" + proj)
# 1. Create .h file
if not os.path.exists(fileHead):
f = open(fileHead, "w")
f.write("#pragma once\n")
f.close()
else:
logging.warning(fileHead + " exist!")
includeHead = '#include "' + proj + '.h"\n'
# # Create .cpp file
# if not os.path.exists(fileCpp):
# f = open(fileCpp, 'w')
# f.write(includeHead)
# f.close()
# else:
# logging.warning(fileCpp + " exist!")
# 2. Create main.cpp file
mainContext = (
includeHead
+ "#include <iostream>\n"
+ "#include <string>\n\n"
+ "int main()\n"
+ "{\n"
+ "\treturn 0;\n"
+ "}\n"
)
if not os.path.exists(mainCpp):
f = open(mainCpp, "w")
f.write(mainContext)
f.close()
else:
logging.warning("main.cpp exist!")
# 3. Create cmake config
addProj = "addProject(" + proj + ")"
isFound = False
with open(cmakeFile) as ff:
if addProj in ff.read():
isFound = True
if not isFound:
f = open(cmakeFile, "a+")
f.write(addProj + "\n")
# Remove project
def removeProject(proj, cmakeFile):
# Process exception
# 0. Remove project directory
if os.path.exists(rootDir + "\\src\\" + proj):
shutil.rmtree(rootDir + "\\src\\" + proj)
else:
logging.warning("Project " + proj + " not exist\n")
pass
# 1. Remove project cmake config
addProj = "addProject(" + proj + ")"
f = open(cmakeFile, "r")
fileContext = ""
for token in f:
if addProj in token:
continue
else:
fileContext += token
f.close()
ff = open(cmakeFile, "w")
ff.write(fileContext)
ff.close()
def renameProject(oldProj, newProj, cmakeFile):
# TODO
# Process exception(last arg)
# 0. Check old project exist or not
if not os.path.exists(rootDir + "\\src\\" + oldProj):
logging.error("Project " + oldProj + " not exist. Check your input\n")
exit()
# 1. Rename project name
os.rename(rootDir + "\\src\\" + oldProj, rootDir + "\\src\\" + newProj)
# 2. Rename default .h file
if os.path.exists(rootDir + "\\src\\" + newProj + "\\" + oldProj + ".h"):
os.rename(
rootDir + "\\src\\" + newProj + "\\" + oldProj + ".h",
rootDir + "\\src\\" + newProj + "\\" + newProj + ".h",
)
else:
f = open(rootDir + "\\src\\" + newProj + "\\" + newProj + ".h", "w")
f.write("#pragma once\n")
f.close()
oldIncludeHead = '#include "' + oldProj + '.h"\n'
newIncludeHead = '#include "' + newProj + '.h"\n'
# 3. Replace default head file in .cpp file
for filePath, dirName, fileNames in os.walk(rootDir + "\\src\\" + newProj):
for fileName in fileNames:
if ".cpp" in fileName:
f = open(filePath + "\\" + fileName, "r")
fileContext = ""
for token in f:
if oldIncludeHead in token:
fileContext += newIncludeHead
else:
fileContext += token
f.close()
ff = open(filePath + "\\" + fileName, "w")
ff.write(fileContext)
ff.close()
# 4. Replace cmake config name
oldAddProj = "addProject(" + oldProj + ")"
newAddProj = "addProject(" + newProj + ")"
f = open(cmakeFile, "r")
fileContext = ""
for token in f:
if oldAddProj in token:
fileContext += newAddProj
else:
fileContext += token
f.close()
ff = open(cmakeFile, "w")
ff.write(fileContext)
ff.close()
if len(sys.argv) == 2:
# Process help
if sys.argv[1] == "--help" or sys.argv[1] == "-h":
printHelp()
# Create project
proj = sys.argv[1]
cmakeFile = rootDir + "\\src\\CMakeLists.txt"
fileHead = rootDir + "\\src\\" + proj + "\\" + proj + ".h"
fileCpp = rootDir + "\\src\\" + proj + "\\" + proj + ".cpp"
mainCpp = rootDir + "\\src\\" + proj + "\\main.cpp"
createProject(proj, cmakeFile, fileHead, fileCpp, mainCpp)
elif len(sys.argv) == 3:
proj = sys.argv[2]
cmakeFile = rootDir + "\\src\\CMakeLists.txt"
fileHead = rootDir + "\\src\\" + proj + "\\" + proj + ".h"
fileCpp = rootDir + "\\src\\" + proj + "\\" + proj + ".cpp"
mainCpp = rootDir + "\\src\\" + proj + "\\main.cpp"
# Create project
if sys.argv[1] == "--add" or sys.argv[1] == "-a":
createProject(proj, cmakeFile, fileHead, fileCpp, mainCpp)
# Remove project
elif sys.argv[1] == "--delete" or sys.argv[1] == "-d":
removeProject(proj, cmakeFile)
else:
logging.error("Illegal operate!\n")
printHelp()
elif len(sys.argv) == 4:
oldProj = sys.argv[2]
newProj = sys.argv[3]
cmakeFile = rootDir + "\\src\\CMakeLists.txt"
if sys.argv[1] == "--rename" or sys.argv[1] == "-r":
renameProject(oldProj, newProj, cmakeFile)
else:
logging.error("Illegal operate!\n")
printHelp()
else:
logging.error("Illegal operate!\n")
printHelp()