1# This script is launched on separate task for each python module
2# It checks for dependencies for that specific module and prints
3# them out, the output of this execution will have all dependencies
4# for a specific module, which will be parsed an dealt on create_manifest.py
5#
6# Author: Alejandro Enedino Hernandez Samaniego "aehs29" <aehs29@gmail.com>
7
8# We can get a log per module, for all the dependencies that were found, but its messy.
9debug=False
10
11import sys
12import os
13
14# We can get a list of the modules which are currently required to run python
15# so we run python-core and get its modules, we then import what we need
16# and check what modules are currently running, if we substract them from the
17# modules we had initially, we get the dependencies for the module we imported.
18
19# We use importlib to achieve this, so we also need to know what modules importlib needs
20import importlib
21
22core_deps=set(sys.modules)
23
24def fix_path(dep_path):
25    import os
26    # We DONT want the path on our HOST system
27    pivot='recipe-sysroot-native'
28    dep_path=dep_path[dep_path.find(pivot)+len(pivot):]
29
30    if '/usr/bin' in dep_path:
31        dep_path = dep_path.replace('/usr/bin''${bindir}')
32
33    # Handle multilib, is there a better way?
34    if '/usr/lib32' in dep_path:
35        dep_path = dep_path.replace('/usr/lib32','${libdir}')
36    if '/usr/lib64' in dep_path:
37        dep_path = dep_path.replace('/usr/lib64','${libdir}')
38    if '/usr/lib' in dep_path:
39        dep_path = dep_path.replace('/usr/lib','${libdir}')
40    if '/usr/include' in dep_path:
41        dep_path = dep_path.replace('/usr/include','${includedir}')
42    if '__init__.' in dep_path:
43        dep_path =  os.path.split(dep_path)[0]
44    return dep_path
45
46
47# Module to import was passed as an argument
48current_module =  str(sys.argv[1]).rstrip()
49if(debug==True):
50    log = open('log_%s' % current_module,'w')
51    log.write('Module %s generated the following dependencies:\n' % current_module)
52try:
53    m = importlib.import_module(current_module)
54    # handle python packages which may not include all modules in the __init__
55    if os.path.basename(m.__file__) == "__init__.py":
56        modulepath = os.path.dirname(m.__file__)
57        for i in os.listdir(modulepath):
58            if i.startswith("_") or not(i.endswith(".py")):
59                continue
60            submodule = "{}.{}".format(current_module, i[:-3])
61            try:
62                importlib.import_module(submodule)
63            except:
64                pass # ignore all import or other exceptions raised during import
65except ImportError as e:
66    if (debug==True):
67        log.write('Module was not found')
68    pass
69
70
71# Get current module dependencies, dif will contain a list of specific deps for this module
72module_deps=set(sys.modules)
73
74# We handle the core package (1st pass on create_manifest.py) as a special case
75if current_module == 'python-core-package':
76    dif = core_deps
77else:
78    # We know this is not the core package, so there must be a difference.
79    dif = module_deps-core_deps
80
81
82# Check where each dependency came from
83for item in dif:
84    dep_path=''
85    try:
86        if (debug==True):
87            log.write('Calling: sys.modules[' + '%s' % item + '].__file__\n')
88        dep_path = sys.modules['%s' % item].__file__
89    except AttributeError as e:
90        # Deals with thread (builtin module) not having __file__ attribute
91        if debug==True:
92            log.write(item + ' ')
93            log.write(str(e))
94            log.write('\n')
95        pass
96    except NameError as e:
97        # Deals with NameError: name 'dep_path' is not defined
98        # because module is not found (wasn't compiled?), e.g. bddsm
99        if (debug==True):
100            log.write(item+' ')
101            log.write(str(e))
102        pass
103
104    # Site-customize is a special case since we (OpenEmbedded) put it there manually
105    if 'sitecustomize' in dep_path:
106        dep_path = '${libdir}/python${PYTHON_MAJMIN}/sitecustomize.py'
107        # Prints out result, which is what will be used by create_manifest
108        print (dep_path)
109        continue
110
111    dep_path = fix_path(dep_path)
112
113    import sysconfig
114    soabi=sysconfig.get_config_var('SOABI')
115    # Check if its a shared library and deconstruct it
116    if soabi in dep_path:
117        if (debug==True):
118            log.write('Shared library found in %s' % dep_path)
119        dep_path = dep_path.replace(soabi,'*')
120        print (dep_path)
121        continue
122    if "_sysconfigdata" in dep_path:
123        dep_path = dep_path.replace(sysconfig._get_sysconfigdata_name(), "_sysconfigdata*")
124
125    if (debug==True):
126        log.write(dep_path+'\n')
127    # Prints out result, which is what will be used by create_manifest
128    print (dep_path)
129
130
131    import imp
132    cpython_tag = imp.get_tag()
133    cached=''
134    # Theres no naive way to find *.pyc files on python3
135    try:
136        if (debug==True):
137            log.write('Calling: sys.modules[' + '%s' % item + '].__cached__\n')
138        cached = sys.modules['%s' % item].__cached__
139    except AttributeError as e:
140        # Deals with thread (builtin module) not having __cached__ attribute
141        if debug==True:
142            log.write(item + ' ')
143            log.write(str(e))
144            log.write('\n')
145        pass
146    except NameError as e:
147        # Deals with NameError: name 'cached' is not defined
148        if (debug==True):
149            log.write(item+' ')
150            log.write(str(e))
151        pass
152    if cached is not None:
153        if (debug==True):
154            log.write(cached)
155        cached = fix_path(cached)
156        cached = cached.replace(cpython_tag,'*')
157        if "_sysconfigdata" in cached:
158            cached = cached.replace(sysconfig._get_sysconfigdata_name(), "_sysconfigdata*")
159        print (cached)
160
161if debug==True:
162    log.close()
163