From: Jeremy Laine Date: Fri, 6 Dec 2002 20:34:27 +0000 (+0000) Subject: lecture d'un fichier de conf ~/.deptrack.conf X-Git-Tag: start~26 X-Git-Url: http://git.polytechnique.org/?a=commitdiff_plain;h=23862e309b69f1717b9246493c6970478b6ea7b4;p=old-projects.git lecture d'un fichier de conf ~/.deptrack.conf --- diff --git a/deptrack/deptrack.pl b/deptrack/deptrack.pl index e9640ae..8e4ee35 100755 --- a/deptrack/deptrack.pl +++ b/deptrack/deptrack.pl @@ -3,30 +3,36 @@ # $Id$ use strict; -use DBI(); +use Config::General; +use DBI; +use Getopt::Std; -my $dsn = "DBI:mysql:database=deptrack;host=localhost"; -my $dbh = DBI->connect($dsn,"deptrack","phptrax"); +############################################################################### +# # +# Global variables # +# # +############################################################################### -my @incpath = split /:/, "./:../"; +my $rootdir; -my $mask_inc = "\\.inc(\\.php)?\$"; -my $mask_php = "(?connect($dsn,$db_user,$db_pwd); +} + # clear a database table sub cleartable { my($table) = @_; @@ -330,17 +341,109 @@ sub parsefile { } } -## do the work -my $nargs = @ARGV; -$nargs || &syntax(); -$rootdir =~ s/\/$//; +############################################################################### +# # +# init and conf file # +# # +############################################################################### + +sub init { + my $path = `pwd`; + $confile="$ENV{'HOME'}/.deptrack.conf"; + + if ( not getopts('dhf:', \%opts) or $opts{'h'}) { + &syntax(); + } + + # check for root directory + my $nargs = @ARGV; + $nargs || &syntax(); + $rootdir = $ARGV[0]; + $rootdir =~ s/\/$//; + + if (!-d $rootdir) { + print "root directory $rootdir not found!\n"; + die; + } else { + print "Using $rootdir as web root directory\n"; + } + + # process options + if ($opts{'d'}){ + print STDERR "Running in debug mode ...\n"; + $debug = 1; + }else{ + $debug = 0; + } + + if ($opts{'f'}){ + if (-r "$opts{'f'}") { + print STDERR "Using $opts{'f'} as config file\n"; + &read_conf("$opts{'f'}"); + } else { + print STDERR "$opts{'f'} : not a valid config file\n"; + exit(1); + } + } else { + print STDERR "Using $confile as config file\n"; + if (-r "$confile") { + &read_conf("$confile"); + } else { + print STDERR "No valid configuration file found, aborting\n"; + exit(1); + } + } + &dbconnect(); +} + + +#Read Configuration and init global vars +sub read_val { + my($default,$value) = @_; + if ($value) { return $value; } + else { return $default; } +} + +sub read_conf { + my $file = shift; + $config = new Config::General( -file =>"$file", + -AllowMultiOptions =>"yes", + -LowerCaseNames =>"yes",); + %conf = $config->getall; + + + $db_db = &read_val("deptrack", $conf{'db_db'}); + $db_host = &read_val("localhost", $conf{'db_host'}); + $db_pwd = &read_val("", $conf{'db_pwd'}); + $db_user = &read_val("deptrack", $conf{'db_user'}); + @incpath = split /:/, &read_val("./", $conf{'include_path'}); + $mask_img = &read_val("\\.(png|gif|jpg)\$", $conf{'mask_img'}); + $mask_inc = &read_val("\\.inc(\\.php)?\$", $conf{'mask_inc'}); + $mask_php = &read_val("(? - use as config file\n", + " -d - debug mode\n"; + exit(1); } + +############################################################################### +# # +# main # +# # +############################################################################### + +&init(); &cleartable("dir"); &cleartable("file"); &cleartable("dep");