From 0cdd6faa30c18009c742ca57edc27869ec8c9a56 Mon Sep 17 00:00:00 2001 From: Vishal Shenoy Date: Fri, 7 Feb 2025 17:40:52 -0800 Subject: [PATCH 1/4] . --- examples/delete_dead_code/README.md | 61 +++++++++++++++++++++++++++++ examples/delete_dead_code/run.py | 42 ++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 examples/delete_dead_code/README.md create mode 100644 examples/delete_dead_code/run.py diff --git a/examples/delete_dead_code/README.md b/examples/delete_dead_code/README.md new file mode 100644 index 0000000..00d539c --- /dev/null +++ b/examples/delete_dead_code/README.md @@ -0,0 +1,61 @@ +# Delete Dead Code + +This example demonstrates how to identify and remove dead code from a codebase using Codegen. The script efficiently cleans up unused functions and variables, helping maintain a lean and efficient codebase. + +> [!NOTE] +> Dead code refers to code that is not being used or referenced anywhere in your codebase. However, some code might appear unused but should not be deleted, such as test files, functions with decorators, public API endpoints, and event handlers. + +## How the Dead Code Removal Script Works + +The script (`run.py`) performs the dead code removal in several key steps: + +1. **Codebase Loading** + ```python + codebase = Codebase.from_repo("tox-dev/tox", programming_language=ProgrammingLanguage.PYTHON) + ``` + - Loads a codebase using the `Codebase.from_repo` method + - This example uses the `tox-dev/tox` repository because it is mostly self-contained + +2. **Function Removal** + ```python + for function in codebase.functions: + if "test" in function.file.filepath: + continue + if function.decorators: + continue + if not function.usages and not function.call_sites: + print(f"๐Ÿ—‘๏ธ Removing unused function: {function.name}") + function.remove() + ``` + - Skips test files and decorated functions + - Removes functions with no usages or call sites + +3. **Variable Cleanup** + ```python + for func in codebase.functions: + for var_assignments in func.code_block.local_var_assignments: + if not var_assignments.local_usages: + print(f"๐Ÿงน Removing unused variable: {var_assignments.name}") + var_assignments.remove() + ``` + - Iterates through local variable assignments + - Removes variables with no local usages + +## Running the Script + +```bash +# Install Codegen +pip install codegen + +# Run the script +python run.py +``` + +## Learn More + +- [Deleting Dead Code](https://docs.codegen.com/tutorials/deleting-dead-code) +- [Codegen Documentation](https://docs.codegen.com) + +## Contributing + +Feel free to submit issues and enhancement requests! diff --git a/examples/delete_dead_code/run.py b/examples/delete_dead_code/run.py new file mode 100644 index 0000000..0c68c8d --- /dev/null +++ b/examples/delete_dead_code/run.py @@ -0,0 +1,42 @@ +import codegen +from codegen import Codebase +from codegen.sdk.enums import ProgrammingLanguage + +@codegen.function("delete-dead-code") +def run(codebase: Codebase): + removed_functions_count = 0 + removed_variables_count = 0 + + for function in codebase.functions: + # Skip test files + if "test" in function.file.filepath: + continue + + # Skip decorated functions + if function.decorators: + continue + + # Check if the function has no usages and no call sites + if not function.usages and not function.call_sites: + print(f"๐Ÿ—‘๏ธ Removing unused function: {function.name}") + function.remove() + removed_functions_count += 1 + + # Clean up unused variables + for func in codebase.functions: + for var_assignments in func.code_block.local_var_assignments: + if not var_assignments.local_usages: + print(f"๐Ÿงน Removing unused variable: {var_assignments.name}") + var_assignments.remove() + removed_variables_count += 1 + + print(f"Total functions removed: {removed_functions_count}") + print(f"Total variables removed: {removed_variables_count}") + + +if __name__ == "__main__": + print("๐Ÿ” Analyzing codebase...") + codebase = Codebase.from_repo("tox-dev/tox", programming_language=ProgrammingLanguage.PYTHON) + + print("๐Ÿš€ Running analysis...") + run(codebase) From e51f37c0d4f77787c9b004fdc8f87c347a5c90e9 Mon Sep 17 00:00:00 2001 From: Vishal Shenoy Date: Fri, 7 Feb 2025 17:46:56 -0800 Subject: [PATCH 2/4] . --- examples/delete_dead_code/README.md | 16 ++++++++++++++++ examples/delete_dead_code/run.py | 7 ++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/examples/delete_dead_code/README.md b/examples/delete_dead_code/README.md index 00d539c..6a05064 100644 --- a/examples/delete_dead_code/README.md +++ b/examples/delete_dead_code/README.md @@ -51,6 +51,22 @@ pip install codegen python run.py ``` +## Example Output + +``` +๏ฟฝ Deleting dead code... + +๐Ÿ—‘๏ธ Removing unused function: _get_parser_doc +๐Ÿงน Removing unused variable: decoded +๐Ÿงน Removing unused variable: shebang_line +... +๐Ÿงน Removing unused variable: _ + +๐Ÿ”ง Total functions removed: 2 +๐Ÿ“ฆ Total variables removed: 240 +``` + + ## Learn More - [Deleting Dead Code](https://docs.codegen.com/tutorials/deleting-dead-code) diff --git a/examples/delete_dead_code/run.py b/examples/delete_dead_code/run.py index 0c68c8d..f295110 100644 --- a/examples/delete_dead_code/run.py +++ b/examples/delete_dead_code/run.py @@ -30,13 +30,14 @@ def run(codebase: Codebase): var_assignments.remove() removed_variables_count += 1 - print(f"Total functions removed: {removed_functions_count}") - print(f"Total variables removed: {removed_variables_count}") + print("\n") + print(f"๐Ÿ”ง Total functions removed: {removed_functions_count}") + print(f"๐Ÿ“ฆ Total variables removed: {removed_variables_count}") if __name__ == "__main__": print("๐Ÿ” Analyzing codebase...") codebase = Codebase.from_repo("tox-dev/tox", programming_language=ProgrammingLanguage.PYTHON) - print("๐Ÿš€ Running analysis...") + print("๐Ÿšฎ Deleting dead code...") run(codebase) From 5f2e077432858896f6397217143aee39cff7007a Mon Sep 17 00:00:00 2001 From: vishalshenoy <34020235+vishalshenoy@users.noreply.github.com> Date: Sat, 8 Feb 2025 01:48:05 +0000 Subject: [PATCH 3/4] Automated pre-commit update --- examples/delete_dead_code/run.py | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/delete_dead_code/run.py b/examples/delete_dead_code/run.py index f295110..eb387d7 100644 --- a/examples/delete_dead_code/run.py +++ b/examples/delete_dead_code/run.py @@ -2,6 +2,7 @@ from codegen import Codebase from codegen.sdk.enums import ProgrammingLanguage + @codegen.function("delete-dead-code") def run(codebase: Codebase): removed_functions_count = 0 From 9ce4bf3f622fc1c4a6181672f1b87a74ad2bb2e3 Mon Sep 17 00:00:00 2001 From: Vishal Shenoy Date: Fri, 7 Feb 2025 17:50:24 -0800 Subject: [PATCH 4/4] commit --- examples/delete_dead_code/run.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/delete_dead_code/run.py b/examples/delete_dead_code/run.py index f295110..b918959 100644 --- a/examples/delete_dead_code/run.py +++ b/examples/delete_dead_code/run.py @@ -37,7 +37,7 @@ def run(codebase: Codebase): if __name__ == "__main__": print("๐Ÿ” Analyzing codebase...") - codebase = Codebase.from_repo("tox-dev/tox", programming_language=ProgrammingLanguage.PYTHON) + codebase = Codebase.from_repo("tox-dev/tox", programming_language=ProgrammingLanguage.PYTHON, commit="b588b696e0940c1813014b31b68d7660d8a1914f") print("๐Ÿšฎ Deleting dead code...") run(codebase)