File:  [LON-CAPA] / loncom / debugging_tools / dump_db.pl
Revision 1.5: download - view: text, annotated - select for diffs
Fri Mar 18 21:36:49 2005 UTC (19 years, 2 months ago) by albertel
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, HEAD
- don't wann pas arugments to these functions

    1: #!/usr/bin/perl -w
    2: #
    3: # The LearningOnline Network
    4: #
    5: # dump_db.pl - dump a GDBM database to standard output, unescaping if asked to.
    6: #
    7: # $Id: dump_db.pl,v 1.5 2005/03/18 21:36:49 albertel Exp $
    8: #
    9: # Copyright Michigan State University Board of Trustees
   10: #
   11: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   12: #
   13: # LON-CAPA is free software; you can redistribute it and/or modify
   14: # it under the terms of the GNU General Public License as published by
   15: # the Free Software Foundation; either version 2 of the License, or
   16: # (at your option) any later version.
   17: #
   18: # LON-CAPA is distributed in the hope that it will be useful,
   19: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   20: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   21: # GNU General Public License for more details.
   22: #
   23: # You should have received a copy of the GNU General Public License
   24: # along with LON-CAPA; if not, write to the Free Software
   25: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   26: #
   27: # /home/httpd/html/adm/gpl.txt
   28: #
   29: # http://www.lon-capa.org/
   30: #
   31: #################################################
   32: use strict;
   33: use Getopt::Long;
   34: use GDBM_File;
   35: use Data::Dumper;
   36: use Storable qw(thaw);
   37: 
   38: #
   39: # Options
   40: my ($unesc,$help,$localize_times) = (0,0,0);
   41: GetOptions("unescape" => \$unesc,
   42:            "u"        => \$unesc,
   43:            "t"        => \$localize_times,
   44:            "help"     => \$help);
   45: 
   46: #
   47: # Help them out if they ask for it
   48: if ($help) {
   49:     print <<END;
   50: dump_db.pl - dump GDBM_File databases to stdout.  
   51: Specify the database filenames on the command line.
   52: Specify --unescape to have all the keys and values unescaped from every
   53: database.
   54: Options:
   55:    --help     Display this help.
   56:    --unescape Unescape the keys and values before printing them out.
   57:    -u        Same as --unescape
   58:    -t        Localize times when possible (human readable times)
   59: Examples: 
   60:     dump_db.pl mydata.db
   61:     dump_db.pl mydata.db yourdata.db ourdata.db theirdata.db
   62:     dump_db.pl --unescape \*db
   63: END
   64:     exit;
   65: }
   66: 
   67: #
   68: # Loop through ARGV getting files.
   69: while (my $fname = shift) {
   70:     my %db;
   71:     if (! tie(%db,'GDBM_File',$fname,&GDBM_READER(),0640)) {
   72:         warn "Unable to tie to $fname";
   73:         next;
   74:     }
   75:     while (my ($key,$value) = each(%db)) {
   76:         if ($value =~ s/^__FROZEN__//) {
   77:             $value = thaw(&unescape($value));
   78:         }
   79:         if ($unesc) {
   80:             $key = &unescape($key);
   81:             $value = &unescape($value) if (! ref($value));
   82:         }
   83:         if ($localize_times && ! ref($value)) {
   84:             $value =~ s/([0-9]{10,10})/localtime($1)/ge;
   85:         }
   86:         print "$key = ".(ref($value)?Dumper($value):$value)."\n";
   87:     }
   88:     untie %db;
   89: }
   90: exit;
   91: 
   92: ######################################
   93: sub unescape {
   94:     my $str=shift;
   95:     $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
   96:     return $str;
   97: }

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