2023-05-01 15:25:37 +00:00
|
|
|
"""This is used by bin/codemod/remove_all_unused_functions to remove a function from a file."""
|
2023-04-29 02:29:46 +00:00
|
|
|
from bowler import Query
|
|
|
|
|
|
|
|
# This came about because vulture (actually dead, from the list of Similar programs at https://pypi.org/project/vulture/)
|
|
|
|
# actually found unused stuff, and I wanted to remove it.
|
|
|
|
# See also https://github.com/craigds/decrapify
|
|
|
|
|
2023-05-01 15:25:37 +00:00
|
|
|
|
2023-04-29 02:29:46 +00:00
|
|
|
def remove_function(filename: str, function_name: str) -> None:
|
2023-05-01 15:25:37 +00:00
|
|
|
"""Does the dirty work of actually removing the function from the file in place, or failing if it cannot."""
|
2023-04-29 02:29:46 +00:00
|
|
|
|
|
|
|
def remove_statement(node, capture, filename):
|
|
|
|
node.remove()
|
|
|
|
|
2023-05-01 15:25:37 +00:00
|
|
|
bowler_query = (
|
|
|
|
Query(filename)
|
2023-04-29 02:29:46 +00:00
|
|
|
.select_function(function_name)
|
|
|
|
.modify(remove_statement)
|
2023-05-01 15:25:37 +00:00
|
|
|
.execute(write=True, silent=True, interactive=False)
|
|
|
|
)
|
2023-04-29 02:29:46 +00:00
|
|
|
|
|
|
|
if len(bowler_query.exceptions) > 0:
|
|
|
|
print(f"Failed to remove function {function_name} from {filename}.")
|
|
|
|
raise Exception(bowler_query.exceptions[0])
|
|
|
|
|
|
|
|
print(f"Function {function_name} successfully removed from {filename}.")
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import argparse
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description="Remove a function from a Python file while preserving comments.")
|
|
|
|
parser.add_argument("filename", help="the file to modify")
|
|
|
|
parser.add_argument("function_name", help="the name of the function to remove")
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
remove_function(args.filename, args.function_name)
|