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 7785 2012-01-24T17:44:12.925376Z 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 shutil.copymode(src,dst) 92 except NameError: 93 self.execute("cp "+src+" "+dst)
94
95 - def writeDictionaryHeader(self,f):
96 """Writes a dummy header so OpenFOAM accepts the file as a dictionary 97 @param f: The file to write to 98 @type f: file""" 99 100 f.write(""" 101 // * * * * * * * * * // 102 FoamFile 103 { 104 version 0.5; 105 format ascii; 106 root "ROOT"; 107 case "CASE"; 108 class dictionary; 109 object nix; 110 } 111 """)
112 113 excludeNames=["^.svn$" , "~$"] 114
115 - def listDirectory(self,d):
116 """Lists the files in a directory, but excludes certain names 117 and files with certain endings 118 @param d: The directory to list 119 @return: List of the found files and directories""" 120 121 result=[] 122 123 excludes=map(re.compile,self.excludeNames) 124 125 for n in listdir(d): 126 ok=True 127 128 for e in excludes: 129 if e.search(n): 130 ok=False 131 break 132 133 if ok: 134 result.append(n) 135 136 return result
137
138 -def execute(cmd,debug=False):
139 """Calls the method of the same name from the Utilites class""" 140 return Utilities().execute(cmd,debug)
141
142 -def writeDictionaryHeader(f):
143 """Calls the method of the same name from the Utilites class""" 144 Utilities().writeDictionaryHeader(f)
145
146 -def listDirectory(d):
147 """Calls the method of the same name from the Utilites class""" 148 return Utilities().listDirectory(d)
149
150 -def rmtree(path,ignore_errors=False):
151 """Calls the method of the same name from the Utilites class""" 152 return Utilities().rmtree(path,ignore_errors=ignore_errors)
153
154 -def copytree(src,dest,symlinks=False):
155 """Calls the method of the same name from the Utilites class""" 156 return Utilities().copytree(src,dest,symlinks=symlinks)
157
158 -def remove(f):
159 """Calls the method of the same name from the Utilites class""" 160 return Utilities().remove(f)
161
162 -def copyfile(src,dest):
163 """Calls the method of the same name from the Utilites class""" 164 return Utilities().copyfile(src,dest)
165