InputLine now regains focus on keypress
[quassel.git] / scripts / build / macosx_DeployApp.py
1 #!/usr/bin/python
2 # -*- coding: iso-8859-1 -*-
3
4 ################################################################################
5 #                                                                              #
6 # 2008 June 27th by Marcus 'EgS' Eggenberger <egs@quassel-irc.org>             #
7 #                                                                              #
8 # The author disclaims copyright to this source code.                          #
9 # This Python Script is in the PUBLIC DOMAIN.                                  #
10 #                                                                              #
11 ################################################################################
12
13 # ==============================
14 #  Imports
15 # ==============================
16 import sys
17 import os
18
19 class InstallQt(object):
20     def __init__(self, appdir, bundle = True):
21         self.appDir = appdir
22         self.bundle = bundle
23         self.executableDir = self.appDir
24         if bundle:
25             self.executableDir += "/MacOS"
26
27         if bundle:
28             self.frameworkDir = self.appDir + "/Frameworks"
29         else:
30             self.frameworkDir = self.executableDir + "/Frameworks"
31
32         self.needFrameworks = []
33
34         executables = [self.executableDir + "/" + executable for executable in os.listdir(self.executableDir)]
35
36         for executable in executables:
37             for framework,lib in self.determineDependancies(executable):
38                 if framework not in self.needFrameworks:
39                     self.needFrameworks.append(framework)
40                     self.installFramework(framework)
41             self.changeDylPath(executable)
42
43     def installFramework(self, framework):
44         try:
45             os.mkdir(self.frameworkDir)
46         except:
47             pass
48
49         # Copy Framework
50         os.system('cp -R "%s" "%s"' % (framework, self.frameworkDir))
51
52         frameworkname = framework.split('/')[-1]
53         localframework = self.frameworkDir + "/" + frameworkname
54
55         # De-Myllify
56         os.system('find "%s" -name *debug* -exec rm -f {} \;' % localframework)
57         os.system('find "%s" -name Headers -exec rm -rf {} \; 2>/dev/null' % localframework)
58
59         # Install new Lib ID and Change Path to Frameworks for the Dynamic linker
60         for lib in os.listdir(localframework + "/Versions/Current"):
61             lib = "%s/Versions/Current/%s" % (localframework, lib)
62             otoolpipe = os.popen('otool -D "%s"' % lib)
63             libname = [line for line in otoolpipe][1].strip()
64             otoolpipe.close()
65             frameworkpath, libpath = libname.split(frameworkname)
66             if self.bundle:
67                 newlibname = "@executable_path/../%s%s" % (frameworkname, libpath)
68             else:
69                 newlibname = "@executable_path/%s%s" % (frameworkname, libpath)
70             #print 'install_name_tool -id "%s" "%s"' % (newlibname, lib)
71             os.system('install_name_tool -id "%s" "%s"' % (newlibname, lib))
72             self.changeDylPath(lib)
73             
74     def determineDependancies(self, app):
75         otoolPipe = os.popen('otool -L "%s"' % app)
76         otoolOutput = [line for line in otoolPipe]
77         otoolPipe.close()
78         libs = [line.split()[0] for line in otoolOutput[1:] if "Qt" in line and not "@executable_path" in line]
79         frameworks = [lib[:lib.find(".framework")+len(".framework")] for lib in libs]
80         return zip(frameworks, libs)
81
82     def changeDylPath(self, obj):
83         for framework, lib in self.determineDependancies(obj):
84             frameworkname = framework.split('/')[-1]
85             frameworkpath, libpath = lib.split(frameworkname)
86             if self.bundle:
87                 newlibname = "@executable_path/../Frameworks/%s%s" % (frameworkname, libpath)
88             else:
89                 newlibname = "@executable_path/Frameworks/%s%s" % (frameworkname, libpath)
90
91             #print 'install_name_tool -change "%s" "%s" "%s"' % (lib, newlibname, obj)
92             os.system('install_name_tool -change "%s" "%s" "%s"' % (lib, newlibname, obj))
93
94 if __name__ == "__main__":
95     if len(sys.argv) < 2:
96         print "Wrong Argument Count (Syntax: %s [--nobundle] $TARGET_APP)" % sys.argv[0]
97         sys.exit(1)
98     else:
99         bundle = True
100         offset = 1
101
102         if sys.argv[1].startswith("--"):
103             offset = 2
104             if sys.argv[1] == "--nobundle":
105                 bundle = False
106
107         targetDir = sys.argv[offset]
108         if bundle:
109             targetDir += "/Contents"
110
111         InstallQt(targetDir, bundle)
112     
113