Skip to content

Instantly share code, notes, and snippets.

@dgrant
Created April 26, 2021 21:27
Show Gist options
  • Save dgrant/abc1b943beec626a488ac1af0d801213 to your computer and use it in GitHub Desktop.
Save dgrant/abc1b943beec626a488ac1af0d801213 to your computer and use it in GitHub Desktop.
Add EOF to all files
#!/usr/bin/env python3
import os
LF_BYTES = b'\n'
LF_BYTE = b'\n'[0]
def get_filetypes():
types = []
for line in open('.gitattributes').readlines():
line = line.split()
if len(line) > 1 and line[1] == 'mctext':
types.append(line[0])
return types
filetypes = sorted(['.' + x.split('.')[-1] for x in get_filetypes()])
exceptions = ('bad_cert.txt',)
for root, dirs, files in os.walk('.'):
for file in files:
if os.path.splitext(file)[1] in filetypes and file not in exceptions:
path = os.path.join(root, file)
text = open(path, 'rb').read()
if len(text) == 0:
continue
changed = False
while text[-1] == LF_BYTE and text[-2] == LF_BYTE:
# if the last two bytes are '\n' remove one of them
text = text[:-1]
changed = True
if text[-1] != LF_BYTE:
# if the last byte is not '\n' add one
text += LF_BYTES
changed = True
if changed:
open(path, 'wb').write(text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment