readme z
[wdl/wdl-ol.git] / IPlugExamples / duplicate.py
blobe36caf4942a924482e5cf792608670f18439897f
1 #!/usr/bin/python
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
9 # NOTES:
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
17 # USAGE:
18 # duplicate.py [inputprojectname] [outputprojectname] [manufacturername]
20 # TODO:
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
31 VERSION = "0.9"
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 = [
39 "app_wrapper",
40 "resources",
41 "installer",
42 "scripts",
43 "manual",
44 "ios_wrapper",
45 "xcschemes",
46 "xcshareddata",
47 "xcuserdata",
48 "en-osx.lproj",
49 "project.xcworkspace"
52 def checkdirname(name, searchproject):
53 "check if directory name matches with the given pattern"
54 print("")
55 if name == searchproject:
56 return True
57 else:
58 return False
60 def replacestrs(filename, s, r):
61 files = glob.glob(filename)
63 for line in fileinput.input(files,inplace=1):
64 line.find(s)
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)):
73 line = r + "\n"
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):
87 yield x
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):
94 yield x
95 elif (f in SUBFOLDERS_TO_SEARCH):
96 print('recursing in ' + f + ' directory: ')
97 for x in dirwalk(fullpath, searchproject, replaceproject, searchman, replaceman):
98 yield x
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)
114 else:
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))
121 yield f, fullpath
122 else:
123 yield f, fullpath
125 def main():
126 global VERSION
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]")
131 sys.exit(1)
132 else:
133 input=sys.argv[1]
134 output=sys.argv[2]
135 manufacturer=sys.argv[3]
137 if ' ' in input:
138 print("error: input project name has spaces")
139 sys.exit(1)
141 if ' ' in output:
142 print("error: output project name has spaces")
143 sys.exit(1)
145 if ' ' in manufacturer:
146 print("error: manufacturer name has spaces")
147 sys.exit(1)
149 # remove a trailing slash if it exists
150 if input[-1:] == "/":
151 input = input[0:-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")
159 sys.exit(1)
161 if os.path.isdir(output):
162 print("error: output folder allready exists")
163 sys.exit(1)
164 # rmtree(output)
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):
172 pass
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__':
181 main()