Advertisement
Guest User

Untitled

a guest
Jan 30th, 2014
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.50 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import sys
  4. import itertools
  5. import subprocess
  6. import re
  7. import pprint
  8. import json
  9.  
  10. def ldd(executable):
  11.     p = subprocess.Popen(["ldd", executable], stdout = subprocess.PIPE)
  12.     values = p.stdout.read()
  13.     libs = [i.split("\t")[1] for i in values.split("\n")[:-1]]    
  14.     return libs
  15.  
  16. def parse(libs):
  17.     pathes = []
  18.     print("\n\n\nLinker defined such shared libraries:")
  19.     for lib in libs:
  20.         print(lib)
  21.         try:
  22.             match = re.search('=>(.*)\(', lib)
  23.             path = match.group(1).strip()
  24.         except Exception, e:
  25.             print("Failed to parse: {0} {1}".format(lib, e))
  26.         else:
  27.             pathes.append(path)
  28.     #print(pathes)
  29.     return pathes
  30.  
  31. def dependence(pathes):
  32.     packages = {}
  33.     for path in pathes:
  34.         p = subprocess.Popen(["rpm", "-q", "--whatprovides", path], stdout = subprocess.PIPE)
  35.         package = p.stdout.read().strip()
  36.         if package == "no package provides":
  37.             print("{0} - no such package".format(path))
  38.         packages[path] = package
  39.     return packages
  40.  
  41. def packages_only(packages):
  42.     print("\n\n\nList of unique packages:")
  43.     unique = sorted(set(packages.values()))
  44.     pprint.pprint(unique)
  45.     return unique
  46.  
  47. if __name__ == "__main__":
  48.     libs = ldd(sys.argv[1])
  49.     pathes = parse(libs)
  50.     packages = dependence(pathes)
  51.     unique = packages_only(packages)
  52.     with open(sys.argv[1].split('/')[-1],"w") as f:
  53.         json.dump(unique, f, indent = 4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement