Created
January 21, 2013 15:30
-
-
Save spulec/4586854 to your computer and use it in GitHub Desktop.
Add encoding and __future__ imports to all .py files in a repo.
This file contains hidden or 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 subprocess | |
import shlex | |
files = subprocess.check_output(shlex.split('git ls-files --full-name')).strip().split('\n') | |
py_files = [file for file in files if file.endswith(".py")] | |
len(py_files) | |
new_header = '# -*- coding: utf-8 -*-\nfrom __future__ import unicode_literals\n' | |
for the_file in py_files: | |
read_file = open(the_file, 'r') | |
lines = list(read_file) | |
result_lines = [] | |
for line in lines: | |
if 'coding: utf-8' in line: | |
continue | |
if 'from __future__ import unicode_literals' in line: | |
continue | |
result_lines.append(line) | |
read_file.close() | |
if result_lines and 'bin' in result_lines[0]: | |
# First line is shebang | |
result_lines.insert(1, new_header) | |
else: | |
result_lines.insert(0, new_header) | |
write_file = open(the_file, 'w') | |
write_file.write(''.join(result_lines)) | |
write_file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment