from glob import glob
import json
import os
import re
import sys
from django.core.management.base import BaseCommand


from squad.core.models import Group
from squad.core.tasks import ReceiveTestRun


class Command(BaseCommand):

    help = """Import data from DIRECTORY into PROJECT. See
    squad/core/management/commands/import_data.rst for documentation on the
    expected format"""

    def add_arguments(self, parser):
        parser.add_argument(
            'PROJECT',
            help='Target project, on the form $group/$project',
        )

        parser.add_argument(
            'DIRECTORY',
            help='Input directory with the files to import',
        )

    def handle(self, *args, **options):
        self.options = options

        group_id, project_id = options['PROJECT'].split('/')
        self.group, _ = Group.objects.get_or_create(slug=group_id)
        self.project, _ = self.group.projects.get_or_create(slug=project_id)
        self.receive_test_run = ReceiveTestRun(self.project)

        for directory in glob(os.path.join(options['DIRECTORY'], '*')):
            self.import_build(directory)
        self.step('\n')

    def import_build(self, directory):
        build_id = os.path.basename(directory)

        for envdir in glob(os.path.join(directory, '*')):
            self.import_environment(build_id, envdir)

    def import_environment(self, build_id, directory):
        environment_slug = os.path.basename(directory)

        for testrundir in glob(os.path.join(directory, '*')):
            self.import_testrun(build_id, environment_slug, testrundir)

    def import_testrun(self, build_id, environment_slug, directory):
        metadata = open(os.path.join(directory, 'metadata.json')).read()
        metrics = open(os.path.join(directory, 'metrics.json')).read()

        self.receive_test_run(
            version=build_id,
            environment_slug=environment_slug,
            metadata=metadata,
            metrics_file=metrics,
        )

        self.step('.')

    silent = False

    def step(self, s):
        if not self.silent:
            sys.stdout.write(s)
            sys.stdout.flush()

@@ -668,24 +668,44 @@
 %0A        )%0A%0A
+    silent = False%0A%0A
     def hand
@@ -728,24 +728,24 @@
 **options):%0A
-
         self
@@ -1031,16 +1031,207 @@
 oject)%0A%0A
+        if not self.silent:%0A            print()%0A            msg = %22Importing project: %25s%22 %25 options%5B%22DIRECTORY%22%5D%0A            print(msg)%0A            print('-' * len(msg))%0A            print()%0A%0A
         
@@ -1338,32 +1338,8 @@
 ory)
-%0A        self.step('%5Cn')
 %0A%0A  
@@ -1992,24 +1992,108 @@
 ')).read()%0A%0A
+        if not self.silent:%0A            print(%22Importing test run: %25s%22 %25 directory)%0A
         self
@@ -2253,16 +2253,16 @@
 etrics,%0A
+
         
@@ -2267,163 +2267,4 @@
   )%0A
-%0A        self.step('.')%0A%0A    silent = False%0A%0A    def step(self, s):%0A        if not self.silent:%0A            sys.stdout.write(s)%0A            sys.stdout.flush()%0A

be verbose