import argparse import os import re import sys from enum import Enum class LineEnding(Enum): CR = '\r', '\\r', 'cr', re.compile(r'\r', re.DOTALL) CRLF = '\r\n', '\\r\\n', 'crlf', re.compile(r'\r\n', re.DOTALL) LF = '\n', '\\n', 'lf', re.compile(r'(? 0 lf_found = lf_nb > 0 if crlf_nb == lf_nb: return MixedLineDetection.UNKNOWN if crlf_found ^ lf_found: return MixedLineDetection.NOT_MIXED if crlf_nb > lf_nb: return MixedLineDetection.MIXED_MOSTLY_CRLF else: return MixedLineDetection.MIXED_MOSTLY_LF def _convert_line_ending(filename, line_ending): # read the file fin = open(filename, 'r') bufin = fin.read() fin.close() # convert line ending bufout = ANY_LINE_ENDING_PATTERN.sub(line_ending, bufin) # write the result in the file fout = open(filename, 'w') fout.write(bufout) fout.close() if __name__ == '__main__': sys.exit(mixed_line_ending())