from pathlib import Path


def _build_order(c, ignore=False, update=False):
    """Return the order in which directories should be traversed

    :param ignore: Ignore the build order configuration
    :param update: If using the build order configuration, add excluded directories to the end
    """

    fs_bo = [d.parent.resolve().name for d in Path('.').glob('*/metadata.csv')]

    if c.metapack.build_order is None or ignore is not False:
        return [Path('.').joinpath(d) for d in fs_bo]
    else:

        bo = c.metapack.build_order

        if update:
            bo += list(set(fs_bo) - set(bo))

        return [Path('.').joinpath(d) for d in bo]