Package gluon :: Module fileutils
[hide private]
[frames] | no frames]

Source Code for Module gluon.fileutils

  1  """ 
  2  This file is part of web2py Web Framework (Copyrighted, 2007) 
  3  Developed by Massimo Di Pierro <mdipierro@cs.depaul.edu> 
  4  License: GPL v2 
  5  """ 
  6   
  7  import storage 
  8  import os 
  9  import re 
 10  import tarfile 
 11  import sys 
 12   
 13  __all__=['listdir', 'cleanpath', 'tar', 'untar', 'tar_compiled',  
 14           'get_session', 'check_credentials'] 
 15   
16 -def listdir(path,expression='^.+$',drop=True,add_dirs=False):
17 """ 18 like os.listdir() but you can specify a regex patter to filter filed. 19 if add_dirs==True the returned items will have the full path. 20 """ 21 if drop: n=len(path) 22 else: n=0 23 regex=re.compile(expression) 24 items=[] 25 for root,dirs,files in os.walk(path): 26 if add_dirs: items.append(root[n:]) 27 for file in files: 28 if regex.match(file): 29 items.append(os.path.join(root,file)[n:]) 30 items.sort() 31 return items
32
33 -def cleanpath(path):
34 """ 35 turns any expression/path into a valid filename. replaces / with _ and 36 removes special characters. 37 """ 38 items=path.split('.') 39 if len(items)>1: path=re.sub('[^\w\.]+','_','_'.join(items[:-1])+'.'+''.join(items[-1:])) 40 else: path=re.sub('[^\w\.]+','_',''.join(items[-1:])) 41 return path
42
43 -def _extractall(filename, path='.', members=None):
44 if not hasattr(tarfile.TarFile, 'extractall'): 45 from tarfile import ExtractError 46 47 class TarFile(tarfile.TarFile): 48 def extractall(self, path=".", members=None): 49 """Extract all members from the archive to the current working 50 directory and set owner, modification time and permissions on 51 directories afterwards. `path' specifies a different directory 52 to extract to. `members' is optional and must be a subset of the 53 list returned by getmembers(). 54 """ 55 directories = [] 56 if members is None: 57 members = self 58 for tarinfo in members: 59 if tarinfo.isdir(): 60 # Extract directory with a safe mode, so that 61 # all files below can be extracted as well. 62 try: 63 os.makedirs(os.path.join(path, tarinfo.name), 0777) 64 except EnvironmentError: 65 pass 66 directories.append(tarinfo) 67 else: 68 self.extract(tarinfo, path) 69 # Reverse sort directories. 70 directories.sort(lambda a, b: cmp(a.name, b.name)) 71 directories.reverse() 72 # Set correct owner, mtime and filemode on directories. 73 for tarinfo in directories: 74 path = os.path.join(path, tarinfo.name) 75 try: 76 self.chown(tarinfo, path) 77 self.utime(tarinfo, path) 78 self.chmod(tarinfo, path) 79 except ExtractError, e: 80 if self.errorlevel > 1: 81 raise 82 else: 83 self._dbg(1, "tarfile: %s" % e)
84 _cls = TarFile 85 else: 86 _cls = tarfile.TarFile 87 88 return _cls(filename, 'r').extractall(path, members) 89
90 -def tar(file,dir,expression='^.+$'):
91 """ 92 tars dir into file, only tars file that match expression 93 """ 94 tar=tarfile.TarFile(file,'w') 95 for file in listdir(dir,expression,add_dirs=True): 96 tar.add(dir+file,file,False)
97
98 -def untar(file, dir):
99 """ 100 untar file into dir 101 """ 102 _extractall(file,dir)
103
104 -def tar_compiled(file,dir,expression='^.+$'):
105 """ 106 used to tar a compiled application. 107 the content of models, views, controllers is not stored in the tar file. 108 """ 109 tar=tarfile.TarFile(file,'w') 110 for file in listdir(dir,expression,add_dirs=True): 111 if file[:6]=='models': continue 112 if file[:5]=='views': continue 113 if file[:11]=='controllers': continue 114 tar.add(dir+file,file,False)
115
116 -def get_session(request,other_application='admin'):
117 """ checks that user is authorized to access other_application""" 118 if request.application==other_application: raise KeyError 119 try: 120 session_id=request.cookies['session_id_'+other_application].value 121 osession=storage.load_storage('applications/%s/sessions/%s' % \ 122 (other_application,session_id)) 123 except: osession=storage.Storage() 124 return osession
125
126 -def check_credentials(request,other_application='admin'):
127 """ checks that user is authorized to access other_application""" 128 return get_session(request,other_application).authorized
129
130 -def fix_newlines(path):
131 regex=re.compile(r'(\r\n|\r|\n)') 132 for filename in listdir(path,'.*\.(py|html)$',drop=False): 133 data=open(filename,'rb').read() 134 data=regex.sub('\n',data) 135 open(filename,'wb').write(data)
136
137 -def copystream(src,dest,size,chunk_size=10**5):
138 """ 139 this is here because I think there is a bug in shutil.copyfileobj 140 """ 141 while size>0: 142 if size<chunk_size: 143 data=src.read(size) 144 else: 145 data=src.read(chunk_size) 146 length=len(data) 147 if length>size: data,length=data[:size],size 148 size-=length 149 if length==0: break 150 dest.write(data) 151 if length<chunk_size: break 152 dest.seek(0) 153 return
154