Advertisement
Guest User

Untitled

a guest
May 25th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. #!/usr/bin/python
  2. # coding: utf-8
  3. from __future__ import absolute_import
  4.  
  5. import sys
  6. import os
  7. from builtins import input
  8.  
  9. UNICODE_LITERALS = 'from __future__ import unicode_literals'
  10.  
  11. ALLOWED_EXTENSION = '.py'
  12. IGNORED_DIRS = ('__init__', '.idea', '.sass-cache', 'media', 'migrations', 'node_modules', 'static', 'templates')
  13.  
  14.  
  15. def listar_arquivos_sem_unicode_literals(directory):
  16. file_list = []
  17. for root, subdirs, files in os.walk(directory):
  18. for filename in files:
  19.  
  20. if not filename.endswith(ALLOWED_EXTENSION):
  21. continue
  22.  
  23. file_path = os.path.join(root, filename)
  24.  
  25. if any([word in file_path for word in IGNORED_DIRS]):
  26. continue
  27.  
  28. valid_file = is_file_valid(file_path)
  29. if not valid_file:
  30. continue
  31.  
  32. have_import = is_file_have_unicode_literals(file_path)
  33. if not have_import:
  34. file_list.append(file_path)
  35. return file_list
  36.  
  37.  
  38. def is_file_valid(one_file):
  39. with open(one_file, 'r') as myfile:
  40. num_lines = sum(1 for line in myfile)
  41. return num_lines >= 4
  42.  
  43.  
  44. def is_file_have_unicode_literals(one_file):
  45. with open(one_file, 'r') as myfile:
  46. content = myfile.read()
  47. return UNICODE_LITERALS in content
  48.  
  49.  
  50. def add_unicode_literals(one_file):
  51. with open(one_file, 'r') as original_file:
  52. conteudo = original_file.read()
  53.  
  54. original_file.seek(0)
  55. lines = conteudo.splitlines()
  56.  
  57. if 'coding' in conteudo:
  58. lines[0] = '# coding: utf-8'
  59. lines.insert(1, UNICODE_LITERALS)
  60. if lines[2] != '':
  61. lines.insert(2, '')
  62. if 'class' in lines[3]:
  63. lines.insert(3, '')
  64. if 'def' in lines[3]:
  65. lines.insert(3, '')
  66. else:
  67. lines.insert(0, UNICODE_LITERALS)
  68. if lines[1] != '':
  69. lines.insert(1, '')
  70. if 'class' in lines[2]:
  71. lines.insert(2, '')
  72. if 'def' in lines[2]:
  73. lines.insert(2, '')
  74.  
  75. lines.append('')
  76.  
  77. with open(one_file, 'w') as output_file:
  78. new_file_content = '\n'.join(lines)
  79. output_file.write(new_file_content)
  80.  
  81.  
  82. if __name__ == '__main__':
  83. if len(sys.argv) == 1:
  84. print('Error, you need to inform a directory')
  85. exit(1)
  86.  
  87. diretorio = sys.argv[1]
  88.  
  89. lista_de_arquivos = listar_arquivos_sem_unicode_literals(diretorio)
  90.  
  91. quantidade = len(lista_de_arquivos)
  92.  
  93. if quantidade > 200:
  94. resposta = input('Um total de {} arquivos serão alterados, deseja prosseguir? '.format(quantidade))
  95.  
  96. if resposta.upper() != 'S':
  97. exit(0)
  98.  
  99. for arquivo in lista_de_arquivos:
  100. print('- {}'.format(arquivo))
  101. add_unicode_literals(arquivo)
  102. print('{} arquivos foram alterados.'.format(quantidade))
  103.  
  104. exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement