Skip to content
Snippets Groups Projects
build_pkgs.py 2.79 KiB
import subprocess
import logging

logger = logging.getLogger("buildout/build_pkgs")

def get_dirs():
    """
    Finds all subdirectories containing setup.py files.
    :return: List of directories as strings.
    """
    logger.debug("Getting list of directories containing setup.py files...")
    find = subprocess.run([
        'find', '.', '-name', 'setup.py', '-not', '-path', './build/recipes/*'
    ], stdout=subprocess.PIPE)
    dirs = find.stdout.decode('utf-8').split('\n')
    dirs_cpy = dirs

    for i, d in enumerate(dirs_cpy):
        dirs[i] = d.replace('/setup.py', '')

    logger.debug("Done getting directories.")
    return dirs

def get_names(dirs):
    """
    Generate list of subproject names based on the rule that the name of the
    subproject directory will be the name of the subproject.
    :return: List of names as strings.
    """
    logger.debug("Generating list of subproject names...")
    names = []
    for d in dirs:
        if d != '':
            name = d.split('/')[-1]

            if name == "archive":
                # Case with ./services/archive having special dir structure
                name = "services"
            names.append(name)

    logger.debug("Done generating.")
    return names

class Recipe:
    def __init__(self, buildout, name, options):
        """
        Initializes fields needed for recipe.
        :param buildout: (Boilerplate) Dictionary of options from buildout section
        of buildout.cfg
        :param name: (Boilerplate) Name of section that uses this recipe.
        :param options: (Boilerplate) Options of section that uses this recipe.
        """
        self.name = name
        self.options = options
        self.pkg_list = get_names(get_dirs())

    def install(self):
        """
        Install method that runs when recipe has components it needs to install.
        :return: Paths to files, as strings, created by the recipe.
        """
        if self.options['name'] == "all":
            logger.warning("WARNING: You've requested all packages to be built. This will take a long time.")
            logger.warning("If only one or a few packages have changed, consider specifying them "
                           "in a comma-separated list.")
            pkgs = self.pkg_list
        else:
            pkgs = self.options['name'].split(',')

        for p in pkgs:
            if p not in self.pkg_list or p == '':
                logger.error(f"Package {p} not valid. Skipping.")
                continue
            subprocess.run(["conda", "build", "build/metadata/{}".format(p), "--output-folder", "build/pkgs/"],
                           stdout=subprocess.PIPE)
        self.options.created("build/pkgs/")

        subprocess.run(["python3", "build/tools/transfer_to_builder.py"])

        return self.options.created()

    update = install