2 # -*- coding: iso-8859-1 -*-
4 ################################################################################
6 # 2008 June 27th by Marcus 'EgS' Eggenberger <egs@quassel-irc.org> #
8 # The author disclaims copyright to this source code. #
9 # This Python Script is in the PUBLIC DOMAIN. #
11 ################################################################################
13 # ==============================
15 # ==============================
20 from subprocess import Popen, PIPE
22 # ==============================
24 # ==============================
25 QT_CONFIG = """[Paths]
29 QT_CONFIG_NOBUNDLE = """[Paths]
35 class InstallQt(object):
36 def __init__(self, appdir, bundle=True, requestedPlugins=[]):
39 self.frameworkDir = self.appDir + "/Frameworks"
40 self.pluginDir = self.appDir + "/plugins"
41 self.executableDir = self.appDir
43 self.executableDir += "/MacOS"
45 self.installedFrameworks = set()
47 self.findFrameworkPath()
49 executables = [self.executableDir + "/" + executable for executable in os.listdir(self.executableDir)]
50 for executable in executables:
51 self.resolveDependancies(executable)
53 self.findPluginsPath()
54 self.installPlugins(requestedPlugins)
57 def qtProperty(self, qtProperty):
59 Query persistent property of Qt via qmake
61 VALID_PROPERTIES = ['QT_INSTALL_PREFIX',
69 'QT_INSTALL_TRANSLATIONS',
70 'QT_INSTALL_CONFIGURATION',
71 'QT_INSTALL_EXAMPLES',
77 if qtProperty not in VALID_PROPERTIES:
80 qmakeProcess = Popen('qmake -query %s' % qtProperty, shell=True, stdout=PIPE, stderr=PIPE)
81 result = qmakeProcess.stdout.read().strip()
82 qmakeProcess.stdout.close()
86 def findFrameworkPath(self):
87 self.sourceFrameworkPath = self.qtProperty('QT_INSTALL_LIBS')
89 def findPluginsPath(self):
90 self.sourcePluginsPath = self.qtProperty('QT_INSTALL_PLUGINS')
92 def findPlugin(self, pluginname):
93 qmakeProcess = Popen('find %s -name %s' % (self.sourcePluginsPath, pluginname), shell=True, stdout=PIPE, stderr=PIPE)
94 result = qmakeProcess.stdout.read().strip()
95 qmakeProcess.stdout.close()
101 def installPlugins(self, requestedPlugins):
103 os.mkdir(self.pluginDir)
107 for plugin in requestedPlugins:
108 if not plugin.isalnum():
109 print "Skipping library '%s'..." % plugin
112 pluginName = "lib%s.dylib" % plugin
115 pluginSource = self.findPlugin(pluginName)
117 print "WARNING: Requested library does not exist: '%s'" % plugin
120 pluginSubDir = os.path.dirname(pluginSource)
121 pluginSubDir = pluginSubDir.replace(self.sourcePluginsPath, '').strip('/')
123 os.mkdir("%s/%s" % (self.pluginDir, pluginSubDir))
127 os.system('cp "%s" "%s/%s"' % (pluginSource, self.pluginDir, pluginSubDir))
129 self.resolveDependancies("%s/%s/%s" % (self.pluginDir, pluginSubDir, pluginName))
131 def installQtConf(self):
132 qtConfName = self.appDir + "/qt.conf"
133 qtConfContent = QT_CONFIG_NOBUNDLE
135 qtConfContent = QT_CONFIG
136 qtConfName = self.appDir + "/Resources/qt.conf"
138 qtConf = open(qtConfName, 'w')
139 qtConf.write(qtConfContent)
142 def resolveDependancies(self, obj):
143 # obj must be either an application binary or a framework library
144 # print "resolving deps for:", obj
145 for framework, lib in self.determineDependancies(obj):
146 self.installFramework(framework)
147 self.changeDylPath(obj, framework, lib)
149 def installFramework(self, framework):
150 # skip if framework is already installed.
151 if framework in self.installedFrameworks:
154 self.installedFrameworks.add(framework)
156 # ensure that the framework directory exists
158 os.mkdir(self.frameworkDir)
162 if not framework.startswith('/'):
163 framework = "%s/%s" % (self.sourceFrameworkPath, framework)
166 os.system('cp -R "%s" "%s"' % (framework, self.frameworkDir))
168 frameworkname = framework.split('/')[-1]
169 localframework = self.frameworkDir + "/" + frameworkname
172 os.system('find "%s" -name *debug* -exec rm -f {} \;' % localframework)
173 os.system('find "%s" -name Headers -exec rm -rf {} \; 2>/dev/null' % localframework)
175 # Install new Lib ID and Change Path to Frameworks for the Dynamic linker
176 for lib in os.listdir(localframework + "/Versions/Current"):
177 lib = "%s/Versions/Current/%s" % (localframework, lib)
178 otoolProcess = Popen('otool -D "%s"' % lib, shell=True, stdout=PIPE, stderr=PIPE)
180 libname = [line for line in otoolProcess.stdout][1].strip()
183 otoolProcess.stdout.close()
184 if otoolProcess.wait() == 1: # we found some Resource dir or similar -> skip
186 frameworkpath, libpath = libname.split(frameworkname)
188 newlibname = "@executable_path/../%s%s" % (frameworkname, libpath)
190 newlibname = "@executable_path/%s%s" % (frameworkname, libpath)
191 # print 'install_name_tool -id "%s" "%s"' % (newlibname, lib)
192 os.system('install_name_tool -id "%s" "%s"' % (newlibname, lib))
194 self.resolveDependancies(lib)
196 def determineDependancies(self, app):
197 otoolPipe = Popen('otool -L "%s"' % app, shell=True, stdout=PIPE).stdout
198 otoolOutput = [line for line in otoolPipe]
200 libs = [line.split()[0] for line in otoolOutput[1:] if ("Qt" in line or "phonon" in line) and "@executable_path" not in line]
201 frameworks = [lib[:lib.find(".framework") + len(".framework")] for lib in libs]
202 frameworks = [framework[framework.rfind('/') + 1:] for framework in frameworks]
203 return zip(frameworks, libs)
205 def changeDylPath(self, obj, framework, lib):
206 newlibname = framework + lib.split(framework)[1]
208 newlibname = "@executable_path/../Frameworks/%s" % newlibname
210 newlibname = "@executable_path/Frameworks/%s" % newlibname
212 # print 'install_name_tool -change "%s" "%s" "%s"' % (lib, newlibname, obj)
213 os.system('install_name_tool -change "%s" "%s" "%s"' % (lib, newlibname, obj))
215 if __name__ == "__main__":
216 if len(sys.argv) < 2:
217 print "Wrong Argument Count (Syntax: %s [--nobundle] [--plugins=plugin1,plugin2,...] $TARGET_APP)" % sys.argv[0]
224 while offset < len(sys.argv) and sys.argv[offset].startswith("--"):
225 if sys.argv[offset] == "--nobundle":
228 if sys.argv[offset].startswith("--plugins="):
229 plugins = sys.argv[offset].split('=')[1].split(',')
233 targetDir = sys.argv[offset]
235 targetDir += "/Contents"
237 InstallQt(targetDir, bundle, plugins)