#!/usr/bin/env python
"""
The JIP job runner that executes a jip scrip on the local machine

usage: jip-run [-h] [-p] [-f] [-k] [-s <spec>] [-C <threads>]
               [--status] [--dry] [--show] [--with-profiler]
               <tool> [<args>...]

Options:
  -p, --pipeline           the file contains a pipeline
  -f, --force              force command execution
  -k, --keep               do not perform a cleanup step after job failure or
                           cancellation
  -C, --threads <threads>  Number of threads assigned to the job. The threads a
                           exposed as JIP_THREADS envorinment variable
                           [default: 1]
  -s, --spec <spec>        Load a pipeline/jobs specification
  --show                   show the rendered script rather than running it
  --dry                    show the configuration of the script/pipeline
  --status                 print status information to stderr
  --with-profiler          execute the run with a profiler
  <tool>                   the tool that will be executed
  <args>                   optional script argument

Other Options:
    -h --help             Show this help message

"""
from __future__ import print_function

import sys

from . import parse_args, dry, colorize, YELLOW, GREEN, RED, BLUE
import jip
import jip.jobs
import jip.profiles
from jip.logger import getLogger
from datetime import datetime, timedelta

log = getLogger('jip.cli.jip_run')


def main(argv=None):
    args = parse_args(__doc__, argv=argv)
    script_file = args["<tool>"]
    script_args = args["<args>"]
    try:
        script = jip.find(script_file, is_pipeline=args['--pipeline'])
    except LookupError as e:
        print(str(e), file=sys.stderr)
        sys.exit(1)

    if args['--dry'] or args['--show']:
        dry(script, script_args, dry=args['--dry'], show=args['--show'])
        return

    keep = args['--keep']
    force = args['--force']
    profiler = args['--with-profiler']
    silent = not args['--status']
    try:
        profile = jip.profiles.Profile()
        if args['--spec']:
            spec_prof = jip.profiles.Profile.from_file(args['--spec'])
            spec_prof.update(profile)
            profile = spec_prof
        profile.load_args(args)

        jobs = jip.jobs.create_jobs(script, args=script_args, keep=keep,
                                    profile=profile)
        # assign job ids
        for i, j in enumerate(jobs):
            j.id = i + 1

        for exe in jip.jobs.create_executions(jobs):
            if exe.completed and not force:
                if not silent:
                    print(colorize("Skipping", YELLOW), exe.name,
                          file=sys.stderr)
            else:
                if not silent:
                    print(colorize("Running", YELLOW),
                          " {name:30} ".format(name=colorize(exe.name, BLUE)),
                          file=sys.stderr)
                    sys.stderr.flush()
                start = datetime.now()
                success = jip.jobs.run_job(exe.job, profiler=profiler)
                end = timedelta(seconds=(datetime.now() - start).seconds)
                if success:
                    if not silent:
                        print(colorize(exe.job.state, GREEN), "[%s]" % end,
                              file=sys.stderr)
                else:
                    if not silent:
                        print(colorize(exe.job.state, RED), file=sys.stderr)
                    sys.exit(1)
    except (jip.ValidationError, jip.ParserException) as va:
        print(str(va), file=sys.stderr)
        sys.exit(1)
    except Exception as va:
        raise


if __name__ == "__main__":
    main()
