annotate TEisotools-1.1.a/commons/core/utils/FileUtils.py @ 15:255c852351c5 draft

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