def partition_files()

in xar/xar_util.py [0:0]


def partition_files(staging, extension_destinations):
    """Partition source_dir into multiple output directories.

    A partition is defined by extension_destinations which maps suffixes (such
    as ".debuginfo") to a PartitionDestination instance.

    dest_dir contains all files that aren't in a partition, and symlinks for
    ones that are.  symlinks are relative and of the form
    "../../../uuid/path/to/file" so that the final symlinks are correct
    relative to /mnt/xar/....
    """
    source_dir = staging.path()
    source_dir = source_dir.rstrip("/")

    for dirpath, _dirnames, filenames in os.walk(staging.path()):
        # path relative to source_dir; used for creating the right
        # file inside the staging dir
        relative_dirname = dirpath[len(source_dir) + 1 :]

        # Special case; if a file is in the root of source_dir, then
        # relative_dirname is empty, but that has the same number of
        # '/' as just 'bin', so we need to special case it the empty
        # value.
        if not relative_dirname:
            relative_depth = 1
        else:
            relative_depth = 2 + relative_dirname.count("/")

        for filename in filenames:
            # Does this extension map to a separate output?
            _, extension = os.path.splitext(filename)
            dest_base = extension_destinations.get(extension, None)
            # This path stays in the source staging directory
            if dest_base is None:
                continue
            # This file is destined for another tree, make a
            # relative symlink in source pointing to the
            # sub-xar destination.
            relative_path = os.path.join(relative_dirname, filename)
            source_path = staging.absolute(relative_path)
            dest_base.staging.move(source_path, relative_path)

            dependency_mountpoint = dest_base.uuid
            staging_symlink = os.path.join(
                "../" * relative_depth, dependency_mountpoint, relative_path
            )
            logging.info("%s %s" % (staging_symlink, source_path))

            staging.symlink(staging_symlink, relative_path)