blob: bf8fe1c245ef4c0dda7c52247a11a42554f5ad39 [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#! /usr/bin/env python3
Guido van Rossum499a6e51998-09-14 15:46:15 +00002
3"Replace LF with CRLF in argument files. Print names of changed files."
4
Guido van Rossum967e5092000-02-14 21:42:14 +00005import sys, re, os
Andrew M. Kuchlinge236b382004-08-09 17:27:55 +00006
7def main():
8 for filename in sys.argv[1:]:
9 if os.path.isdir(filename):
Collin Winter6afaeb72007-08-03 17:06:41 +000010 print(filename, "Directory!")
Andrew M. Kuchlinge236b382004-08-09 17:27:55 +000011 continue
Éric Araujo1bf5b6a2011-08-12 19:40:05 +020012 with open(filename, "rb") as f:
13 data = f.read()
14 if b'\0' in data:
Collin Winter6afaeb72007-08-03 17:06:41 +000015 print(filename, "Binary!")
Andrew M. Kuchlinge236b382004-08-09 17:27:55 +000016 continue
Éric Araujo1bf5b6a2011-08-12 19:40:05 +020017 newdata = re.sub(b"\r?\n", b"\r\n", data)
Andrew M. Kuchlinge236b382004-08-09 17:27:55 +000018 if newdata != data:
Collin Winter6afaeb72007-08-03 17:06:41 +000019 print(filename)
Éric Araujo1bf5b6a2011-08-12 19:40:05 +020020 with open(filename, "wb") as f:
21 f.write(newdata)
Andrew M. Kuchlinge236b382004-08-09 17:27:55 +000022
23if __name__ == '__main__':
24 main()