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]
37 class InstallQt(object):
38 def __init__(self, appdir, bundle = True, requestedPlugins=[]):
41 self.frameworkDir = self.appDir + "/Frameworks"
42 self.pluginDir = self.appDir + "/plugins"
43 self.executableDir = self.appDir
45 self.executableDir += "/MacOS"
47 self.installedFrameworks = set()
49 self.findFrameworkPath()
51 executables = [self.executableDir + "/" + executable for executable in os.listdir(self.executableDir)]
52 for executable in executables:
53 self.resolveDependancies(executable)
56 self.findPluginsPath()
57 self.installPlugins(requestedPlugins)
60 def qtProperty(self, qtProperty):
62 Query persistent property of Qt via qmake
64 VALID_PROPERTIES = ['QT_INSTALL_PREFIX',
72 'QT_INSTALL_TRANSLATIONS',
73 'QT_INSTALL_CONFIGURATION',
74 'QT_INSTALL_EXAMPLES',
80 if qtProperty not in VALID_PROPERTIES:
83 qmakeProcess = Popen('qmake -query %s' % qtProperty, shell=True, stdout=PIPE, stderr=PIPE)
84 result = qmakeProcess.stdout.read().strip()
85 qmakeProcess.stdout.close()
89 def findFrameworkPath(self):
90 self.sourceFrameworkPath = self.qtProperty('QT_INSTALL_LIBS')
92 def findPluginsPath(self):
93 self.sourcePluginsPath = self.qtProperty('QT_INSTALL_PLUGINS')
95 def findPlugin(self, pluginname):
96 qmakeProcess = Popen('find %s -name %s' % (self.sourcePluginsPath, pluginname), shell=True, stdout=PIPE, stderr=PIPE)
97 result = qmakeProcess.stdout.read().strip()
98 qmakeProcess.stdout.close()
105 def installPlugins(self, requestedPlugins):
107 os.mkdir(self.pluginDir)
111 for plugin in requestedPlugins:
112 if not plugin.isalnum():
113 print "Skipping library '%s'..." % plugin
116 pluginName = "lib%s.dylib" % plugin
119 pluginSource = self.findPlugin(pluginName)
121 print "WARNING: Requested library does not exist: '%s'" % plugin
124 pluginSubDir = os.path.dirname(pluginSource)
125 pluginSubDir = pluginSubDir.replace(self.sourcePluginsPath, '').strip('/')
127 os.mkdir("%s/%s" % (self.pluginDir, pluginSubDir))
131 os.system('cp "%s" "%s/%s"' % (pluginSource, self.pluginDir, pluginSubDir))
133 self.resolveDependancies("%s/%s/%s" % (self.pluginDir, pluginSubDir, pluginName))
135 def installQtConf(self):
136 qtConfName = self.appDir + "/qt.conf"
137 qtConfContent = QT_CONFIG_NOBUNDLE
139 qtConfContent = QT_CONFIG
140 qtConfName = self.appDir + "/Resources/qt.conf"
142 qtConf = open(qtConfName, 'w')
143 qtConf.write(qtConfContent)
146 def resolveDependancies(self, obj):
147 # obj must be either an application binary or a framework library
148 #print "resolving deps for:", obj
149 for framework, lib in self.determineDependancies(obj):
150 self.installFramework(framework)
151 self.changeDylPath(obj, framework, lib)
153 def installFramework(self, framework):
154 # skip if framework is already installed.
155 if framework in self.installedFrameworks:
158 self.installedFrameworks.add(framework)
160 # ensure that the framework directory exists
162 os.mkdir(self.frameworkDir)
166 if not framework.startswith('/'):
167 framework = "%s/%s" % (self.sourceFrameworkPath, framework)
170 os.system('cp -R "%s" "%s"' % (framework, self.frameworkDir))
172 frameworkname = framework.split('/')[-1]
173 localframework = self.frameworkDir + "/" + frameworkname
176 os.system('find "%s" -name *debug* -exec rm -f {} \;' % localframework)
177 os.system('find "%s" -name Headers -exec rm -rf {} \; 2>/dev/null' % localframework)
179 # Install new Lib ID and Change Path to Frameworks for the Dynamic linker
180 for lib in os.listdir(localframework + "/Versions/Current"):
181 lib = "%s/Versions/Current/%s" % (localframework, lib)
182 otoolProcess = Popen('otool -D "%s"' % lib, shell=True, stdout=PIPE, stderr=PIPE)
184 libname = [line for line in otoolProcess.stdout][1].strip()
187 otoolProcess.stdout.close()
188 if otoolProcess.wait() == 1: # we found some Resource dir or similar -> skip
190 frameworkpath, libpath = libname.split(frameworkname)
192 newlibname = "@executable_path/../%s%s" % (frameworkname, libpath)
194 newlibname = "@executable_path/%s%s" % (frameworkname, libpath)
195 #print 'install_name_tool -id "%s" "%s"' % (newlibname, lib)
196 os.system('install_name_tool -id "%s" "%s"' % (newlibname, lib))
198 self.resolveDependancies(lib)
200 def determineDependancies(self, app):
201 otoolPipe = Popen('otool -L "%s"' % app, shell=True, stdout=PIPE).stdout
202 otoolOutput = [line for line in otoolPipe]
204 libs = [line.split()[0] for line in otoolOutput[1:] if ("Qt" in line
206 and not "@executable_path" in line]
207 frameworks = [lib[:lib.find(".framework")+len(".framework")] for lib in libs]
208 frameworks = [framework[framework.rfind('/')+1:] for framework in frameworks]
209 return zip(frameworks, libs)
212 def changeDylPath(self, obj, framework, lib):
213 newlibname = framework + lib.split(framework)[1]
215 newlibname = "@executable_path/../Frameworks/%s" % newlibname
217 newlibname = "@executable_path/Frameworks/%s" % newlibname
219 #print 'install_name_tool -change "%s" "%s" "%s"' % (lib, newlibname, obj)
220 os.system('install_name_tool -change "%s" "%s" "%s"' % (lib, newlibname, obj))
222 if __name__ == "__main__":
223 if len(sys.argv) < 2:
224 print "Wrong Argument Count (Syntax: %s [--nobundle] [--plugins=plugin1,plugin2,...] $TARGET_APP)" % sys.argv[0]
231 while offset < len(sys.argv) and sys.argv[offset].startswith("--"):
232 if sys.argv[offset] == "--nobundle":
235 if sys.argv[offset].startswith("--plugins="):
236 plugins = sys.argv[offset].split('=')[1].split(',')
240 targetDir = sys.argv[offset]
242 targetDir += "/Contents"
244 InstallQt(targetDir, bundle, plugins)