diff --git a/pythonbpf/vmlinux_parser/dependency_handler.py b/pythonbpf/vmlinux_parser/dependency_handler.py index 4a3574e..fb49b00 100644 --- a/pythonbpf/vmlinux_parser/dependency_handler.py +++ b/pythonbpf/vmlinux_parser/dependency_handler.py @@ -1,5 +1,5 @@ from typing import Optional, Dict, List, Iterator -from dependency_node import DependencyNode +from .dependency_node import DependencyNode class DependencyHandler: diff --git a/pythonbpf/vmlinux_parser/import_detector.py b/pythonbpf/vmlinux_parser/import_detector.py index 9aff52d..a91884f 100644 --- a/pythonbpf/vmlinux_parser/import_detector.py +++ b/pythonbpf/vmlinux_parser/import_detector.py @@ -1,8 +1,11 @@ import ast import logging -from typing import List, Tuple +from typing import List, Tuple, Dict import importlib import inspect + +from .dependency_handler import DependencyHandler +from .ir_generation import IRGenerator from .vmlinux_class_handler import process_vmlinux_class logger = logging.getLogger(__name__) @@ -75,6 +78,11 @@ def detect_import_statement(tree: ast.AST) -> List[Tuple[str, ast.ImportFrom]]: def vmlinux_proc(tree: ast.AST, module): import_statements = detect_import_statement(tree) + # initialise dependency handler + handler = DependencyHandler() + # initialise assignment dictionary of name to type + assignments: Dict[str, type] = {} + if not import_statements: logger.info("No vmlinux imports found") return @@ -100,13 +108,13 @@ def vmlinux_proc(tree: ast.AST, module): found = False for mod_node in mod_ast.body: if isinstance(mod_node, ast.ClassDef) and mod_node.name == imported_name: - process_vmlinux_class(mod_node, module) + process_vmlinux_class(mod_node, module, handler) found = True break if isinstance(mod_node, ast.Assign): for target in mod_node.targets: if isinstance(target, ast.Name) and target.id == imported_name: - process_vmlinux_assign(mod_node, module) + process_vmlinux_assign(mod_node, module, assignments) found = True break if found: @@ -114,5 +122,7 @@ def vmlinux_proc(tree: ast.AST, module): if not found: logger.info(f"{imported_name} not found as ClassDef or Assign in vmlinux") -def process_vmlinux_assign(node, module): + IRGenerator(module, handler) + +def process_vmlinux_assign(node, module, assignments: Dict[str, type]): raise NotImplementedError("Assignment handling has not been implemented yet") diff --git a/pythonbpf/vmlinux_parser/ir_generation.py b/pythonbpf/vmlinux_parser/ir_generation.py index e83efaa..470b6d8 100644 --- a/pythonbpf/vmlinux_parser/ir_generation.py +++ b/pythonbpf/vmlinux_parser/ir_generation.py @@ -1 +1,8 @@ -# here, we will iterate through the dependencies and generate IR once dependencies are resolved fully \ No newline at end of file +# here, we will iterate through the dependencies and generate IR once dependencies are resolved fully +from .dependency_handler import DependencyHandler + + +class IRGenerator: + def __init__(self, module, handler): + self.module = module + self.handler: DependencyHandler = handler diff --git a/pythonbpf/vmlinux_parser/vmlinux_class_handler.py b/pythonbpf/vmlinux_parser/vmlinux_class_handler.py index e156bb5..24c9949 100644 --- a/pythonbpf/vmlinux_parser/vmlinux_class_handler.py +++ b/pythonbpf/vmlinux_parser/vmlinux_class_handler.py @@ -2,6 +2,8 @@ import ast import logging from functools import lru_cache import importlib +from .dependency_handler import DependencyHandler +from .dependency_node import DependencyNode logger = logging.getLogger(__name__) @@ -10,6 +12,12 @@ def get_module_symbols(module_name: str): module = importlib.import_module(module_name) return [name for name in dir(module)] -def process_vmlinux_class(node, module, num=0): +def process_vmlinux_class(node, module, handler: DependencyHandler, parent=""): symbols_in_module = get_module_symbols("vmlinux") + current_symbol_name = node.name + if current_symbol_name not in symbols_in_module: + raise ImportError(f"{current_symbol_name} not present in module vmlinux") + logger.info(f"Resolving vmlinux class {current_symbol_name}") + # Now we find fields present in current class + pass