|
1 # |
|
2 # Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 # All rights reserved. |
|
4 # This component and the accompanying materials are made available |
|
5 # under the terms of the License "Eclipse Public License v1.0" |
|
6 # which accompanies this distribution, and is available |
|
7 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 # |
|
9 # Initial Contributors: |
|
10 # Nokia Corporation - initial contribution. |
|
11 # |
|
12 # Contributors: |
|
13 # |
|
14 # Description: |
|
15 # dos2unix - python version |
|
16 # This module converts file from dos to unix i.e. replaces Dos/Windows EOL (CRLF) with Unix EOL (LF). |
|
17 # example usage: |
|
18 # dos2unix.py windowsFile.txt |
|
19 # |
|
20 |
|
21 import os |
|
22 import re |
|
23 import sys |
|
24 import stat |
|
25 #------------------------------------------------------------------------------ |
|
26 # Converts string from dos to unix i.e. replaces CRLF with LF as a line terminator (EOL) |
|
27 #------------------------------------------------------------------------------ |
|
28 def convertDos2Unix(inputString): |
|
29 regExp = re.compile("\r\n|\n|\r") |
|
30 return regExp.sub("\n",inputString) |
|
31 |
|
32 #------------------------------------------------------------------------------ |
|
33 # Validates input |
|
34 #------------------------------------------------------------------------------ |
|
35 def validateInput(argv): |
|
36 if not(len(argv) > 1): |
|
37 print "Error No parameter given: fileName to convert." |
|
38 sys.exit(); |
|
39 |
|
40 #------------------------------------------------------------------------------ |
|
41 # Reads input file |
|
42 #------------------------------------------------------------------------------ |
|
43 def readInputFile(fileName): |
|
44 inputFile = open(fileName, 'r') |
|
45 # read file content |
|
46 originalFileContent = inputFile.read() |
|
47 inputFile.close() |
|
48 return originalFileContent |
|
49 |
|
50 #------------------------------------------------------------------------------ |
|
51 # Writes string to given file (in binary mode) |
|
52 #------------------------------------------------------------------------------ |
|
53 def writeToBinaryFile(string,fileName): |
|
54 os.chmod(fileName, stat.S_IRWXU) |
|
55 outputFile = open(fileName, 'wb') |
|
56 outputFile.write(string) |
|
57 outputFile.close() |
|
58 |
|
59 # Main script |
|
60 |
|
61 #------------------------------------------------------------------------------ |
|
62 # Coverts dos/windows EOL to UNIX EOL (CRLF->LF) in given file |
|
63 #------------------------------------------------------------------------------ |
|
64 def dos2unix(fileName): |
|
65 originalFileContent = readInputFile(fileName) |
|
66 convertedFileContent = convertDos2Unix(originalFileContent) |
|
67 writeToBinaryFile(convertedFileContent,fileName) |
|
68 |