Beispiel #1
0
    def _inject_config(self, extension_config, extension_name):
        """Injects any additions to the configuration file that
        the extension might have.
        
        Returns True if it modified the config file, False otherwise.
        """
        self.logger.log("Adding sections to configuration file", level=2)
        # make a copy so we can modify the sections to fit the existing
        # configuration
        cfg = dict(extension_config)

        save_config = False

        # prepend any html paths with HTML_ROOT from existing configuration
        weecfg.prepend_path(cfg, "HTML_ROOT", self.config_dict["StdReport"]["HTML_ROOT"])

        # If the extension uses a database, massage it so it's compatible
        # with the new V3.2 way of specifying database options
        if "Databases" in cfg:
            for db in cfg["Databases"]:
                db_dict = cfg["Databases"][db]
                # Does this extension use the V3.2+ 'database_type' option?
                if "database_type" not in db_dict:
                    # There is no database type specified. In this case, the
                    # driver type better appear. Fail hard, with a KeyError,
                    # if it does not. Also, if the driver is not for sqlite
                    # or MySQL, then we don't know anything about it.
                    # Assume the extension author knows what s/he is doing,
                    # and leave it be.
                    if db_dict["driver"] == "weedb.sqlite":
                        db_dict["database_type"] = "SQLite"
                        db_dict.pop("driver")
                    elif db_dict["driver"] == "weedb.mysql":
                        db_dict["database_type"] = "MySQL"
                        db_dict.pop("driver")

        new_top_level = []
        # Remember any new top-level sections so we can inject a major
        # comment block
        for top_level in cfg:
            if top_level not in self.config_dict:
                new_top_level.append(top_level)
                self.logger.log("Adding section %s" % top_level, level=3)

        if not self.dry_run:
            # Inject any new config data into the configuration file
            weeutil.weeutil.conditional_merge(self.config_dict, cfg)

            # Include the major comment block for any new top level sections
            for new_section in new_top_level:
                self.config_dict.comments[new_section] = weecfg.major_comment_block + [
                    "# Options for extension '%s'" % extension_name
                ]

            self._reorder(cfg)
            save_config = True

        self.logger.log("Merged extension settings into configuration file", level=3)
        return save_config
Beispiel #2
0
    def _inject_config(self, extension_config, extension_name):
        """Injects any additions to the configuration file that the extension might have.
        
        Returns True if it modified the config file, False otherwise.
        """
        self.logger.log("Adding sections to configuration file", level=2)
        # Make a copy so we can modify the sections to fit the existing configuration
        if isinstance(extension_config, configobj.Section):
            cfg = weeutil.config.deep_copy(extension_config)
        else:
            cfg = dict(extension_config)

        save_config = False

        # Prepend any html paths with HTML_ROOT from existing configuration
        weecfg.prepend_path(cfg, 'HTML_ROOT',
                            self.config_dict['StdReport']['HTML_ROOT'])

        # If the extension uses a database, massage it so it's compatible with the new V3.2 way of
        # specifying database options
        if 'Databases' in cfg:
            for db in cfg['Databases']:
                db_dict = cfg['Databases'][db]
                # Does this extension use the V3.2+ 'database_type' option?
                if 'database_type' not in db_dict:
                    # There is no database type specified. In this case, the driver type better
                    # appear. Fail hard, with a KeyError, if it does not. Also, if the driver is
                    # not for sqlite or MySQL, then we don't know anything about it. Assume the
                    # extension author knows what s/he is doing, and leave it be.
                    if db_dict['driver'] == 'weedb.sqlite':
                        db_dict['database_type'] = 'SQLite'
                        db_dict.pop('driver')
                    elif db_dict['driver'] == 'weedb.mysql':
                        db_dict['database_type'] = 'MySQL'
                        db_dict.pop('driver')

        if not self.dry_run:
            # Inject any new config data into the configuration file
            weeutil.config.conditional_merge(self.config_dict, cfg)

            self._reorder(cfg)
            save_config = True

        self.logger.log("Merged extension settings into configuration file",
                        level=3)
        return save_config
