[Python-Dev] linktree.py
David MacQuigg
macquigg@cadence.com
Fri, 27 Dec 2002 16:41:33 -0800
Hello,
I'm new to Python, and I'm very impressed with the potential of this =
language to simplify much of the software my company provides. As a =
first project, I'm looking to replace some tricky scripts written in csh =
for setting up our tool hierarchies. An example is a script to =
construct a "mock hierarchy" out of some patch files and links to a =
production hierarchy. Our hierarchies are typically two gigabytes, =
thousands of files, lots of strange links, many of which are broken. So =
the script needs to be fairly robust. The functions in 'shutil' won't =
do it. 'copytree' works, but the cost is a huge amount of disk space. =
A mock hierarchy takes not much more space than the patch files =
themselves.
After a brief search on the Python website, I couldn't find a suitable =
utility, and decided to write my own (see below). It seems like this =
could be a useful, general-purpose addition to 'shutil'.
I've tested this on a large hierarchy on a Solaris workstation, and it =
seems to work. There are some dark corners which worry me however, so I =
thought I would get some advice from the experts. Has anything like =
this already been done or is it planned? Would a function like this be =
a useful addition to 'shutil'? I'm intending to continue work on it, =
and will be glad to contribute it to the library.
import os, sys
from os.path import *
def linktree(oldtree, newtree):
"""linktree(oldtree, newtree) -> None
Leave 'oldtree' undisturbed. Recursively add links to 'newtree', =
until it
looks just like 'oldtree', but with the new files patched in.=20
"""
names =3D os.listdir(oldtree)
print 'Candidates: ', names
for name in names:
oldpath =3D abspath(join(oldtree, name))
newpath =3D abspath(join(newtree, name))
if not exists(newpath) and not islink(newpath):
if not exists(oldpath): # Don't link to a bad link.
print '*** Error ***\nPath does not exist:\n', oldpath
continue
print 'New link: ', newpath
os.symlink(oldpath, newpath) # OK if oldpath is a good link.
elif isfile(newpath) and not islink(newpath):
print 'As is: ', newpath=20
pass # Leave new files as is.
elif isdir(newpath) and not islink(newpath):
print 'Down one level \n', newpath
linktree(oldpath, newpath) # Recursive call
print 'Up one level'
else:
print '*** Error ***\nNot a file or directory:\n', newpath
- Dave
************************************************************* *
* David MacQuigg * email: macquigg@cadence.com * *
* Principal Product Engineer * phone: USA 520-721-4583 * * *
* Analog Artist * * *
* * 9320 East Mikelyn Lane * * *
* Cadence Design Systems, Inc. * Tucson, Arizona 85710 *
************************************************************* *=20