Created
November 2, 2015 06:45
-
-
Save Techcable/19839099bb1aacded6c4 to your computer and use it in GitHub Desktop.
A dos2unix python clone that supports recusrively finding files (doesn't suck)
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 argparse | |
| import os | |
| import re | |
| parser = argparse.ArgumentParser("Convert line endings to \\n") | |
| parser.add_argument('--excluded', help='Patterns of files or directories to exclude', nargs='*') | |
| parser.add_argument('--quiet', '-q', help='Suppress non-error output', action='store_true') | |
| parser.add_argument('input', nargs='+', help='Files or directories to convert line endings in') | |
| args = parser.parse_args() | |
| def is_excluded(file): | |
| excluded = args.excluded | |
| if excluded is None: | |
| return False | |
| if isinstance(excluded, str): | |
| excluded = [str] | |
| for excluded in args.excluded: | |
| found = re.search(excluded, file) | |
| if found is not None and len(found.group(0)) > 0: | |
| return True | |
| return False | |
| def process(file): | |
| if is_excluded(file): | |
| if not args.quiet: | |
| print(file, "was excluded from conversion") | |
| return | |
| try: | |
| original_lines = open(file).readlines() | |
| except UnicodeDecodeError: | |
| print("ignoring", file, "because it is a binary file") | |
| return | |
| new_lines = [] | |
| for original_line in original_lines: | |
| for new_line in original_line.splitlines(False): | |
| new_line += '\n' | |
| new_lines.append(new_line) | |
| if original_lines == new_lines: | |
| return | |
| if not args.quiet: | |
| print("Converting lines in", file) | |
| open(file, "w+").writelines(new_lines) | |
| for input_file in args.input: | |
| if os.path.isdir(input_file): | |
| for dir, subDirs, files in os.walk(input_file): | |
| for file in files: | |
| file = os.path.join(dir, file) | |
| process(file) | |
| else: | |
| process(input_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment