|
#!/usr/bin/env python3 |
|
|
|
import os |
|
import subprocess |
|
import sys |
|
import argparse |
|
|
|
def get_modules_dependencies(): |
|
modules_list = get_modules_list() |
|
modules_dependencies = {} |
|
|
|
for module in modules_list: |
|
dependencies = subprocess.getoutput(f"modinfo -F depends {module} 2>/dev/null") |
|
dep_list = dependencies.split(',') |
|
# transform any empty list from [''] to [] |
|
dep_list = [dep.strip() for dep in dep_list if dep.strip() != ''] |
|
modules_dependencies[module] = dep_list |
|
|
|
return modules_dependencies |
|
|
|
def get_modules_list(): |
|
output = subprocess.check_output("lsmod", text=True) |
|
modules_list = [line.split()[0] for line in output.splitlines()[1:]] |
|
return modules_list |
|
|
|
def find_dependents(module_name, all_modules_dependencies): |
|
dependent_modules = [] |
|
|
|
for module, dependencies in all_modules_dependencies.items(): |
|
if module_name in dependencies: |
|
dependent_modules.append(module) |
|
|
|
if not dependent_modules: |
|
pass |
|
#print(f"No modules depend on {module_name}") |
|
else: |
|
for dep_module in dependent_modules: |
|
print(f"- {dep_module}") |
|
|
|
def find_dependents_all(module_name, all_modules_dependencies, indent=""): |
|
dependent_modules = [] |
|
|
|
for module, dependencies in all_modules_dependencies.items(): |
|
if module_name in dependencies: |
|
dependent_modules.append(module) |
|
|
|
if dependent_modules: |
|
for dep in dependent_modules: |
|
print(f"{indent}- {dep}") |
|
find_dependents_all(dep, all_modules_dependencies, indent + " ") |
|
|
|
def find_dependencies(module_name, all_modules_dependencies, indent=""): |
|
dependencies = all_modules_dependencies.get(module_name) |
|
|
|
if dependencies: |
|
for dep in dependencies: |
|
print(f"{indent}- {dep}") |
|
|
|
def find_all_dependencies(module_name, all_modules_dependencies, indent=""): |
|
dependencies = all_modules_dependencies.get(module_name) |
|
|
|
if dependencies: |
|
for dep in dependencies: |
|
print(f"{indent}- {dep}") |
|
find_all_dependencies(dep, all_modules_dependencies, indent + " ") |
|
|
|
def main(): |
|
parser = argparse.ArgumentParser(description="Find module dependencies.") |
|
parser.add_argument("module_name", help="The name of the module to analyze") |
|
parser.add_argument("--reverse", action="store_true", help="Show reverse dependencies") |
|
parser.add_argument("--all", action="store_true", help="Show all dependencies") |
|
|
|
args = parser.parse_args() |
|
|
|
module_name = args.module_name |
|
|
|
all_modules_dependencies = get_modules_dependencies() |
|
|
|
if args.reverse and args.all: |
|
find_dependents_all(module_name, all_modules_dependencies) |
|
|
|
if args.reverse: |
|
find_dependents(module_name, all_modules_dependencies) |
|
elif args.all: |
|
find_all_dependencies(module_name, all_modules_dependencies) |
|
else: |
|
find_dependencies(module_name, all_modules_dependencies) |
|
|
|
if __name__ == "__main__": |
|
main() |