pull-from-transifex.sh: pull newly added languages as well
[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 from subprocess import Popen, PIPE
20
21 class InstallQt(object):
22     def __init__(self, appdir, bundle = True):
23         self.appDir = appdir
24         self.bundle = bundle
25         self.executableDir = self.appDir
26         if bundle:
27             self.executableDir += "/MacOS"
28
29         if bundle:
30             self.frameworkDir = self.appDir + "/Frameworks"
31         else:
32             self.frameworkDir = self.executableDir + "/Frameworks"
33
34         self.needFrameworks = []
35
36         executables = [self.executableDir + "/" + executable for executable in os.listdir(self.executableDir)]
37
38         for executable in executables:
39             for framework,lib in self.determineDependancies(executable):
40                 if framework not in self.needFrameworks:
41                     self.needFrameworks.append(framework)
42                     self.installFramework(framework)
43             self.changeDylPath(executable)
44
45     def installFramework(self, framework):
46         try:
47             os.mkdir(self.frameworkDir)
48         except:
49             pass
50
51         # Copy Framework
52         os.system('cp -R "%s" "%s"' % (framework, self.frameworkDir))
53
54         frameworkname = framework.split('/')[-1]
55         localframework = self.frameworkDir + "/" + frameworkname
56
57         # De-Myllify
58         os.system('find "%s" -name *debug* -exec rm -f {} \;' % localframework)
59         os.system('find "%s" -name Headers -exec rm -rf {} \; 2>/dev/null' % localframework)
60
61         # Install new Lib ID and Change Path to Frameworks for the Dynamic linker
62         for lib in os.listdir(localframework + "/Versions/Current"):
63             lib = "%s/Versions/Current/%s" % (localframework, lib)
64             otoolProcess = Popen('otool -D "%s"' % lib, shell=True, stdout=PIPE, stderr=PIPE)
65             try:
66                 libname = [line for line in otoolProcess.stdout][1].strip()
67             except:
68                 libname = ''
69             otoolProcess.stdout.close()
70             if otoolProcess.wait() == 1: # we found some Resource dir or similar -> skip
71                 continue
72             frameworkpath, libpath = libname.split(frameworkname)
73             if self.bundle:
74                 newlibname = "@executable_path/../%s%s" % (frameworkname, libpath)
75             else:
76                 newlibname = "@executable_path/%s%s" % (frameworkname, libpath)
77             #print 'install_name_tool -id "%s" "%s"' % (newlibname, lib)
78             os.system('install_name_tool -id "%s" "%s"' % (newlibname, lib))
79             self.changeDylPath(lib)
80             
81     def determineDependancies(self, app):
82         otoolPipe = Popen('otool -L "%s"' % app, shell=True, stdout=PIPE).stdout
83         otoolOutput = [line for line in otoolPipe]
84         otoolPipe.close()
85         libs = [line.split()[0] for line in otoolOutput[1:] if "Qt" in line and not "@executable_path" in line]
86         frameworks = [lib[:lib.find(".framework")+len(".framework")] for lib in libs]
87         return zip(frameworks, libs)
88
89     def changeDylPath(self, obj):
90         for framework, lib in self.determineDependancies(obj):
91             frameworkname = framework.split('/')[-1]
92             frameworkpath, libpath = lib.split(frameworkname)
93             if self.bundle:
94                 newlibname = "@executable_path/../Frameworks/%s%s" % (frameworkname, libpath)
95             else:
96                 newlibname = "@executable_path/Frameworks/%s%s" % (frameworkname, libpath)
97
98             #print 'install_name_tool -change "%s" "%s" "%s"' % (lib, newlibname, obj)
99             os.system('install_name_tool -change "%s" "%s" "%s"' % (lib, newlibname, obj))
100
101 if __name__ == "__main__":
102     if len(sys.argv) < 2:
103         print "Wrong Argument Count (Syntax: %s [--nobundle] $TARGET_APP)" % sys.argv[0]
104         sys.exit(1)
105     else:
106         bundle = True
107         offset = 1
108
109         if sys.argv[1].startswith("--"):
110             offset = 2
111             if sys.argv[1] == "--nobundle":
112                 bundle = False
113
114         targetDir = sys.argv[offset]
115         if bundle:
116             targetDir += "/Contents"
117
118         InstallQt(targetDir, bundle)
119     
120