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 # ==============================
19 from subprocess import Popen, PIPE
21 class InstallQt(object):
22 def __init__(self, appdir, bundle = True):
25 self.executableDir = self.appDir
27 self.executableDir += "/MacOS"
30 self.frameworkDir = self.appDir + "/Frameworks"
32 self.frameworkDir = self.executableDir + "/Frameworks"
34 self.needFrameworks = []
36 self.findFrameworkPath()
38 executables = [self.executableDir + "/" + executable for executable in os.listdir(self.executableDir)]
40 for executable in executables:
41 for framework,lib in self.determineDependancies(executable):
42 if framework not in self.needFrameworks:
43 self.needFrameworks.append(framework)
44 self.installFramework(framework)
45 self.changeDylPath(executable)
47 def findFrameworkPath(self):
48 qmakeProcess = Popen('qmake -query QT_INSTALL_LIBS', shell=True, stdout=PIPE, stderr=PIPE)
49 self.sourceFrameworkPath = qmakeProcess.stdout.read().strip()
50 qmakeProcess.stdout.close()
54 def installFramework(self, framework):
56 os.mkdir(self.frameworkDir)
60 if not framework.startswith('/'):
61 framework = "%s/%s" % (self.sourceFrameworkPath, framework)
64 os.system('cp -R "%s" "%s"' % (framework, self.frameworkDir))
66 frameworkname = framework.split('/')[-1]
67 localframework = self.frameworkDir + "/" + frameworkname
70 os.system('find "%s" -name *debug* -exec rm -f {} \;' % localframework)
71 os.system('find "%s" -name Headers -exec rm -rf {} \; 2>/dev/null' % localframework)
73 # Install new Lib ID and Change Path to Frameworks for the Dynamic linker
74 for lib in os.listdir(localframework + "/Versions/Current"):
75 lib = "%s/Versions/Current/%s" % (localframework, lib)
76 otoolProcess = Popen('otool -D "%s"' % lib, shell=True, stdout=PIPE, stderr=PIPE)
78 libname = [line for line in otoolProcess.stdout][1].strip()
81 otoolProcess.stdout.close()
82 if otoolProcess.wait() == 1: # we found some Resource dir or similar -> skip
84 frameworkpath, libpath = libname.split(frameworkname)
86 newlibname = "@executable_path/../%s%s" % (frameworkname, libpath)
88 newlibname = "@executable_path/%s%s" % (frameworkname, libpath)
89 #print 'install_name_tool -id "%s" "%s"' % (newlibname, lib)
90 os.system('install_name_tool -id "%s" "%s"' % (newlibname, lib))
91 self.changeDylPath(lib)
93 def determineDependancies(self, app):
94 otoolPipe = Popen('otool -L "%s"' % app, shell=True, stdout=PIPE).stdout
95 otoolOutput = [line for line in otoolPipe]
97 libs = [line.split()[0] for line in otoolOutput[1:] if ("Qt" in line
99 and not "@executable_path" in line]
100 frameworks = [lib[:lib.find(".framework")+len(".framework")] for lib in libs]
101 return zip(frameworks, libs)
103 def changeDylPath(self, obj):
104 for framework, lib in self.determineDependancies(obj):
105 frameworkname = framework.split('/')[-1]
106 frameworkpath, libpath = lib.split(frameworkname)
108 newlibname = "@executable_path/../Frameworks/%s%s" % (frameworkname, libpath)
110 newlibname = "@executable_path/Frameworks/%s%s" % (frameworkname, libpath)
112 #print 'install_name_tool -change "%s" "%s" "%s"' % (lib, newlibname, obj)
113 os.system('install_name_tool -change "%s" "%s" "%s"' % (lib, newlibname, obj))
115 if __name__ == "__main__":
116 if len(sys.argv) < 2:
117 print "Wrong Argument Count (Syntax: %s [--nobundle] $TARGET_APP)" % sys.argv[0]
123 if sys.argv[1].startswith("--"):
125 if sys.argv[1] == "--nobundle":
128 targetDir = sys.argv[offset]
130 targetDir += "/Contents"
132 InstallQt(targetDir, bundle)