1
2 """Pseudo-Cases for Regions, built from symlinks"""
3
4 from .SolutionDirectory import SolutionDirectory
5 from PyFoam.Error import error
6 from glob import glob
7 from PyFoam.Basics.Utilities import rmtree
8
9 from os import path,mkdir,symlink,unlink,listdir,renames
10
12 """Builds pseudocases for the regions"""
13
14 - def __init__(self,sol,clean=False,processorDirs=True):
15 """@param sol: solution directory
16 @param clean: Remove old pseudo-cases"""
17
18 self.master=sol
19 regions=self.master.getRegions()
20 if len(regions)<=0:
21 error("No regions in",self.master.name)
22 if clean:
23 self.cleanAll()
24 else:
25 for r in regions:
26 rName=self.master.name+"."+r
27 if path.exists(rName):
28 error("Directory",rName,"alread existing. Did not clean up?")
29
30 for r in regions:
31 rName=self.master.name+"."+r
32 mkdir(rName)
33
34 mkdir(path.join(rName,"system"))
35 for f in listdir(self.master.systemDir(region=r)):
36 self._mklink(self.master.name,r,"system",prefix=path.pardir,postfix=f)
37 symlink(path.join(path.pardir,path.pardir,self.master.name,"system","controlDict"),
38 path.join(rName,"system","controlDict"))
39 symlink(path.join(path.pardir,path.pardir,self.master.name,"system","decomposeParDict"),
40 path.join(rName,"system","decomposeParDict"))
41
42 self._mklink(self.master.name,r,"constant")
43 for t in self.master.getTimes():
44 self._mklink(self.master.name,r,t)
45 if processorDirs:
46 for p in self.master.processorDirs():
47 pDir=path.join(self.master.name,p)
48 sDir=path.join(self.master.name+"."+r,p)
49 if not path.exists(sDir):
50 mkdir(sDir)
51 for f in listdir(pDir):
52 self._mklink(self.master.name,r,path.join(p,f),prefix=path.pardir)
53
55 """Update the master Case from all the region-cases"""
56
57 for r in self.master.getRegions():
58 self.resync(r)
59
61 """Update the master case from a region case
62 @param region: Name of the region"""
63 rCase=SolutionDirectory(self.master.name+"."+region)
64 rTimes=rCase.getTimes()
65 for t in rTimes+["constant"]:
66 if path.exists(path.join(rCase.name,t)):
67 if not path.exists(path.join(self.master.name,t,region)):
68 self._rename(self.master.name,region,t)
69 for p in rCase.processorDirs():
70 pDir=path.join(self.master.name,p)
71 if not path.exists(pDir):
72 mkdir(pDir)
73 symlink(path.join(path.pardir,"system"),path.join(pDir,"system"))
74
75 if path.exists(path.join(rCase.name,p,t)):
76 if not path.exists(path.join(pDir,region,t)):
77 self._rename(self.master.name,region,t,processor=p,prefix=path.pardir)
78 if t=="constant":
79 for f in listdir(path.join(self.master.name,t,region)):
80 if f!="polyMesh":
81
82
83 dest=path.join(pDir,"constant",region,f)
84 src=path.join(path.pardir,path.pardir,path.pardir,"constant",region,f)
85 if not path.exists(dest):
86 symlink(src,dest)
87
88 - def _mklink(self,master,region,name,prefix="",postfix=""):
89 """Makes a link from the master case to the pseudo-case
90 @param master: Name of the master directory
91 @param region: Name of one region
92 @param name: Name of the directory to link
93 @param prefix: A prefix to the path
94 @param postfix: An actual file to the path"""
95
96 destname=path.join(master+"."+region,name)
97 srcname=path.join(prefix,path.pardir,master,name,region,postfix)
98 if postfix!="":
99 destname=path.join(destname,postfix)
100
101
102
103 symlink(srcname,destname)
104
105 return path.exists(srcname)
106
107 - def _rename(self,master,region,name,prefix="",processor=""):
108 """Moves a directory from
109 @param master: Name of the master directory
110 @param region: Name of one region
111 @param name: Name of the directory to link
112 @param prefix: A prefix to the path"""
113
114 rName=master+"."+region
115
116 if processor=="":
117 destName=path.join(master,name,region)
118 srcName=path.join(rName,name)
119 prefix=path.pardir
120 else:
121 destName=path.join(master,processor,name,region)
122 srcName=path.join(rName,processor,name)
123 prefix=path.join(path.pardir,path.pardir)
124
125
126
127 if not path.exists(destName):
128 renames(srcName,destName)
129 symlink(path.join(prefix,destName),srcName)
130
134
136 rmtree(self.master.name+"."+region,ignore_errors=True)
137
138
139