summaryrefslogtreecommitdiffstats
path: root/dracut/python-deps
blob: be51e78077a73ce4ee14f573776076be442ddfff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/python
# python-deps - find the dependencies of a given python script.

import os, sys
from modulefinder import ModuleFinder
from distutils.sysconfig import *

sitedir = get_python_lib()
libdir = get_config_var('LIBDEST')

# A couple helper functions...
def moduledir(pyfile):
    '''Given a python file, return the module dir it belongs to, or None.'''
    for topdir in sitedir, libdir:
        relpath = os.path.relpath(pyfile, topdir)
        if '/' not in relpath: continue
        modname = relpath.split('/')[0]
        if modname not in ('..', 'site-packages'):
            return os.path.join(topdir, modname)

def pyfiles(moddir):
    '''basically, "find $moddir -type f -name "*.py"'''
    for curdir, dirs, files in os.walk(moddir):
        for f in files:
            if f.endswith(".py"):
                yield os.path.join(curdir, f)

# OK. Use modulefinder to find all the modules etc. this script uses!

finder = ModuleFinder()
finder.run_script(sys.argv[1]) # parse the script

mods = []
deps = []

for name, mod in finder.modules.iteritems():
    if not mod.__file__: # this module is builtin, so we can skip it
        continue

    if mod.__file__ not in deps: # grab the file itself
        deps.append(mod.__file__)

    moddir = moduledir(mod.__file__)  # if it's part of a module...
    if moddir and moddir not in mods: #
        deps += list(pyfiles(moddir)) # ...get the whole module
        mods.append(moddir)

# Include some bits that the python install itself needs
print get_makefile_filename()
print get_config_h_filename()
print os.path.join(libdir,'site.py')
print os.path.join(libdir,'sysconfig.py')

# And print the list of deps.
for d in deps:
    print d