Package PyFoam :: Package Basics :: Module Utilities
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Basics.Utilities

  1  #  ICE Revision: $Id: /local/openfoam/Python/PyFoam/PyFoam/Basics/Utilities.py 7538 2011-07-19T09:17:44.150060Z bgschaid  $  
  2  """ Utility functions 
  3   
  4  Can be used via a class or as functions""" 
  5   
  6  import sys 
  7   
  8  if sys.version_info<(2,6): 
  9      from popen2 import popen4 
 10  else: 
 11      from subprocess import Popen,PIPE,STDOUT 
 12  from os import listdir,path,remove as removeFile 
 13   
 14  import re 
 15   
 16  try: 
 17      import shutil 
 18  except ImportError: 
 19      # this is an old python-version without it. We'll try to work around it 
 20      pass 
 21   
22 -class Utilities(object):
23 """Class with utility methods 24 25 Can be inherited without side effects by classes that need these 26 methods""" 27
28 - def __init__(self):
29 pass
30
31 - def execute(self,cmd,debug=False):
32 """Execute the command cmd 33 34 Currently no error-handling is done 35 @return: A list with all the output-lines of the execution""" 36 if debug: 37 print cmd 38 39 if sys.version_info<(2,6): 40 raus,rein = popen4(cmd) 41 else: 42 p = Popen(cmd, shell=True, 43 stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True) 44 (rein,raus)=(p.stdin,p.stdout) 45 tmp=raus.readlines() 46 # line=raus.readline() 47 # while line!="": 48 # print line 49 # line=raus.readline() 50 51 return tmp
52
53 - def remove(self,f):
54 """Remove a file if it exists.""" 55 if path.exists(f): 56 removeFile(f)
57
58 - def rmtree(self,path,ignore_errors=False):
59 """Encapsulates the shutil rmtree and provides an alternative for 60 old Python-version""" 61 try: 62 shutil.rmtree(path,ignore_errors=ignore_errors) 63 except NameError: 64 self.execute("rm -rf "+path)
65
66 - def copytree(self,src,dst, 67 symlinks=False):
68 """Encapsulates the shutil copytree and provides an alternative for 69 old Python-version""" 70 try: 71 if path.isdir(dst): 72 dst=path.join(dst,path.basename(path.abspath(src))) 73 if path.isdir(src): 74 shutil.copytree(src,dst, 75 symlinks=symlinks) 76 else: 77 self.copyfile(src,dst) 78 except NameError: 79 cpOptions="-R" 80 if not symlinks: 81 cpOptions+=" -L" 82 self.execute("cp "+cpOptions+" "+src+" "+dst)
83
84 - def copyfile(self,src,dst):
85 """Encapsulates the shutil copyfile and provides an alternative for 86 old Python-version""" 87 try: 88 if path.isdir(dst): 89 dst=path.join(dst,path.basename(path.abspath(src))) 90 shutil.copyfile(src,dst) 91 except NameError: 92 self.execute("cp "+src+" "+dst)
93
94 - def writeDictionaryHeader(self,f):
95 """Writes a dummy header so OpenFOAM accepts the file as a dictionary 96 @param f: The file to write to 97 @type f: file""" 98 99 f.write(""" 100 // * * * * * * * * * // 101 FoamFile 102 { 103 version 0.5; 104 format ascii; 105 root "ROOT"; 106 case "CASE"; 107 class dictionary; 108 object nix; 109 } 110 """)
111 112 excludeNames=["^.svn$" , "~$"] 113
114 - def listDirectory(self,d):
115 """Lists the files in a directory, but excludes certain names 116 and files with certain endings 117 @param d: The directory to list 118 @return: List of the found files and directories""" 119 120 result=[] 121 122 excludes=map(re.compile,self.excludeNames) 123 124 for n in listdir(d): 125 ok=True 126 127 for e in excludes: 128 if e.search(n): 129 ok=False 130 break 131 132 if ok: 133 result.append(n) 134 135 return result
136
137 -def execute(cmd,debug=False):
138 """Calls the method of the same name from the Utilites class""" 139 return Utilities().execute(cmd,debug)
140
141 -def writeDictionaryHeader(f):
142 """Calls the method of the same name from the Utilites class""" 143 Utilities().writeDictionaryHeader(f)
144
145 -def listDirectory(d):
146 """Calls the method of the same name from the Utilites class""" 147 return Utilities().listDirectory(d)
148
149 -def rmtree(path,ignore_errors=False):
150 """Calls the method of the same name from the Utilites class""" 151 return Utilities().rmtree(path,ignore_errors=ignore_errors)
152
153 -def copytree(src,dest,symlinks=False):
154 """Calls the method of the same name from the Utilites class""" 155 return Utilities().copytree(src,dest,symlinks=symlinks)
156
157 -def remove(f):
158 """Calls the method of the same name from the Utilites class""" 159 return Utilities().remove(f)
160
161 -def copyfile(src,dest):
162 """Calls the method of the same name from the Utilites class""" 163 return Utilities().copyfile(src,dest)
164