#!/usr/bin/env python3
"""
Dump SLURM script that executes the selected experiment with all projects.

This basically provides the same as pprof run, except that it just
dumps a slurm batch script that executes everything as an array job
on a configurable SLURM cluster.
"""
import os
from plumbum import cli
from pprof.settings import CFG
from pprof.experiments import *  # pylint: disable=W0401,W0614
from pprof.projects import *  # pylint: disable=W0401,W0614
from pprof import experiment, project
from pprof.utils import slurm


class Slurm(cli.Application):
    """ Generate a SLURM script. """

    def __init__(self, executable):
        super(Slurm, self).__init__(executable)
        self._experiment = None
        self._project_names = None
        self._group_name = None
        self._description = None

    @cli.switch(["-E", "--experiment"],
                str,
                mandatory=True,
                help="Specify experiments to run")
    def experiment(self, cfg_experiment):
        """Specify experiments to run"""
        self._experiment = cfg_experiment

    @cli.switch(["-P", "--project"],
                str,
                list=True,
                requires=["--experiment"],
                help="Specify projects to run")
    def projects(self, projects):
        """Specify projects to run"""
        self._project_names = projects

    @cli.switch(["-D", "--description"],
                str,
                help="A description for this experiment run")
    def experiment_tag(self, description):
        """A description for this experiment run"""
        self._description = description

    @cli.switch(["-G", "--group"],
                str,
                list=True,
                requires=["--experiment"],
                help="Run a group of projects under the given experiments")
    def group(self, groups):
        """Run a group of projects under the given experiments"""
        self._group_names = groups

    def __go__(self, project_names, group_name, exp_name):
        prj_registry = project.ProjectRegistry
        projects = prj_registry.projects
        project_names = self._project_names
        group_names = self._group_names
        if project_names is not None:
            allkeys = set(list(projects.keys()))
            usrkeys = set(project_names)
            projects = {x: projects[x] for x in allkeys & usrkeys}

        if group_names is not None:
            groupkeys = set(group_names)
            projects = {
                name: cls
                for name, cls in projects.items() if cls.GROUP in groupkeys
            }

        projects = {x: projects[x]
                    for x in projects
                    if projects[x].DOMAIN != "debug"}

        prj_keys = sorted(projects.keys())
        print("{} Projects".format(len(prj_keys)))

        slurm.prepare_slurm_script(exp_name, prj_keys)

    def main(self):
        """Main entry point of pprof run."""
        exp_registry = experiment.ExperimentRegistry
        project_names = self._project_names
        group_name = self._group_name
        exp_name = self._experiment

        if self._description:
            CFG["experiment_description"] = self._description

        CFG["slurm"]["logs"] = os.path.abspath(os.path.join(CFG[
            'build_dir'].value(), CFG['slurm']['logs'].value()))
        CFG["build_dir"] = CFG["slurm"]["node_dir"].value()

        print("Experiment: " + exp_name)
        if exp_name in exp_registry.experiments:
            self.__go__(project_names, group_name, exp_name)
        else:
            from logging import error
            error("Could not find {} in the experiment registry.", exp_name)

@@ -769,24 +769,25 @@
 oup_name
+s
  = None%0A
         
@@ -770,32 +770,32 @@
 up_names = None%0A
-
         self._de
@@ -1994,36 +1994,24 @@
 oject_names,
- group_name,
  exp_name):%0A
@@ -2146,48 +2146,8 @@
 mes%0A
-        group_names = self._group_names%0A
     
@@ -2338,16 +2338,56 @@
 rkeys%7D%0A%0A
+        group_names = self._group_names%0A
         
@@ -3042,46 +3042,8 @@
 mes%0A
-        group_name = self._group_name%0A
     
@@ -3449,16 +3449,16 @@
 iments:%0A
+
         
@@ -3491,20 +3491,8 @@
 mes,
- group_name,
  exp

initialize group_names to default None