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