1
2 """ Utility functions
3
4 Can be used via a class or as functions"""
5
6 import sys
7 from PyFoam.ThirdParty.six import print_
8 from PyFoam.Error import warning
9 import subprocess
10 import os,fnmatch
11
12 if sys.version_info<(2,6):
13 from popen2 import popen4
14 else:
15 from subprocess import Popen,PIPE,STDOUT
16 from os import listdir,path,remove as removeFile
17
18 import re
19
20 try:
21 import shutil
22 except ImportError:
23
24 pass
25
27 """Class with utility methods
28
29 Can be inherited without side effects by classes that need these
30 methods"""
31
34
36 """Execute the command cmd
37
38 Currently no error-handling is done
39 @return: A list with all the output-lines of the execution"""
40 if debug:
41 print_(cmd)
42
43 if sys.version_info<(2,6):
44 raus,rein = popen4(cmd)
45 else:
46 p = Popen(cmd, shell=True,
47 stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
48 (rein,raus)=(p.stdin,p.stdout)
49 tmp=raus.readlines()
50
51
52
53
54
55 return tmp
56
58 """Remove a file if it exists."""
59 if path.exists(f):
60 removeFile(f)
61
62 - def rmtree(self,path,ignore_errors=False):
63 """Encapsulates the shutil rmtree and provides an alternative for
64 old Python-version"""
65 try:
66 shutil.rmtree(path,ignore_errors=ignore_errors)
67 except NameError:
68 self.execute("rm -rf "+path)
69
70 - def copytree(self,src,dst,
71 symlinks=False):
72 """Encapsulates the shutil copytree and provides an alternative for
73 old Python-version"""
74 try:
75 if path.isdir(dst):
76 dst=path.join(dst,path.basename(path.abspath(src)))
77 if path.isdir(src):
78 shutil.copytree(src,dst,
79 symlinks=symlinks)
80 else:
81 self.copyfile(src,dst)
82 except NameError:
83 cpOptions="-R"
84 if not symlinks:
85 cpOptions+=" -L"
86 self.execute("cp "+cpOptions+" "+src+" "+dst)
87
89 """Encapsulates the shutil copyfile and provides an alternative for
90 old Python-version"""
91 try:
92 if path.isdir(dst):
93 dst=path.join(dst,path.basename(path.abspath(src)))
94 shutil.copyfile(src,dst)
95 shutil.copymode(src,dst)
96 except NameError:
97 self.execute("cp "+src+" "+dst)
98
100 """Writes a dummy header so OpenFOAM accepts the file as a dictionary
101 @param f: The file to write to
102 @type f: file"""
103
104 f.write("""
105 // * * * * * * * * * //
106 FoamFile
107 {
108 version 0.5;
109 format ascii;
110 root "ROOT";
111 case "CASE";
112 class dictionary;
113 object nix;
114 }
115 """)
116
117 excludeNames=["^.svn$" , "~$"]
118
120 """Lists the files in a directory, but excludes certain names
121 and files with certain endings
122 @param d: The directory to list
123 @return: List of the found files and directories"""
124
125 result=[]
126
127 excludes=list(map(re.compile,self.excludeNames))
128
129 for n in listdir(d):
130 ok=True
131
132 for e in excludes:
133 if e.search(n):
134 ok=False
135 break
136
137 if ok:
138 result.append(n)
139
140 return result
141
142 - def which(self,progname):
143 """Get the full path. Return None if not found"""
144 pipe = subprocess.Popen('which '+progname,
145 shell=True,
146 stdout=subprocess.PIPE,
147 stderr=subprocess.STDOUT)
148
149 (fullname, errout) = pipe.communicate(input=input)
150
151 stat = pipe.returncode
152
153 if stat:
154 warning("which can not find a match for",progname)
155 return None
156 else:
157 return fullname
158
159 - def find(self,pattern, path,directoriesToo=True):
160 """Find all files whose names match
161 @param pattern: glob-style pattern
162 @param path: path under which this files are to be searched
163 @param directoriesToo: also match directories?"""
164 result = []
165 for root, dirs, files in os.walk(path):
166 for name in files:
167 if fnmatch.fnmatch(name, pattern):
168 result.append(os.path.join(root, name))
169 if directoriesToo:
170 for name in dirs:
171 if fnmatch.fnmatch(name, pattern):
172 result.append(os.path.join(root, name))
173 return result
174
176 """Calls the method of the same name from the Utilites class"""
177 return Utilities().which(prog)
178
180 """Calls the method of the same name from the Utilites class"""
181 return Utilities().execute(cmd,debug)
182
186
190
191 -def rmtree(path,ignore_errors=False):
192 """Calls the method of the same name from the Utilites class"""
193 return Utilities().rmtree(path,ignore_errors=ignore_errors)
194
196 """Calls the method of the same name from the Utilites class"""
197 return Utilities().copytree(src,dest,symlinks=symlinks)
198
200 """Calls the method of the same name from the Utilites class"""
201 return Utilities().remove(f)
202
204 """Calls the method of the same name from the Utilites class"""
205 return Utilities().copyfile(src,dest)
206
207 -def find(pattern,path,directoriesToo=True):
208 """Calls the method of the same name from the Utilites class"""
209 return Utilities().find(pattern,path,directoriesToo=directoriesToo)
210
211
212