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 executables = [self.executableDir + "/" + executable for executable in os.listdir(self.executableDir)]
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)
45 def installFramework(self, framework):
47 os.mkdir(self.frameworkDir)
52 os.system('cp -R "%s" "%s"' % (framework, self.frameworkDir))
54 frameworkname = framework.split('/')[-1]
55 localframework = self.frameworkDir + "/" + frameworkname
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)
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)
66 libname = [line for line in otoolProcess.stdout][1].strip()
69 otoolProcess.stdout.close()
70 if otoolProcess.wait() == 1: # we found some Resource dir or similar -> skip
72 frameworkpath, libpath = libname.split(frameworkname)
74 newlibname = "@executable_path/../%s%s" % (frameworkname, libpath)
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)
81 def determineDependancies(self, app):
82 otoolPipe = Popen('otool -L "%s"' % app, shell=True, stdout=PIPE).stdout
83 otoolOutput = [line for line in otoolPipe]
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)
89 def changeDylPath(self, obj):
90 for framework, lib in self.determineDependancies(obj):
91 frameworkname = framework.split('/')[-1]
92 frameworkpath, libpath = lib.split(frameworkname)
94 newlibname = "@executable_path/../Frameworks/%s%s" % (frameworkname, libpath)
96 newlibname = "@executable_path/Frameworks/%s%s" % (frameworkname, libpath)
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))
101 if __name__ == "__main__":
102 if len(sys.argv) < 2:
103 print "Wrong Argument Count (Syntax: %s [--nobundle] $TARGET_APP)" % sys.argv[0]
109 if sys.argv[1].startswith("--"):
111 if sys.argv[1] == "--nobundle":
114 targetDir = sys.argv[offset]
116 targetDir += "/Contents"
118 InstallQt(targetDir, bundle)