~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Martin Pool
  • Date: 2006-08-15 13:19:12 UTC
  • mfrom: (1923 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1925.
  • Revision ID: mbp@sourcefrog.net-20060815131912-7bbc6d387bb32d16
[merge] bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
945
945
                pending.append(dir)
946
946
 
947
947
 
 
948
def copy_tree(from_path, to_path, handlers={}):
 
949
    """Copy all of the entries in from_path into to_path.
 
950
 
 
951
    :param from_path: The base directory to copy. 
 
952
    :param to_path: The target directory. If it does not exist, it will
 
953
        be created.
 
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.
 
960
    """
 
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.
 
967
 
 
968
    def copy_dir(source, dest):
 
969
        os.mkdir(dest)
 
970
 
 
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)
 
975
 
 
976
    real_handlers = {'file':shutil.copy2,
 
977
                     'symlink':copy_link,
 
978
                     'directory':copy_dir,
 
979
                    }
 
980
    real_handlers.update(handlers)
 
981
 
 
982
    if not os.path.exists(to_path):
 
983
        real_handlers['directory'](from_path, to_path)
 
984
 
 
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)
 
988
 
 
989
 
948
990
def path_prefix_key(path):
949
991
    """Generate a prefix-order path key for path.
950
992