1
2 """
3 Application class that implements pyFoamCompareDictionary.py
4 """
5
6 import re
7 from os import path
8
9 from PyFoamApplication import PyFoamApplication
10
11 from PyFoam.RunDictionary.ParsedParameterFile import ParsedParameterFile
12 from PyFoam.Basics.DataStructures import DictProxy,Dimension,Tensor,SymmTensor,Vector,Field,TupleProxy
13 from PyFoam.Basics.FoamFileGenerator import makeString
14
15 from PyFoam.Error import error,warning
16
19 description="""
20 Takes two dictionary and compares them semantically (by looking at the
21 structure, not the textual representation. If the dictionaries do not
22 have the same name, it looks for the destination file by searching the
23 equivalent place in the destination case
24 """
25
26 PyFoamApplication.__init__(self,args=args,description=description,usage="%prog [options] <source> <destination-case>",nr=2,interspersed=True)
27
29 self.parser.add_option("--not-equal",
30 action="store_true",
31 default=False,
32 dest="notequal",
33 help="Allow source and destination to have different names")
34 self.parser.add_option("--debug",
35 action="store_true",
36 default=False,
37 dest="debug",
38 help="Debug the comparing process")
39 self.parser.add_option("--long-field-threshold",
40 action="store",
41 type="int",
42 default=None,
43 dest="longlist",
44 help="Fields that are longer than this won't be parsed, but read into memory (nad compared as strings)")
45
46
47
49 sName=path.abspath(self.parser.getArgs()[0])
50 dName=path.abspath(self.parser.getArgs()[1])
51
52 try:
53 source=ParsedParameterFile(sName,backup=False,listLengthUnparsed=self.opts.longlist)
54 except IOError,e:
55 self.error("Problem with file",sName,":",e)
56
57 found=False
58
59 if path.isfile(sName) and path.isfile(dName):
60 found=True
61
62 if not found and not self.opts.notequal and path.basename(sName)!=path.basename(dName):
63 parts=sName.split(path.sep)
64 for i in range(len(parts)):
65 tmp=apply(path.join,[dName]+parts[-(i+1):])
66
67 if path.exists(tmp):
68 found=True
69 dName=tmp
70 warning("Found",dName,"and using this")
71 break
72
73 if not found:
74 error("Could not find a file named",path.basename(sName),"in",dName)
75
76 if path.samefile(sName,dName):
77 error("Source",sName,"and destination",dName,"are the same")
78
79 try:
80 dest=ParsedParameterFile(dName,backup=False,listLengthUnparsed=self.opts.longlist)
81 except IOError,e:
82 self.error("Problem with file",dName,":",e)
83
84 self.pling=False
85
86 self.compareDict(source.content,dest.content,1,path.basename(sName))
87
88 if not self.pling:
89 print "\nNo differences found"
90
92 return "%s[%s]" % (path,name)
93
95 return "%s[%d]" % (path,index)
96
97 - def compare(self,src,dst,depth,name):
98 if type(src)!=type(dst):
99 print ">><<",name,": Types differ\n>>Source:\n",makeString(src),"\n<<Destination:\n",makeString(dst)
100 self.pling=True
101 elif type(src) in [tuple,list,TupleProxy]:
102 self.compareIterable(src,dst,depth,name)
103 elif type(src) in [str,float,int,long,type(None)]:
104 self.comparePrimitive(src,dst,depth,name)
105 elif src.__class__ in [Dimension,Tensor,SymmTensor,Vector]:
106 self.comparePrimitive(src,dst,depth,name)
107 elif src.__class__==Field:
108 self.compareField(src,dst,depth,name)
109 elif type(src) in [DictProxy,dict]:
110 self.compareDict(src,dst,depth,name)
111 else:
112 warning("Type of",name,"=",type(src),"unknown")
113 if self.opts.debug:
114 try:
115 print "Class of",name,"=",src.__class__,"unknown"
116 except:
117 pass
118
120 if src!=dst:
121 self.pling=True
122 print ">><< Field",name,": Differs\n>>Source:\n",
123 if src.uniform:
124 print src
125 else:
126 print "nonuniform - field not printed"
127 print "<<Destination:\n",
128 if dst.uniform:
129 print dst
130 else:
131 print "nonuniform - field not printed"
132
134 if src!=dst:
135 print ">><<",name,": Differs\n>>Source:\n",src,"\n<<Destination:\n",dst
136 self.pling=True
137
139 nr=min(len(src),len(dst))
140
141 for i in range(nr):
142 if self.opts.debug:
143 print "Comparing",self.iterString(name,i)
144 self.compare(src[i],dst[i],depth+1,self.iterString(name,i))
145
146 if nr<len(src):
147 print ">>>>",self.iterString(name,nr),"to",self.iterString(name,len(src)-1),"missing from destination\n",makeString(src[nr:])
148 self.pling=True
149 elif nr<len(dst):
150 print "<<<<",self.iterString(name,nr),"to",self.iterString(name,len(dst)-1),"missing from source\n",makeString(dst[nr:])
151 self.pling=True
152
154 for n in src:
155 if not n in dst:
156 print ">>>>",self.dictString(name,n),": Missing from destination\n",makeString(src[n])
157 self.pling=True
158 else:
159 if self.opts.debug:
160 print "Comparing",self.dictString(name,n)
161 self.compare(src[n],dst[n],depth+1,self.dictString(name,n))
162
163 for n in dst:
164 if not n in src:
165 print "<<<<",self.dictString(name,n),": Missing from source\n",makeString(dst[n])
166 self.pling=True
167