Last active
January 9, 2017 13:29
-
-
Save zombience/7982dedcb0039cecac3d to your computer and use it in GitHub Desktop.
Convert text files to CRLF or LF line endings. I use code generation via template in Unity, and Unity saves line endings as LF by default. Working in Visual Studio, this creates constant warnings about inconsistent line endings. This is a quick fix. Can be done recursively or only on target directory. Allows for ignored strings.
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
#! /usr/env python3 | |
import os | |
import sys | |
from optparse import OptionParser | |
#TODO: | |
# options are getting out of control | |
# use a config file instead | |
parser = OptionParser() | |
parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False, help='convert files and report') | |
parser.add_option('-r', '--recursive', action='store_true', dest='recursive', default=False, help='search and convert files in subdirectories. otherwise only files in target directory are converted') | |
parser.add_option('-d', '--dir', dest='directory', default=os.getcwd(), help='target directory to search and convert. default is current working directory') | |
parser.add_option('-e', '--extension', dest='extension', default='cs', help='filetype to search and convert. defaults to .cs') | |
parser.add_option('-u', '--unix', action='store_true',dest='unix', help='set line endings to unix-style. default is windows') | |
parser.add_option('-a', '--active', action='store_true',dest='active', default=False, help='if this is not set, converter will run in debug-only mode. no files will be converted. this is for development') | |
parser.add_option('-i', '--ignore', action='append', dest='ignore', default=[], help='list of strings to ignore. please note, this is a very basic ignore method. it may not work as thoroughly as necessary. this is marked as TODO') | |
(options, args) = parser.parse_args() | |
converted_count = 0 | |
ignored_files = [] | |
ignored_dirs = [] | |
def traverse_directories(dir): | |
if not options.recursive: | |
get_and_convert_files_in_dir(dir) | |
return | |
if options.verbose: | |
print('recursively walking directories, starting in: ', dir) | |
for root, subFolders, files in os.walk(dir, onerror=error): | |
for i in options.ignore: | |
for s in subFolders: | |
if i in s and not s in ignored_dirs: | |
ignored_dirs.append(s) | |
subFolders[:] = [d for d in subFolders if not d in ignored_dirs] | |
get_and_convert_files_in_dir(root) | |
def get_and_convert_files_in_dir(dir): | |
global ignored_files | |
skip = False | |
files = [os.path.join(dir, f) for f in os.listdir(dir) if os.path.isfile(os.path.join(dir, f)) and f.endswith('.' + options.extension)] | |
for f in files: | |
for i in options.ignore: | |
if i in os.path.abspath(f): | |
skip = True | |
if skip: | |
if options.verbose: | |
print('file matched ignore pattern, skipping: ', f) | |
ignored_files.append(f) | |
else: | |
convert_file(f) | |
def convert_file(file): | |
global converted_count | |
skip = False | |
if options.active and not skip: | |
out_file = None | |
endings = '\n' if options.unix else '\r\n' | |
out_file = open(file, 'r').read() | |
f = open(file, 'w', newline=endings) | |
f.write(out_file) | |
f.close() | |
converted_count += 1 if not skip else 0 | |
if options.verbose: | |
print('converted line endings on file: ', os.path.abspath(file)) | |
def error(e): | |
raise OSError(e) | |
traverse_directories(options.directory) | |
if not options.active: | |
print('\n*****WARNING*****\n\n\tno files were harmed in the running of this script.\n\tfiles were not converted, because the -a "active" flag was not set.') | |
print('\tthis is for your own good. you must set the flag to active to prevent any accidental conversions\n\n') | |
if options.verbose: | |
endings = ('LF (unix)', 'CRLF (windows)') | |
print('filetype converted: ', options.extension) | |
print('files converted from ', endings[1 if options.unix else 0], ' to ', endings[0 if options.unix else 1]) | |
print('ignore tokens:\n', '\n'.join(options.ignore)) | |
print('skipped directories:\n ', '\n'.join(ignored_dirs)) | |
print('skipped files: \n', '\n'.join(ignored_files)) | |
print('total files converted: ', converted_count) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment