File:  [LON-CAPA] / loncom / debugging_tools / seed_accesscount.pl
Revision 1.4: download - view: text, annotated - select for diffs
Tue Jan 13 18:13:34 2004 UTC (20 years, 4 months ago) by matthew
Branches: MAIN
CVS tags: version_2_1_X, version_2_1_3, version_2_1_2, version_2_1_1, version_2_1_0, version_2_0_X, version_2_0_99_1, version_2_0_2, version_2_0_1, version_2_0_0, version_1_99_3, version_1_99_2, version_1_99_1_tmcc, version_1_99_1, version_1_99_0_tmcc, version_1_99_0, version_1_3_X, version_1_3_3, version_1_3_2, version_1_3_1, version_1_3_0, version_1_2_X, version_1_2_99_1, version_1_2_99_0, version_1_2_1, version_1_2_0, version_1_1_X, version_1_1_99_5, version_1_1_99_4, version_1_1_99_3, version_1_1_99_2, version_1_1_99_1, version_1_1_99_0, version_1_1_3, version_1_1_2, HEAD
Ensure www is owner of files when we are done with them.

    1: #!/usr/bin/perl -w
    2: #
    3: # The LearningOnline Network
    4: #
    5: # $Id: seed_accesscount.pl,v 1.4 2004/01/13 18:13:34 matthew Exp $
    6: #
    7: # Copyright Michigan State University Board of Trustees
    8: #
    9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   10: #
   11: # LON-CAPA is free software; you can redistribute it and/or modify
   12: # it under the terms of the GNU General Public License as published by
   13: # the Free Software Foundation; either version 2 of the License, or
   14: # (at your option) any later version.
   15: #
   16: # LON-CAPA is distributed in the hope that it will be useful,
   17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   19: # GNU General Public License for more details.
   20: #
   21: # You should have received a copy of the GNU General Public License
   22: # along with LON-CAPA; if not, write to the Free Software
   23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   24: #
   25: # /home/httpd/html/adm/gpl.txt
   26: #
   27: # http://www.lon-capa.org/
   28: #
   29: #################################################
   30: use strict;
   31: use Getopt::Long;
   32: use GDBM_File;
   33: 
   34: #
   35: # Options
   36: my ($verbose,$help) = (0);
   37: GetOptions("v"    => \$verbose,
   38:            "help" => \$help);
   39: 
   40: #
   41: # Help them out if they ask for it
   42: if ($help) {
   43:     print <<END;
   44: seed_accesscount.pl 
   45: END
   46:     exit;
   47: }
   48: 
   49: #
   50: # Loop through ARGV getting files.
   51: $|=1;
   52: while (my $resDBname = shift()) {
   53:     my ($path) = ($resDBname =~ /^(.*)nohist_resevaldata.db$/);
   54:     print STDERR $path.$/;
   55:     my %resevalDB;
   56:     if (! tie(%resevalDB,'GDBM_File',$resDBname,&GDBM_READER,0640)) {
   57:         warn "Unable to tie to $resDBname";
   58:         next;
   59:     }
   60:     #
   61:     my $accessDBname = $path.'nohist_accesscount.db';
   62:     my %accessDB;
   63:     if (! tie(%accessDB,'GDBM_File',$accessDBname,&GDBM_WRCREAT,0640)) {
   64:         warn "Unable to tie to $accessDBname";
   65:         next;
   66:     }
   67:     #
   68:     my @Keys;
   69:     my ($basekey,$value);
   70:     #
   71:     $! = 0;
   72:     while (eval('($basekey,$value) = each(%resevalDB);')) {
   73:         if ($!) {
   74:             print STDERR $1.$/;
   75:             $!=0;
   76:         }
   77:         my $key = &unescape($basekey);
   78:         my $src;
   79:         next if (! ((undef,$src) = ($key =~ /^(.*)___(.*)___count/)));
   80:         my $value = &unescape($value);
   81:         $src = &escape($src);
   82:         if (exists($accessDB{$src})) {
   83:             $accessDB{$src}+=$value;
   84:         } else {
   85:             $accessDB{$src}=$value;
   86:         }
   87:         push (@Keys,$basekey);
   88:     }
   89:     #
   90:     untie %accessDB;    
   91:     untie %resevalDB;
   92:     system("chown www:www $accessDBname");
   93:     # remove the keys we saved.
   94:     next if (! scalar(@Keys)); # skip it if we did not get anything...
   95:     my $dbptr;
   96:     if (! ($dbptr = tie(%resevalDB,'GDBM_File',$resDBname,&GDBM_WRITER,0640))){
   97:         die "Unable to re-tie to $resDBname.  No deletes occured.";
   98:     }
   99:     foreach my $basekey (@Keys) {
  100:         delete($resevalDB{$basekey});
  101:     }
  102:     # Squish the file down
  103:     $dbptr->reorganize();
  104:     $dbptr = undef;
  105:     untie(%resevalDB);
  106:     system("chown www:www $resDBname");
  107: }
  108: exit;
  109: 
  110: ######################################
  111: sub escape {
  112:     my $str=shift;
  113:     $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
  114:     return $str;
  115: }
  116: 
  117: sub unescape {
  118:     my $str=shift;
  119:     $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  120:     return $str;
  121: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>