Beispiel #3
0
    def _inject_config(self, extension_config, extension_name):
        """Injects any additions to the configuration file that
        the extension might have.
        
        Returns True if it modified the config file, False otherwise.
        """
        self.logger.log("Adding sections to configuration file", level=2)
        # make a copy so we can modify the sections to fit the existing
        # configuration
        cfg = dict(extension_config)

        save_config = False

        # prepend any html paths with HTML_ROOT from existing configuration
        weecfg.prepend_path(cfg, 'HTML_ROOT',
                            self.config_dict['StdReport']['HTML_ROOT'])

        # If the extension uses a database, massage it so it's compatible
        # with the new V3.2 way of specifying database options
        if 'Databases' in cfg:
            for db in cfg['Databases']:
                db_dict = cfg['Databases'][db]
                # Does this extension use the V3.2+ 'database_type' option?
                if 'database_type' not in db_dict:
                    # There is no database type specified. In this case, the
                    # driver type better appear. Fail hard, with a KeyError,
                    # if it does not. Also, if the driver is not for sqlite
                    # or MySQL, then we don't know anything about it.
                    # Assume the extension author knows what s/he is doing,
                    # and leave it be.
                    if db_dict['driver'] == 'weedb.sqlite':
                        db_dict['database_type'] = 'SQLite'
                        db_dict.pop('driver')
                    elif db_dict['driver'] == 'weedb.mysql':
                        db_dict['database_type'] = 'MySQL'
                        db_dict.pop('driver')

        new_top_level = []
        # Remember any new top-level sections so we can inject a major
        # comment block
        for top_level in cfg:
            if top_level not in self.config_dict:
                new_top_level.append(top_level)
                self.logger.log("Adding section %s" % top_level, level=3)

        if not self.dry_run:
            # Inject any new config data into the configuration file
            weeutil.weeutil.conditional_merge(self.config_dict, cfg)

            # Include the major comment block for any new top level sections
            for new_section in new_top_level:
                self.config_dict.comments[new_section] = \
                    weecfg.major_comment_block + \
                    ["# Options for extension '%s'" % extension_name]

            self._reorder(cfg)
            save_config = True

        self.logger.log("Merged extension settings into configuration file",
                        level=3)
        return save_config
Beispiel #4
0
    def install_from_dir(self, extension_dir):
        """Install the extension whose components are in extension_dir"""
        self.logger.log("Request to install extension found in directory %s" %
                        extension_dir, level=2)

        # The "installer" is actually a dictionary containing what is to be
        # installed and where. The "installer_path" is the path to the file
        # containing that dictionary.        
        installer_path, installer = weecfg.get_extension_installer(extension_dir)
        extension_name = installer.get('name', 'Unknown')
        self.logger.log("Found extension with name '%s'" % extension_name,
                        level=2)

        # Go through all the files used by the extension. A "source tuple" is
        # something like (bin, [user/myext.py, user/otherext.py]). The first
        # element is the directory the files go in, the second element is a
        # list of files to be put in that directory
        self.logger.log("Copying new files", level=2)
        N = 0
        for source_tuple in installer['files']:
            # For each set of sources, see if it's a type we know about
            for directory in ExtensionEngine.target_dirs:
                # This will be something like 'bin', or 'skins':
                source_type = os.path.commonprefix((source_tuple[0], directory))
                # If there is a match, source_type will be something other
                # than an empty string:
                if source_type:
                    # This will be something like 'BIN_ROOT' or 'SKIN_ROOT':
                    root_type = ExtensionEngine.target_dirs[source_type]
                    # Now go through all the files of the source tuple
                    for install_file in source_tuple[1]:
                        source_path = os.path.join(extension_dir, install_file)
                        dst_file = ExtensionEngine._strip_leading_dir(install_file)
                        destination_path = os.path.abspath(os.path.join(self.root_dict[root_type], dst_file))
                        self.logger.log("Copying from '%s' to '%s'" %
                                        (source_path, destination_path),
                                        level=3)
                        if not self.dry_run:
                            try:
                                os.makedirs(os.path.dirname(destination_path))
                            except OSError:
                                pass
                            shutil.copy(source_path, destination_path)
                            N += 1
                    break
            else:
                sys.exit("Unknown destination for file %s" % source_tuple)
        self.logger.log("Copied %d files" % N, level=2)
        
        save_config = False
        new_top_level = []
        # Look for options that have to be injected into the configuration file
        if 'config' in installer:
            self.logger.log("Adding sections to configuration file", level=2)
            # make a copy so we can modify the sections to fit the existing
            # configuration
            cfg = dict(installer['config'])

            # prepend any html paths with HTML_ROOT from existing configuration
            weecfg.prepend_path(cfg, 'HTML_ROOT',
                                self.config_dict['StdReport']['HTML_ROOT'])

            # massage the database dictionaries for this extension
            # FIXME: use parameterized root if possible
            try:
                sqlitecfg = self.config_dict['Databases'].get('archive_sqlite',
                                                              None)
                mysqlcfg = self.config_dict['Databases'].get('archive_mysql',
                                                             None)
                for i in cfg['Databases']:
                    db = cfg['Databases'][i]
                    if db['driver'] == 'weedb.sqlite' and sqlitecfg:
                        db['database_name'] = os.path.join(os.path.dirname(sqlitecfg['database_name']), db['database_name'])
                        db['root'] = sqlitecfg['root']
                    elif db['driver'] == 'weedb.mysql' and mysqlcfg:
                        db['host'] = mysqlcfg['host']
                        db['user'] = mysqlcfg['user']
                        db['password'] = mysqlcfg['password']
            except:
                pass

            # Remember any new top-level sections so we can inject a major
            # comment block
            for top_level in cfg:
                if top_level not in self.config_dict:
                    new_top_level.append(top_level)
                    self.logger.log("Adding section %s" % top_level, level=3)

            if not self.dry_run:
                # Inject any new config data into the configuration file
                weecfg.conditional_merge(self.config_dict, cfg)
                
                # Include the major comment block for any new top level sections
                for new_section in new_top_level:
                    self.config_dict.comments[new_section] = \
                        weecfg.major_comment_block + \
                        ["# Options for extension '%s'" % extension_name]
                    
                save_config = True
            self.logger.log("Merged extension settings into configuration file",
                            level=3)

        # Go through all the possible service groups and see if the extension
        # includes any services that belong in any of them.
        self.logger.log("Adding services to service lists", level=2)
        for service_group in all_service_groups:
            if service_group in installer:
                extension_svcs = weeutil.weeutil.option_as_list(installer[service_group])
                # Be sure that the leaf node is actually a list
                svc_list = weeutil.weeutil.option_as_list(self.config_dict['Engine']['Services'][service_group])
                for svc in extension_svcs:
                    # See if this service is already in the service group
                    if svc not in svc_list:
                        if not self.dry_run:
                            # Add the new service into the appropriate
                            # service group
                            svc_list.append(svc)
                            self.config_dict['Engine']['Services'][service_group] = svc_list
                            save_config = True
                        self.logger.log("Added new service %s to %s" %
                                        (svc, service_group), level=3)

        # Save the extension's install.py file in the extension's installer
        # directory for later use enumerating and uninstalling
        extension_installer_dir = os.path.join(self.root_dict['EXT_ROOT'],
                                               extension_name)
        self.logger.log("Saving installer file to %s" % extension_installer_dir)
        if not self.dry_run:
            try:
                os.makedirs(os.path.join(extension_installer_dir))
            except OSError:
                pass
            shutil.copy2(installer_path, extension_installer_dir)
                                
        if save_config:
            backup_path = weecfg.save_with_backup(self.config_dict,
                                                  self.config_path)
            self.logger.log("Saved configuration dictionary. Backup copy at %s"
                            % backup_path)
