2019-06-12 05:41:31 +00:00
|
|
|
'''
|
|
|
|
Convert LF line endings to CRLF.
|
|
|
|
'''
|
2021-09-24 06:42:34 +00:00
|
|
|
import argparse
|
2019-06-12 05:41:31 +00:00
|
|
|
import sys
|
|
|
|
|
2021-09-24 06:42:34 +00:00
|
|
|
from voussoirkit import pathclass
|
2019-06-12 05:41:31 +00:00
|
|
|
from voussoirkit import pipeable
|
|
|
|
|
|
|
|
CR = b'\x0D'
|
|
|
|
LF = b'\x0A'
|
|
|
|
CRLF = CR + LF
|
|
|
|
|
2021-09-24 06:42:34 +00:00
|
|
|
def crlf(file):
|
2021-10-05 00:21:14 +00:00
|
|
|
content = file.read('rb')
|
2021-09-24 06:42:34 +00:00
|
|
|
|
|
|
|
original = content
|
2019-06-12 05:41:31 +00:00
|
|
|
content = content.replace(CRLF, LF)
|
|
|
|
content = content.replace(LF, CRLF)
|
2021-09-24 06:42:34 +00:00
|
|
|
if content == original:
|
|
|
|
return
|
|
|
|
|
2021-10-05 00:21:14 +00:00
|
|
|
file.write('wb', content)
|
2019-06-12 05:41:31 +00:00
|
|
|
|
2021-09-24 06:42:34 +00:00
|
|
|
def crlf_argparse(args):
|
|
|
|
patterns = pipeable.input_many(args.patterns, skip_blank=True, strip=True)
|
2021-12-22 00:58:26 +00:00
|
|
|
files = pathclass.glob_many_files(patterns)
|
2021-09-24 06:42:34 +00:00
|
|
|
for file in files:
|
|
|
|
crlf(file)
|
2021-10-05 00:21:14 +00:00
|
|
|
pipeable.stdout(file.absolute_path)
|
2021-09-24 06:42:34 +00:00
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def main(argv):
|
|
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
|
|
|
|
|
|
parser.add_argument('patterns')
|
|
|
|
parser.set_defaults(func=crlf_argparse)
|
|
|
|
|
|
|
|
args = parser.parse_args(argv)
|
|
|
|
return args.func(args)
|
2019-06-12 05:41:31 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
raise SystemExit(main(sys.argv[1:]))
|