-
Notifications
You must be signed in to change notification settings - Fork 7
/
duplicate.py
executable file
·138 lines (109 loc) · 4.18 KB
/
duplicate.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
#!/usr/bin/python
# USAGE:
# duplicate.py [inputprojectname] [outputprojectname]
import fileinput, glob, string, sys, os, re, uuid
from shutil import copy, copytree, ignore_patterns, rmtree
from os.path import join
VERSION = "0.9"
# binary files that we don't want to do find and replace inside
FILTERED_FILE_EXTENSIONS = [".ico",".icns", ".pdf", ".png", ".zip", ".exe", ".wav", ".aif"]
# files that we don't want to duplicate
DONT_COPY = ("*.exe", "*.dmg", "*.pkg", "*.mpkg", "*.svn", "*.ncb", "*.suo", "*sdf", "ipch", "build-*", "*.layout", "*.depend", ".DS_Store", "*manual.pdf", "xcuserdata")
SUBFOLDERS_TO_SEARCH = [
"resources",
"installer",
"manual",
"xcschemes",
"xcshareddata",
"project.xcworkspace",
"Builds",
"Source",
"MacOSX",
"VisualStudio2017"
]
def checkdirname(name, searchproject):
"check if directory name matches with the given pattern"
print("")
if name == searchproject:
return True
else:
return False
def replacestrs(filename, s, r):
files = glob.glob(filename)
s_low = s.lower()
r_low = r.lower()
for line in fileinput.input(files,inplace=1):
line = line.replace(s, r)
line = line.replace(s_low, r_low)
sys.stdout.write(line)
def dirwalk(dir, searchproject, replaceproject):
for f in os.listdir(dir):
fullpath = os.path.join(dir, f)
if os.path.isdir(fullpath) and not os.path.islink(fullpath):
if checkdirname(f, searchproject + ".xcodeproj"):
os.rename(fullpath, os.path.join(dir, replaceproject + ".xcodeproj"))
fullpath = os.path.join(dir, replaceproject + ".xcodeproj")
print("recursing in main xcode project directory: ")
for x in dirwalk(fullpath, searchproject, replaceproject):
yield x
elif checkdirname(f, searchproject + "-ios.xcodeproj"):
os.rename(fullpath, os.path.join(dir, replaceproject + "-ios.xcodeproj"))
fullpath = os.path.join(dir, replaceproject + "-ios.xcodeproj")
print("recursing in ios xcode project directory: ")
for x in dirwalk(fullpath, searchproject, replaceproject):
yield x
elif (f in SUBFOLDERS_TO_SEARCH):
print('recursing in ' + f + ' directory: ')
for x in dirwalk(fullpath, searchproject, replaceproject):
yield x
if os.path.isfile(fullpath):
filename = os.path.basename(fullpath)
newfilename = filename.replace(searchproject, replaceproject)
base, extension = os.path.splitext(filename)
if (not(extension in FILTERED_FILE_EXTENSIONS)):
print("Replacing project name strings in file " + filename)
replacestrs(fullpath, searchproject, replaceproject)
print("Replacing captitalized project name strings in file " + filename)
replacestrs(fullpath, searchproject.upper(), replaceproject.upper())
else:
print("NOT replacing name strings in file " + filename)
if filename != newfilename:
print("Renaming file " + filename + " to " + newfilename)
os.rename(fullpath, os.path.join(dir, newfilename))
yield f, fullpath
else:
yield f, fullpath
def main():
if len(sys.argv) < 3:
print("Usage: duplicate.py inputprojectname outputprojectname")
sys.exit(1)
else:
input=sys.argv[1]
output=sys.argv[2]
if ' ' in input:
print("error: input project name has spaces")
sys.exit(1)
if ' ' in output:
print("error: output project name has spaces")
sys.exit(1)
# remove a trailing slash if it exists
if input[-1:] == "/":
input = input[0:-1]
if output[-1:] == "/":
output = output[0:-1]
#check that the folders are OK
if os.path.isdir(input) == False:
print("error: input project not found")
sys.exit(1)
if os.path.isdir(output):
print("error: output folder allready exists")
sys.exit(1)
# rmtree(output)
print("copying " + input + " folder to " + output)
copytree(input, output, ignore=ignore_patterns(*DONT_COPY))
cpath = os.path.join(os.getcwd(), output)
#replace manufacturer name strings
for dir in dirwalk(cpath, input, output):
pass
if __name__ == '__main__':
main()