1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
#!/usr/bin/python3
###############################################################################
##
## Copyright (C) 2014-2016, New York University.
## Copyright (C) 2011-2014, NYU-Poly.
## Copyright (C) 2006-2011, University of Utah.
## All rights reserved.
## Contact: contact@vistrails.org
##
## This file is part of VisTrails.
##
## "Redistribution and use in source and binary forms, with or without
## modification, are permitted provided that the following conditions are met:
##
## - Redistributions of source code must retain the above copyright notice,
## this list of conditions and the following disclaimer.
## - Redistributions in binary form must reproduce the above copyright
## notice, this list of conditions and the following disclaimer in the
## documentation and/or other materials provided with the distribution.
## - Neither the name of the New York University nor the names of its
## contributors may be used to endorse or promote products derived from
## this software without specific prior written permission.
##
## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
## THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
## PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
## EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
## PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
## OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
## WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
## OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
## ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
##
###############################################################################
import os
import shutil
import popen2
import re
import os.path
import sys
def usage():
print("Usage: ")
print(" %s path_to_itk_files destination_folder" % sys.argv[0])
sys.exit(1)
try:
path_to_libs = os.path.realpath(os.path.join(sys.argv[1],'bin'))
path_to_python_files = os.path.realpath(os.path.join(sys.argv[1],
'Wrapping',
'WrapITK',
'Python'))
destination_libs = os.path.realpath(os.path.join(sys.argv[2], 'Frameworks'))
destination_python_files = os.path.realpath(os.path.join(sys.argv[2],
'Resources',
'lib',
'python2.5'))
except IndexError:
usage()
print("This will copy the *.dylib to", destination_libs, " and *.so and the python files to ", destination_python_files)
libnames = re.compile(r'.*\.dylib')
itklibnames = re.compile(r'(.*emanuele*.*\.dylib) .*')
sonames = re.compile(r'.*\.so')
usrlocalnames = re.compile(r'(.*local*.*\.dylib) .*')
updatenames = re.compile(r'(@executable_path.*[^ ]*) .*')
#we first build the list of everything to change to minimize risk of failing
# after partial changes
def link_or_copy(src, dst):
"""link_or_copy(src:str, dst:str) -> None
Tries to create a hard link to a file. If it is not possible, it will
copy file src to dst
"""
# Links if possible, but we're across devices, we need to copy.
try:
os.link(src, dst)
except OSError as e:
if e.errno == 18:
# Across-device linking is not possible. Let's copy.
shutil.copyfile(src, dst)
else:
raise e
def build_cmdline(file_name, original_path, lib_name):
new_path = '@executable_path/../Frameworks/%s' % (lib_name)
cmd_line = ('install_name_tool -change %s %s %s' %
(original_path,
new_path,
file_name))
return cmd_line
def build_id_cmdline(file_name, original_path):
new_id = os.path.basename(original_path)
cmd_line = ('install_name_tool -id %s %s' %
(new_id, file_name))
return cmd_line
for root, dirs, file_names in os.walk(path_to_libs):
break
files_to_visit = set()
links_to_visit = set()
so_to_visit = set()
for f in file_names:
if libnames.match(f):
if not os.path.islink(os.path.join(path_to_libs,f)):
files_to_visit.add(f)
else:
links_to_visit.add(f)
elif sonames.match(f):
so_to_visit.add(f)
if len(files_to_visit) == 0:
print("looks like you started the script from the wrong directory")
sys.exit(1)
while len(files_to_visit):
f = files_to_visit.pop()
print("Visiting file", f)
src = os.path.join(path_to_libs,f)
dst = os.path.join(destination_libs,f)
print("Copyng to destination folder...")
link_or_copy(src,dst)
pout, pin = popen2.popen2("otool -L %s" % dst)
# print pout.readlines()
#changing id
lines = pout.readlines()
m = itklibnames.match(lines[0][:-1].strip())
if m:
cmd_line = build_id_cmdline(dst, m.groups()[0])
result = os.system(cmd_line)
for l in lines[1:]:
for r in [itklibnames,
usrlocalnames]:
m = r.match(l[:-1].strip())
if m:
#print " * matched: ", l[:-1].strip()
libname = os.path.basename(m.groups()[0])
cmd_line = build_cmdline(dst, m.groups()[0], libname)
#print "new file!", f
result = os.system(cmd_line)
if result != 0:
print("Something went wrong with install_name_tool. Ouch.")
sys.exit(1)
# creating symbolic links
cur_dir = os.getcwd()
os.chdir(destination_libs)
print("Creating symbolic links in %s " % os.getcwd())
while len(links_to_visit):
f = links_to_visit.pop()
src = os.readlink(os.path.join(path_to_libs,f))
print(" ", f, " -> " , src)
os.symlink(src,f)
os.chdir(cur_dir)
print("Dealing with *.so files... ")
while len(so_to_visit):
f = so_to_visit.pop()
print("Visiting file", f)
src = os.path.join(path_to_libs,f)
dst = os.path.join(destination_python_files,f)
print("Copyng to destination folder...")
link_or_copy(src,dst)
pout, pin = popen2.popen2("otool -L %s" % dst)
# print pout.readlines()
#changing id
lines = pout.readlines()
m = itklibnames.match(lines[0][:-1].strip())
if m:
cmd_line = build_id_cmdline(dst, m.groups()[0])
result = os.system(cmd_line)
for l in lines[1:]:
for r in [itklibnames,
usrlocalnames]:
m = r.match(l[:-1].strip())
if m:
#print " * matched: ", l[:-1].strip()
libname = os.path.basename(m.groups()[0])
cmd_line = build_cmdline(dst, m.groups()[0], libname)
#print "new file!", f
result = os.system(cmd_line)
if result != 0:
print("Something went wrong with install_name_tool. Ouch.")
sys.exit(1)
#copying python files
print("copying python files... ")
src = os.path.join(path_to_python_files,'*')
os.system("cp -r %s %s" % (src, destination_python_files))
print("copying python files in %s..." % path_to_libs)
src = os.path.join(path_to_libs,'*.py')
os.system("cp %s %s" % (src, destination_python_files))
print("copying static libs...")
src = os.path.join(path_to_libs, "*.a")
os.system("cp %s %s" % (src, destination_libs))
print("Done with ITK.")
|