-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuplicateProject.py
More file actions
103 lines (86 loc) · 3.37 KB
/
DuplicateProject.py
File metadata and controls
103 lines (86 loc) · 3.37 KB
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
# -*- coding: utf-8 -*-
import os
import re
import shutil
import codecs
listexts = ("cpp", "h", "c", "cc", "tlh", "tli", "dsw", "dsp", "txt", "hpp", "rc", "rc2", "def", "sln", "vcproj", "vcxproj", "filters")
encs = ("gbk", "utf-8", "utf-16-le", "utf-16-be")
def DuplicateProject():
oriprjpath = input("Please input the original project path:")
if not os.path.exists(oriprjpath):
print('Not a valid path')
return
repprj = input("Please input the new name of project:")
sp = os.path.split(oriprjpath)
higherdir = sp[0]
oriprjname = sp[1]
newprjpath = os.path.join(higherdir, repprj)
if os.path.exists(newprjpath):
shutil.rmtree(newprjpath)
shutil.copytree(oriprjpath, newprjpath)
newprjname = os.path.split(newprjpath)[1]
RenameFileFolder(newprjpath, newprjname, oriprjname)
ReplaceInFile(newprjpath, newprjname, oriprjname)
return
def RenameFileFolder(path, newprjname, oriprjname):
names = os.listdir(path)
for name in names:
srcname = os.path.join(path,name)
if os.path.isdir(srcname):
RenameFileFolder(srcname, newprjname, oriprjname)
if name == oriprjname:
dstdirname = os.path.join(path, newprjname)
srcdirname = os.path.join(path, name)
os.rename(srcdirname, dstdirname)
else:
if name.find(oriprjname) != -1:
ofi = name.replace(oriprjname, newprjname)
dstname = os.path.join(path, ofi)
os.rename(srcname, dstname)
#sp = os.path.splitext(name)
#fn = sp[0]#fn的前面也可能包含oriprjname
#ext = sp[1]
#if fn == oriprjname:
# ofi = newprjname + ext
# dstname = os.path.join(path,ofi)
# os.rename(srcname, dstname)
return
def ReplaceInFile(newprjpath, newprjname, oriprjname):
names = os.listdir(newprjpath)
lowre = re.compile(oriprjname)
upperre = re.compile(oriprjname.upper())
for name in names:
srcname = os.path.join(newprjpath, name)
if os.path.isdir(srcname):
ReplaceInFile(srcname, newprjname, oriprjname)
else:
bisaneed_rep_file = False
ext = os.path.splitext(srcname)[1][1:]
for ex in listexts:
if ex == ext:
bisaneed_rep_file = True
if bisaneed_rep_file:
en = ""
for e in encs:
try:
fh = codecs.open(srcname, 'r', encoding=e)
fh.readline()
fh.close()
except UnicodeDecodeError:
print('got unicode error with', e, 'trying different encoding')
else:
print('opening', name, 'with encoding: ', e)
en = e
break
fp = codecs.open(srcname, 'r', en)
text = fp.read()
fp.close()
#ost = re.sub(oriprjname, newprjname, text)
ost = lowre.sub(newprjname, text)
ost = upperre.sub(newprjname.upper(), ost)
ost2 = codecs.encode(ost,en)
fp = open(srcname, 'wb')
fp.write(ost2)
fp.close()
return
DuplicateProject()