annotate TEisotools-1.0/commons/core/utils/FileUtils.py @ 6:20ec0d14798e draft

Uploaded
author urgi-team
date Wed, 20 Jul 2016 05:00:24 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
1 # Copyright INRA (Institut National de la Recherche Agronomique)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
2 # http://www.inra.fr
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
3 # http://urgi.versailles.inra.fr
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
4 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
5 # This software is governed by the CeCILL license under French law and
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
6 # abiding by the rules of distribution of free software. You can use,
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
7 # modify and/ or redistribute the software under the terms of the CeCILL
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
8 # license as circulated by CEA, CNRS and INRIA at the following URL
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
9 # "http://www.cecill.info".
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
10 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
11 # As a counterpart to the access to the source code and rights to copy,
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
12 # modify and redistribute granted by the license, users are provided only
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
13 # with a limited warranty and the software's author, the holder of the
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
14 # economic rights, and the successive licensors have only limited
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
15 # liability.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
16 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
17 # In this respect, the user's attention is drawn to the risks associated
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
18 # with loading, using, modifying and/or developing or reproducing the
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
19 # software by the user in light of its specific status of free software,
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
20 # that may mean that it is complicated to manipulate, and that also
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
21 # therefore means that it is reserved for developers and experienced
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
22 # professionals having in-depth computer knowledge. Users are therefore
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
23 # encouraged to load and test the software's suitability as regards their
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
24 # requirements in conditions enabling the security of their systems and/or
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
25 # data to be ensured and, more generally, to use and operate it in the
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
26 # same conditions as regards security.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
27 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
28 # The fact that you are presently reading this means that you have had
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
29 # knowledge of the CeCILL license and that you accept its terms.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
30
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
31
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
32 import os
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
33 import re
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
34 import sys
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
35 import math
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
36 import glob
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
37 import shutil
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
38 import subprocess
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
39 from operator import itemgetter
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
40 try:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
41 import hashlib
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
42 except:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
43 pass
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
44
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
45
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
46 class FileUtils( object ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
47
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
48 ## Return the number of lines in the given file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
49 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
50 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
51 def getNbLinesInSingleFile( fileName ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
52 cmd = "wc -l %s" % fileName
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
53 r = subprocess.Popen(cmd.split(' '), stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
54 nbLines = int(r.split()[0])
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
55
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
56 toAdd = 0
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
57 if nbLines:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
58 cmd = "tail -1 %s" % fileName
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
59 r = subprocess.Popen(cmd.split(' '), stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
60
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
61 if r == '\n':
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
62 toAdd -= 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
63 elif '\n' not in r:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
64 toAdd += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
65
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
66 return nbLines + toAdd
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
67
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
68 ## Return the number of lines in the files in the given list
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
69 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
70 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
71 def getNbLinesInFileList( lFileNames ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
72 count = 0
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
73 for fileName in lFileNames:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
74 count += FileUtils.getNbLinesInSingleFile( fileName )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
75 return count
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
76
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
77 ## Return True if the given file exists, False otherwise
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
78 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
79 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
80 def isRessourceExists( fileName ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
81 return os.path.exists( fileName )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
82
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
83 ## Return True if the given file is empty, False otherwise
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
84 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
85 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
86 def isEmpty( fileName ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
87 return 0 == FileUtils.getNbLinesInSingleFile( fileName )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
88
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
89 ## Return True if both files are identical, False otherwise
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
90 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
91 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
92 def are2FilesIdentical( file1, file2 ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
93 tmpFile = "diff_%s_%s" % ( os.path.basename(file1), os.path.basename(file2) )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
94 cmd = "diff %s %s >> %s" % ( file1, file2, tmpFile )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
95 returnStatus = os.system( cmd )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
96 if returnStatus != 0:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
97 print "WARNING: 'diff' returned '%i'" % returnStatus
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
98 os.remove( tmpFile )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
99 return False
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
100 if FileUtils.isEmpty( tmpFile ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
101 os.remove( tmpFile )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
102 return True
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
103 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
104 os.remove( tmpFile )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
105 return False
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
106
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
107 ## Return a string with all the content of the files in the given list
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
108 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
109 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
110 def getFileContent( lFiles ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
111 content = ""
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
112 lFiles.sort()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
113 for fileName in lFiles:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
114 currentFile = open( fileName, "r" )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
115 content += currentFile.read()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
116 currentFile.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
117 return content
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
118
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
119 ## Save content of the given file after having sorted it
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
120 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
121 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
122 def sortFileContent( inFile, outFile="" ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
123 inFileHandler = open(inFile, "r" )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
124 lines = inFileHandler.readlines()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
125 inFileHandler.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
126 lines.sort()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
127 if outFile == "":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
128 outFile = inFile
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
129 outFileHandler = open( outFile, "w" )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
130 outFileHandler.writelines( lines )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
131 outFileHandler.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
132
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
133 ## Add end-of-line symbol to the given file content if necessary
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
134 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
135 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
136 def addNewLineAtTheEndOfFileContent( fileContent ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
137 if not fileContent.endswith('\n') and len(fileContent) != 0:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
138 fileContent += '\n'
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
139 return fileContent
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
140
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
141 ## Concatenate files in the given list
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
142 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
143 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
144 def catFilesFromList( lFiles, outFile, sort=True, skipHeaders = False, separator = "" ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
145 if sort:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
146 lFiles.sort()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
147 outFileHandler = open( outFile, "a" )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
148 isFirstFile = True
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
149 for singleFile in lFiles:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
150 if not isFirstFile:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
151 outFileHandler.write(separator)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
152 isFirstFile = False
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
153 singleFileHandler = open( singleFile, "r" )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
154 if skipHeaders:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
155 singleFileHandler.readline()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
156 line = singleFileHandler.readline()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
157 while line:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
158 outFileHandler.write(line)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
159 line = singleFileHandler.readline()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
160 singleFileHandler.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
161 outFileHandler.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
162
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
163 ## Concatenate files according to the given pattern
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
164 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
165 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
166 def catFilesByPattern( pattern, outFile, skipHeaders = False, separator = "" ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
167 lFiles = glob.glob( pattern )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
168 FileUtils.catFilesFromList( lFiles, outFile, skipHeaders = skipHeaders, separator = separator )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
169
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
170 ## Cat all files of a given directory
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
171 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
172 # @param dir string directory name
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
173 # @param outFileName string output file name
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
174 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
175 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
176 def catFilesOfDir(directory, outFileName):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
177 FileUtils.catFilesByPattern("%s/*" % directory, outFileName)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
178
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
179 ## Remove files listed according to the given pattern
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
180 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
181 # @example prefix="/home/tmp/dummy*.txt"
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
182 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
183 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
184 def removeFilesByPattern( prefix ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
185 lFiles = glob.glob( prefix )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
186 for f in lFiles:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
187 os.remove( f )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
188
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
189 ## Remove files listed according to the suffixes in the given list
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
190 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
191 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
192 def removeFilesBySuffixList( targetPath, lSuffixes ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
193 if targetPath[-1] == "/":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
194 targetPath = targetPath[:-1]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
195 for suffix in lSuffixes:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
196 pattern = "%s/*%s" % ( targetPath, suffix )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
197 FileUtils.removeFilesByPattern( pattern )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
198
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
199 ## Remove repeated blanks in the given file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
200 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
201 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
202 def removeRepeatedBlanks( inFile, outFile="" ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
203 if outFile == "":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
204 outFile = inFile
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
205 tmpFile = "tr_%s_%s" % ( inFile, outFile )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
206 cmd = "tr -s ' ' < %s > %s" % ( inFile, tmpFile )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
207 os.system( cmd )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
208 os.rename( tmpFile, outFile )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
209
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
210 ## Remove files in the given list
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
211 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
212 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
213 def removeFilesFromList(lFiles):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
214 for f in lFiles:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
215 os.remove(f)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
216
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
217 ## Remove files in the given list if exist
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
218 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
219 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
220 def removeFilesFromListIfExist(lFiles):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
221 for fileName in lFiles:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
222 if FileUtils.isRessourceExists(fileName):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
223 os.remove(fileName)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
224
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
225 ## Append the content of a file to another file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
226 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
227 # @param inFile string name of the input file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
228 # @param outFile string name of the output file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
229 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
230 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
231 def appendFileContent( inFile, outFile ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
232 outFileHandler = open( outFile, "a" )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
233 inFileHandler = open( inFile, "r" )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
234 shutil.copyfileobj( inFileHandler, outFileHandler )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
235 inFileHandler.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
236 outFileHandler.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
237
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
238
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
239 ## Replace Windows end-of-line by Unix end-of-line
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
240 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
241 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
242 def fromWindowsToUnixEof( inFile ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
243 tmpFile = "%s.tmp" % ( inFile )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
244 shutil.copyfile( inFile, tmpFile )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
245 os.remove( inFile )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
246 tmpFileHandler = open( tmpFile, "r" )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
247 inFileHandler = open( inFile, "w" )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
248 while True:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
249 line = tmpFileHandler.readline()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
250 if line == "":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
251 break
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
252 inFileHandler.write( line.replace("\r\n","\n") )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
253 tmpFileHandler.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
254 inFileHandler.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
255 os.remove( tmpFile )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
256
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
257
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
258 ## Remove duplicated lines in a file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
259 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
260 # @note it preserves the initial order and handles blank lines
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
261 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
262 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
263 def removeDuplicatedLines( inFile ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
264 tmpFile = "%s.tmp" % ( inFile )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
265 shutil.copyfile( inFile, tmpFile )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
266 os.remove( inFile )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
267
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
268 tmpFileHandler = open( tmpFile, "r" )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
269 lLines = list( tmpFileHandler.read().split("\n") )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
270 if lLines[-1] == "":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
271 del lLines[-1]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
272 sLines = set( lLines )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
273 tmpFileHandler.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
274 os.remove( tmpFile )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
275
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
276 inFileHandler = open( inFile, "w" )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
277 for line in lLines:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
278 if line in sLines:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
279 inFileHandler.write( "%s\n" % ( line ) )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
280 sLines.remove( line )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
281 inFileHandler.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
282
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
283
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
284 ## Write a list of lines in a given file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
285 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
286 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
287 def writeLineListInFile( inFile, lLines ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
288 inFileHandler = open( inFile, "w" )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
289 for line in lLines:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
290 inFileHandler.write( line )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
291 inFileHandler.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
292
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
293
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
294 ## Give the list of absolute path of each directory in the given directory
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
295 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
296 # @param rootPath string absolute path of the given directory
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
297 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
298 # @return lDirPath list of absolute directory path
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
299 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
300 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
301 def getAbsoluteDirectoryPathList(rootPath):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
302 lDirPath = []
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
303 lPaths = glob.glob(rootPath + "/*")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
304 for ressource in lPaths:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
305 if os.path.isdir(ressource) :
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
306 lDirPath.append(ressource)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
307 return lDirPath
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
308
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
309
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
310 ## Get a sublist of which each element matches/doesn't match a pattern
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
311 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
312 # @param lPath string list of paths
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
313 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
314 # @param pattern string pattern
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
315 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
316 # @param match bool
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
317 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
318 # @return lPathMatching list of path matching pattern
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
319 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
320 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
321 def getSubListAccordingToPattern(lPath, pattern, match = True):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
322 lPathMatching = []
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
323 for path in lPath:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
324 if match:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
325 if re.match(".*%s.*" % pattern, path):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
326 lPathMatching.append(path)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
327 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
328 if not re.match(".*%s.*" % pattern, path):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
329 lPathMatching.append(path)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
330 return lPathMatching
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
331
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
332
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
333 ## Give the list of file names found in the given directory
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
334 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
335 # @param dirPath string absolute path of the given directory
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
336 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
337 # @return lFilesInDir list of file names
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
338 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
339 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
340 def getFileNamesList( dirPath, patternFileFilter = ".*" ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
341 lFilesInDir = []
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
342 lPaths = glob.glob( dirPath + "/*" )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
343 for ressource in lPaths:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
344 if os.path.isfile( ressource ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
345 fileName = os.path.basename( ressource )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
346 if re.match(patternFileFilter, fileName):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
347 lFilesInDir.append( fileName )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
348 return lFilesInDir
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
349
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
350 ## Return the MD5 sum of a file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
351 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
352 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
353 def getMd5SecureHash( inFile ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
354 if "hashlib" in sys.modules:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
355 md5 = hashlib.md5()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
356 inFileHandler = open( inFile, "r" )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
357 while True:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
358 line = inFileHandler.readline()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
359 if line == "":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
360 break
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
361 md5.update( line )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
362 inFileHandler.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
363 return md5.hexdigest()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
364 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
365 return ""
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
366
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
367 ## Return True if size file > 0 octet
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
368 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
369 # @param fileName string file name
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
370 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
371 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
372 def isSizeNotNull(fileName):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
373 size = os.path.getsize(fileName)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
374 if size > 0:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
375 return True
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
376 return False
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
377
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
378 ## Split one file into N Files by lines
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
379 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
380 # @param fileName string file name
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
381 # @param N int number of files to create
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
382 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
383 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
384 def splitFileIntoNFiles(fileName, N):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
385 nbLine = FileUtils.getNbLinesInSingleFile(fileName)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
386 nbLinesInEachFile = nbLine
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
387 if N > nbLine:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
388 N = nbLine
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
389 if N != 0:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
390 nbLinesInEachFile = math.ceil(float(nbLine) / N)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
391 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
392 N = 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
393 filePrefix, fileExt = os.path.splitext(os.path.basename(fileName))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
394 fileHandler = open(fileName, "r")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
395 for i in range(1,N+1):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
396 with open("%s-%s%s" %(filePrefix, i, fileExt), "w") as f:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
397 j = 0
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
398 while j < nbLinesInEachFile:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
399 j += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
400 f.write(fileHandler.readline())
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
401 fileHandler.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
402
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
403 ## Split one file into files of N lines
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
404 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
405 # @param fileName string input file name
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
406 # @param N int lines number per files
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
407 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
408 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
409 def splitFileAccordingToLineNumber(fileName, N):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
410 filePrefix, fileExt = os.path.splitext(os.path.basename(fileName))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
411 with open(fileName) as inF:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
412 fileNb = 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
413 line = inF.readline()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
414 if not line or N == 0:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
415 outFileName = "%s-%s%s" %(filePrefix, fileNb, fileExt)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
416 f = open(outFileName, "wb")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
417 shutil.copyfileobj(open(fileName, "rb"), f)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
418 f.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
419 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
420 while line:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
421 outFileName = "%s-%s%s" %(filePrefix, fileNb, fileExt)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
422 with open(outFileName, "w") as outF:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
423 lineNb = 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
424 while lineNb <= N and line:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
425 outF.write(line)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
426 line = inF.readline()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
427 lineNb += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
428 fileNb += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
429
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
430 ## Concatenates names from a list, using a given separator and a given extension.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
431 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
432 # @param lNames list of file names
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
433 # @param sep separator used to join names
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
434 # @param ext extension of the return file name. If None, the most represented extension in lNames is used.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
435 # If there is several, the first extension of theses several in alphabetical order is used
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
436 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
437 # @return concatName name concatenated
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
438 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
439 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
440 def concatenateFileNamesFromList(lNames, sep = "_", ext = None):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
441 concatName = ""
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
442 if lNames:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
443 lNames.sort()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
444 tBaseNames, tExt = zip(*[os.path.splitext(os.path.basename(name)) for name in lNames])
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
445
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
446 if ext is None:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
447 dtExtToNb = {}
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
448 for extension in set(tExt):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
449 dtExtToNb[extension] = tExt.count(extension)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
450
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
451 items = sorted(dtExtToNb.items(), key = itemgetter(0))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
452 items.sort(key = itemgetter(1), reverse = True)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
453 ext = items[0][0]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
454
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
455 if ext and ext[0] != '.':
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
456 ext = ".%s" % ext
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
457
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
458 concatName = "%s%s" % (sep.join(tBaseNames), ext)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
459 return concatName
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
460
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
461 ## Concatenates names from a string, using a given separator and a given extension. Names are split from the string using splitSep
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
462 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
463 # @param filesNames list of file names
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
464 # @param splitSep separator used to split names from the input string
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
465 # @param joinSep separator used to join names
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
466 # @param ext extension of the return file name. If None, the most represented extension in lNames is used.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
467 # If there is several, the first extension of theses several in alphabetical order is used
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
468 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
469 # @return concatName,lFilesNames name concatenated and split files list sorted alphabetically. Return original name if splitSep is empty.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
470 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
471 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
472 def concatenateFileNamesFromString(filesNames, splitSep = ",", joinSep = "_", ext = None):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
473 if splitSep:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
474 lFilesNames = filesNames.split(splitSep)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
475 return FileUtils.concatenateFileNamesFromList(lFilesNames, joinSep, ext), lFilesNames
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
476 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
477 print "WARNING: no split separator provided, returning input string"
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
478 return filesNames, [filesNames]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
479