First (incomplete) rough draft of the Mussagl Manual
[mussa.git] / osxdist.py
1 #!/usr/bin/env python
2
3 import os
4 import sys
5 from shutil import copy, copytree
6
7 def system(cmdline):
8   #print >>sys.stderr, cmdline
9   return os.system(cmdline)
10
11 def mkdir(path):
12   path_list = []
13   head, tail = os.path.split(path)
14   path_list.insert(0, tail)
15   while len(head) > 0:
16     head, tail = os.path.split(head)
17     path_list.insert(0, tail)
18   created_path = ""
19   for path_element in path_list:
20     created_path = os.path.join(created_path, path_element)
21     if not os.path.exists(created_path):
22       os.mkdir(created_path)
23     
24 # useful information about building dmgs
25 # http://developer.apple.com/documentation/Porting/Conceptual/PortingUnix
26 # http://developer.apple.com/documentation/developertools/Conceptual/SoftwareDistribution/Concepts/sd_disk_images.html
27
28 def makedmg(app_dir):
29   # need to detect what the real volume name is
30   mussa_mount = '/Volumes/Mussa'
31   mussarw_dmg = 'mussarw.dmg'
32   mussa_dmg = 'mussa.dmg'
33   system('hdiutil detach '+mussa_mount)
34   if os.path.exists(mussa_mount):
35     print >>sys.stderr, "Something is in", mussa_mount
36     return 
37   if os.path.exists(mussarw_dmg):
38     os.unlink(mussarw_dmg)
39   if os.path.exists(mussa_dmg):
40     os.unlink(mussa_dmg)
41   system('hdiutil create -size 64m -fs HFS+ -volname "Mussa" '+mussarw_dmg)
42   system('hdiutil attach '+mussarw_dmg)
43   # copy files
44   copytree(app_dir, os.path.join(mussa_mount, app_dir))
45
46   system('hdiutil detach '+mussa_mount)
47   system('hdiutil convert '+mussarw_dmg +' -format UDZO -o '+mussa_dmg)
48   #system('hdiutil internet-enable -yes '+mussa_dmg)
49
50 def main(args):
51   framework_subpath = os.path.join("%(framework)s.framework", "Versions", "4.0", "%(framework)s")
52   frameworks = ['QtCore', 'QtGui', 'QtOpenGL']
53
54   qt_lib_dir="/usr/local/qt/4.1.0-shared/lib"
55   qt_framework=os.path.join(qt_lib_dir, framework_subpath)
56
57   app_name="mussagl"
58   app_dir=app_name+".app"
59   app_binary=app_dir+"/Contents/MacOS/"+app_name
60   app_framework=os.path.join(app_dir, "Contents", "Frameworks", framework_subpath)
61   if not os.path.exists(app_dir):
62     print >>sys.stderr, "please build mussagl first"
63     return 1
64
65   # install frameworks and update binary
66   for frame in frameworks:
67     qtframe = qt_framework % ({'framework': frame})
68     appframe = app_framework % ({'framework': frame})
69     exe_path = "@executable_path/../Frameworks/" + framework_subpath
70     exe_path %= ({'framework': frame})
71     mkdir(os.path.split(appframe)[0])
72     # update binary
73     copy(qtframe, appframe)
74     system("install_name_tool -id "+exe_path+" "+appframe)
75     system("install_name_tool -change "+qtframe+" "+exe_path+" "+app_binary)
76     # now that everything is in place change the frameworks 
77     # references
78     for frame2 in frameworks:
79       qtframe2 = qt_framework % ({'framework': frame2})
80       contents_exe_path = "@executable_path/../Frameworks/" + framework_subpath
81       contents_exe_path %= ({'framework': frame2})
82       system("install_name_tool -change "+qtframe2+" "+contents_exe_path+" "+
83              appframe)
84   makedmg(app_dir)
85
86 if __name__ == "__main__":
87   main(sys.argv[1:])