Package PyFoam :: Package Applications :: Module SymlinkToFile
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Applications.SymlinkToFile

 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   
12 -class SymlinkToFile(PyFoamApplication):
13 - def __init__(self,args=None):
14 description="""Takes a list of files. If they are symlinks 15 then replace them with the file/directory they are pointing too. 16 17 Used to convert single files after using 'pyFoamCloneCase.py' ii 18 --symlink-mode 19 """ 20 PyFoamApplication.__init__(self, 21 args=args, 22 description=description, 23 usage="%prog <file1> ...", 24 changeVersion=False, 25 interspersed=True, 26 exactNr=False, 27 nr=1)
28
29 - def addOptions(self):
30 behave=OptionGroup(self.parser, 31 "Behaviour") 32 self.parser.add_option_group(behave) 33 behave.add_option("--follow-symlinks", 34 action="store_true", 35 dest="followSymlinks", 36 default=False, 37 help="Follow symlinks instead of just copying them")
38
39 - def run(self):
40 files=self.parser.getArgs() 41 42 for f in files: 43 if not path.exists(f): 44 self.error("File",f,"does not exists") 45 if not path.islink(f): 46 self.warning("File",f,"is not a symbolic link") 47 continue 48 real=path.realpath(f) 49 while path.islink(real) and self.opts.followSymlinks: 50 real=path.realpath(real) 51 bakName=f+".backupfileForSymlink" 52 copytree(real,bakName) 53 remove(f) 54 rename(bakName,f)
55 56 # Should work with Python3 and Python2 57