From 2ad99c20bcc297362aeccabb1c51e20c4bd4b533 Mon Sep 17 00:00:00 2001 From: Thomas Hochstein Date: Tue, 3 Sep 2013 09:21:55 +0200 Subject: [PATCH 1/3] Redo directory structure. * Move all scripts to /bin * Move configuration to /etc * Move NewsStats.pm to /lib * Add new path to NewsStats.pm to all scripts * Set $HomePath to top level directory * Move setting of config file name to ReadConf() Signed-off-by: Thomas Hochstein --- .gitignore | 2 +- feedlog.pl => bin/feedlog.pl | 11 ++++++----- gatherstats.pl => bin/gatherstats.pl | 11 ++++++----- groupstats.pl => bin/groupstats.pl | 11 ++++++----- newsstats.conf.sample => etc/newsstats.conf.sample | 0 install/install.pl | 12 ++++++------ NewsStats.pm => lib/NewsStats.pm | 10 ++++++++-- 7 files changed, 33 insertions(+), 24 deletions(-) rename feedlog.pl => bin/feedlog.pl (97%) rename gatherstats.pl => bin/gatherstats.pl (98%) rename groupstats.pl => bin/groupstats.pl (99%) rename newsstats.conf.sample => etc/newsstats.conf.sample (100%) rename NewsStats.pm => lib/NewsStats.pm (99%) diff --git a/.gitignore b/.gitignore index 40a80e5..a71cb91 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ tmp/ tmp/* -newsstats.conf +etc/newsstats.conf diff --git a/feedlog.pl b/bin/feedlog.pl similarity index 97% rename from feedlog.pl rename to bin/feedlog.pl index a5ecfb0..ef9429c 100755 --- a/feedlog.pl +++ b/bin/feedlog.pl @@ -15,7 +15,8 @@ BEGIN { our $VERSION = "0.01"; use File::Basename; - push(@INC, dirname($0)); + # we're in .../bin, so our module is in ../lib + push(@INC, dirname($0).'/../lib'); } use strict; use warnings; @@ -75,7 +76,7 @@ GetOptions ('d|debug!' => \$OptDebug, 'V|version' => \&ShowVersion) or exit 1; ### read configuration -my %Conf = %{ReadConfig($HomePath.'/newsstats.conf')}; +my %Conf = %{ReadConfig('')}; ### init syslog openlog($0, 'nofatal,pid', LOG_NEWS); @@ -218,15 +219,15 @@ See L for further information. =over 4 -=item F +=item F The script itself. -=item F +=item F Library functions for the NewsStats package. -=item F +=item F Runtime configuration file. diff --git a/gatherstats.pl b/bin/gatherstats.pl similarity index 98% rename from gatherstats.pl rename to bin/gatherstats.pl index f1fe7de..f545456 100755 --- a/gatherstats.pl +++ b/bin/gatherstats.pl @@ -15,7 +15,8 @@ BEGIN { our $VERSION = "0.01"; use File::Basename; - push(@INC, dirname($0)); + # we're in .../bin, so our module is in ../lib + push(@INC, dirname($0).'/../lib'); } use strict; use warnings; @@ -52,7 +53,7 @@ GetOptions ('c|checkgroups=s' => \$OptCheckgroupsFile, 'V|version' => \&ShowVersion) or exit 1; ### read configuration -my %Conf = %{ReadConfig($HomePath.'/newsstats.conf')}; +my %Conf = %{ReadConfig('')}; ### override configuration via commandline options my %ConfOverride; @@ -368,15 +369,15 @@ checking against checkgroups-*: =over 4 -=item F +=item F The script itself. -=item F +=item F Library functions for the NewsStats package. -=item F +=item F Runtime configuration file. diff --git a/groupstats.pl b/bin/groupstats.pl similarity index 99% rename from groupstats.pl rename to bin/groupstats.pl index 499e333..f1e566b 100755 --- a/groupstats.pl +++ b/bin/groupstats.pl @@ -15,7 +15,8 @@ BEGIN { our $VERSION = "0.01"; use File::Basename; - push(@INC, dirname($0)); + # we're in .../bin, so our module is in ../lib + push(@INC, dirname($0).'/../lib'); } use strict; use warnings; @@ -81,7 +82,7 @@ if ($OptReportType) { my $ValidGroups = &ReadGroupList($OptCheckgroupsFile) if $OptCheckgroupsFile; ### read configuration -my %Conf = %{ReadConfig($HomePath.'/newsstats.conf')}; +my %Conf = %{ReadConfig('')}; ### override configuration via commandline options my %ConfOverride; @@ -635,15 +636,15 @@ machine-readable form (without formatting): =over 4 -=item F +=item F The script itself. -=item F +=item F Library functions for the NewsStats package. -=item F +=item F Runtime configuration file. diff --git a/newsstats.conf.sample b/etc/newsstats.conf.sample similarity index 100% rename from newsstats.conf.sample rename to etc/newsstats.conf.sample diff --git a/install/install.pl b/install/install.pl index ff392df..a5860d4 100755 --- a/install/install.pl +++ b/install/install.pl @@ -14,8 +14,8 @@ BEGIN { our $VERSION = "0.01"; use File::Basename; - # we're in .../install, so our module is in .. - push(@INC, dirname($0).'/..'); + # we're in .../install, so our module is in ../lib + push(@INC, dirname($0).'/../lib'); } use strict; use warnings; @@ -42,7 +42,7 @@ my $Path = cwd(); ### read configuration print("Reading configuration.\n"); -my %Conf = %{ReadConfig($Path.'/newsstats.conf')}; +my %Conf = %{ReadConfig('')}; ##### -------------------------------------------------------------------------- ##### Database table definitions @@ -294,15 +294,15 @@ Don't do a fresh install, but update from I. =over 4 -=item F +=item F The script itself. -=item F +=item F Library functions for the NewsStats package. -=item F +=item F Runtime configuration file. diff --git a/NewsStats.pm b/lib/NewsStats.pm similarity index 99% rename from NewsStats.pm rename to lib/NewsStats.pm index a237774..8a62179 100644 --- a/NewsStats.pm +++ b/lib/NewsStats.pm @@ -54,15 +54,19 @@ our $PackageVersion = '0.01'; use Data::Dumper; use File::Basename; +use Cwd qw(realpath); use Config::Auto; use DBI; #####-------------------------------- Vars --------------------------------##### -# trim the path +# save $0 in $FullPath our $FullPath = $0; -our $HomePath = dirname($0); +# strip filename and /bin or /install directory to create the $HomePath +our $HomePath = dirname(realpath($0)); +$HomePath =~ s/\/(bin|install)//; +# trim $0 $0 =~ s%.*/%%; # set version string our $MyVersion = "$0 $::VERSION (NewsStats.pm $VERSION)"; @@ -99,6 +103,8 @@ sub ReadConfig { ### IN : $ConfFile: config filename ### OUT: reference to a hash containing the configuration my ($ConfFile) = @_; + # set default + $ConfFile = $HomePath . '/etc/newsstats.conf' if !$ConfFile; # mandatory configuration options my @Mandatory = ('DBDriver','DBHost','DBUser','DBPw','DBDatabase', 'DBTableRaw','DBTableGrps'); From dfc2b81c37c38c2b7bcc8afeead0ec927845b90f Mon Sep 17 00:00:00 2001 From: Thomas Hochstein Date: Tue, 3 Sep 2013 09:41:36 +0200 Subject: [PATCH 2/3] Fix some whitespace. Signed-off-by: Thomas Hochstein --- bin/feedlog.pl | 4 ++-- bin/gatherstats.pl | 6 +++--- bin/groupstats.pl | 6 +++--- install/install.pl | 6 +++--- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/bin/feedlog.pl b/bin/feedlog.pl index ef9429c..7c64cab 100755 --- a/bin/feedlog.pl +++ b/bin/feedlog.pl @@ -4,12 +4,12 @@ # # This script will log headers and other data to a database # for further analysis by parsing a feed from INN. -# +# # It is part of the NewsStats package. # # Copyright (c) 2010-2013 Thomas Hochstein # -# It can be redistributed and/or modified under the same terms under +# It can be redistributed and/or modified under the same terms under # which Perl itself is published. BEGIN { diff --git a/bin/gatherstats.pl b/bin/gatherstats.pl index f545456..92d0545 100755 --- a/bin/gatherstats.pl +++ b/bin/gatherstats.pl @@ -4,12 +4,12 @@ # # This script will gather statistical information from a database # containing headers and other information from a INN feed. -# +# # It is part of the NewsStats package. # # Copyright (c) 2010-2013 Thomas Hochstein # -# It can be redistributed and/or modified under the same terms under +# It can be redistributed and/or modified under the same terms under # which Perl itself is published. BEGIN { @@ -292,7 +292,7 @@ conjunction with B<--test> ... everything else seems a bit pointless. Set processing period to a single month in YYYY-MM format or to a time period between two month in YYYY-MM:YYYY-MM format (two month, separated -by a colon). +by a colon). =item B<-s>, B<--stats> I diff --git a/bin/groupstats.pl b/bin/groupstats.pl index f1e566b..fc13934 100755 --- a/bin/groupstats.pl +++ b/bin/groupstats.pl @@ -4,12 +4,12 @@ # # This script will get statistical data on newgroup usage # from a database. -# +# # It is part of the NewsStats package. # # Copyright (c) 2010-2013 Thomas Hochstein # -# It can be redistributed and/or modified under the same terms under +# It can be redistributed and/or modified under the same terms under # which Perl itself is published. BEGIN { @@ -347,7 +347,7 @@ Print out version and copyright information and exit. Print this man page and exit. -=item B<-m>, B<--month> I +=item B<-m>, B<--month> I Set processing period to a single month in YYYY-MM format or to a time period between two month in YYYY-MM:YYYY-MM format (two month, separated diff --git a/install/install.pl b/install/install.pl index a5860d4..1ef21fb 100755 --- a/install/install.pl +++ b/install/install.pl @@ -3,12 +3,12 @@ # install.pl # # This script will create database tables as necessary. -# +# # It is part of the NewsStats package. # # Copyright (c) 2010-2013 Thomas Hochstein # -# It can be redistributed and/or modified under the same terms under +# It can be redistributed and/or modified under the same terms under # which Perl itself is published. BEGIN { @@ -185,7 +185,7 @@ if (!$OptUpdate) { &CreateTable($Table); }; print "Database table generation done.\n"; - + # Display install instructions print $Install; } else { From 23ab67a099e32a9ee8c0700fe46bc43680a60721 Mon Sep 17 00:00:00 2001 From: Thomas Hochstein Date: Tue, 3 Sep 2013 09:58:17 +0200 Subject: [PATCH 3/3] Make configuration file configurable. Add --conffile option to all scripts to overrride standard config file location etc/newsstats.conf. Signed-off-by: Thomas Hochstein --- bin/feedlog.pl | 13 +++++++++---- bin/gatherstats.pl | 13 +++++++++---- bin/groupstats.pl | 13 +++++++++---- install/install.pl | 21 +++++++++++++-------- 4 files changed, 40 insertions(+), 20 deletions(-) diff --git a/bin/feedlog.pl b/bin/feedlog.pl index 7c64cab..bbfd814 100755 --- a/bin/feedlog.pl +++ b/bin/feedlog.pl @@ -69,14 +69,15 @@ sub PrepareDB { ################################# Main program ################################# ### read commandline options -my ($OptDebug,$OptQuiet); +my ($OptDebug,$OptQuiet,$OptConfFile); GetOptions ('d|debug!' => \$OptDebug, 'q|test!' => \$OptQuiet, + 'conffile=s' => \$OptConfFile, 'h|help' => \&ShowPOD, 'V|version' => \&ShowVersion) or exit 1; ### read configuration -my %Conf = %{ReadConfig('')}; +my %Conf = %{ReadConfig($OptConfFile)}; ### init syslog openlog($0, 'nofatal,pid', LOG_NEWS); @@ -130,7 +131,7 @@ while (<>) { }; }; $DBQuery->finish; - + warn sprintf("-----\nDay: %s\nDate: %s\nMID: %s\nTS: %s\nToken: %s\n". "Size: %s\nPeer: %s\nPath: %s\nNewsgroups: %s\nHeaders: %s\n", $Day, $Date, $Mid, $Timestamp, $Token, $Size, $Peer, $Path, @@ -152,7 +153,7 @@ feedlog - log data from an INN feed to a database =head1 SYNOPSIS -B [B<-Vhdq>] +B [B<-Vhdq>] [--conffile I] =head1 REQUIREMENTS @@ -198,6 +199,10 @@ find that information most probably in your B F file. Suppress logging to syslog. +=item B<--conffile> I + +Load configuration from I instead of F. + =back =head1 INSTALLATION diff --git a/bin/gatherstats.pl b/bin/gatherstats.pl index 92d0545..6096417 100755 --- a/bin/gatherstats.pl +++ b/bin/gatherstats.pl @@ -38,7 +38,7 @@ my %LegalStats; ### read commandline options my ($OptCheckgroupsFile,$OptClientsDB,$OptDebug,$OptGroupsDB,$OptTLH, - $OptHostsDB,$OptMonth,$OptRawDB,$OptStatsType,$OptTest); + $OptHostsDB,$OptMonth,$OptRawDB,$OptStatsType,$OptTest,$OptConfFile); GetOptions ('c|checkgroups=s' => \$OptCheckgroupsFile, 'clientsdb=s' => \$OptClientsDB, 'd|debug!' => \$OptDebug, @@ -49,11 +49,12 @@ GetOptions ('c|checkgroups=s' => \$OptCheckgroupsFile, 'rawdb=s' => \$OptRawDB, 's|stats=s' => \$OptStatsType, 't|test!' => \$OptTest, + 'conffile=s' => \$OptConfFile, 'h|help' => \&ShowPOD, 'V|version' => \&ShowVersion) or exit 1; ### read configuration -my %Conf = %{ReadConfig('')}; +my %Conf = %{ReadConfig($OptConfFile)}; ### override configuration via commandline options my %ConfOverride; @@ -161,7 +162,7 @@ foreach my $Month (&ListMonth($Period)) { } }; }; - + # delete old data for that month if (!$OptTest) { $DBQuery = $DBHandle->do(sprintf("DELETE FROM %s.%s WHERE month = ?", @@ -207,7 +208,7 @@ gatherstats - process statistical data from a raw source =head1 SYNOPSIS -B [B<-Vhdt>] [B<-m> I | I] [B<-s> I] [B<-c> I]] [B<--hierarchy> I] [B<--rawdb> I] [B<-groupsdb> I] [B<--clientsdb> I] [B<--hostsdb> I] +B [B<-Vhdt>] [B<-m> I | I] [B<-s> I] [B<-c> I]] [B<--hierarchy> I] [B<--rawdb> I] [B<-groupsdb> I] [B<--clientsdb> I] [B<--hostsdb> I] [--conffile I] =head1 REQUIREMENTS @@ -340,6 +341,10 @@ Override I from F. Override I from F. +=item B<--conffile> I + +Load configuration from I instead of F. + =back =head1 INSTALLATION diff --git a/bin/groupstats.pl b/bin/groupstats.pl index fc13934..7a3f7e4 100755 --- a/bin/groupstats.pl +++ b/bin/groupstats.pl @@ -32,7 +32,7 @@ Getopt::Long::config ('bundling'); ### read commandline options my ($OptBoundType,$OptCaptions,$OptCheckgroupsFile,$OptComments, $OptFileTemplate,$OptFormat,$OptGroupBy,$OptGroupsDB,$LowBound,$OptMonth, - $OptNewsgroups,$OptOrderBy,$OptReportType,$OptSums,$UppBound); + $OptNewsgroups,$OptOrderBy,$OptReportType,$OptSums,$UppBound,$OptConfFile); GetOptions ('b|boundary=s' => \$OptBoundType, 'c|captions!' => \$OptCaptions, 'checkgroups=s' => \$OptCheckgroupsFile, @@ -48,6 +48,7 @@ GetOptions ('b|boundary=s' => \$OptBoundType, 'r|report=s' => \$OptReportType, 's|sums!' => \$OptSums, 'u|upper=i' => \$UppBound, + 'conffile=s' => \$OptConfFile, 'h|help' => \&ShowPOD, 'V|version' => \&ShowVersion) or exit 1; # parse parameters @@ -82,7 +83,7 @@ if ($OptReportType) { my $ValidGroups = &ReadGroupList($OptCheckgroupsFile) if $OptCheckgroupsFile; ### read configuration -my %Conf = %{ReadConfig('')}; +my %Conf = %{ReadConfig($OptConfFile)}; ### override configuration via commandline options my %ConfOverride; @@ -245,7 +246,7 @@ if ($OptCaptions && $OptComments) { ($OptOrderBy and $OptOrderBy =~ /posting/i) ? 'by number of postings ' : '', ($OptOrderBy and $OptOrderBy =~ /-?desc$/i) ? 'descending' : 'ascending'); } - + # output data &OutputData($OptFormat,$OptComments,$GroupBy,$Precision, $OptCheckgroupsFile ? $ValidGroups : '', @@ -264,7 +265,7 @@ groupstats - create reports on newsgroup usage =head1 SYNOPSIS -B [B<-Vhcs> B<--comments>] [B<-m> I[:I] | I] [B<-n> I] [B<--checkgroups> I] [B<-r> I] [B<-l> I] [B<-u> I] [B<-b> I] [B<-g> I] [B<-o> I] [B<-f> I] [B<--filetemplate> I] [B<--groupsdb> I] +B [B<-Vhcs> B<--comments>] [B<-m> I[:I] | I] [B<-n> I] [B<--checkgroups> I] [B<-r> I] [B<-l> I] [B<-u> I] [B<-b> I] [B<-g> I] [B<-o> I] [B<-f> I] [B<--filetemplate> I] [B<--groupsdb> I] [--conffile I] =head1 REQUIREMENTS @@ -593,6 +594,10 @@ B<--nocomments> is enforced, see above. Override I from F. +=item B<--conffile> I + +Load configuration from I instead of F. + =back =head1 INSTALLATION diff --git a/install/install.pl b/install/install.pl index 1ef21fb..12cc8ec 100755 --- a/install/install.pl +++ b/install/install.pl @@ -31,8 +31,9 @@ Getopt::Long::config ('bundling'); ################################# Main program ################################# ### read commandline options -my ($OptUpdate); +my ($OptUpdate,$OptConfFile); GetOptions ('u|update=s' => \$OptUpdate, + 'conffile=s' => \$OptConfFile, 'h|help' => \&ShowPOD, 'V|version' => \&ShowVersion) or exit 1; @@ -42,7 +43,7 @@ my $Path = cwd(); ### read configuration print("Reading configuration.\n"); -my %Conf = %{ReadConfig('')}; +my %Conf = %{ReadConfig($OptConfFile)}; ##### -------------------------------------------------------------------------- ##### Database table definitions @@ -53,9 +54,9 @@ CREATE DATABASE IF NOT EXISTS `$Conf{'DBDatabase'}` DEFAULT CHARSET=utf8; SQLDB my %DBCreate = ('DBTableRaw' => < <prepare($DBCreate); $DBQuery->execute() or &Bleat(2, sprintf("Can't create database %s: %s%\n", $Conf{'DBDatabase'}, $DBI::errstr)); - + printf("Database table %s created succesfully.\n",$Conf{'DBDatabase'}); $DBHandle->disconnect; }; @@ -255,7 +256,7 @@ install - installation script =head1 SYNOPSIS -B [B<-Vh> [--update I] +B [B<-Vh> [--update I] [--conffile I] =head1 REQUIREMENTS @@ -288,6 +289,10 @@ Print this man page and exit. Don't do a fresh install, but update from I. +=item B<--conffile> I + +Load configuration from I instead of F. + =back =head1 FILES