Guest User

Untitled

a guest
May 21st, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.65 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. #import subprocess
  4. import envoy
  5. import re
  6. import os
  7.  
  8. debline = re.compile(r"^deb https?://([^\s]+)\s*([^\s]+)\s*((main|contrib|non-free)\s*)*$")
  9.  
  10. def read_repos_from(path):
  11.         if os.path.isdir(path):
  12.                 for i in os.listdir(path):
  13.                         read_repos_from(path + '/' + i)
  14.                 return
  15.  
  16.         f = file(path, 'r')
  17.         for line in f:
  18.                 m = debline.match(line)
  19.                 if m:
  20.                         if(m.group(2)[-1] == '/'):
  21.                                 yield ('_'.join(m.groups()[0:2]), m.group(1))
  22.                         else:
  23.                                 yield ('_dists_'.join(m.groups()[0:2]), m.group(2))
  24.  
  25. repos = [i for i in read_repos_from('/etc/apt/sources.list')]
  26. repos.extend([i for i in read_repos_from('/etc/apt/sources.list.d')])
  27.  
  28. reponames = []
  29.  
  30. for (path, name) in repos:
  31.         path = path.replace('/','_').replace('__','_')
  32.         if path[-1] == '_':
  33.                 path = path[0:-1]
  34.         reponames.append((path, name))
  35.  
  36. packages = envoy.run('dpkg -l')
  37.  
  38. pkgs = []
  39.  
  40. for line in packages.std_out.splitlines()[5:]:
  41.         pkgs.append(line.split()[1:3])
  42.  
  43.  
  44. for i in pkgs:
  45.         s = envoy.run("apt-cache showpkg " + i[0]).std_out
  46.         l = s.index(i[1])
  47.         r = s.index("\n", l)
  48.         pkg_instance = s[l:r]
  49.  
  50.         l = pkg_instance.index('(')
  51.         r = pkg_instance.index(')',l)
  52.         pkg_path = pkg_instance[pkg_instance.rindex('/',l,r) + 1:r]
  53.         for (path, name) in reponames:
  54.                 if pkg_path.find(path,0, len(path)) == 0:
  55.                         print(i[0] + " " + i[1] + " from " + name)
Add Comment
Please, Sign In to add comment