1 """
2 Application-class that implements pyFoamCloneCase.py
3 """
4
5 from optparse import OptionGroup
6
7 from .PyFoamApplication import PyFoamApplication
8
9 from PyFoam.RunDictionary.SolutionDirectory import SolutionDirectory
10 from PyFoam.Error import error,warning
11
12 from os import path
13
14 from PyFoam.Basics.GeneralVCSInterface import getVCS
15
17 - def __init__(self,
18 args=None,
19 **kwargs):
20 description="""\
21 Clones a case by copying the system, constant and 0-directories
22
23 If the case is under VCS then the cloning mechanism of the VCS is used
24 """
25 PyFoamApplication.__init__(self,
26 args=args,
27 description=description,
28 usage="%prog <source> <destination>",
29 changeVersion=False,
30 interspersed=True,
31 nr=2,
32 **kwargs)
33
35 what=OptionGroup(self.parser,
36 "What",
37 "Define what should be cloned")
38 self.parser.add_option_group(what)
39
40 what.add_option("--chemkin",
41 action="store_true",
42 dest="chemkin",
43 default=False,
44 help="Also copy the Chemkin-directory")
45 what.add_option("--add-item",
46 action="append",
47 dest="additional",
48 default=[],
49 help="Add a subdirectory to the list of cloned items (can be used more often than once)")
50 what.add_option("--no-pyfoam",
51 action="store_false",
52 dest="dopyfoam",
53 default=True,
54 help="Don't copy PyFoam-specific stuff")
55 what.add_option("--latest-time",
56 action="store_true",
57 dest="latest",
58 default=[],
59 help="Add the latest time-step")
60
61 behave=OptionGroup(self.parser,
62 "Behaviour")
63 self.parser.add_option_group(behave)
64 behave.add_option("--parallel",
65 action="store_true",
66 dest="parallel",
67 default=False,
68 help="Clone the processor-directories")
69 behave.add_option("--force",
70 action="store_true",
71 dest="force",
72 default=False,
73 help="Overwrite destination if it exists")
74 behave.add_option("--follow-symlinks",
75 action="store_true",
76 dest="followSymlinks",
77 default=False,
78 help="Follow symlinks instead of just copying them")
79 behave.add_option("--no-vcs",
80 action="store_false",
81 dest="vcs",
82 default=True,
83 help="Do NOT use the VCS-clone mechanism if the case is under source control")
84 behave.add_option("--symlink-mode",
85 action="store_true",
86 dest="symlinkMode",
87 default=False,
88 help="Clone in symbolic link mode. This means that all directories down to the level specified in 'symlink-level' are created and populated with symbolic links to the appropriate files. Below that level a symlink to the whole directory is created. Exception are the 'polyMesh'-directories where all files except 'blockMeshDict' are being copied. If for certain files a copy instead of the symbolic link is wanted use 'pyFoamSymlinkToFile.py'")
89 behave.add_option("--symlink-level",
90 action="store",
91 dest="symlinkLevel",
92 type=int,
93 default=1,
94 help="Level until which no symlinks to directories are created. Default: %default")
95 behave.add_option("--relative-symlink",
96 action="store_true",
97 dest="symlinkRelative",
98 default=False,
99 help="With '--symlink-mode' make the symbolic links relative instead of absolute")
101 if len(self.parser.getArgs())>2:
102 error("Too many arguments:",self.parser.getArgs()[2:],"can not be used")
103
104 sName=self.parser.getArgs()[0]
105 dName=self.parser.getArgs()[1]
106
107 if path.exists(dName):
108 if self.parser.getOptions().force:
109 warning("Replacing",dName,"(--force option)")
110 elif path.exists(path.join(dName,"system","controlDict")):
111 error("Destination",dName,"already existing and a Foam-Case")
112 elif path.isdir(dName):
113 dName=path.join(dName,path.basename(sName))
114 if path.exists(dName) and not self.parser.getOptions().force:
115 error(dName,"already existing")
116 elif not path.exists(path.dirname(dName)):
117 warning("Directory",path.dirname(dName),"does not exist. Creating")
118
119 sol=SolutionDirectory(sName,
120 archive=None,
121 paraviewLink=False,
122 addLocalConfig=True,
123 parallel=self.opts.parallel)
124
125 if sol.determineVCS()!=None and self.opts.vcs:
126 if self.opts.chemkin or self.opts.additional or self.opts.latest:
127 self.error("Using an unimplemented option together with VCS")
128
129 vcsInter=getVCS(sol.determineVCS(),
130 path=sol.name)
131 vcsInter.clone(dName)
132 return
133
134 if self.parser.getOptions().chemkin:
135 sol.addToClone("chemkin")
136
137 if self.parser.getOptions().dopyfoam:
138 sol.addToClone("customRegexp")
139
140 for a in self.parser.getOptions().additional:
141 sol.addToClone(a)
142
143 if self.parser.getOptions().latest:
144 sol.addToClone(sol.getLast())
145
146 if self.opts.symlinkMode:
147 sol.symlinkCase(
148 dName,
149 followSymlinks=self.parser.getOptions().followSymlinks,
150 maxLevel=self.opts.symlinkLevel,
151 relPath=self.opts.symlinkRelative
152 )
153 else:
154 sol.cloneCase(
155 dName,
156 followSymlinks=self.parser.getOptions().followSymlinks
157 )
158
159 self.addToCaseLog(dName,"Cloned to",dName)
160
161
162