Skip to content

Instantly share code, notes, and snippets.

@elsonidoq
Last active November 28, 2016 03:56
Show Gist options
  • Save elsonidoq/6954369 to your computer and use it in GitHub Desktop.
Save elsonidoq/6954369 to your computer and use it in GitHub Desktop.
Find dependencies of .so files on linux machines, and copies them on a specified directory. It is very simple, and does not have a nice heuristic for picking upon different versions of the same file. However, you can use the option --skip-patterns to add a comma sepparated list of strings that should not appear on the path of the file (you can f…
import shutil
from optparse import OptionParser
import sys
import functools
import re
import subprocess
import os
READELF = '/usr/bin/readelf'
def get_dependencies(fname, skip_patterns):
cmd = '%s -d %s' % (READELF, fname)
p = subprocess.Popen(cmd.split(), stdout=-1)
stdout = p.stdout.read()
pat = re.compile('\[(?P<lib>.*?)\]')
res = []
for line in stdout.split('\n'):
if 'NEEDED' not in line: continue
lib = pat.search(line).groupdict()['lib']
res.append(locate_fname(lib, skip_patterns))
return res
def memoize(obj):
cache = obj.cache = {}
@functools.wraps(obj)
def memoizer(*args, **kwargs):
if args not in cache:
cache[args] = obj(*args, **kwargs)
return cache[args]
return memoizer
@memoize
def locate_fname(fname, skip_patterns):
cmd = '/usr/bin/locate %s' % os.path.basename(fname)
p = subprocess.Popen(cmd.split(), stdout=-1)
stdout = p.stdout.read()
matches= stdout.split('\n')
matches.sort()
candidates = []
for e in matches:
if os.path.basename(e) == os.path.basename(fname):
for pat in skip_patterns:
if pat in e: break
else:
candidates.append(e)
if len(candidates) > 1:
print "WARNING: multiple candidates for %s" % fname
for c in candidates: print "\t%s" % c
elif len(candidates) == 0:
raise Exception("No candidates for %s" % fname)
return candidates[0]
def get_dependencies_clique(fnames, skip_patterns):
res = set(fnames)
while True:
new = set()
for e in res:
new.update(get_dependencies(e, skip_patterns))
if new.issubset(res):
return res
else:
print "found new deps:"
for e in new:
if e in res: continue
print "\t%s" % e
res.update(new)
def main():
parser = OptionParser()
parser.add_option("-o", "--output", dest="output",
help="output dir", default='libs')
parser.add_option("-s", "--skip", dest="skip",
help="skip patterns, comma separated", default='')
options, fnames = parser.parse_args()
out_dir = options.output
if os.path.exists(out_dir):
print "Output dir already exists"
ans = raw_input("Delete? [Y/n] ")
if ans.lower().strip() == 'n':return
shutil.rmtree(out_dir)
if not os.path.exists(out_dir): os.makedirs(out_dir)
skip_patterns = options.skip.split(',')
skip_patterns = [e.strip() for e in skip_patterns]
skip_patterns = [e for e in skip_patterns if len(e) > 0]
skip_patterns = tuple(skip_patterns)
deps = get_dependencies_clique(fnames, skip_patterns)
for dep in deps:
shutil.copy(dep, out_dir)
if __name__ == '__main__': main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment