Last active
December 26, 2023 04:38
-
-
Save 11010001101001/c5c4a5a781a8d142bd2cec055f15d203 to your computer and use it in GitHub Desktop.
Cleaner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import os | |
""" | |
Script to find dead modules and unused functions inside swift project | |
""" | |
DIR = 'abs path to your project' | |
def time_tracer(func): | |
def inner(): | |
try: | |
start = time.time() | |
func() | |
finish = time.time() | |
spent = finish - start | |
print(f'\n✅ Success! Done for {round(spent, 3)} sec.') | |
except BaseException as e: | |
print(f'\n💥 Failure! Error: {e}') | |
return inner | |
@time_tracer | |
def find_unused_modules(): | |
route_maps = {} | |
functions = set() | |
funcs_storage = {} | |
def get_route_maps(): | |
for subdir, dirs, files in os.walk(DIR): | |
for file in files: | |
path = f'{subdir}/{file}' | |
name = os.path.splitext(file) | |
_format = name[1] | |
if _format == '.swift' and 'RouteMap' in file and 'Default' not in file and 'Assembly' not in file: | |
route_maps[file] = path | |
def get_functions(): | |
nonlocal functions | |
def get_names(el): | |
if 'func ' in el: | |
no_spaces = el.strip() | |
no_func = no_spaces.replace('func ', '') | |
return no_func.split('(')[0] | |
for route_map in route_maps: | |
path = route_maps[route_map] | |
contents = open(f'{path}', 'r').readlines() | |
filtered = map(lambda el: get_names(el), contents) | |
map_functions = list(filter(lambda el: el is not None, filtered)) | |
functions.update(set(map_functions)) | |
def get_functions_usage_frequency(): | |
for subdir, dirs, files in os.walk(DIR): | |
for file in files: | |
path = f'{subdir}/{file}' | |
name = os.path.splitext(file) | |
_format = name[1] | |
if _format == '.swift': | |
contents = open(f'{path}', 'r').read() | |
for func in functions: | |
if func in contents: | |
if func not in funcs_storage.keys(): | |
funcs_storage[func] = 1 | |
else: | |
funcs_storage[func] += 1 | |
def find_garbage_functions(): | |
""" In our project unused usage count is 2: in protocol and class. If so, | |
func is not called anywhere else, thus it can be deleted | |
""" | |
print('Potentially unused modules: \n') | |
for el in funcs_storage: | |
if funcs_storage[el] <= 2: | |
print(f'{el} :', funcs_storage[el]) | |
get_route_maps() | |
get_functions() | |
get_functions_usage_frequency() | |
find_garbage_functions() | |
@time_tracer | |
def find_unused_functions(): | |
functions = set() | |
funcs_storage = {} | |
def get_functions(): | |
nonlocal functions | |
def get_names(el): | |
no_spaces = el.strip() | |
if 'func ' in el: | |
no_func = no_spaces.replace('func ', '') | |
no_contents_name = no_func.split('(')[0] | |
if ' ' in no_contents_name: | |
devided = no_contents_name.split(' ') | |
name = devided[len(devided) - 1] | |
return name | |
else: | |
return no_contents_name | |
for subdir, dirs, files in os.walk(DIR): | |
for file in files: | |
path = f'{subdir}/{file}' | |
name = os.path.splitext(file) | |
_format = name[1] | |
if _format == '.swift': | |
contents = open(f'{path}', 'r').readlines() | |
filtered = map(lambda el: get_names(el), contents) | |
map_functions = list(filter(lambda el: el is not None, filtered)) | |
functions.update(set(map_functions)) | |
def get_functions_usage_frequency(): | |
nonlocal functions | |
for func in functions: | |
for subdir, dirs, files in os.walk(DIR): | |
for file in files: | |
path = f'{subdir}/{file}' | |
name = os.path.splitext(file) | |
_format = name[1] | |
if _format == '.swift': | |
contents = open(f'{path}', 'r').read().split() | |
for word in contents: | |
if func in word: | |
if func not in funcs_storage.keys(): | |
funcs_storage[func] = 1 | |
else: | |
funcs_storage[func] += 1 | |
def find_garbage_functions(): | |
""" | |
Usage count equal 1 means func is just inited, not called anywhere, thus it can be deleted (except override, libs funcs etc.) | |
""" | |
print('Potentially unused functions: \n') | |
for el in funcs_storage: | |
if funcs_storage[el] == 1 and 'test' not in el.lower(): | |
print(f'{el} :', funcs_storage[el]) | |
get_functions() | |
get_functions_usage_frequency() | |
find_garbage_functions() | |
if __name__ == '__main__': | |
print('\nStep 1: searching unused modules... ⏳\n') | |
find_unused_modules() | |
print('\nStep 2: searching unused functions...⏳\n') | |
find_unused_functions() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment