Created
January 18, 2019 21:24
-
-
Save jklemm/f9fd5c02db611b973bcabd35a3f388f2 to your computer and use it in GitHub Desktop.
Procura código morto dentro de um projeto Django
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
#!/usr/bin/python | |
# coding: utf-8 | |
from __future__ import absolute_import | |
import re | |
import sys | |
import os | |
from os import listdir | |
from os.path import isfile, join, isdir | |
ALLOWED_EXTENSION = '.py' | |
IGNORED_DIRS = {'.idea', '.sass-cache', 'media', 'migrations', 'node_modules', 'tests'} | |
IGNORED_METHODS = {'get', 'post', 'put'} | |
def listar_arquivos_validos(directory): | |
""" | |
Todos arquivos que contém código nosso, ou seja, que possa ser excluído | |
""" | |
file_list = [] | |
for root, subdirs, files in os.walk(directory): | |
for filename in files: | |
if not filename.endswith(ALLOWED_EXTENSION): | |
continue | |
file_path = os.path.join(root, filename) | |
if any([word in file_path for word in IGNORED_DIRS]): | |
continue | |
valid_file = is_file_valid(file_path) | |
if not valid_file: | |
continue | |
file_list.append(file_path) | |
return file_list | |
def is_file_valid(one_file): | |
with open(one_file, 'r') as myfile: | |
num_lines = sum(1 for line in myfile) | |
return num_lines >= 2 | |
def extrair_metodos_do_arquivo(one_file): | |
method_matcher = re.compile(r"def\s([a-zA-Z0-9\s\_]*)?\(") | |
methods = set() | |
with open(one_file) as arquivo: | |
for match in method_matcher.finditer(arquivo.read()): | |
methods.add(match.group(1)) | |
return methods | |
def extrair_classes_do_arquivo(one_file): | |
klass_matcher = re.compile(r"class\s([a-zA-Z0-9\s\_]*)?\(") | |
klasses = set() | |
with open(one_file) as arquivo: | |
for match in klass_matcher.finditer(arquivo.read()): | |
klasses.add(match.group(1)) | |
return klasses | |
if __name__ == '__main__': | |
if len(sys.argv) == 1: | |
print('Error, you need to inform a directory') | |
exit(1) | |
diretorio = sys.argv[1] | |
lista_de_arquivos = listar_arquivos_validos(diretorio) | |
quantidade = len(lista_de_arquivos) | |
for arquivo in lista_de_arquivos: | |
print('- {}'.format(arquivo)) | |
metodos = extrair_metodos_do_arquivo(arquivo) | |
print('-- {}'.format(metodos)) | |
classes = extrair_classes_do_arquivo(arquivo) | |
print('-- {}'.format(classes)) | |
exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment