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