예제 #1
0
    def _updatePlugin(cls, plugin):
        # Load JSON
        plugin_path = os.path.normpath(plugins_path + '/' + plugin.name)
        jsonfile = os.path.normpath(plugin_path + '/' + plugin.name.lower() + '.json')
        try:
            metadata = json.load(open(jsonfile))
        except:
            log.err("Invalid JSON file for plugin {plugin} : {file}".format(plugin = plugin.name, file = jsonfile))
            return

        # Parse file
        for item in metadata:
            if item == 'enabled':
                if metadata[item] == 0 or (type(metadata[item]) == str and metadata[item].lower() == 'false'):
                    setattr(plugin, item, False)
                else:
                    setattr(plugin, item, True)
            elif item != 'crons':
                setattr(plugin, item, metadata[item])

        # Delete older items
        d = plugin.__dict__.copy()
        for k in d:
            if k.startswith("_") == False and k not in metadata and k != 'enabled':
                delattr(plugin, k)

        # TODO remove when uid are not useful
        setattr(plugin, 'uid', plugin.pk)

        #TODO
        setattr(plugin, 'steps', {'count': 0, 'first': None, 'last': None})

        # Add langages from directory search
        setattr(plugin, 'lang', [])
        localedir = os.path.normpath(plugin_path + '/lang')
        for x in os.listdir(localedir):
            try:
                if os.path.isfile("{localedir}/{lang}/LC_MESSAGES/{plugin}.po".format(localedir = localedir, lang = x, plugin = plugin.name.lower())) == True:
                    plugin.lang.append(x)
            except:
                pass

        # Add items
        setattr(plugin, 'path', plugin_path)
        setattr(plugin, 'module', '.'.join(['lisa.plugins', plugin.name, 'modules', plugin.name.lower(), plugin.name]))

        # Save updated plugin
        plugin.save()

        # Update crons
        remove_list = list(Cron.objects(plugin = plugin))
        if metadata.has_key('crons'):
           for cron_name, cron_item in metadata['crons'].iteritems():
                # Get cron from DB
                cron = None
                cron_list = Cron.objects(plugin = plugin, name = cron_name)
                if cron_list is not None and len(cron_list) == 1:
                    cron = cron_list[0]

                # It's a new cron
                if cron is None:
                    # Create a new cron
                    cron = Cron()
                    new_cron= True
                else:
                    # Remove cron from list of crons to remove
                    remove_list.remove(cron)
                    new_cron = False

                # Update cron
                for parameter in cron_item:
                    if parameter != 'enabled':
                        setattr(cron, parameter, cron_item[parameter])

                # Delete older items
                d = cron.__dict__.copy()
                for k in d:
                    if k.startswith("_") == False and k not in cron_item and k != 'enabled':
                        delattr(cron, k)

                # Set enabled only for new crons
                if new_cron == True:
                    if cron_item.has_key('enabled') == True and (cron_item['enabled'] == 0 or (type(cron_item['enabled']) == str and cron_item['enabled'].lower() == 'false')):
                        setattr(cron, 'enabled', False)
                    else:
                        setattr(cron, 'enabled', True)

                # Add items
                setattr(cron, 'name', cron_name)

                # Connect cron to plugin
                cron.plugin = plugin

                # Save cron
                cron.save()

        # Delete older crons for this plugin
        for i in remove_list:
            i.delete()

        # Update intents
        remove_list = list(Intent.objects(plugin = plugin))
        metadata_intents = None
        if metadata.has_key('configuration') and metadata['configuration'].has_key('intents'):
            metadata_intents = metadata['configuration']['intents']
        if metadata.has_key('intents'):
            metadata_intents = metadata['intents']
        if metadata_intents is not None:
            for wit_intent, intent_item in metadata_intents.iteritems():
                # Get intent from DB
                intent = None
                intent_list = Intent.objects(plugin = plugin, name = wit_intent)
                if intent_list is not None and len(intent_list) == 1:
                    intent = intent_list[0]

                # It's a new intent
                if intent is None:
                    # Create a new intent
                    intent = Intent()
                    new_intent= True
                else:
                    # Remove intent from list of intents to remove
                    remove_list.remove(intent)
                    new_intent = False

                # Update intent
                for parameter in intent_item:
                    if parameter == 'method':
                        setattr(intent, 'method_name', intent_item[parameter])
                    elif parameter == 'i_can':
                        setattr(intent, parameter, intent_item[parameter])

                # Delete older items
                d = intent.__dict__.copy()
                for k in d:
                    if k.startswith("_") == False and k not in intent_item and k != 'enabled':
                        delattr(intent, k)

                # Set enabled only for new intents
                if new_intent == True:
                    setattr(intent, 'enabled', True)

                # Connect intent to plugin
                intent.plugin = plugin
                intent.plugin_name = plugin.name

                # Add items
                setattr(intent, 'name', wit_intent)

                # Save intent
                intent.save()

        # Delete older intents for this plugin
        for i in remove_list:
            i.delete()
예제 #2
0
    def _updatePlugin(cls, plugin):
        # Load JSON
        plugin_path = os.path.normpath(plugins_path + '/' + plugin.name)
        jsonfile = os.path.normpath(plugin_path + '/' + plugin.name.lower() +
                                    '.json')
        try:
            metadata = json.load(open(jsonfile))
        except:
            log.err("Invalid JSON file for plugin {plugin} : {file}".format(
                plugin=plugin.name, file=jsonfile))
            return

        # Parse file
        for item in metadata:
            if item == 'enabled':
                if metadata[item] == 0 or (type(metadata[item]) == str and
                                           metadata[item].lower() == 'false'):
                    setattr(plugin, item, False)
                else:
                    setattr(plugin, item, True)
            elif item != 'crons':
                setattr(plugin, item, metadata[item])

        # Delete older items
        d = plugin.__dict__.copy()
        for k in d:
            if k.startswith(
                    "_") == False and k not in metadata and k != 'enabled':
                delattr(plugin, k)

        # TODO remove when uid are not useful
        setattr(plugin, 'uid', plugin.pk)

        #TODO
        setattr(plugin, 'steps', {'count': 0, 'first': None, 'last': None})

        # Add langages from directory search
        setattr(plugin, 'lang', [])
        localedir = os.path.normpath(plugin_path + '/lang')
        for x in os.listdir(localedir):
            try:
                if os.path.isfile(
                        "{localedir}/{lang}/LC_MESSAGES/{plugin}.po".format(
                            localedir=localedir,
                            lang=x,
                            plugin=plugin.name.lower())) == True:
                    plugin.lang.append(x)
            except:
                pass

        # Add items
        setattr(plugin, 'path', plugin_path)
        setattr(
            plugin, 'module', '.'.join([
                'lisa.plugins', plugin.name, 'modules',
                plugin.name.lower(), plugin.name
            ]))

        # Save updated plugin
        plugin.save()

        # Update crons
        remove_list = list(Cron.objects(plugin=plugin))
        if metadata.has_key('crons'):
            for cron_name, cron_item in metadata['crons'].iteritems():
                # Get cron from DB
                cron = None
                cron_list = Cron.objects(plugin=plugin, name=cron_name)
                if cron_list is not None and len(cron_list) == 1:
                    cron = cron_list[0]

                # It's a new cron
                if cron is None:
                    # Create a new cron
                    cron = Cron()
                    new_cron = True
                else:
                    # Remove cron from list of crons to remove
                    remove_list.remove(cron)
                    new_cron = False

                # Update cron
                for parameter in cron_item:
                    if parameter != 'enabled':
                        setattr(cron, parameter, cron_item[parameter])

                # Delete older items
                d = cron.__dict__.copy()
                for k in d:
                    if k.startswith(
                            "_"
                    ) == False and k not in cron_item and k != 'enabled':
                        delattr(cron, k)

                # Set enabled only for new crons
                if new_cron == True:
                    if cron_item.has_key('enabled') == True and (
                            cron_item['enabled'] == 0 or
                        (type(cron_item['enabled']) == str
                         and cron_item['enabled'].lower() == 'false')):
                        setattr(cron, 'enabled', False)
                    else:
                        setattr(cron, 'enabled', True)

                # Add items
                setattr(cron, 'name', cron_name)

                # Connect cron to plugin
                cron.plugin = plugin

                # Save cron
                cron.save()

        # Delete older crons for this plugin
        for i in remove_list:
            i.delete()

        # Update intents
        remove_list = list(Intent.objects(plugin=plugin))
        metadata_intents = None
        if metadata.has_key('configuration'
                            ) and metadata['configuration'].has_key('intents'):
            metadata_intents = metadata['configuration']['intents']
        if metadata.has_key('intents'):
            metadata_intents = metadata['intents']
        if metadata_intents is not None:
            for wit_intent, intent_item in metadata_intents.iteritems():
                # Get intent from DB
                intent = None
                intent_list = Intent.objects(plugin=plugin, name=wit_intent)
                if intent_list is not None and len(intent_list) == 1:
                    intent = intent_list[0]

                # It's a new intent
                if intent is None:
                    # Create a new intent
                    intent = Intent()
                    new_intent = True
                else:
                    # Remove intent from list of intents to remove
                    remove_list.remove(intent)
                    new_intent = False

                # Update intent
                for parameter in intent_item:
                    if parameter == 'method':
                        setattr(intent, 'method_name', intent_item[parameter])
                    elif parameter == 'i_can':
                        setattr(intent, parameter, intent_item[parameter])

                # Delete older items
                d = intent.__dict__.copy()
                for k in d:
                    if k.startswith(
                            "_"
                    ) == False and k not in intent_item and k != 'enabled':
                        delattr(intent, k)

                # Set enabled only for new intents
                if new_intent == True:
                    setattr(intent, 'enabled', True)

                # Connect intent to plugin
                intent.plugin = plugin
                intent.plugin_name = plugin.name

                # Add items
                setattr(intent, 'name', wit_intent)

                # Save intent
                intent.save()

        # Delete older intents for this plugin
        for i in remove_list:
            i.delete()
예제 #3
0
    def installPlugin(self, plugin_name=None, test_mode=False, dev_mode=False, version=None):
        version_str = ""
        if Plugin.objects(name=plugin_name):
            return {'status': 'fail', 'log': unicode(_('Plugin already installed'))}

        if version:
            version_str = ''.join(["==", version])
        if not dev_mode:
            # This test mode is here only for travis to allow installing plugin in a readable directory
            if test_mode:
                pip.main(['install', '--quiet', '--install-option=--install-platlib=' + os.getcwd() + '/../',
                          '--install-option=--install-purelib=' + os.getcwd() + '/../', 'lisa-plugin-' +
                                                                                        plugin_name + version_str])
            else:
                pip.main(['install', 'lisa-plugin-' + plugin_name + version_str])
        jsonfile = self.pkgpath + '/' + plugin_name + '/' + plugin_name.lower() + '.json'
        metadata = json.load(open(jsonfile))

        plugin = Plugin()
        description_list = []
        for item in metadata:
            if item != 'crons' and item != 'rules':
                if item == 'description':
                    for description in metadata[item]:
                        oDescription = Description()
                        for k,v in description.iteritems():
                            setattr(oDescription, k, v)
                        description_list.append(oDescription)
                    setattr(plugin, item, description_list)
                elif item == 'enabled':
                    if metadata[item] == 0:
                        setattr(plugin, item, False)
                    else:
                        setattr(plugin, item, True)
                else:
                    setattr(plugin, item, metadata[item])
        plugin.save()

        for item in metadata:
            if item == 'rules':
                for rule_item in metadata['rules']:
                    rule = Rule()
                    for parameter in rule_item:
                        if parameter == 'enabled':
                            if rule_item[parameter] == 0:
                                setattr(rule, parameter, False)
                            else:
                                setattr(rule, parameter, True)
                        else:
                            setattr(rule, parameter, rule_item[parameter])
                    rule.plugin = plugin
                    rule.save()
            if item == 'crons':
                for cron_item in metadata['crons']:
                    cron = Cron()
                    for parameter in cron_item:
                        if parameter == 'enabled':
                            if cron_item[parameter] == 0:
                                setattr(cron, parameter, False)
                            else:
                                setattr(cron, parameter, True)
                        else:
                            setattr(cron, parameter, cron_item[parameter])
                    cron.plugin = plugin
                    cron.save()

        for intent, value in metadata['configuration']['intents'].iteritems():
            oIntent = Intent()
            oIntent.name = intent
            oIntent.function = value['method']
            oIntent.module = '.'.join(['lisa.plugins', plugin_name, 'modules', plugin_name.lower(),
                                       plugin_name])
            oIntent.enabled = True
            oIntent.plugin = plugin
            oIntent.save()
        return {'status': 'success', 'log': unicode(_('Plugin installed'))}