|
| 1 | +#!/usr/bin/env python |
| 2 | +import sys |
| 3 | +import os |
| 4 | +import shutil |
| 5 | +import pkgutil |
| 6 | +import importlib |
| 7 | +import collections |
| 8 | + |
| 9 | +# Name this file (module) |
| 10 | +this_module_name = os.path.basename(__file__).rsplit('.')[0] |
| 11 | + |
| 12 | +# Dict for loaders with their modules |
| 13 | +loaders = collections.OrderedDict() |
| 14 | + |
| 15 | +# Names's of build-in modules |
| 16 | +for module_name in sys.builtin_module_names: |
| 17 | + |
| 18 | + # Find an information about a module by name |
| 19 | + module = importlib.util.find_spec(module_name) |
| 20 | + |
| 21 | + # Add a key about a loader in the dict, if not exists yet |
| 22 | + if module.loader not in loaders: |
| 23 | + loaders[module.loader] = [] |
| 24 | + |
| 25 | + # Add a name and a location about imported module in the dict |
| 26 | + loaders[module.loader].append((module.name, module.origin)) |
| 27 | + |
| 28 | +# All available non-build-in modules |
| 29 | +for module_name in pkgutil.iter_modules(): |
| 30 | + |
| 31 | + # Ignore this module |
| 32 | + if this_module_name == module_name[1]: |
| 33 | + continue |
| 34 | + |
| 35 | + # Find an information about a module by name |
| 36 | + module = importlib.util.find_spec(module_name[1]) |
| 37 | + |
| 38 | + # Add a key about a loader in the dict, if not exists yet |
| 39 | + loader = type(module.loader) |
| 40 | + if loader not in loaders: |
| 41 | + loaders[loader] = [] |
| 42 | + |
| 43 | + # Add a name and a location about imported module in the dict |
| 44 | + loaders[loader].append((module.name, module.origin)) |
| 45 | + |
| 46 | +# Pretty print |
| 47 | +# line = '-' * shutil.get_terminal_size().columns |
| 48 | +# for loader, modules in loaders.items(): |
| 49 | +# print('{0}\n{1}: {2}\n{0}'.format(line, len(modules), loader)) |
| 50 | +# for module in modules: |
| 51 | +# print('{0:30} | {1}'.format(module[0], module[1])) |
| 52 | + |
| 53 | + |
0 commit comments