source: ZMS/trunk/_fileutil.py @ 1743

Revision 1743, 14.5 KB checked in by zmsdev, 2 months ago (diff)

applied minor fixes for (ajax) file-upload

Line 
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.
20from ZPublisher.Iterators import filestream_iterator
21import fnmatch
22import os
23import shutil
24import stat
25import tempfile
26import zipfile
27# Product Imports.
28import _globals
29
30
31"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
32_fileutil.import_zexp:
33
34Import zexp.
35"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
36def 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
57Import file from specified path.
58"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
59def 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
68Extract filename from path.
69IN:  path
70"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
71def 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
82Extract fileextension from path.
83IN:  path
84OUT: extension
85"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
86def extractFileExt(path):
87  items = path.split('.')
88  lastitem = items[len(items)-1]
89  return lastitem
90
91
92"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
93_fileutil.getOSPath:
94
95Return path with OS separators.
96"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
97def 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
113Return absolute-path with OS separators.
114"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
115def 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
131Extract filepath from path (cut-off filename).
132IN:  path
133OUT: filepath
134"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
135def 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
149Searches path and all subdirectories for file with extension and returns
150complete filepath. Returns None if no file with specified extension exists.
151IN:  extension
152     path
153OUT: filepath
154"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
155def 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
174Reads path.
175"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
176def 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
232Reads file (threshold for filesteam_iterator is 128 kb).
233"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
234def 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
256Removes path.
257"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
258def 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
270Display string for file-size.
271"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
272def 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"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
297def executeCommand(path, command):
298  os.chdir(path)
299  os.system(command)
300
301
302"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
303_fileutil.exportObj:
304"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
305def 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
347Make directory.
348"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
349def mkDir(path):
350  try:
351    os.makedirs( path)
352  except:
353    pass
354
355
356"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
357_fileutil.readDir
358"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
359def 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
388Extract files from zip-archive and return list of extracted files.
389"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
390def 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
417Unpack ZIP-Archive.
418"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
419def 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"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
444def 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
461Pack ZIP-Archive and return data.
462"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
463def 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################################################################################
Note: See TracBrowser for help on using the repository browser.