Beispiel #5
0
    def _inject_config(self, extension_config, extension_name):
        """Injects any additions to the configuration file that the extension might have.
        
        Returns True if it modified the config file, False otherwise.
        """
        self.logger.log("Adding sections to configuration file", level=2)
        # Make a copy so we can modify the sections to fit the existing configuration
        if isinstance(extension_config, configobj.Section):
            cfg = weeutil.config.deep_copy(extension_config)
        else:
            cfg = dict(extension_config)

        save_config = False

        # Prepend any html paths with HTML_ROOT from existing configuration
        weecfg.prepend_path(cfg, 'HTML_ROOT', self.config_dict['StdReport']['HTML_ROOT'])

        # If the extension uses a database, massage it so it's compatible with the new V3.2 way of
        # specifying database options
        if 'Databases' in cfg:
            for db in cfg['Databases']:
                db_dict = cfg['Databases'][db]
                # Does this extension use the V3.2+ 'database_type' option?
                if 'database_type' not in db_dict:
                    # There is no database type specified. In this case, the driver type better
                    # appear. Fail hard, with a KeyError, if it does not. Also, if the driver is
                    # not for sqlite or MySQL, then we don't know anything about it. Assume the
                    # extension author knows what s/he is doing, and leave it be.
                    if db_dict['driver'] == 'weedb.sqlite':
                        db_dict['database_type'] = 'SQLite'
                        db_dict.pop('driver')
                    elif db_dict['driver'] == 'weedb.mysql':
                        db_dict['database_type'] = 'MySQL'
                        db_dict.pop('driver')

        if not self.dry_run:
            # Inject any new config data into the configuration file
            weeutil.config.conditional_merge(self.config_dict, cfg)

            # For ConfigObj type objects, mark each new section with a major comment block.
            # If cfg is not a ConfigObj type object, an exception will occur, so be prepared to
            # catch it.
            try:
                for section_name in cfg.sections:
                    # The very first section has to be handled differently because it needs not
                    # only a major comment block, but its comments are held in "initial_comment",
                    # not in the attribute .comments like other sections. Quirk of how ConfigObj
                    # works.
                    if section_name == cfg.sections[0]:
                        # Is there an initial comment?
                        if any([x.strip() for x in cfg.initial_comment]):
                            # There is an initial comment. Include not only the major comment
                            # block, but also the initial_comment
                            self.config_dict.comments[section_name] \
                                = weecfg.major_comment_block + cfg.initial_comment
                        else:
                            # No initial comment. Include the major comment block, plus a
                            # synthesized new comment.
                            self.config_dict.comments[section_name] \
                                = weecfg.major_comment_block \
                                  + ["# Options for '%s'" % section_name]
                    else:
                        # Not the first section. Just add a major comment block in front of any
                        # existing comments.
                        self.config_dict.comments[section_name][0:0] = weecfg.major_comment_block
            except AttributeError:
                pass

            self._reorder(cfg)
            save_config = True

        self.logger.log("Merged extension settings into configuration file", level=3)
        return save_config