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