gestion des lignes commentees
authorJeremy Laine <jeremy.laine@m4x.org>
Fri, 22 Nov 2002 22:59:42 +0000 (22:59 +0000)
committerJeremy Laine <jeremy.laine@m4x.org>
Fri, 22 Nov 2002 22:59:42 +0000 (22:59 +0000)
insertion des noms de fichiers dans la base de donnees

deptrack/deptrack.pl

index 15b1183..f54491d 100755 (executable)
@@ -2,6 +2,8 @@
 #
 # $Id$
 
+use DBI;
+
 # return program syntax 
 sub syntax {
   print "Syntax:\n\tdeptrack directory\n";
@@ -13,7 +15,7 @@ sub tagcrunch {
   my($tag1,$tag2,$intag,@lines) = @_;
   my $line;
   my $pos;
-  my $out;
+  my @out;
   my $add;
    
   while ($line = shift(@lines)) {
@@ -29,7 +31,7 @@ sub tagcrunch {
       }
       $add =~ s/^\s*//;
       $add =~ s/\s*$//;
-      if (length($add)>0) { $out .= "$add\n"; }
+      if (length($add)>0) { push @out, "$add\n"; }
     }
   
     if (!$intag) {
@@ -46,11 +48,11 @@ sub tagcrunch {
        }
         $add =~ s/^\s*//;
        $add =~ s/\s*$//;
-        if (length($add)>0) { $out .= "$add\n"; }
+        if (length($add)>0) { push @out, "$add\n"; }
       }
     } 
   }
-  return $out;
+  return @out;
 }
 
 # return only blocks of @lines between tag1 and tag2
@@ -65,38 +67,103 @@ sub tagstrip {
   return tagcrunch($tag2,$tag1,1,@lines);
 }
 
+# for each line, remove whatever is after the comment marker
+sub commentstrip {
+  my($tag,@lines) = @_;
+  my @out;
+  
+  foreach my $line (@lines) {
+    $line =~ s/$tag.*//;
+    $line =~ s/\s*$//;
+    if (length($line)>0) { push @out, "$line\n"; }
+  }
+  return @out;
+}
+
 # parse a directory
 sub parsedir {
+  my ($dir,$vdir) = @_;
   my $dirhandle;
-  my $dir = $_[0];
-  my $file;
-  
-  opendir($dirhandle,$dir);
-  my @files=grep /.*.(php|inc)/,readdir($dirhandle);
+
+  # build virtual path
+  if (length($vdir)>0) {
+    $vdir="$vdir/"
+  }
+
+  opendir($dirhandle, $dir);
+  my @list = grep /^[^\.]/, readdir($dirhandle);
   closedir($dirhandle);
-  foreach $file (@files) {
-    parsefile($file);
+  
+  my @phpfiles=grep /.*.(php|inc)/,@list;
+  foreach my $file (@phpfiles) {
+    parsefile("$dir/$file","$vdir$file");
+  }
+
+  foreach my $entry (@list) {
+    if (-d $entry) {
+      if ($entry !~ /^CVS$/) {
+        &parsedir("$dir/$entry", "$vdir$entry");
+      }
+    }
   }
 }
 
 # parse a file
 sub parsefile {
+  my ($file,$vfile) = @_;
   my $fhandle;
-  my $file = $_[0];
-  
   open($fhandle,$file);
   @lines = <$fhandle>;
   close($fhandle);
+
   @lines = tagstrip("<!--","-->",@lines);
   @lines = tagextract("<?php","?>",@lines);
   @lines = tagstrip("/*","*/",@lines);
+  @lines = commentstrip("\/\/",@lines);
+  @lines = grep /(require|include)\s*\(/, @lines;
   print @lines;
 }
 
-my $dir;
+# create DB entries for a directory
+sub listdir {
+  my ($dir,$vdir) = @_;
+  my $dirhandle;
+  
+  # build virtual path
+  if (length($vdir)>0) {
+    $vdir="$vdir/"
+  }
+
+  opendir($dirhandle, $dir);
+  my @list = grep /^[^\.]/, readdir($dirhandle);
+  closedir($dirhandle);
+  
+  my @phpfiles=grep /.*.(php|inc)/,@list;
+  foreach my $file (@phpfiles) {
+    my $sth = $dbh->prepare("insert into file set path='$vdir$file'");
+    $sth->execute;
+  }
+  
+  foreach my $entry (@list) {
+    if (-d $entry) {
+      if ($entry !~ /^CVS$/) {
+        &listdir("$dir/$entry", "$vdir$entry");
+      }
+    }
+  }
+}
+
+## do the work
 my $nargs = @ARGV;
 $nargs || syntax();
 
-foreach $dir (@ARGV) {
-  parsedir($dir);
-}
+$dsn = "DBI:mysql:database=deptrack;host=localhost";
+$dbh = DBI->connect($dsn,"deptrack","phptrax");
+
+my $topdir = $ARGV[0];
+$topdir =~ s/\/$//;
+
+listdir($topdir,"");
+parsedir($topdir,"");
+#parsefile("p.php","");