-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added script to convert newlines between unix mac and windows.
- Loading branch information
Showing
1 changed file
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#!/usr/bin/env python | ||
|
||
import os | ||
import sys | ||
import re | ||
from optparse import OptionParser | ||
|
||
|
||
op = OptionParser() | ||
op.add_option("-u", "--to-unix", dest="u", | ||
help="Windows/Mac -> Unix", | ||
action='store_true', default=False) | ||
op.add_option("-m", "--to-mac", dest="m", | ||
help="Windows/Unix -> Mac", | ||
action='store_true', default=False) | ||
op.add_option("-w", "--to-windows", dest="w", | ||
help="Unix/Mac -> Windows", | ||
action='store_true', default=False) | ||
|
||
op.usage += """ [<file-or-directory> ...] | ||
Forcefully converts newlines as required. | ||
Specify individual files or directories to process all files therein. | ||
Each file will be backupped with a .bak extension. | ||
""" | ||
|
||
mac_re = re.compile('\r(?!\n)') | ||
unix_re = re.compile('(?<!\r)\n') | ||
win_re = re.compile('\r\n') | ||
|
||
transform = { | ||
# target: | ||
# Unix Mac Windows | ||
(True, False, False): ((win_re, '\n'), (mac_re, '\n')), | ||
(False, True, False): ((win_re, '\r'), (unix_re, '\r')), | ||
(False, False, True): ((unix_re, '\r\n'), (mac_re, '\r\n')), | ||
} | ||
|
||
|
||
def convert(u, m, w, data): | ||
for expr, repl in transform[u, m, w]: | ||
data = expr.sub(repl, data) | ||
return data | ||
|
||
|
||
def iter_args(args): | ||
for arg in args: | ||
if os.path.isfile(arg): | ||
yield arg | ||
elif os.path.isdir(arg): | ||
for sub in iter_args(os.path.join(arg, f) | ||
for f in os.listdir(arg)): | ||
yield sub | ||
|
||
|
||
if __name__ == '__main__': | ||
opts, args = op.parse_args(sys.argv[1:]) | ||
|
||
if (opts.w + opts.u + opts.m) != 1: | ||
print "Please specify exactly one of -u, -w, -m." | ||
op.parse_args(["-h"]) | ||
sys.exit(-1) | ||
|
||
if len(args) == 0: | ||
print "Please specify at least one file or directory." | ||
op.parse_args(["-h"]) | ||
sys.exit(-1) | ||
|
||
for f in iter_args(args): | ||
data = open(f).read() | ||
open(f + '.bak', 'wb').write(data) | ||
open(f, 'wb').write(convert(opts.u, opts.m, opts.w, data)) |