Benjamin Peterson | 90f5ba5 | 2010-03-11 22:53:45 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
Guido van Rossum | 499a6e5 | 1998-09-14 15:46:15 +0000 | [diff] [blame] | 2 | |
| 3 | "Replace LF with CRLF in argument files. Print names of changed files." |
| 4 | |
Guido van Rossum | 967e509 | 2000-02-14 21:42:14 +0000 | [diff] [blame] | 5 | import sys, re, os |
Andrew M. Kuchling | e236b38 | 2004-08-09 17:27:55 +0000 | [diff] [blame] | 6 | |
| 7 | def main(): |
| 8 | for filename in sys.argv[1:]: |
| 9 | if os.path.isdir(filename): |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 10 | print(filename, "Directory!") |
Andrew M. Kuchling | e236b38 | 2004-08-09 17:27:55 +0000 | [diff] [blame] | 11 | continue |
Éric Araujo | 1bf5b6a | 2011-08-12 19:40:05 +0200 | [diff] [blame] | 12 | with open(filename, "rb") as f: |
| 13 | data = f.read() |
| 14 | if b'\0' in data: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 15 | print(filename, "Binary!") |
Andrew M. Kuchling | e236b38 | 2004-08-09 17:27:55 +0000 | [diff] [blame] | 16 | continue |
Éric Araujo | 1bf5b6a | 2011-08-12 19:40:05 +0200 | [diff] [blame] | 17 | newdata = re.sub(b"\r?\n", b"\r\n", data) |
Andrew M. Kuchling | e236b38 | 2004-08-09 17:27:55 +0000 | [diff] [blame] | 18 | if newdata != data: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 19 | print(filename) |
Éric Araujo | 1bf5b6a | 2011-08-12 19:40:05 +0200 | [diff] [blame] | 20 | with open(filename, "wb") as f: |
| 21 | f.write(newdata) |
Andrew M. Kuchling | e236b38 | 2004-08-09 17:27:55 +0000 | [diff] [blame] | 22 | |
| 23 | if __name__ == '__main__': |
| 24 | main() |