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