lecture d'un fichier de conf ~/.deptrack.conf
authorJeremy Laine <jeremy.laine@m4x.org>
Fri, 6 Dec 2002 20:34:27 +0000 (20:34 +0000)
committerJeremy Laine <jeremy.laine@m4x.org>
Fri, 6 Dec 2002 20:34:27 +0000 (20:34 +0000)
deptrack/deptrack.pl

index e9640ae..8e4ee35 100755 (executable)
@@ -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 = "(?<!\\.inc)\\.php\$";
-my $mask_img = "\\.(png|gif|jpg)\$"; 
+my %conf;
+my $config;
+my $confile;
+my $debug;
+my %opts;
+my @incpath;
 
-my $rootdir = $ARGV[0];
+# file masks
+my($mask_inc,$mask_php,$mask_img); 
 
-my $debug = 0;
+# db connection
+my($db_db,$db_host,$db_user,$db_pwd);
+my($dsn,$dbh);
 
-# return program syntax 
-sub syntax {
-  print "Syntax:\n\tdeptrack root_directory\n\n";
-  exit 0;
-}
 
 sub dprint {
-  my($text) = @_;
-  print($text) if ($debug);
+  my $text = shift;
+  print STDERR $text if ($debug);
 }
 
 ###############################################################################
@@ -109,6 +115,11 @@ sub commentstrip {
 #                                                                             #
 ###############################################################################
 
+sub dbconnect {
+  $dsn = "DBI:mysql:database=$db_db;host=$db_host";
+  $dbh = DBI->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("(?<!\\.inc)\\.php\$", $conf{'mask_php'});
+}
+
 
-if (!-d $rootdir) {
- print "root directory $rootdir not found!\n";
- die;
+# return program syntax 
+sub syntax {
+  print "\n[ This is deptrack, a PHP depency tracker ]\n\n",
+        "Syntax:\n",
+        "  deptrack.pl [options] root_directory\n\n",
+       " Options:\n",
+        "  -h          - this help message\n",
+        "  -f <file>   - use <file> as config file\n",
+        "  -d          - debug mode\n";
+  exit(1);
 }
 
+
+###############################################################################
+#                                                                             #
+#                                       main                                  #
+#                                                                             #
+###############################################################################
+
+&init();
 &cleartable("dir");
 &cleartable("file");
 &cleartable("dep");