b215b9dc9779d104a259a53d0f089f1845686283
[blender.git] / tools / scons / bs / bs_arc.py
1 # archive targets ('scons release')
2 # and extra functions
3 #
4 # Mac OS: appit
5 # Unices: zipit -> .tar.gz
6 # Windows: zipit -> .zip
7
8
9 import os
10 import sys
11 import string
12 import bs_dirs
13
14 import bs_globals
15
16 def add2arc(arc, file):
17         """
18         Add file to arc. For win32 arc is a Zipfile, for unices it's a Tarfile
19         """
20         if sys.platform == 'win32':
21                 arc.write(file)
22         else:
23                 arc.add(file)
24
25 def appit(target, source, env):
26         if sys.platform == 'darwin':
27                 import shutil
28                 import commands
29                 import os.path
30                                                 
31                 target = 'blender' 
32                 sourceinfo = "source/darwin/%s.app/Contents/Info.plist"%target
33                 targetinfo = "%s.app/Contents/Info.plist"%target
34
35                 cmd = '%s.app'%target
36                 if os.path.isdir(cmd):
37                         shutil.rmtree('%s.app'%target)
38                 shutil.copytree("source/darwin/%s.app"%target, '%s.app'%target)
39                 cmd = "cat %s | sed s/VERSION/`cat release/VERSION`/ | sed s/DATE/`date +'%%Y-%%b-%%d'`/ > %s"%(sourceinfo,targetinfo)
40                 commands.getoutput(cmd)
41                 cmd = 'cp %s %s.app/Contents/MacOS/%s'%(target, target, target)
42                 commands.getoutput(cmd)
43                 if  bs_globals.user_options_dict['BUILD_BINARY'] == 'debug':
44                         print "building debug"
45                 else :
46                         cmd = 'strip -u -r %s.app/Contents/MacOS/%s'%(target, target)
47                         commands.getoutput(cmd)
48                 cmd = '%s.app/Contents/Resources/'%target
49                 shutil.copy('bin/.blender/.bfont.ttf', cmd)
50                 shutil.copy('bin/.blender/.Blanguages', cmd)
51                 cmd = 'cp -R bin/.blender/locale %s.app/Contents/Resources/'%target
52                 commands.getoutput(cmd) 
53                 cmd = 'mkdir %s.app/Contents/MacOS/.blender'%target
54                 commands.getoutput(cmd)
55                 cmd = 'cp -R release/bpydata %s.app/Contents/MacOS/.blender'%target
56                 commands.getoutput(cmd)
57                 cmd = 'cp -R release/scripts %s.app/Contents/MacOS/.blender/'%target
58                 commands.getoutput(cmd)
59                 cmd = 'cp -R release/plugins %s.app/Contents/Resources/'%target 
60                 commands.getoutput(cmd)
61                 cmd = 'chmod +x  %s.app/Contents/MacOS/%s'%(target, target)
62                 commands.getoutput(cmd)
63                 cmd = 'find %s.app -name CVS -prune -exec rm -rf {} \;'%target
64                 commands.getoutput(cmd)
65                 cmd = 'find %s.app -name .DS_Store -exec rm -rf {} \;'%target
66                 commands.getoutput(cmd)
67                 
68                 if bs_globals.user_options_dict['BUILD_BLENDER_PLAYER'] == 1:
69                         target = 'blenderplayer' 
70                         sourceinfo = "source/darwin/%s.app/Contents/Info.plist"%target
71                         targetinfo = "%s.app/Contents/Info.plist"%target
72
73                         cmd = '%s.app'%target
74                         if os.path.isdir(cmd):
75                                 shutil.rmtree('%s.app'%target)
76                         shutil.copytree("source/darwin/%s.app"%target, '%s.app'%target)
77                         cmd = "cat %s | sed s/VERSION/`cat release/VERSION`/ | sed s/DATE/`date +'%%Y-%%b-%%d'`/ > %s"%(sourceinfo,targetinfo)
78                         commands.getoutput(cmd)
79                         cmd = 'cp %s %s.app/Contents/MacOS/%s'%(target, target, target)
80                         commands.getoutput(cmd)
81                         if  bs_globals.user_options_dict['BUILD_BINARY'] == 'debug':
82                                 print "building debug player"
83                         else :
84                                 cmd = 'strip -u -r %s.app/Contents/MacOS/%s'%(target, target)
85                                 commands.getoutput(cmd)
86                         cmd = '%s.app/Contents/Resources/'%target
87                         shutil.copy('bin/.blender/.bfont.ttf', cmd)
88                         shutil.copy('bin/.blender/.Blanguages', cmd)
89                         cmd = 'cp -R bin/.blender/locale %s.app/Contents/Resources/'%target
90                         commands.getoutput(cmd)
91                         cmd = 'cp -R release/bpydata %s.app/Contents/MacOS/.blender'%target
92                         commands.getoutput(cmd)
93                         cmd = 'cp -R release/scripts %s.app/Contents/MacOS/.blender/'%target
94                         commands.getoutput(cmd)
95                         cmd = 'cp -R release/plugins %s.app/Contents/Resources/'%target 
96                         commands.getoutput(cmd)
97                         cmd = 'chmod +x  %s.app/Contents/MacOS/%s'%(target, target)
98                         commands.getoutput(cmd)
99                         cmd = 'find %s.app -name CVS -prune -exec rm -rf {} \;'%target
100                         commands.getoutput(cmd)
101                         cmd = 'find %s.app -name .DS_Store -exec rm -rf {} \;'%target
102                         commands.getoutput(cmd)
103                 
104         else:
105                 print "This target is for the Os X platform only"
106
107 def zipit(env, target, source):
108         try:
109                 if sys.platform == 'win32':
110                         import zipfile
111                 else:
112                         import tarfile
113         except:
114                 if sys.platform == 'win32':
115                         print "no zipfile module found"
116                 else:
117                         print "no tarfile module found"
118                         print "make sure you use python 2.3"
119                 print
120                 return
121         
122         import shutil
123         import glob
124         import time
125         
126         startdir = os.getcwd()
127         pf=""
128         zipext = ""
129         zipname = ""
130         
131         today = time.strftime("%Y%m%d", time.gmtime()) # get time in the form 20040714
132         
133         if bs_dirs.preparedist()==0:
134                 print "check output for error"
135                 return
136         
137         if sys.platform == 'win32':
138                 zipext += ".zip"
139                 pf = "windows"
140         elif sys.platform == 'linux2' or sys.platform == 'linux-i386':
141                 zipext += ".tar.gz"
142                 pf = "linux"
143         elif sys.platform == 'freebsd4':
144                 zipext += ".tar.gz"
145                 pf = "freebsd4"
146         elif sys.platform == 'freebsd5':
147                 zipext += ".tar.gz"
148                 pf = "freebsd5"
149         elif sys.platform == 'cygwin':
150                 zipext += ".tar.gz"
151                 pf = "cygwin"
152         
153         if bs_globals.user_options_dict['BUILD_BINARY'] == 'release':
154                 if sys.platform == 'cygwin':
155                         blendname = "blender-" + bs_globals.version + "-" + pf
156                 else:
157                         blendname = "blender-" + bs_globals.version + "-" + bs_globals.config_guess
158         else:
159                 blendname = "bf_blender_" + pf + "_" + today
160         
161         zipname = blendname + zipext
162
163         if os.path.isdir(blendname):
164                 shutil.rmtree(blendname)
165         shutil.move(startdir + os.sep + "dist", blendname)
166
167         print
168         if sys.platform == 'win32':
169                 print "Create the zip!"
170         else:
171                 print "Create the tarball!"
172         print
173         
174         if sys.platform == 'win32':
175                 thezip = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED)
176         else:
177                 thezip = tarfile.open(zipname, 'w:gz')
178         
179         for root, dirs, files in os.walk(blendname, topdown=False):
180                 for name in files:
181                         if name in [zipname]:
182                                 print "skipping self"
183                         else:
184                                 file = root + "/" + name
185                                 print "adding: " + file
186                                 add2arc(thezip, file)
187         
188         thezip.close()
189         
190         os.chdir(startdir)
191         shutil.move(blendname, startdir + os.sep + "dist")
192         
193         if bs_dirs.finalisedist(zipname)==0:
194                 print "encountered an error in finalisedist"
195                 print
196                 return
197         
198         print
199         print "Blender has been successfully packaged"
200         print "You can find the file %s in the root source directory"%zipname
201         print
202
203 def printadd(env, target, source):
204         """
205         Print warning message if platform hasn't been added to zipit() yet
206         """
207         
208         print
209         print "############"
210         print 
211         print "Make sure zipit() works for your platform:"
212         print "  - binaries to copy (naming?)"
213         print "  - possible libraries?"
214         print "  - archive format?"
215         print
216         print "/Nathan Letwory (jesterKing)"
217         print
218
219 def BlenderRelease(target):
220         """
221         Make a Release package (tarball, zip, bundle).
222         
223         target = Name of package to make (string)
224         eg: BlenderRelease('blender')
225         """
226         
227         if sys.platform == 'darwin':
228                 app_env = bs_globals.init_env.Copy()
229                 Mappit = app_env.Command('appit', bs_globals.appname, appit)
230                 if bs_globals.user_options_dict['BUILD_BLENDER_PLAYER'] == 1:
231                         app_env.Depends(Mappit, bs_globals.playername)
232                 app_env.Alias("release", Mappit)
233         elif sys.platform in ['win32', 'linux2', 'linux-i386', 'freebsd4', 'freebsd5','cygwin']:
234                 release_env = bs_globals.init_env.Copy()
235                 releaseit = release_env.Command('blenderrelease', bs_globals.appname, zipit)
236                 if bs_globals.user_options_dict['BUILD_BLENDER_PLAYER'] == 1:
237                         release_env.Depends(releaseit, bs_globals.playername)
238                 release_env.Alias("release", releaseit)
239         else:
240                 release_env = bs_globals.init_env.Copy()
241                 releaseit = release_env.Command('blender.tar.gz', bs_globals.appname, printadd)
242                 if bs_globals.user_options_dict['BUILD_BLENDER_PLAYER'] == 1:
243                         release_env.Depends(releaseit, bs_globals.playername)
244                 release_env.Alias("release", releaseit)