Last active
February 12, 2021 15:50
-
-
Save jklemm/5d858005c781c52f437805cf119c5752 to your computer and use it in GitHub Desktop.
Remove from future and encoding lines from top source
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 sys | |
import os | |
from builtins import input | |
import subprocess | |
CODING_UTF8 = '# coding: utf-8' | |
CODING_UTF8_2 = '# coding=utf-8' | |
CODING_UTF8_3 = '# -*- coding: utf-8 -*-' | |
ABSOLUTE_IMPORT = 'from __future__ import absolute_import' | |
DIVISION = 'from __future__ import division' | |
UNICODE_LITERALS = 'from __future__ import unicode_literals' | |
PRINT_FUNCTION = 'from __future__ import print_function' | |
ALLOWED_EXTENSION = '.py' | |
IGNORED_DIRS = ('__init__', '.idea', '.sass-cache', 'media', 'migrations', 'node_modules', 'static', 'templates') | |
def run(*args): | |
return subprocess.check_call(['git'] + list(args)) | |
def commit(commit_message): | |
subprocess.check_call(['cd', 'web', '&&', 'git', 'commit', '-am', commit_message]) | |
def listar_arquivos_sem_unicode_literals(directory): | |
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 | |
if has_absolute_import(file_path): | |
file_list.append(file_path) | |
if has_unicode_literals(file_path): | |
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 >= 4 | |
def has_absolute_import(one_file): | |
with open(one_file, 'r') as myfile: | |
content = myfile.read() | |
return ABSOLUTE_IMPORT in content | |
def has_unicode_literals(one_file): | |
with open(one_file, 'r') as myfile: | |
content = myfile.read() | |
return UNICODE_LITERALS in content | |
def remove_unicode_literals(one_file): | |
with open(one_file, 'r') as original_file: | |
conteudo = original_file.read() | |
original_file.seek(0) | |
lines = conteudo.splitlines() | |
try: | |
lines.remove(CODING_UTF8) | |
except: | |
pass | |
try: | |
lines.remove(CODING_UTF8_2) | |
except: | |
pass | |
try: | |
lines.remove(CODING_UTF8_3) | |
except: | |
pass | |
try: | |
lines.remove(ABSOLUTE_IMPORT) | |
except: | |
pass | |
try: | |
lines.remove(DIVISION) | |
except: | |
pass | |
try: | |
lines.remove(UNICODE_LITERALS) | |
except: | |
pass | |
try: | |
lines.remove(PRINT_FUNCTION) | |
except: | |
pass | |
if len(lines[0]) == 0: | |
del lines[0] | |
with open(one_file, 'w') as output_file: | |
new_file_content = '\n'.join(lines) + '\n' | |
output_file.write(new_file_content) | |
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_sem_unicode_literals(diretorio) | |
quantidade = len(lista_de_arquivos) | |
if quantidade > 200: | |
resposta = input('Um total de {} arquivos serão alterados, deseja prosseguir? '.format(quantidade)) | |
if resposta.upper() != 'S': | |
exit(0) | |
for arquivo in lista_de_arquivos: | |
print('- {}'.format(arquivo)) | |
remove_unicode_literals(arquivo) | |
commit(f'Remove from future do diretório {diretorio}') | |
print('{} arquivos foram alterados.'.format(quantidade)) | |
exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment