
import sys
import time

from django.core.management.base import BaseCommand, CommandError

from apps.predict.models import PredictStrain
from apps.pipeline.models import ProgramRun

class Command(BaseCommand):
    help = """Schedule each of the pipeline tasks with the shell or LSF"""

    def handle(self, **options):
        for strain in PredictStrain.objects.filter(piperun__isnull=True):
            sys.stderr.write("Strain: %s, " % str(strain))
            dl = strain.files_status
            if dl is 0:
                sys.stderr.write("NOT-READY [SKIP]\n")
            elif dl is 1:
                sys.stderr.write("DOWNLOADING [SKIP]\n")
            elif dl is 2:
                sys.stderr.write("DOWNLOAD ERROR [SKIP]\n")
            elif strain.run():
                sys.stderr.write("SUBMITTED [OK]\n")
            else:
                sys.stderr.write("SUBMITTED [ERROR]\n")
            time.sleep(1)

        sys.stderr.write("\n")

        for strain in PredictStrain.objects.filter(
                piperun__programs__is_submitted=False,
                piperun__programs__is_complete=True,
                piperun__programs__is_error=True,
                ).distinct():
            sys.stderr.write("Re-Strain: %s " % str(strain))
            if strain.piperun.rerun():
                sys.stderr.write("SUBMITTED [OK]\n")
            else:
                sys.stderr.write("SUBMITTED [ERROR]\n")
            time.sleep(1)

        for progrun in ProgramRun.objects.filter(is_submitted=True, is_complete=False, is_error=False):
            sys.stderr.write("Run Status: %s " % str(progrun))
            if progrun.update_status():
                sys.stderr.write(" [COMPLETE]\n")
                progrun.piperun.predictstrain_set.first().update_status()
            elif progrun.is_error:
                sys.stderr.write(" [ERROR]\n")
            else:
                sys.stderr.write(" [WAITING]\n")


@@ -313,16 +313,47 @@
 tions):%0A
+        skipped, errors = 0, 0%0A
         
@@ -559,45 +559,19 @@
    s
-ys.stderr.write(%22NOT-READY %5BSKIP%5D%5Cn%22)
+kipped += 1
 %0A   
@@ -614,47 +614,19 @@
    s
-ys.stderr.write(%22DOWNLOADING %5BSKIP%5D%5Cn%22)
+kipped += 1
 %0A   
@@ -668,51 +668,19 @@
     
-sys.stderr.write(%22DOWNLOAD ERROR %5BSKIP%5D%5Cn%22)
+errors += 1
 %0A   
@@ -873,29 +873,120 @@
     
-sys.stderr.write(%22%5Cn%22
+if skipped or errors:%0A            sys.stderr.write(%22DOWNLOADS WARN: %25d SKIPPED, %25d ERRORS%5Cn%22 %25 (skipped, errors)
 )%0A%0A 

Improve log output for pipeline run