Skip to content

Instantly share code, notes, and snippets.

@baitisj
Created December 3, 2018 04:14
Show Gist options
  • Save baitisj/2ab196d7bfad6728bafbed78a99c4555 to your computer and use it in GitHub Desktop.
Save baitisj/2ab196d7bfad6728bafbed78a99c4555 to your computer and use it in GitHub Desktop.
FreeBSD: Inspect all libraries passed on command line and find broken ones -- and report a list of packages that might need rebuilding
import subprocess;
import sys;
import re;
import pprint;
import string;
pkgs = set(); # Packages that contain the broken libraries
d = dict(); # contains key: library name, and list of values: libraries that are not found.
nf_rgx=re.compile('^\s+(.+) =>.+\(0\)'); # Matches a 'not found' result from ldd
last_key="";
val="";
def pkgwhich(filename):
p = subprocess.Popen(["pkg", "which", "-q", filename],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE);
for l in iter(p.stdout.readline, b''):
line = l.decode('utf-8');
itm=line.rstrip();
pkgs.add(itm);
def ldd(filename):
libs = []
for x in filename:
p = subprocess.Popen(["ldd", x],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE);
for l in iter(p.stdout.readline, b''):
line = l.decode('utf-8');
# print (line);
nm = nf_rgx.match(line);
if nm:
pkgwhich(x);
val=nm.group(1);
if x in d:
d[x].append(val);
else:
d[x] = [val];
if __name__ == "__main__":
if len(sys.argv) <= 1:
print("Iterates over files on command line and prints broken ldd links. Usage: %s filename1 filename2 ..."%(sys.argv[0]));
else:
ldd(sys.argv[1:]);
pp = pprint.PrettyPrinter(indent=2);
pp.pprint(d);
print("Consider rebuilding the following packages:");
pp.pprint(pkgs);
# Example usage:
# cd /usr/local/lib
# find . -name "*"so | python3.6 path/to/find_broken_library_packages.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment