Esempio n. 1
0
File: test.py Progetto: zeth/magpy
    def handle(self, *args, **kwargs):
        """
        Run the unit tests for all the test labels in the provided list.
        Labels must be of the form:
         - app,TestClass,test_method
            Run a single specific test method
         - app,TestClass
            Run all the test methods in a given class
         - app
            Search for doctests and unittests in the named application.

        When looking for tests, the test runner will look in the models and
        tests modules for the application.

        A list of 'extra' tests may also be provided; these tests
        will be added to the test suite.

        Returns the number of tests that failed.
        """

        if args:
            labels = args
        else:
            database = Database()
            labels = database.get_app_list()

        suite = self.get_suite(labels)

        verbosity = int(kwargs['verbosity'])
        failures = unittest.TextTestRunner(verbosity=verbosity).run(suite)
        if failures:
            sys.exit(bool(failures))
Esempio n. 2
0
class InstanceLoader(object):
    """Load instance into the database."""

    def __init__(self,
                 handle_none=False,
                 validation=True,
                 database=None,
                 embedded=False):
        """Startup the loader."""
        self.handle_none = handle_none
        self.skip_validation = not validation
        database_type = None
        if embedded:
            database_type = 'ainodb'
        else:
            database_type = None

        self.database = Database(
            database_name=database,
            database_type=database_type)

    def add_instance(self, instance):
        """Add instance to the db."""
        self.database.add_instance(
            instance,
            skip_validation=self.skip_validation,
            handle_none=self.handle_none)

    def add_instances(self, instances):
        """All several instances to the db."""
        for instance in instances:
            self.add_instance(instance)
Esempio n. 3
0
File: test.py Progetto: zeth/magpy
    def handle(self, *args, **kwargs):
        """
        Run the unit tests for all the test labels in the provided list.
        Labels must be of the form:
         - app,TestClass,test_method
            Run a single specific test method
         - app,TestClass
            Run all the test methods in a given class
         - app
            Search for doctests and unittests in the named application.

        When looking for tests, the test runner will look in the models and
        tests modules for the application.

        A list of 'extra' tests may also be provided; these tests
        will be added to the test suite.

        Returns the number of tests that failed.
        """

        if args:
            labels = args
        else:
            database = Database()
            labels = database.get_app_list()

        suite = self.get_suite(labels)

        verbosity = int(kwargs['verbosity'])
        failures = unittest.TextTestRunner(verbosity=verbosity).run(suite)
        if failures:
            sys.exit(bool(failures))
Esempio n. 4
0
class InstanceLoader(object):
    """Load instance into the database."""
    def __init__(self,
                 handle_none=False,
                 validation=True,
                 database=None,
                 embedded=False):
        """Startup the loader."""
        self.handle_none = handle_none
        self.skip_validation = not validation
        database_type = None
        if embedded:
            database_type = 'ainodb'
        else:
            database_type = None

        self.database = Database(database_name=database,
                                 database_type=database_type)

    def add_instance(self, instance):
        """Add instance to the db."""
        self.database.add_instance(instance,
                                   skip_validation=self.skip_validation,
                                   handle_none=self.handle_none)

    def add_instances(self, instances):
        """All several instances to the db."""
        for instance in instances:
            self.add_instance(instance)
Esempio n. 5
0
    def handle(self, *args, **kwargs):
        print(args)
        database = Database()
        pkl_file = open(args[0], 'rb')
        instances = pickle.load(pkl_file)
        collection = database.get_collection(args[1])
        for instance in instances:
            collection.insert(instance)
            print("Added", instance['_id'])

        pkl_file.close()
Esempio n. 6
0
    def handle(self, *args, **kwargs):
        print(args)
        database = Database()
        pkl_file = open(args[0], 'rb')
        instances = pickle.load(pkl_file)
        collection = database.get_collection(args[1])
        for instance in instances:
            collection.insert(instance)
            print("Added", instance['_id'])

        pkl_file.close()
Esempio n. 7
0
    def setUp(self):  # pylint: disable=C0103
        """Open a database connection."""
        super(TestEmbedModificationValidationB, self).setUp()

        # Create a new test model
        instance_loader = InstanceLoader(database='test', validation=False)
        instance_loader.add_instances(six.itervalues(EMBEDDED_MODELS_B))
        # Kill any test existing instances
        database = Database(database_name='test')
        self.collection = database.get_collection('article')
        self.collection.remove()
        # Add the test article
        self.collection.insert(TEST_ARTICLE_B)
Esempio n. 8
0
    def setUp(self):  # pylint: disable=C0103
        """Open a database connection."""
        super(TestEmbedModificationValidationB, self).setUp()

        # Create a new test model
        instance_loader = InstanceLoader(
            database='test',
            validation=False)
        instance_loader.add_instances(six.itervalues(EMBEDDED_MODELS_B))
        # Kill any test existing instances
        database = Database(database_name='test')
        self.collection = database.get_collection('article')
        self.collection.remove()
        # Add the test article
        self.collection.insert(TEST_ARTICLE_B)
Esempio n. 9
0
    def __init__(self,
                 handle_none=False,
                 validation=True,
                 database=None,
                 embedded=False):
        """Startup the loader."""
        self.handle_none = handle_none
        self.skip_validation = not validation
        database_type = None
        if embedded:
            database_type = 'ainodb'
        else:
            database_type = None

        self.database = Database(database_name=database,
                                 database_type=database_type)
Esempio n. 10
0
 def handle(self, *args, **kwargs):
     """Get the Urls."""
     database = Database()
     apps = database.get_app_list()
     urls = URLS
     if apps:
         for app in apps:
             url_path = '%s.urls' % app
             try:
                 url_module = importlib.import_module(url_path)
             except:
                 pass
             else:
                 urls.extend(getattr(url_module, 'URLS', []))
     for url in urls:
         print(url)
Esempio n. 11
0
class URLLoader(object):
    """Load URLs from apps."""

    def __init__(self):
        """Load URLs."""
        self.apps = []
        self.database = Database()

    def get_urls(self):
        """Get the urls."""
        apps = self.database.get_app_list()
        if not apps:
            return URLS

        urls = URLS
        for app in apps:
            url_path = '%s.urls' % app
            try:
                url_module = importlib.import_module(url_path)
            except:
                print("Cannot import: %s" % url_path)
                print("Skipping app: %s" % app)
            else:
                urls.extend(getattr(url_module, 'URLS', []))
        return urls
Esempio n. 12
0
    def handle(self, *args, **options):
        """Collect the static files of an application."""
        database = Database()

        # 0. Get the target directory
        target = database.get_setting('static', 'root')
        if not target:
            raise CommandError(
                u'No static root setting. Set static root with:\n'
                u'mag.py add_setting static root <directory-name>\n'
                u'E.g.\n'
                u'mag.py add_setting static root /var/www/static')

        # 1. Get the list of applications
        apps = database.get_app_list()
        if apps:
            apps.append('magpy')
        else:
            apps = ['magpy']
            
        # 2. Build a dictionary of static file names
        static_files = []
        for app in apps:
            print("Looking for static files in ", app)
            path = self.get_path(app)
            if path:
                static_files.extend(
                    self.get_filenames(
                        path, target, app, options["appify"]))

        # 3. Check if the timestamps on the static files are newer than the
        # the saved files, and cut out any that are not
        if options['check_time']:
            static_files = filter(self.check_newer, static_files)

        # 4. Copy the files
        for source, target in static_files:
            # Make sure the target directory exist
            target_dir = os.path.dirname(target)
            if not os.path.exists(target_dir):
                os.makedirs(target_dir)
            # Copy the file
            print("Copying %s to %s" % (source, target))
            copy(source, target)
Esempio n. 13
0
    def setUp(self):  # pylint: disable=C0103
        """Open a database connection and load the models."""
        super(MagEmbedTestCase, self).setUp()

        # Create test models
        EMBEDDED_MODELS['article']['_permissions'] = {
            'create': True,
            'read': True,
            'update': True,
            'delete': True,
        }

        instance_loader = InstanceLoader(database='test', validation=False)
        instance_loader.add_instances(tuple(six.itervalues(EMBEDDED_MODELS)))

        # Kill any test existing instances
        database = Database(database_name='test')
        collection = database.get_collection('article')
        collection.remove()
Esempio n. 14
0
    def handle(self, *args, **options):
        """Collect the static files of an application."""
        database = Database()

        # 0. Get the target directory
        target = database.get_setting('static', 'root')
        if not target:
            raise CommandError(
                u'No static root setting. Set static root with:\n'
                u'mag.py add_setting static root <directory-name>\n'
                u'E.g.\n'
                u'mag.py add_setting static root /var/www/static')

        # 1. Get the list of applications
        apps = database.get_app_list()
        if apps:
            apps.append('magpy')
        else:
            apps = ['magpy']

        # 2. Build a dictionary of static file names
        static_files = []
        for app in apps:
            print("Looking for static files in ", app)
            path = self.get_path(app)
            if path:
                static_files.extend(
                    self.get_filenames(path, target, app, options["appify"]))

        # 3. Check if the timestamps on the static files are newer than the
        # the saved files, and cut out any that are not
        if options['check_time']:
            static_files = filter(self.check_newer, static_files)

        # 4. Copy the files
        for source, target in static_files:
            # Make sure the target directory exist
            target_dir = os.path.dirname(target)
            if not os.path.exists(target_dir):
                os.makedirs(target_dir)
            # Copy the file
            print("Copying %s to %s" % (source, target))
            copy(source, target)
Esempio n. 15
0
def get_commands():
    """
    Returns a dictionary mapping command names to their callback applications.

    This works by looking for a management.commands package in magpy, and
    in each installed application -- if a commands package exists, all commands
    in that package are registered.

    The dictionary is in the format {command_name: app_name}. Key-value
    pairs from this dictionary can then be used in calls to
    load_command_class(app_name, command_name)

    If a specific version of a command must be loaded (e.g., with the
    startapp command), the instantiated module can be placed in the
    dictionary in place of the application name.

    The dictionary is cached on the first call and reused on subsequent
    calls.
    """
    global _COMMANDS  # pylint: disable-msg=W0603
    if _COMMANDS is None:
        # Find the builtin commands
        magpy_path = find_management_module('magpy')
        _COMMANDS = dict([(name, 'magpy') for \
                              name in find_commands(magpy_path)])
        # Find the installed apps
        database = Database()
        apps = database.get_app_list()
        if apps == None:
            apps = []

        # Find and load the management module for each installed app.
        for app_name in apps:
            try:
                path = find_management_module(app_name)
                _COMMANDS.update(dict([(name, app_name)
                                       for name in find_commands(path)]))
            except ImportError:
                pass  # No management module - ignore this app

    return _COMMANDS
Esempio n. 16
0
    def setUp(self):  # pylint: disable=C0103
        """Open a database connection and load the models."""
        super(MagEmbedTestCase, self).setUp()

        # Create test models
        EMBEDDED_MODELS['article']['_permissions'] = {
            'create': True,
            'read': True,
            'update': True,
            'delete': True,
            }

        instance_loader = InstanceLoader(
            database='test',
            validation=False)
        instance_loader.add_instances(tuple(six.itervalues(EMBEDDED_MODELS)))

        # Kill any test existing instances
        database = Database(database_name='test')
        collection = database.get_collection('article')
        collection.remove()
Esempio n. 17
0
    def __init__(self,
                 handle_none=False,
                 validation=True,
                 database=None,
                 embedded=False):
        """Startup the loader."""
        self.handle_none = handle_none
        self.skip_validation = not validation
        database_type = None
        if embedded:
            database_type = 'ainodb'
        else:
            database_type = None

        self.database = Database(
            database_name=database,
            database_type=database_type)
Esempio n. 18
0
class URLLoader(object):
    """Load URLs from apps."""
    def __init__(self):
        """Load URLs."""
        self.apps = []
        self.database = Database()

    def get_urls(self):
        """Get the urls."""
        apps = self.database.get_app_list()
        if not apps:
            return URLS

        urls = URLS
        for app in apps:
            url_path = '%s.urls' % app
            try:
                url_module = importlib.import_module(url_path)
            except:
                print("Cannot import: %s" % url_path)
                print("Skipping app: %s" % app)
            else:
                urls.extend(getattr(url_module, 'URLS', []))
        return urls
