1
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
13
14 import re
15
17 """Class with utility methods
18
19 Can be inherited without side effects by classes that need these
20 methods"""
21
24
26 """Execute the command cmd
27
28 Currently no error-handling is done
29 @return: A list with all the output-lines of the execution"""
30 if debug:
31 print cmd
32
33 if sys.version_info<(2,6):
34 raus,rein = popen4(cmd)
35 else:
36 p = Popen(cmd, shell=True,
37 stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
38 (rein,raus)=(p.stdin,p.stdout)
39 tmp=raus.readlines()
40
41
42
43
44
45 return tmp
46
48 """Writes a dummy header so OpenFOAM accepts the file as a dictionary
49 @param f: The file to write to
50 @type f: file"""
51
52 f.write("""
53 // * * * * * * * * * //
54 FoamFile
55 {
56 version 0.5;
57 format ascii;
58 root "ROOT";
59 case "CASE";
60 class dictionary;
61 object nix;
62 }
63 """)
64
65 excludeNames=["^.svn$" , "~$"]
66
68 """Lists the files in a directory, but excludes certain names
69 and files with certain endings
70 @param d: The directory to list
71 @return: List of the found files and directories"""
72
73 result=[]
74
75 excludes=map(re.compile,self.excludeNames)
76
77 for n in listdir(d):
78 ok=True
79
80 for e in excludes:
81 if e.search(n):
82 ok=False
83 break
84
85 if ok:
86 result.append(n)
87
88 return result
89
91 """Calls the method of the same name from the Utilites class"""
92 return Utilities().execute(cmd,debug)
93
97
101