# Path: percept/management/commands.py
# class BaseCommand(object):
#     """
#     All commands inherit from this class
#     """
#     #Allowed options
#     option_list = (
#         make_option('--settings',
#                     help='The Python path to a settings module, e.g. "myproject.settings.main".'),
#         make_option('--pythonpath',
#                     help='A directory to add to the Python path'),
#     )
#     command_help = ''
#     args = ''
# 
#     def usage(self, subcommand):
#         """
#         Print the usage for a given subcommand
#         """
#         usage = '%%prog %s [options] %s' % (subcommand, self.args)
#         return usage
# 
#     def create_parser(self, prog_name, subcommand):
#         """
#         Create an OptionParser
#         prog_name - Name of a command
#         subcommand - Name of a subcommand
#         """
#         parser = OptionParser(prog=prog_name,
#                               usage=self.usage(subcommand),
#                               option_list=self.option_list)
#         return parser
# 
#     def print_help(self, prog_name, subcommand):
#         """
#         Print the help that the parser generates
#         prog_name - Name of a command
#         subcommand - Name of a subcommand
#         """
#         parser = self.create_parser(prog_name, subcommand)
#         parser.print_help()
# 
#     def run_from_argv(self, argv):
#         """
#         Given certain command line arguments, run the basecommand
#         argv - command line arguments
#         """
#         parser = self.create_parser(argv[0], argv[1])
#         options, args = parser.parse_args(argv[2:])
#         options = handle_default_options(options)
#         self.execute(*args, **options.__dict__)
# 
#     def execute(self, *args, **options):
#         from percept.conf.base import settings
#         self.command(*args, **options)
# 
#     def command(self, *args, **options):
#         """
#         Overridden by the class that inherits from this.  Contains logic to run the command.
#         """
#         raise NotImplementedError()
# 
# Path: percept/conf/base.py
# class Settings(object):
#     def _initialize(self, settings_module):
#     def _setup(self):
#     def __getattr__(self, name):
#     def _configure_logging(self):
#     def configured(self):


import nose
import pkgutil
import os
from percept.management.commands import BaseCommand
from percept.conf.base import settings
from importlib import import_module
from optparse import make_option
"""
Command line command to run tests
"""



class Command(BaseCommand):
    args = 'cover'
    def command(self, *args, **options):
        try:
            cover = args[0]
            cover = cover.lower() == "true"
        except IndexError:
            cover = False
        if cover:
            #Grab the pythonpath argument and look for tests there
            app_names = settings.INSTALLED_APPS
            #Grab the top level app name from installed_apps
            app_labels = list(set([a.split('.')[0] for a in app_names]))
            app_paths = []
            #We want to figure out coverage for the "lower-level" apps, so import all the top level apps
            #and get their paths
            for al in app_labels:
                mod = import_module(al)
                app_paths.append(os.path.dirname(mod.__file__))
            #Pass paths to pkgutil to get the names of the submodules

# Path: percept/management/commands.py
# class BaseCommand(object):
#     """
#     All commands inherit from this class
#     """
#     #Allowed options
#     option_list = (
#         make_option('--settings',
#                     help='The Python path to a settings module, e.g. "myproject.settings.main".'),
#         make_option('--pythonpath',
#                     help='A directory to add to the Python path'),
#     )
#     command_help = ''
#     args = ''
# 
#     def usage(self, subcommand):
#         """
#         Print the usage for a given subcommand
#         """
#         usage = '%%prog %s [options] %s' % (subcommand, self.args)
#         return usage
# 
#     def create_parser(self, prog_name, subcommand):
#         """
#         Create an OptionParser
#         prog_name - Name of a command
#         subcommand - Name of a subcommand
#         """
#         parser = OptionParser(prog=prog_name,
#                               usage=self.usage(subcommand),
#                               option_list=self.option_list)
#         return parser
# 
#     def print_help(self, prog_name, subcommand):
#         """
#         Print the help that the parser generates
#         prog_name - Name of a command
#         subcommand - Name of a subcommand
#         """
#         parser = self.create_parser(prog_name, subcommand)
#         parser.print_help()
# 
#     def run_from_argv(self, argv):
#         """
#         Given certain command line arguments, run the basecommand
#         argv - command line arguments
#         """
#         parser = self.create_parser(argv[0], argv[1])
#         options, args = parser.parse_args(argv[2:])
#         options = handle_default_options(options)
#         self.execute(*args, **options.__dict__)
# 
#     def execute(self, *args, **options):
#         from percept.conf.base import settings
#         self.command(*args, **options)
# 
#     def command(self, *args, **options):
#         """
#         Overridden by the class that inherits from this.  Contains logic to run the command.
#         """
#         raise NotImplementedError()
# 
# Path: percept/conf/base.py
# class Settings(object):
#     def _initialize(self, settings_module):
#     def _setup(self):
#     def __getattr__(self, name):
#     def _configure_logging(self):
#     def configured(self):


# Path: percept/tests/commands/test.py
import nose
import pkgutil
import os
from percept.management.commands import BaseCommand
from percept.conf.base import settings
from importlib import import_module
from optparse import make_option

"""
Command line command to run tests
"""



class Command(BaseCommand):
    args = 'cover'
    def command(self, *args, **options):
        try:
            cover = args[0]
            cover = cover.lower() == "true"
        except IndexError:
            cover = False
        if cover:
            #Grab the pythonpath argument and look for tests there
            app_names = settings.INSTALLED_APPS
            #Grab the top level app name from installed_apps
            app_labels = list(set([a.split('.')[0] for a in app_names]))
            app_paths = []
            #We want to figure out coverage for the "lower-level" apps, so import all the top level apps
            #and get their paths
            for al in app_labels:
                mod = import_module(al)
                app_paths.append(os.path.dirname(mod.__file__))
            #Pass paths to pkgutil to get the names of the submodules

            sub_labels = [name for _, name, _ in pkgutil.iter_modules(app_paths) if name not in settings.DO_NOT_COVER]