1 """
2 Application-class that implements pyFoamSymlinkToFile.py
3 """
4
5 from optparse import OptionGroup
6
7 from .PyFoamApplication import PyFoamApplication
8 from PyFoam.Basics.Utilities import copytree,remove
9
10 from os import path,rename
11
13 - def __init__(self,
14 args=None,
15 **kwargs):
16 description="""Takes a list of files. If they are symlinks
17 then replace them with the file/directory they are pointing too.
18
19 Used to convert single files after using 'pyFoamCloneCase.py' ii
20 --symlink-mode
21 """
22 PyFoamApplication.__init__(self,
23 args=args,
24 description=description,
25 usage="%prog <file1> ...",
26 changeVersion=False,
27 interspersed=True,
28 exactNr=False,
29 nr=1,
30 **kwargs)
31
33 behave=OptionGroup(self.parser,
34 "Behaviour")
35 self.parser.add_option_group(behave)
36 behave.add_option("--follow-symlinks",
37 action="store_true",
38 dest="followSymlinks",
39 default=False,
40 help="Follow symlinks instead of just copying them")
41
43 files=self.parser.getArgs()
44
45 for f in files:
46 if not path.exists(f):
47 self.error("File",f,"does not exists")
48 if not path.islink(f):
49 self.warning("File",f,"is not a symbolic link")
50 continue
51 real=path.realpath(f)
52 while path.islink(real) and self.opts.followSymlinks:
53 real=path.realpath(real)
54 bakName=f+".backupfileForSymlink"
55 copytree(real,bakName)
56 remove(f)
57 rename(bakName,f)
58
59
60