forked from aixp/obc-OberonSystem.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linesep.py
executable file
·40 lines (33 loc) · 854 Bytes
/
linesep.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#! /usr/bin/env python2.7
#
# A. V. Shiryaev, 2012.01
import sys, re, os
def splitToLines (text):
lines = []
[ [ lines.append(l1) for l1 in re.split( '[\r\n]', l ) ] for l in text.split('\r\n') ]
return lines
def setLineSep (fileName, lineSep):
try:
fh = open(fileName, 'rb')
text = fh.read()
fh.close()
except Exception, e:
sys.stderr.write("%s\n" % str(e))
else:
text = lineSep.join(splitToLines(text))
fh = open(fileName, 'wb')
fh.write(text)
fh.close()
def usage ():
sys.stderr.write("usage: %s ( dos | unix | mac ) file { file }\n" % (sys.argv[0],))
def main ():
if len(sys.argv) < 3:
usage()
else:
lineSep = { 'unix': '\n', 'dos': '\r\n', 'mac': '\r' }.get(sys.argv[1])
if lineSep == None:
usage()
else:
[ setLineSep(fileName, lineSep) for fileName in sys.argv[2:] ]
if __name__ == '__main__':
main()