From d5d920e992c24e5bce65e2a95f5c1d95dd41405e Mon Sep 17 00:00:00 2001 From: Oliver Tappe Date: Sat, 21 Jul 2007 19:16:57 +0000 Subject: * first part of configuration file migration to Config::General git-svn-id: http://svn.openslx.org/svn/openslx/trunk@1268 95ad53e4-c205-0410-b2fa-d234c58c8868 --- Makefile | 4 ++-- bin/slxsettings | 17 +++++++++++------ lib/OpenSLX/Basics.pm | 30 +++++++++++------------------- 3 files changed, 24 insertions(+), 27 deletions(-) diff --git a/Makefile b/Makefile index 4ace8a22..dbdf8358 100644 --- a/Makefile +++ b/Makefile @@ -95,8 +95,8 @@ install: @ # many of the following modules are part of core perl, but we check @ # for them just to be sure... - @for m in Carp DBI Digest::MD5 Fcntl File::Basename FindBin Getopt::Long \ - Pod::Usage ; do \ + @for m in Carp Config::General DBI Digest::MD5 Fcntl File::Basename FindBin \ + Getopt::Long Pod::Usage ; do \ if ! perl -e "use $$m" 2>>${SLX_INSTALL_LOG} ; then \ echo " The perl-module '$$m' is required, please install it."; \ exit 1; \ diff --git a/bin/slxsettings b/bin/slxsettings index adb26f69..be318bb5 100755 --- a/bin/slxsettings +++ b/bin/slxsettings @@ -36,6 +36,7 @@ use lib "$FindBin::RealBin/../lib"; use lib "$FindBin::RealBin"; # development path to config-db stuff +use Config::General; use Getopt::Long qw(:config pass_through); use Pod::Usage; @@ -87,7 +88,11 @@ while (scalar @ARGV) { } # fetch current content of local settings file... -my $settings = slurpFile("$openslxConfig{'config-path'}/settings"); +my $fileName = "$openslxConfig{'config-path'}/settings"; +my $configObj = Config::General->new( + -ConfigFile => $fileName, -SplitPolicy => 'equalsign', +); +my %settings = $configObj->getall(); my %changed; @@ -103,8 +108,8 @@ foreach my $key (keys %givenSettings) { } vlog(0, _tr("setting %s to '%s'", $key, $value)) unless $quiet; my $externalKey = externalKeyFor($key); - if (!($settings =~ s[^\s*$externalKey=.*?$][$externalKey=$value]ms)) { - $settings .= "$externalKey=$value\n"; + if (!exists $settings{$externalKey} || $settings{$externalKey} ne $value) { + $settings{$externalKey} = $value; } $changed{$key}++; } @@ -112,7 +117,8 @@ foreach my $key (keys %givenSettings) { # reset specified keys to fall back to default: foreach my $key (@reset) { my $externalKey = externalKeyFor($key); - if ($settings =~ s[^\s*?$externalKey=.*?\n][]ms) { + if (exists $settings{$externalKey}) { + delete $settings{$externalKey}; vlog(0, _tr("removing option '%s' from local settings", $key)) unless $quiet; @@ -126,8 +132,7 @@ foreach my $key (@reset) { # ... and write local settings file if necessary if (keys %changed) { - my $fileName = "$openslxConfig{'config-path'}/settings"; - spitFile($fileName, $settings); + $configObj->save_file($fileName, \%settings); openslxInit(); diff --git a/lib/OpenSLX/Basics.pm b/lib/OpenSLX/Basics.pm index 4c3bf49c..fb7db676 100644 --- a/lib/OpenSLX/Basics.pm +++ b/lib/OpenSLX/Basics.pm @@ -50,6 +50,7 @@ use Carp::Heavy; # use it here to have it loaded immediately, not at # be at a point in time where the script executes in # a chrooted environment, such that the module can't # be loaded anymore). +use Config::General; use Encode; require File::Glob; use FindBin; @@ -160,38 +161,29 @@ sub openslxInit my $configPath = $cmdlineConfig{'config-path'} || $openslxConfig{'config-path'}; my $sharePath = "$openslxConfig{'base-path'}/share"; - my $configFH; my $verboseLevel = $cmdlineConfig{'verbose-level'} || 0; foreach my $f ("$sharePath/settings.default", "$configPath/settings", "$ENV{HOME}/.openslx/settings") { - next unless open($configFH, '<', $f); + next unless -e $f; if ($verboseLevel >= 2) { vlog(0, "reading config-file $f..."); } - while (<$configFH>) { - chomp; - s/#.*//; - s/^\s+//; - s/\s+$//; - next unless length; - if (!/^(\w+)=(.*)$/) { - die _tr("config-file <%s> has incorrect syntax here:\n\t%s\n", - $f, $_); - } - my ($key, $value) = ($1, $2); - - # N.B.: the config files are used by shell-scripts, too, so in + my %config = ParseConfig( + -ConfigFile => $f, -AutoTrue => 1, -LowerCaseNames => 1 + ); + foreach my $key (keys %config) { + # N.B.: these config files are used by shell-scripts, too, so in # order to comply with shell-style, the config files use shell # syntax and an uppercase, underline-as-separator format. # Internally, we use lowercase, minus-as-separator format, so we # need to convert the environment variable names to our own # internal style here (e.g. 'SLX_BASE_PATH' to 'base-path'): - $key =~ s[^SLX_][]; - $key =~ tr/[A-Z]_/[a-z]-/; - $openslxConfig{$key} = $value; + my $ourKey = $key; + $ourKey =~ s[^slx_][]; + $ourKey =~ tr/_/-/; + $openslxConfig{$ourKey} = $config{$key}; } - close $configFH; } # push any cmdline argument into our config hash, possibly overriding any -- cgit v1.2.3-55-g7522