945
945
pending.append(dir)
948
def copy_tree(from_path, to_path, handlers={}):
949
"""Copy all of the entries in from_path into to_path.
951
:param from_path: The base directory to copy.
952
:param to_path: The target directory. If it does not exist, it will
954
:param handlers: A dictionary of functions, which takes a source and
955
destinations for files, directories, etc.
956
It is keyed on the file kind, such as 'directory', 'symlink', or 'file'
957
'file', 'directory', and 'symlink' should always exist.
958
If they are missing, they will be replaced with 'os.mkdir()',
959
'os.readlink() + os.symlink()', and 'shutil.copy2()', respectively.
961
# Now, just copy the existing cached tree to the new location
962
# We use a cheap trick here.
963
# Absolute paths are prefixed with the first parameter
964
# relative paths are prefixed with the second.
965
# So we can get both the source and target returned
966
# without any extra work.
968
def copy_dir(source, dest):
971
def copy_link(source, dest):
972
"""Copy the contents of a symlink"""
973
link_to = os.readlink(source)
974
os.symlink(link_to, dest)
976
real_handlers = {'file':shutil.copy2,
978
'directory':copy_dir,
980
real_handlers.update(handlers)
982
if not os.path.exists(to_path):
983
real_handlers['directory'](from_path, to_path)
985
for dir_info, entries in walkdirs(from_path, prefix=to_path):
986
for relpath, name, kind, st, abspath in entries:
987
real_handlers[kind](abspath, relpath)
948
990
def path_prefix_key(path):
949
991
"""Generate a prefix-order path key for path.