3 # Python shell script for Duplicating WDL-OL IPlug Projects
4 # Oli Larkin 2012-2014 http://www.olilarkin.co.uk
5 # License: WTFPL http://sam.zoy.org/wtfpl/COPYING
6 # Modified from this script by Bibha Tripathi http://code.activestate.com/recipes/435904-sedawk-python-script-to-rename-subdirectories-of-a/
7 # Author accepts no responsibilty for wiping your hd
10 # should work with Python2 or Python3
11 # not designed to be fool proof- think carefully about what you choose for a project name
12 # best to stick to standard characters in your project names - avoid spaces, numbers and dots
13 # windows users need to install python and set it up so you can run it from the command line
14 # see http://www.voidspace.org.uk/python/articles/command_line.shtml
15 # this involves adding the python folder e.g. C:\Python27\ to your %PATH% environment variable
18 # duplicate.py [inputprojectname] [outputprojectname] [manufacturername]
21 # - indentation of directory structure
22 # - variable manufacturer name
25 from __future__
import generators
27 import fileinput
, glob
, string
, sys
, os
, re
, uuid
28 from shutil
import copy
, copytree
, ignore_patterns
, rmtree
29 from os
.path
import join
33 # binary files that we don't want to do find and replace inside
34 FILTERED_FILE_EXTENSIONS
= [".ico",".icns", ".pdf", ".png", ".zip", ".exe", ".wav", ".aif"]
35 # files that we don't want to duplicate
36 DONT_COPY
= ("*.exe", "*.dmg", "*.pkg", "*.mpkg", "*.svn", "*.ncb", "*.suo", "*sdf", "ipch", "build-*", "*.layout", "*.depend", ".DS_Store")
38 SUBFOLDERS_TO_SEARCH
= [
52 def checkdirname(name
, searchproject
):
53 "check if directory name matches with the given pattern"
55 if name
== searchproject
:
60 def replacestrs(filename
, s
, r
):
61 files
= glob
.glob(filename
)
63 for line
in fileinput
.input(files
,inplace
=1):
65 line
= line
.replace(s
, r
)
66 sys
.stdout
.write(line
)
68 def replacestrsChop(filename
, s
, r
):
69 files
= glob
.glob(filename
)
71 for line
in fileinput
.input(files
,inplace
=1):
72 if(line
.startswith(s
)):
74 sys
.stdout
.write(line
)
76 def dirwalk(dir, searchproject
, replaceproject
, searchman
, replaceman
):
77 for f
in os
.listdir(dir):
78 fullpath
= os
.path
.join(dir, f
)
80 if os
.path
.isdir(fullpath
) and not os
.path
.islink(fullpath
):
81 if checkdirname(f
, searchproject
+ ".xcodeproj"):
82 os
.rename(fullpath
, os
.path
.join(dir, replaceproject
+ ".xcodeproj"))
83 fullpath
= os
.path
.join(dir, replaceproject
+ ".xcodeproj")
85 print("recursing in main xcode project directory: ")
86 for x
in dirwalk(fullpath
, searchproject
, replaceproject
, searchman
, replaceman
):
88 elif checkdirname(f
, searchproject
+ "-ios.xcodeproj"):
89 os
.rename(fullpath
, os
.path
.join(dir, replaceproject
+ "-ios.xcodeproj"))
90 fullpath
= os
.path
.join(dir, replaceproject
+ "-ios.xcodeproj")
92 print("recursing in ios xcode project directory: ")
93 for x
in dirwalk(fullpath
, searchproject
, replaceproject
, searchman
, replaceman
):
95 elif (f
in SUBFOLDERS_TO_SEARCH
):
96 print('recursing in ' + f
+ ' directory: ')
97 for x
in dirwalk(fullpath
, searchproject
, replaceproject
, searchman
, replaceman
):
100 if os
.path
.isfile(fullpath
):
101 filename
= os
.path
.basename(fullpath
)
102 newfilename
= filename
.replace(searchproject
, replaceproject
)
103 base
, extension
= os
.path
.splitext(filename
)
105 if (not(extension
in FILTERED_FILE_EXTENSIONS
)):
106 print("Replacing project name strings in file " + filename
)
107 replacestrs(fullpath
, searchproject
, replaceproject
)
109 print("Replacing captitalized project name strings in file " + filename
)
110 replacestrs(fullpath
, searchproject
.upper(), replaceproject
.upper())
112 print("Replacing manufacturer name strings in file " + filename
)
113 replacestrs(fullpath
, searchman
, replaceman
)
115 print("NOT replacing name strings in file " + filename
)
117 if filename
!= newfilename
:
118 print("Renaming file " + filename
+ " to " + newfilename
)
119 os
.rename(fullpath
, os
.path
.join(dir, newfilename
))
127 print("\nIPlug Project Duplicator v" + VERSION
+ " by Oli Larkin ------------------------------\n")
129 if len(sys
.argv
) != 4:
130 print("Usage: duplicate.py inputprojectname outputprojectname [manufacturername]")
135 manufacturer
=sys
.argv
[3]
138 print("error: input project name has spaces")
142 print("error: output project name has spaces")
145 if ' ' in manufacturer
:
146 print("error: manufacturer name has spaces")
149 # remove a trailing slash if it exists
150 if input[-1:] == "/":
153 if output
[-1:] == "/":
154 output
= output
[0:-1]
156 #check that the folders are OK
157 if os
.path
.isdir(input) == False:
158 print("error: input project not found")
161 if os
.path
.isdir(output
):
162 print("error: output folder allready exists")
166 print("copying " + input + " folder to " + output
)
167 copytree(input, output
, ignore
=ignore_patterns(*DONT_COPY
))
168 cpath
= os
.path
.join(os
.getcwd(), output
)
170 #replace manufacturer name strings
171 for dir in dirwalk(cpath
, input, output
, "AcmeInc", manufacturer
):
174 print("\ncopying gitignore template into project folder")
176 copy('gitignore_template', output
+ "/.gitignore")
178 print("\ndone - don't forget to change PLUG_UID and MFR_UID in config.h")
180 if __name__
== '__main__':