927
927
pending.append(dir)
930
def copy_tree(from_path, to_path, handlers={}):
931
"""Copy all of the entries in from_path into to_path.
933
:param from_path: The base directory to copy.
934
:param to_path: The target directory. If it does not exist, it will
936
:param handlers: A dictionary of functions, which takes a source and
937
destinations for files, directories, etc.
938
It is keyed on the file kind, such as 'directory', 'symlink', or 'file'
939
'file', 'directory', and 'symlink' should always exist.
940
If they are missing, they will be replaced with 'os.mkdir()',
941
'os.readlink() + os.symlink()', and 'shutil.copy2()', respectively.
943
# Now, just copy the existing cached tree to the new location
944
# We use a cheap trick here.
945
# Absolute paths are prefixed with the first parameter
946
# relative paths are prefixed with the second.
947
# So we can get both the source and target returned
948
# without any extra work.
950
def copy_dir(source, dest):
953
def copy_link(source, dest):
954
"""Copy the contents of a symlink"""
955
link_to = os.readlink(source)
956
os.symlink(link_to, dest)
958
real_handlers = {'file':shutil.copy2,
960
'directory':copy_dir,
962
real_handlers.update(handlers)
964
if not os.path.exists(to_path):
965
real_handlers['directory'](from_path, to_path)
967
for dir_info, entries in walkdirs(from_path, prefix=to_path):
968
for relpath, name, kind, st, abspath in entries:
969
real_handlers[kind](abspath, relpath)
930
972
def path_prefix_key(path):
931
973
"""Generate a prefix-order path key for path.