import sysimport osimport shutilimport pkgutilimport importlibimport collections
if sys.version_info.major == 2:raise NotImplementedError('CPython 2 is not supported yet')
def main():
# name this file (module)this_module_name = os.path.basename(__file__).rsplit('.')[0]
# dict for loaders with their modulesloaders = collections.OrderedDict()
# names`s of build-in modulesfor module_name in sys.builtin_module_names:
# find an information about a module by namemodule = importlib.util.find_spec(module_name)
# add a key about a loader in the dict, if not exists yetif module.loader not in loaders:loaders[module.loader] = []
# add a name and a location about imported module in the dictloaders[module.loader].append((module.name, module.origin))
# all available non-build-in modulesfor module_name in pkgutil.iter_modules():
# ignore this moduleif this_module_name == module_name[1]:continue
# find an information about a module by namemodule = importlib.util.find_spec(module_name[1])
# add a key about a loader in the dict, if not exists yetloader = type(module.loader)if loader not in loaders:loaders[loader] = []
# add a name and a location about imported module in the dictloaders[loader].append((module.name, module.origin))
# pretty printline = '-' * shutil.get_terminal_size().columnsfor loader, modules in loaders.items():print('{0}\n{1}: {2}\n{0}'.format(line, len(modules), loader))for module in modules:print('{0:30} | {1}'.format(module[0], module[1]))
if __name__ == '__main__':main()
import pkgutil
__version__ = '0.1.1'
def get_ver(name):try:return str(__import__(name).__version__)except:return None
def lambda_handler(event, context):return {'statusCode': 200,'body': [{'path': m.module_finder.path,'name': m.name,'version': get_ver(m.name),} for m in list(pkgutil.iter_modules())#if m.module_finder.path == "/var/runtime" # Uncomment this if you only care about a certain path],}