22
33import os
44import re
5+ import stat
56import platform
67from commands import getstatusoutput
78
@@ -109,14 +110,11 @@ def get_filetype(path_name):
109110 """
110111 check the output of the file command for given string
111112 """
112- file_command = which ("file" )
113- output = file_command ("-b" ,
114- path_name ,
115- output = str ,
116- fail_on_error = False ,
117- error = str )
118- if file_command .returncode != 0 :
119- tty .warn ('getting filetype of "%s" failed' % path_name )
113+ file_command = "LC_ALL=C file -b -h \" %s\" " % path_name
114+ status , output = getstatusoutput (file_command )
115+
116+ if status != 0 :
117+ tty .warn ('getting filetype of "%s" failed' % path_name )
120118 return None
121119 return output .strip ()
122120
@@ -149,18 +147,20 @@ def relocate_binary(path_name, topdir, new_root_dir, patchelf_executable):
149147 Change RPATHs in given file
150148 """
151149 orig_rpath = get_existing_rpath (path_name , patchelf_executable )
152- new_rpath = substitute_rpath (orig_rpath , topdir , new_root_dir )
153- modify_rpath (path_name , orig_rpath , new_rpath , patchelf_executable )
150+ if orig_rpath :
151+ new_rpath = substitute_rpath (orig_rpath , topdir , new_root_dir )
152+ modify_rpath (path_name , orig_rpath , new_rpath , patchelf_executable )
154153
155154
156155def get_existing_rpath (path_name , patchelf_executable ):
157156 if platform .system () == 'Darwin' :
158157 command = which ('otool' )
159- output = command ("-l" , path_name )
158+ output = command ("-l" , path_name , output = str , err = str )
160159 if command .returncode != 0 :
161160 tty .warn ('failed reading rpath for %s.' % path_name )
162161 return False
163162 last_cmd = None
163+ path = None
164164 for line in output .split ('\n ' ):
165165 match = re .search ('( *[a-zA-Z]+ )(.*)' , line )
166166 if match :
@@ -173,7 +173,10 @@ def get_existing_rpath(path_name, patchelf_executable):
173173 last_cmd = rhs
174174 if lhs == 'path' and last_cmd == 'LC_RPATH' :
175175 path = rhs
176- return path .split (':' )
176+ if path == None :
177+ return False
178+ else :
179+ return path .split (':' )
177180 elif platform .system () == 'Linux' :
178181 command = '%s --print-rpath %s ' % (patchelf_executable , path_name )
179182 status , output = getstatusoutput (command )
@@ -198,20 +201,18 @@ def substitute_rpath(orig_rpath, topdir, new_root_path):
198201
199202
200203def modify_rpath (path_name , orig_rpath , new_rpath , patchelf_executable ):
204+ st = os .stat (path_name )
205+ wmode = os .access (path_name ,os .W_OK )
206+ if not wmode : os .chmod (path_name ,st .st_mode | stat .S_IWUSR )
201207 if platform .system () == 'Darwin' :
202208 orig_joined = ':' .join (orig_rpath )
203209 new_joined = ':' .join (new_rpath )
204- command = "install_name_tool -rpath '%s' '%s' '%s'" % \
205- (orig_joined , new_joined , path_name )
206- status , output = getstatusoutput (command )
207- if status != 0 :
208- tty .warn ('failed writing rpath for %s.' % path_name )
209210 for orig , new in zip (orig_rpath , new_rpath ):
210- command = "install_name_tool -rpath '%s' '%s' '%s'" % \
211- ( orig , new , path_name )
212- status , output = getstatusoutput ( command )
213- if status != 0 :
214- tty .warn ('failed writing rpath for %s.' % path_name )
211+ command = which ( "install_name_tool" )
212+ output = command ( "-rpath" , "%s" % orig ,\
213+ "%s" % new , "%s" % path_name , output = str , err = str )
214+ if command . returncode != 0 :
215+ tty .warn ('failed writing rpath for %s.' % path_name )
215216 elif platform .system () == 'Linux' :
216217 new_joined = ':' .join (new_rpath )
217218 command = "%s --force-rpath --set-rpath '%s' '%s'" % \
@@ -221,7 +222,7 @@ def modify_rpath(path_name, orig_rpath, new_rpath, patchelf_executable):
221222 tty .warn ('failed writing rpath for %s.' % path_name )
222223 else :
223224 tty .die ('relocation not supported for this platform' )
224-
225+ os . chmod ( path_name , st . st_mode )
225226
226227def relocate_text (path_name , original_path , new_path ):
227228 """
0 commit comments