1
2 """
3 Application class that implements pyFoamUpgradeDictionariesTo17
4 """
5
6 from optparse import OptionGroup
7 from os import path
8
9 from .PyFoamApplication import PyFoamApplication
10
11 from PyFoam.RunDictionary.ParsedParameterFile import ParsedParameterFile
12 from PyFoam.Basics.Utilities import copyfile
13 from PyFoam.Basics.DataStructures import DictProxy
14 from PyFoam.Basics.FoamFileGenerator import makeString
15 from PyFoam.Error import error
16
17 from PyFoam.ThirdParty.six import print_
18
20 """This class knows how to detect old versions and how to upgrade them"""
22 self.case=None
23 self.enabled=True
24 self.fName=None
25 self.noHeader=False
26 self.listDict=False
27
30
36
39
42
48
67
73
77
79 return path.join("system","fvSolution")
80
83
85 if "solvers" not in content:
86 return False
87
88 for s in content["solvers"]:
89 if type(content["solvers"][s]) not in [dict,DictProxy]:
90 return True
91 return False
92
94 for s in content["solvers"]:
95 comment=self.makeComment(content["solvers"][s])
96 alg,rest=content["solvers"][s]
97 rest["solver"]=alg
98 content["solvers"][s]=rest
99 content["solvers"].addDecoration(s,comment)
100
102 - def __init__(self,
103 args=None,
104 description=None,
105 **kwargs):
106 if not description:
107 description="""\
108 Examines dictionaries in a case and tries to upgrade them to a form
109 that is compatible with OpenFOAM 1.7
110
111 If only a file is specified then the mode of that file has to be specified
112 """
113
114 self.dicts=[]
115
116 self.addDicts()
117
118 PyFoamApplication.__init__(self,
119 args=args,
120 description=description,
121 usage="%prog [options] <case>",
122 changeVersion=False,
123 nr=1,
124 interspersed=True,
125 **kwargs)
126
129
131 behaveGroup=OptionGroup(self.parser,
132 "Behaviour",
133 "General behaviour of the program")
134
135 behaveGroup.add_option("--apply-changes",
136 action="store_true",
137 dest="applyChanges",
138 default=False,
139 help="Apply changes to the dictionaries in question. Without this option only the results of the analysis will be shown")
140
141 behaveGroup.add_option("--print",
142 action="store_true",
143 dest="print_",
144 default=False,
145 help="Only print the modified dictionaries to the screen")
146
147 behaveGroup.add_option("--verbose",
148 action="store_true",
149 dest="verbose",
150 default=False,
151 help="Speak out aloud which decisions are made")
152
153 behaveGroup.add_option("--force",
154 action="store_true",
155 dest="force",
156 default=False,
157 help="Force even if backup-files exist")
158
159 self.parser.add_option_group(behaveGroup)
160
161 self.dictGroup=OptionGroup(self.parser,
162 "Dictionaries",
163 "Dictionaries that should be updated")
164
165 for d in self.dicts:
166 self.dictGroup.add_option("--disable-"+"-".join(reversed(d.location().split(path.sep))),
167 action="callback",
168 callback=d.disableCallback,
169 help="Disable the modification of "+d.location())
170
171 self.parser.add_option_group(self.dictGroup)
172
173 self.modes={}
174
175 for d in self.dicts:
176 self.modes[d.name()]=d
177
178 choices=self.modes.keys()
179 choices.sort()
180
181 behaveGroup.add_option("--file-mode",
182 action="store",
183 type="choice",
184 dest="fileMode",
185 default=None,
186 choices=choices,
187 help="The file should be treated as what while upgrading. Possible modes: "+", ".join(choices))
188
190 fName=self.parser.getArgs()[0]
191 if path.isdir(fName):
192 if self.opts.fileMode:
193 self.error("Filemode",self.opts.fileMode,"specified. But",
194 fName,"is a directory")
195 case=fName
196 self.checkCase(case)
197
198 if self.opts.verbose:
199 print_("Working on case",case)
200
201 for d in self.dicts:
202 d.case=case
203 if self.opts.verbose:
204 print_(" Checking",d.location())
205
206 if not d.enabled:
207 if self.opts.verbose:
208 print_(" Disabled")
209 continue
210
211 if not path.exists(d.path()):
212 d.disable()
213 if self.opts.verbose:
214 print_(" Does not exist - disabling")
215 continue
216
217 if not d.needsUpgrade():
218 d.disable()
219 if self.opts.verbose:
220 print_(" Does not need an upgrade - disabling")
221 continue
222
223 print_(d.location(),"needs an upgrade")
224
225 if self.opts.applyChanges or self.opts.print_:
226 print_()
227 if self.opts.applyChanges:
228 print_("Doing the upgrades")
229 for d in self.dicts:
230 if d.enabled:
231 if self.opts.verbose:
232 print_("Upgrading",d.location())
233 d.upgrade(self.opts.force,self.opts.print_)
234 else:
235 if not self.opts.fileMode:
236 self.error(fName,"is a file, but no --file-mode specified")
237 d=self.modes[self.opts.fileMode]
238 d.setFile(fName)
239 if self.opts.verbose:
240 print_("Handling",fName,"as",self.opts.fileMode)
241 if not d.needsUpgrade():
242 d.disable()
243 if self.opts.verbose:
244 print_(" Does not need an upgrade")
245 else:
246 if self.opts.verbose:
247 print_(" Needs an upgrade")
248 if self.opts.applyChanges or self.opts.print_:
249 print_()
250 if self.opts.verbose:
251 print_("Upgrading",fName)
252 d.upgrade(self.opts.force,self.opts.print_)
253
254
255