Overview
Theutils module provides essential utilities for parsing error stacks, building dependency graphs, and managing file relationships in Python projects.
Functions
parse_error_stack
Parses Python error traceback strings to extract all unique file paths involved in the error.Signature
Parameters
The full error stack trace as a string
Returns
A list of unique file paths involved in the error, in order of appearance. Only includes paths that exist on the filesystem.
Example Usage
build_adjacency_list
Builds a dependency graph (adjacency list) showing import relationships between Python files.Signature
Parameters
List of Python file paths to analyze for import relationships
Root directory of the project to ensure valid paths and scope analysis
Returns
An adjacency list where each key is a file path and its value is a list of file paths it imports. Recursively processes all imported files within the project.
How It Works
- Uses Python’s
astmodule to parse source files - Extracts
importandfrom ... importstatements - Resolves module names to file paths
- Recursively processes imported files
- Handles syntax errors gracefully by continuing analysis
Example Usage
get_nth_related_files
Finds all files related to a set of starting files through any degree of connection in the dependency graph.Signature
Parameters
The files to start with for finding related files
The adjacency list representing relationships between files (from
build_adjacency_list)Returns
A set of all files related to
start_files through any degree of connection (breadth-first traversal)Example Usage
run_mock_repopack
Reads file contents and formats them into a consolidated string representation.Signature
Parameters
List of file paths to be processed
Output style (currently not used in implementation, reserved for future use)
Returns
A string containing all file contents with headers and separators:
Example Usage
is_project_file
Checks if a file path is within the project root directory.Signature
Parameters
File path to check
Root directory of the project
Returns
True if the file is within the project root, False otherwiseExample Usage
kill_process_on_port
Kills any process running on the specified port (Unix/Linux/macOS only).Signature
Parameters
Port number to check and kill process on
Example Usage
Notes
- Uses
lsofto find process ID - Uses
os.kill()withSIGKILLto terminate - Silently returns if no process found
- Unix/Linux/macOS only (requires
lsofcommand)
Complete Workflow Example
Notes
- All file operations gracefully handle missing or invalid files
build_adjacency_listuses AST parsing to safely analyze Python syntax- Syntax errors in source files don’t stop the analysis
- File paths are resolved relative to project root and current directory
- The module handles both absolute and relative import paths
- External library imports (not in project) are safely ignored