debpydep.py 1.27 KB
Newer Older
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
from __future__ import print_function
4 5 6 7 8 9 10 11 12 13 14 15 16

import os
import subprocess
import sys

selection = set()
dependencies = set()

for root, dirs, files in os.walk(sys.argv[1]):
    for f in files:
        if f.endswith('.py') and f != '__init__.py':
            s = "from %s import %s" % (root.strip('/').replace('/', '.'), f[:-3])
            try:
17
                exec(s)
18
            except ImportError as e:
19
                print(str(e), file=sys.stderr)
20 21 22 23 24 25 26 27 28 29 30
            else:
                m = eval(f[:-3])
                for attrname in dir(m):
                    try:
                        attr = getattr(m, attrname)
                        selection.add(attr.__file__)
                    except AttributeError:
                        pass
for f in selection:
    f = f.replace('.pyc', '.py')
    try:
31
        f = os.path.abspath(os.path.join(os.path.split(f)[0], os.readlink(f)))
32 33 34 35 36 37
    except OSError:
        pass

    p = subprocess.Popen(['/usr/bin/dpkg', '-S', f], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    if p.wait() == 0:
        for line in p.stdout.readlines():
38
            line = line.decode('utf-8')
39 40
            dependencies.add(line.strip().split(':')[0])
    else:
41
        print('not found: %s' % f)
42 43

for d in dependencies:
44
    print(d)