Blame SOURCES/remove-origin-from-rpaths

0a1101
#!/usr/bin/python
0a1101
0a1101
# Usage: $0 file1 file2 ....
0a1101
#
0a1101
# Given binary files, remove all rpath entries from them containing
0a1101
# $ORIGIN. Other rpath entries are not modified
0a1101
0a1101
import subprocess;
0a1101
import sys;
0a1101
0a1101
def print_usage(name):
0a1101
    print '''Usage: %s file1 file2 ....
0a1101
0a1101
    Given binary files, remove all rpath entries from them containing
0a1101
    $ORIGIN. Other rpath entries are not modified''' % (name,)
0a1101
0a1101
def call(args):
0a1101
    pop = subprocess.Popen(args, bufsize=-1, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
0a1101
    stdout, stderr = pop.communicate()
0a1101
    if (len(stderr.strip())) != 0:
0a1101
        print stderr
0a1101
    if pop.returncode != 0:
0a1101
        raise OSError('Error calling %s' % (str(args),))
0a1101
    return stdout
0a1101
0a1101
def check_chrpath_present():
0a1101
    try:
0a1101
        print(call(['/usr/bin/chrpath', '-v']))
0a1101
        return True
0a1101
    except OSError:
0a1101
        return False
0a1101
0a1101
def main(args):
0a1101
    binaries = args[1:]
0a1101
    if len(binaries) == 0:
0a1101
        print_usage(args[0])
0a1101
        return 1
0a1101
0a1101
    if not check_chrpath_present():
0a1101
        print('Could not execute "chrpath". Is it installed?')
0a1101
        return 1
0a1101
0a1101
    for binary in binaries:
0a1101
        try:
0a1101
            rpath_output = call(['chrpath', '-l', binary])
0a1101
        except OSError:
0a1101
            continue
0a1101
0a1101
        #print '"' + rpath_output + '"'
0a1101
        # the output is "file: RUNPATH=path1:path2:path3\n"
0a1101
        rpath_output = rpath_output.strip()
0a1101
        start = rpath_output.find('RPATH=')
0a1101
        rpath = rpath_output[start+len('RPATH='):]
0a1101
        rpath_parts = rpath.split(':')
0a1101
        modified_rpath = ''
0a1101
        for part in rpath_parts:
0a1101
            if not '$ORIGIN' in part:
0a1101
                modified_rpath = modified_rpath + ':' + part
0a1101
        # shave off the last ':'
0a1101
        modified_rpath = modified_rpath[1:]
0a1101
        #print '"' + modified_rpath + '"'
0a1101
        if len(modified_rpath) == 0:
0a1101
            call(['chrpath', '-d', binary])
0a1101
            print '%s: Deleted RPATH' % (binary,)
0a1101
        else:
0a1101
            call(['chrpath', '-r', modified_rpath, binary])
0a1101
            print '%s: RPATH=%s' % (binary,modified_rpath)
0a1101
    return 0
0a1101
0a1101
if __name__ == '__main__':
0a1101
    exit(main(sys.argv))