| 1 | ################################################################################
|
|---|
| 2 | # _fileutil.py
|
|---|
| 3 | #
|
|---|
| 4 | # This program is free software; you can redistribute it and/or
|
|---|
| 5 | # modify it under the terms of the GNU General Public License
|
|---|
| 6 | # as published by the Free Software Foundation; either version 2
|
|---|
| 7 | # of the License, or (at your option) any later version.
|
|---|
| 8 | #
|
|---|
| 9 | # This program is distributed in the hope that it will be useful,
|
|---|
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 12 | # GNU General Public License for more details.
|
|---|
| 13 | #
|
|---|
| 14 | # You should have received a copy of the GNU General Public License
|
|---|
| 15 | # along with this program; if not, write to the Free Software
|
|---|
| 16 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|---|
| 17 | ################################################################################
|
|---|
| 18 |
|
|---|
| 19 | # Imports.
|
|---|
| 20 | from ZPublisher.Iterators import filestream_iterator
|
|---|
| 21 | import fnmatch
|
|---|
| 22 | import os
|
|---|
| 23 | import shutil
|
|---|
| 24 | import stat
|
|---|
| 25 | import tempfile
|
|---|
| 26 | import zipfile
|
|---|
| 27 | # Product Imports.
|
|---|
| 28 | import _globals
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 32 | _fileutil.import_zexp:
|
|---|
| 33 |
|
|---|
| 34 | Import zexp.
|
|---|
| 35 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 36 | def import_zexp(self, zexp, new_id, id_prefix, _sort_id=0):
|
|---|
| 37 | # Import
|
|---|
| 38 | filename = zexp.title_or_id()
|
|---|
| 39 | fileid = filename[:filename.find('.')]
|
|---|
| 40 | filepath = INSTANCE_HOME + '/import/' + filename
|
|---|
| 41 | exportObj( zexp, filepath)
|
|---|
| 42 | importZexp( self, filename)
|
|---|
| 43 |
|
|---|
| 44 | # Rename
|
|---|
| 45 | if new_id != fileid:
|
|---|
| 46 | self.manage_renameObject(fileid,new_id)
|
|---|
| 47 |
|
|---|
| 48 | ## Normalize Sort-IDs
|
|---|
| 49 | obj = getattr( self, new_id)
|
|---|
| 50 | obj.sort_id = _sort_id
|
|---|
| 51 | self.normalizeSortIds( id_prefix)
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 55 | _fileutil.importZexp:
|
|---|
| 56 |
|
|---|
| 57 | Import file from specified path.
|
|---|
| 58 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 59 | def importZexp(self, filename):
|
|---|
| 60 | filepath = INSTANCE_HOME + '/import/' + filename
|
|---|
| 61 | self.manage_importObject(str(filename))
|
|---|
| 62 | remove(filepath)
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 66 | _fileutil.extractFilename:
|
|---|
| 67 |
|
|---|
| 68 | Extract filename from path.
|
|---|
| 69 | IN: path
|
|---|
| 70 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 71 | def extractFilename(path, sep=None, undoable=False):
|
|---|
| 72 | if sep is None:
|
|---|
| 73 | path = getOSPath(path,undoable=undoable)
|
|---|
| 74 | items = path.split( os.sep)
|
|---|
| 75 | lastitem = items[len(items)-1]
|
|---|
| 76 | return lastitem
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 80 | _fileutil.extractFileExt:
|
|---|
| 81 |
|
|---|
| 82 | Extract fileextension from path.
|
|---|
| 83 | IN: path
|
|---|
| 84 | OUT: extension
|
|---|
| 85 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 86 | def extractFileExt(path):
|
|---|
| 87 | items = path.split('.')
|
|---|
| 88 | lastitem = items[len(items)-1]
|
|---|
| 89 | return lastitem
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 93 | _fileutil.getOSPath:
|
|---|
| 94 |
|
|---|
| 95 | Return path with OS separators.
|
|---|
| 96 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 97 | def getOSPath(path, chs=range(32)+[34,39,60,62,63,127], undoable=False):
|
|---|
| 98 | path = path.replace('\\',os.sep)
|
|---|
| 99 | path = path.replace('/',os.sep)
|
|---|
| 100 | if type( path) is str:
|
|---|
| 101 | path = unicode(path, 'latin-1')
|
|---|
| 102 | if undoable or os.name != "nt":
|
|---|
| 103 | path = path.encode('ascii', 'replace') # replace uncodable characters by ? (63)
|
|---|
| 104 | if len( chs) > 0:
|
|---|
| 105 | for ch in chs:
|
|---|
| 106 | path = path.replace(chr(ch),'')
|
|---|
| 107 | return path
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 111 | _fileutil.absoluteOSPath:
|
|---|
| 112 |
|
|---|
| 113 | Return absolute-path with OS separators.
|
|---|
| 114 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 115 | def absoluteOSPath(path):
|
|---|
| 116 | path = getOSPath(path)
|
|---|
| 117 | l0 = path.split(os.sep)
|
|---|
| 118 | l1 = []
|
|---|
| 119 | for i in l0:
|
|---|
| 120 | if i == '..':
|
|---|
| 121 | l1 = l1[:-1]
|
|---|
| 122 | else:
|
|---|
| 123 | l1 = l1 + [i]
|
|---|
| 124 | path = os.sep.join(l1)
|
|---|
| 125 | return path
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 129 | _fileutil.getFilePath:
|
|---|
| 130 |
|
|---|
| 131 | Extract filepath from path (cut-off filename).
|
|---|
| 132 | IN: path
|
|---|
| 133 | OUT: filepath
|
|---|
| 134 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 135 | def getFilePath(path):
|
|---|
| 136 | items = getOSPath(path).split(os.sep)
|
|---|
| 137 | filepath = ''
|
|---|
| 138 | for i in range(len(items)-1):
|
|---|
| 139 | filepath = filepath + items[i] + os.sep
|
|---|
| 140 | if len(filepath) > 0:
|
|---|
| 141 | if filepath[-1] == os.sep:
|
|---|
| 142 | filepath = filepath[:-1]
|
|---|
| 143 | return filepath
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 147 | _fileutil.findExtension:
|
|---|
| 148 |
|
|---|
| 149 | Searches path and all subdirectories for file with extension and returns
|
|---|
| 150 | complete filepath. Returns None if no file with specified extension exists.
|
|---|
| 151 | IN: extension
|
|---|
| 152 | path
|
|---|
| 153 | OUT: filepath
|
|---|
| 154 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 155 | def findExtension(extension, path, deep=1):
|
|---|
| 156 | rtn = None
|
|---|
| 157 | path = getOSPath(path)
|
|---|
| 158 | for file in os.listdir(path):
|
|---|
| 159 | filepath = path + os.sep + file
|
|---|
| 160 | if extractFileExt(file).lower() == extension:
|
|---|
| 161 | return filepath
|
|---|
| 162 | elif deep:
|
|---|
| 163 | mode = os.stat(filepath)[stat.ST_MODE]
|
|---|
| 164 | if stat.S_ISDIR(mode):
|
|---|
| 165 | rtn = findExtension(extension,filepath,deep)
|
|---|
| 166 | if rtn is not None:
|
|---|
| 167 | return rtn
|
|---|
| 168 | return rtn
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 172 | _fileutil.readPath:
|
|---|
| 173 |
|
|---|
| 174 | Reads path.
|
|---|
| 175 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 176 | def readPath(path, data=True, recursive=True):
|
|---|
| 177 | l = []
|
|---|
| 178 | path = path.replace('\\',os.sep)
|
|---|
| 179 | path = path.replace('/',os.sep)
|
|---|
| 180 | filter = extractFilename(path, os.sep)
|
|---|
| 181 | if filter.find('*') >= 0 and filter.find('.') >= 0:
|
|---|
| 182 | path = getFilePath( path)
|
|---|
| 183 | else:
|
|---|
| 184 | filter = None
|
|---|
| 185 | mode = os.stat(path)[stat.ST_MODE]
|
|---|
| 186 | if stat.S_ISDIR(mode):
|
|---|
| 187 | for filename in os.listdir(path):
|
|---|
| 188 | local_filename = path + os.sep + filename
|
|---|
| 189 | mode = os.stat(local_filename)[stat.ST_MODE]
|
|---|
| 190 | if filter is None or fnmatch.fnmatch(filename, filter):
|
|---|
| 191 | u_local_filename = local_filename
|
|---|
| 192 | if type(u_local_filename) is not unicode:
|
|---|
| 193 | u_local_filename = unicode(local_filename,'latin-1')
|
|---|
| 194 | d = {}
|
|---|
| 195 | d['local_filename']=u_local_filename.encode('utf-8')
|
|---|
| 196 | d['filename']=extractFilename(u_local_filename).encode('utf-8')
|
|---|
| 197 | if stat.S_ISDIR(mode):
|
|---|
| 198 | mtime = os.path.getmtime( local_filename)
|
|---|
| 199 | d['mtime']=mtime
|
|---|
| 200 | d['isdir']=True
|
|---|
| 201 | l.append(d)
|
|---|
| 202 | else:
|
|---|
| 203 | fdata = None
|
|---|
| 204 | if data:
|
|---|
| 205 | fdata, mt, enc, fsize = readFile( local_filename, 'b', -1)
|
|---|
| 206 | d['data']=fdata
|
|---|
| 207 | d['content_type']=mt
|
|---|
| 208 | d['encoding']=enc
|
|---|
| 209 | else:
|
|---|
| 210 | try:
|
|---|
| 211 | mt, enc = _globals.guess_contenttype( local_filename)
|
|---|
| 212 | except:
|
|---|
| 213 | mt, enc = 'content/unknown', ''
|
|---|
| 214 | d['content_type']=mt
|
|---|
| 215 | d['encoding']=enc
|
|---|
| 216 | fsize = os.path.getsize( local_filename)
|
|---|
| 217 | d['size']=fsize
|
|---|
| 218 | mtime = os.path.getmtime( local_filename)
|
|---|
| 219 | d['mtime']=mtime
|
|---|
| 220 | d['isdir']=False
|
|---|
| 221 | l.append(d)
|
|---|
| 222 | if stat.S_ISDIR(mode) and recursive:
|
|---|
| 223 | if filter is not None:
|
|---|
| 224 | local_filename = local_filename + '/' + filter
|
|---|
| 225 | l.extend(readPath(local_filename, data, recursive))
|
|---|
| 226 | return l
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 230 | _fileutil.readFile:
|
|---|
| 231 |
|
|---|
| 232 | Reads file (threshold for filesteam_iterator is 128 kb).
|
|---|
| 233 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 234 | def readFile(filename, mode='b', threshold=2 << 16):
|
|---|
| 235 | size = os.path.getsize( filename)
|
|---|
| 236 | if size < threshold or -1 == threshold:
|
|---|
| 237 | f = None
|
|---|
| 238 | try:
|
|---|
| 239 | f = open( filename, 'r'+mode)
|
|---|
| 240 | data = f.read()
|
|---|
| 241 | finally:
|
|---|
| 242 | if f is not None:
|
|---|
| 243 | f.close()
|
|---|
| 244 | else:
|
|---|
| 245 | data = filestream_iterator( filename, 'r'+mode)
|
|---|
| 246 | try:
|
|---|
| 247 | mt, enc = _globals.guess_contenttype( filename, data)
|
|---|
| 248 | except:
|
|---|
| 249 | mt, enc = 'content/unknown', ''
|
|---|
| 250 | return data, mt, enc, size
|
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 254 | _fileutil.remove:
|
|---|
| 255 |
|
|---|
| 256 | Removes path.
|
|---|
| 257 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 258 | def remove(path, deep=0):
|
|---|
| 259 | path = getOSPath(path)
|
|---|
| 260 | mode = os.stat(path)[stat.ST_MODE]
|
|---|
| 261 | if stat.S_ISDIR(mode):
|
|---|
| 262 | shutil.rmtree(path)
|
|---|
| 263 | else:
|
|---|
| 264 | os.remove(path)
|
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 268 | _fileutil.getDataSizeStr:
|
|---|
| 269 |
|
|---|
| 270 | Display string for file-size.
|
|---|
| 271 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 272 | def getDataSizeStr(len):
|
|---|
| 273 | s = ''
|
|---|
| 274 | try:
|
|---|
| 275 | mod = 0
|
|---|
| 276 | while len > 1024.0 and mod < 3:
|
|---|
| 277 | len = len / 1024.0
|
|---|
| 278 | mod = mod + 1
|
|---|
| 279 | s = str(int(len))
|
|---|
| 280 | n = str(int(10*(len - int(len))))
|
|---|
| 281 | if mod == 0:
|
|---|
| 282 | s = "%sBytes"%s
|
|---|
| 283 | elif mod == 1:
|
|---|
| 284 | s = "%sKB"%s
|
|---|
| 285 | elif mod == 2:
|
|---|
| 286 | s = "%s.%s MB"%(s,n)
|
|---|
| 287 | elif mod == 3:
|
|---|
| 288 | s = "%s.%s GB"%(s,n)
|
|---|
| 289 | except:
|
|---|
| 290 | pass
|
|---|
| 291 | return s
|
|---|
| 292 |
|
|---|
| 293 |
|
|---|
| 294 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 295 | _fileutil.executeCommand:
|
|---|
| 296 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 297 | def executeCommand(path, command):
|
|---|
| 298 | os.chdir(path)
|
|---|
| 299 | os.system(command)
|
|---|
| 300 |
|
|---|
| 301 |
|
|---|
| 302 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 303 | _fileutil.exportObj:
|
|---|
| 304 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 305 | def exportObj(obj, filename, filetype='b'):
|
|---|
| 306 |
|
|---|
| 307 | #-- Try to create directory-tree.
|
|---|
| 308 | filename = getOSPath(filename)
|
|---|
| 309 | filepath = getFilePath(filename)
|
|---|
| 310 | mkDir(filepath)
|
|---|
| 311 |
|
|---|
| 312 | #-- Get object data.
|
|---|
| 313 | data = None
|
|---|
| 314 | try: # ImageFile
|
|---|
| 315 | f = open(obj.path,'r%s'%filetype)
|
|---|
| 316 | data = f.read()
|
|---|
| 317 | f.close()
|
|---|
| 318 | except:
|
|---|
| 319 | try: # Image / File
|
|---|
| 320 | data = obj.data
|
|---|
| 321 | if len(data) == 0:
|
|---|
| 322 | data = obj.getData()
|
|---|
| 323 | except:
|
|---|
| 324 | try:
|
|---|
| 325 | data = obj.raw # DTML Method
|
|---|
| 326 | except:
|
|---|
| 327 | try:
|
|---|
| 328 | data = obj.read() # REQUEST.enctype multipart/form-data
|
|---|
| 329 | except:
|
|---|
| 330 | data = str(obj)
|
|---|
| 331 |
|
|---|
| 332 | #-- Save to file.
|
|---|
| 333 | if data is not None:
|
|---|
| 334 | objfile = open(filename,'w%s'%filetype)
|
|---|
| 335 | if type(data) is str or type(data) is unicode:
|
|---|
| 336 | objfile.write(data)
|
|---|
| 337 | else:
|
|---|
| 338 | while data is not None:
|
|---|
| 339 | objfile.write(data.data)
|
|---|
| 340 | data=data.next
|
|---|
| 341 | objfile.close()
|
|---|
| 342 |
|
|---|
| 343 |
|
|---|
| 344 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 345 | _fileutil.mkDir:
|
|---|
| 346 |
|
|---|
| 347 | Make directory.
|
|---|
| 348 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 349 | def mkDir(path):
|
|---|
| 350 | try:
|
|---|
| 351 | os.makedirs( path)
|
|---|
| 352 | except:
|
|---|
| 353 | pass
|
|---|
| 354 |
|
|---|
| 355 |
|
|---|
| 356 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 357 | _fileutil.readDir
|
|---|
| 358 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 359 | def readDir(path):
|
|---|
| 360 | obs = []
|
|---|
| 361 | path = getOSPath(path)
|
|---|
| 362 | for file in os.listdir(path):
|
|---|
| 363 | ob = {}
|
|---|
| 364 | ob['path'] = path + os.sep
|
|---|
| 365 | ob['file'] = file
|
|---|
| 366 | filepath = path + os.sep + file
|
|---|
| 367 | ob['mtime'] = os.path.getmtime(filepath)
|
|---|
| 368 | ob['size'] = os.path.getsize(filepath)
|
|---|
| 369 | mode = os.stat(filepath)[stat.ST_MODE]
|
|---|
| 370 | if stat.S_ISDIR(mode):
|
|---|
| 371 | ob['type'] = 'd'
|
|---|
| 372 | else:
|
|---|
| 373 | ob['type'] = 'f'
|
|---|
| 374 | obs.append((ob['type'],ob))
|
|---|
| 375 | obs.sort()
|
|---|
| 376 | return map(lambda ob: ob[1],obs)
|
|---|
| 377 |
|
|---|
| 378 |
|
|---|
| 379 | ################################################################################
|
|---|
| 380 | ###
|
|---|
| 381 | ### ZIP
|
|---|
| 382 | ###
|
|---|
| 383 | ################################################################################
|
|---|
| 384 |
|
|---|
| 385 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 386 | _fileutil.getZipArchive:
|
|---|
| 387 |
|
|---|
| 388 | Extract files from zip-archive and return list of extracted files.
|
|---|
| 389 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 390 | def getZipArchive(f):
|
|---|
| 391 | l = []
|
|---|
| 392 |
|
|---|
| 393 | # Saved zip-file in temp-folder.
|
|---|
| 394 | tempfolder = tempfile.mktemp()
|
|---|
| 395 | filename = tempfolder + os.sep + extractFilename(tempfolder) + '.zip'
|
|---|
| 396 | exportObj(f,filename)
|
|---|
| 397 |
|
|---|
| 398 | # Unzip zip-file.
|
|---|
| 399 | extractZipArchive(filename)
|
|---|
| 400 |
|
|---|
| 401 | # Remove zip-file.
|
|---|
| 402 | remove(filename)
|
|---|
| 403 |
|
|---|
| 404 | # Read extracted files.
|
|---|
| 405 | l = readPath(tempfolder,data=True)
|
|---|
| 406 |
|
|---|
| 407 | # Remove temp-folder.
|
|---|
| 408 | remove(tempfolder,deep=1)
|
|---|
| 409 |
|
|---|
| 410 | # Return list of files.
|
|---|
| 411 | return l
|
|---|
| 412 |
|
|---|
| 413 |
|
|---|
| 414 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 415 | _fileutil.extractZipArchive:
|
|---|
| 416 |
|
|---|
| 417 | Unpack ZIP-Archive.
|
|---|
| 418 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 419 | def extractZipArchive(file):
|
|---|
| 420 | l = []
|
|---|
| 421 |
|
|---|
| 422 | zf = zipfile.ZipFile( file, 'r')
|
|---|
| 423 | for name in zf.namelist():
|
|---|
| 424 | dir = getOSPath( name)
|
|---|
| 425 | i = dir.rfind( os.sep)
|
|---|
| 426 | if i > 0:
|
|---|
| 427 | dir = getFilePath(file) + os.sep + dir[ :i]
|
|---|
| 428 | mkDir( dir)
|
|---|
| 429 | localname = getOSPath( getFilePath(file) + os.sep + name)
|
|---|
| 430 | if localname[-1] != os.sep:
|
|---|
| 431 | l.append( localname)
|
|---|
| 432 | f = open( localname, 'wb')
|
|---|
| 433 | f.write( zf.read( name))
|
|---|
| 434 | f.close()
|
|---|
| 435 | zf.close()
|
|---|
| 436 |
|
|---|
| 437 | # Return list of files.
|
|---|
| 438 | return l
|
|---|
| 439 |
|
|---|
| 440 |
|
|---|
| 441 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 442 | _fileutil.writeZipFile:
|
|---|
| 443 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 444 | def writeZipFile( zf, basepath, path, filter):
|
|---|
| 445 | for file in os.listdir( path):
|
|---|
| 446 | filepath = path+os.sep+file
|
|---|
| 447 | mode = os.stat( filepath)[stat.ST_MODE]
|
|---|
| 448 | if stat.S_ISDIR( mode):
|
|---|
| 449 | writeZipFile( zf, basepath, filepath, filter)
|
|---|
| 450 | else:
|
|---|
| 451 | arcname = filepath[len( basepath)+1:]
|
|---|
| 452 | match = False
|
|---|
| 453 | for pattern in filter.split(';'):
|
|---|
| 454 | match = match or fnmatch.fnmatch( arcname, pattern)
|
|---|
| 455 | if match:
|
|---|
| 456 | zf.write( filepath, str(arcname))
|
|---|
| 457 |
|
|---|
| 458 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 459 | _fileutil.buildZipArchive:
|
|---|
| 460 |
|
|---|
| 461 | Pack ZIP-Archive and return data.
|
|---|
| 462 | """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
|---|
| 463 | def buildZipArchive( files, get_data=True):
|
|---|
| 464 |
|
|---|
| 465 | # Create temporary zip-file.
|
|---|
| 466 | zipfilename = tempfile.mktemp() + '.zip'
|
|---|
| 467 |
|
|---|
| 468 | # Write filtered files to zip-file.
|
|---|
| 469 | files = getOSPath( files)
|
|---|
| 470 | path = getFilePath( files)
|
|---|
| 471 | filter = extractFilename( files)
|
|---|
| 472 | zf = zipfile.ZipFile( zipfilename, 'w')
|
|---|
| 473 | writeZipFile( zf, path, path, filter)
|
|---|
| 474 | zf.close()
|
|---|
| 475 |
|
|---|
| 476 | # Read data from zip-file as return value.
|
|---|
| 477 | data = zipfilename
|
|---|
| 478 | if get_data:
|
|---|
| 479 | f = open( zipfilename, 'rb')
|
|---|
| 480 | data = f.read()
|
|---|
| 481 | f.close()
|
|---|
| 482 |
|
|---|
| 483 | # Remove temporary zip-file.
|
|---|
| 484 | os.remove( zipfilename)
|
|---|
| 485 |
|
|---|
| 486 | # Returns filename or data of zip-file.
|
|---|
| 487 | return data
|
|---|
| 488 |
|
|---|
| 489 | ################################################################################ |
|---|