[wellwell-devel] [wellwell] Add new module WellWell::Plugin for plugin handling.
Stefan Hornburg
wellwell-devel at rt.icdevgroup.org
Wed Oct 13 10:40:38 UTC 2010
commit 2b67a21f3fa831c7b13ad5f0e4b5949e27d27680
Author: Stefan Hornburg (Racke) <racke at linuxia.de>
Date: Wed Oct 13 12:39:52 2010 +0200
Add new module WellWell::Plugin for plugin handling.
Currently supports plugin scan and activation.
catalog.cfg | 5 ++-
lib/WellWell/Core.pm | 24 ++++++++++
lib/WellWell/Data.pm | 12 +++++
lib/WellWell/Plugin.pm | 112 ++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 152 insertions(+), 1 deletions(-)
---
diff --git a/catalog.cfg b/catalog.cfg
index c73693f..5f99a97 100644
--- a/catalog.cfg
+++ b/catalog.cfg
@@ -27,7 +27,10 @@ ImageDir __IMAGE_URL__/
include code/*.*
# Functions to run after completion of the configuration
-StartupHooks prepare_database
+StartupHooks <<EOH
+ prepare_database
+ plugin_scan
+EOH
# Profiles
OrderProfile profiles/*.profile
diff --git a/lib/WellWell/Core.pm b/lib/WellWell/Core.pm
index d6793bc..c718ce7 100644
--- a/lib/WellWell/Core.pm
+++ b/lib/WellWell/Core.pm
@@ -26,6 +26,7 @@ use Vend::Config;
use WellWell::Cart;
use WellWell::Data;
+use WellWell::Plugin qw/plugin_scan plugin_enable/;
# setup configuration directives
Vend::Config::parse_directive('Hook', 'Hook hook');
@@ -33,6 +34,7 @@ Vend::Config::parse_directive('StartupHooks', 'StartupHooks startup_hooks');
# predefined startup hooks
Vend::Config::parse_subroutine('GlobalSub', 'prepare_database WellWell::Data::prepare_database');
+Vend::Config::parse_subroutine('GlobalSub', 'plugin_scan WellWell::Core::plugin_scan_sub');
# all what we want is to transfer CGI values from CGI to the Values
# space, and nothing else
@@ -47,6 +49,28 @@ sub plugins {
return @plugins;
}
+# called at startup to scan plugins
+sub plugin_scan_sub {
+ my ($dbif, $plref);
+
+ $dbif = WellWell::Data::easy_handle();
+ $plref = WellWell::Plugin::plugin_scan($dbif, "$Vend::Cfg->{VendRoot}/plugins",
+ "$Vend::Cfg->{VendRoot}/local/plugins");
+
+ for my $plugin (plugins()) {
+ if (exists $plref->{$plugin}) {
+ if ($plref->{$plugin}->{status} eq 0) {
+ Vend::Config::config_error("Plugin $plugin used in PLUGIN variable is explicitly disabled in plugins table.");
+ return;
+ }
+ elsif (! defined($plref->{$plugin}->{status})) {
+ Vend::Config::config_warn("Enabling plugin $plugin in plugins table.");
+ plugin_enable($dbif, $plugin);
+ }
+ }
+ }
+}
+
sub hooks {
my ($function, $name, @args) = @_;
diff --git a/lib/WellWell/Data.pm b/lib/WellWell/Data.pm
index b8d8ed4..7048981 100644
--- a/lib/WellWell/Data.pm
+++ b/lib/WellWell/Data.pm
@@ -23,6 +23,7 @@ use strict;
use warnings;
use Vend::Data;
+use DBIx::Easy;
sub prepare_database {
my ($userdb_ref, $users_table, %dbif);
@@ -71,4 +72,15 @@ sub prepare_database {
return;
}
+sub easy_handle {
+ my ($dbh, $dbif);
+
+ $dbh = database_exists_ref('products')->dbh();
+
+ $dbif = new DBIx::Easy ($dbh->{Driver}->{Name}, $dbh->{Name});
+ $dbif->{CONN} = $dbh->clone;
+
+ return $dbif;
+}
+
1;
diff --git a/lib/WellWell/Plugin.pm b/lib/WellWell/Plugin.pm
new file mode 100644
index 0000000..80cdcb5
--- /dev/null
+++ b/lib/WellWell/Plugin.pm
@@ -0,0 +1,112 @@
+# WellWell::Plugin - WellWell plugin routines
+#
+# Copyright (C) 2010 Stefan Hornburg (Racke) <racke at linuxia.de>.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public
+# License along with this program; if not, write to the Free
+# Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA 02110-1301, USA.
+
+package WellWell::Plugin;
+
+use vars qw/@ISA @EXPORT_OK/;
+
+require Exporter;
+our @ISA = qw(Exporter);
+our @EXPORT_OK = qw/plugin_scan plugin_enable/;
+
+sub plugin_scan {
+ my ($dbif, @dirs) = @_;
+ my (@plugins, $dirname, $infofile, $plugininfo, $dbref);
+ my ($plugin, $pluginrec);
+ my ($sth, $href);
+ my (%plugins);
+
+ # read current plugins from database
+ $sth = $dbif->process('select * from plugins');
+
+ while ($href = $sth->fetchrow_hashref()) {
+ $plugins{$href->{name}} = $href;
+ }
+
+ for my $dir (@dirs) {
+ opendir(PLUGINS, $dir);
+ while ($dirname = readdir(PLUGINS)) {
+ next unless -d "plugins/$dirname";
+ next if $dirname =~ /^\./;
+
+ # info file ?
+ $infofile = "plugins/$dirname/$dirname.info";
+ if (-f $infofile) {
+ $plugininfo = plugin_get_info($infofile);
+
+ if (exists $plugins{$dirname}) {
+ # existing plugin
+ }
+ else {
+ # new plugin
+ $pluginrec = {name => $dirname,
+ version => $plugininfo->{version},
+ label => $plugininfo->{label} || $dirname,
+ active => undef};
+
+ $dbif->insert('plugins', %$pluginrec);
+
+ $plugins{$dirname} = $pluginrec;
+ }
+ }
+
+ if ($plugininfo->{require}) {
+ my @modules;
+
+ @modules = split(/\s*,\s*/, $plugininfo->{require});
+
+ for (@modules) {
+# warn("Require $_.\n");
+ }
+ }
+ }
+ closedir(PLUGINS);
+ }
+
+ return \%plugins;
+}
+
+sub plugin_enable {
+ my ($dbif, $plugin) = @_;
+
+ $dbif->update('plugins', 'name = ' . $dbif->quote($plugin), active => 1);
+}
+
+sub plugin_get_info {
+ my ($infofile) = @_;
+ my %info;
+
+ open(INFOFILE, $infofile)
+ || die "Failed to open plugin info file $infofile\n";
+ while (<INFOFILE>) {
+ chomp;
+ # skip emptylines and comments
+ next if ! /\S/;
+ next if /^\s*#/;
+ next unless /=/;
+
+ my ($key, $value) = split(/\s*=\s*/, $_, 2);
+ $info{$key} = $value;
+ }
+ close(INFOFILE);
+
+ return \%info;
+}
+
+1;
More information about the wellwell-devel
mailing list