Esempio n. 19
0
 def handle(self, *args, **kwargs):
     database = Database()
     if len(args) != 2:
         raise CommandError
     database.remove_setting(args[0], args[1])
Esempio n. 20
0
 def handle(self, *args, **kwargs):
     database = Database()
     for arg in args:
         database.add_app(arg)
Esempio n. 21
0
 def handle(self, *args, **kwargs):
     database = Database()
     if len(args) != 2:
         raise CommandError
     database.remove_setting(args[0], args[1])
Esempio n. 22
0
    def autocomplete(self):  # pylint: disable-msg=R0914
        """
        Output completion suggestions for BASH.

        The output of this function is passed to BASH's `COMREPLY` variable and
        treated as completion suggestions. `COMREPLY` expects a space
        separated string as the result.

        The `COMP_WORDS` and `COMP_CWORD` BASH environment variables are used
        to get information about the cli input. Please refer to the BASH
        man-page for more information about this variables.

        Subcommand options are saved as pairs. A pair consists of
        the long option string (e.g. '--exclude') and a boolean
        value indicating if the option requires arguments. When printing to
        stdout, a equal sign is appended to options which require arguments.

        Note: If debugging this function, it is recommended to write the debug
        output in a separate file. Otherwise the debug output will be treated
        and formatted as potential completion suggestions.
        """
        # Don't complete if user hasn't sourced bash_completion file.
        # This is found in django-trunk/extras/django_bash_completion
        if 'DJANGO_AUTO_COMPLETE' not in os.environ:
            return

        cwords = os.environ['COMP_WORDS'].split()[1:]
        cword = int(os.environ['COMP_CWORD'])

        try:
            curr = cwords[cword - 1]
        except IndexError:
            curr = ''

        subcommands = get_commands().keys() + ['help']
        options = [('--help', None)]

        # subcommand
        if cword == 1:
            debug_text = ' '.join(sorted(filter(lambda x: x.startswith(curr),
                                                subcommands)))
            print(debug_text)
        # subcommand options
        # special case: the 'help' subcommand has no options
        elif cwords[0] in subcommands and cwords[0] != 'help':
            subcommand_cls = self.fetch_command(cwords[0])
            # special case: add the names of installed apps to options
            if cwords[0] in ('dumpdata', 'sql', 'sqlall', 'sqlclear',
                    'sqlcustom', 'sqlindexes', 'sqlsequencereset', 'test'):
                try:
                    database = Database()
                    # Get the last part of the dotted path as the app name.
                    options += [(a.split('.')[-1], 0) for \
                                    a in database.get_app_list()]
                except ImportError:
                    # Fail silently if DJANGO_SETTINGS_MODULE isn't set. The
                    # user will find out once they execute the command.
                    pass
            options += [(s_opt.get_opt_string(), s_opt.nargs) for s_opt in
                        subcommand_cls.option_list]
            # filter out previously specified options from available options
            prev_opts = [x.split('=')[0] for x in cwords[1:cword - 1]]

            # Original Python 2 version
            #options = filter(lambda (x, v): x not in prev_opts, options)
            # Python 3 version?
            #options = filter(lambda x_v: x_v[0] not in prev_opts, options)
            options = [opt for opt in options if opt[0] not in prev_opts]

            # filter options by current input
            options = sorted([(k, v) for k, v in \
                                  options if k.startswith(curr)])
            for option in options:
                opt_label = option[0]
                # append '=' to options which require args
                if option[1]:
                    opt_label += '='
                print(opt_label)
        sys.exit(1)
Esempio n. 23
0
 def handle(self, *args, **kwargs):
     database = Database()
     for arg in args:
         database.add_app(arg)
Esempio n. 24
0
 def __init__(self):
     """Load URLs."""
     self.apps = []
     self.database = Database()
Esempio n. 25
0
 def __init__(self):
     """Load URLs."""
     self.apps = []
     self.database = Database()
Esempio n. 26
0
 def handle(self, *args, **kwargs):
     database = Database()
     if len(args) != 3:
         raise CommandError
     database.add_setting(args[0], args[1], args[2])