Annotation of loncom/debugging_tools/rebuild_db_from_hist.pl, revision 1.2
1.1 matthew 1: #!/usr/bin/perl -w
2: #
3: # The LearningOnline Network
4: #
5: # rebuild_db_from_hist.pl Rebuild a *.db file from a *.hist file
6: #
1.2 ! matthew 7: # $Id: rebuild_db_from_hist.pl,v 1.1 2004/12/08 22:06:48 matthew Exp $
1.1 matthew 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:
36: #
37: # Options
38: my ($help,$debug,$test);
39: GetOptions("help" => \$help,
40: "debug" => \$debug,
41: "test" => \$test);
42:
43: if (! defined($debug)) { $debug = 0; }
44: if (! defined($test)) { $test = 0; }
45:
46: #
47: # Help them out if they ask for it
48: if ($help) {
49: print <<'END';
50: rebuild_db_from_hist.pl - recreate a db file from a hist file.
51: Options:
52: -help Display this help.
1.2 ! matthew 53: -debug Output debugging code (not much is output yet)
! 54: -test Verify the given *.hist file will reconstruct the current db file
! 55: Sends error messages to STDERR.
1.1 matthew 56: Examples:
1.2 ! matthew 57: rebuild_db_from_hist.pl -t $file.hist # Perform a test rebuild
! 58: rebuild_db_from_hist.pl $file.hist
1.1 matthew 59: END
60: exit;
61: }
62:
63: #
64: # Loop through ARGV getting files.
65: while (my $fname = shift) {
66: my $db_filename = $fname;
67: $db_filename =~ s/\.hist$/\.db/;
68: if (-e $db_filename && ! $test) {
69: print STDERR "Aborting: The target file $db_filename exists.".$/;
70: next;
71: }
72: my ($error,$constructed_hash) = &process_file($fname,$db_filename);
73: if (! defined($error) || ! $test) {
74: $error = &write_hash($db_filename,$constructed_hash);
75: }
76: if ($test) {
77: my $error = &test_hash($db_filename,$constructed_hash);
78: if (defined($error)) {
79: print "Error processing ".$fname.$/;
80: print STDERR $error;
81: } else {
82: print "Everything looks good for ".$fname.$/;
83: }
84: }
85: if (defined($error)) {
86: print $error.$/;
87: }
88: }
89:
90: exit;
91:
92: ######################################################
93: ######################################################
94: sub process_file {
95: my ($fname,$db_filename,$debug) = @_;
96: #
97: open(HISTFILE,$fname);
98: my %db_to_store;
99: my $no_action_count = 0;
100: while (my $command = <HISTFILE>) {
101: chomp($command);
102: my $error = undef;
103: # Each line can begin with:
104: # P:put
105: # D:delete
106: my ($action,$time,$concatenated_data) = split(':',$command,3);
107: my @data = split('&',$concatenated_data);
108: foreach my $k_v_pair (@data) {
109: my ($key,$value) = split('=',$k_v_pair,2);
110: if (defined($action) && $action eq 'P') {
111: if (defined($value)) {
112: $db_to_store{$key}=$value;
113: } else {
114: $no_action_count++;
115: }
116: } elsif ($action eq 'D') {
117: delete($db_to_store{$key});
118: } else {
119: $error = "Unable to understand action '".$action."'";
120: }
121: }
122: if (defined($error)) {
123: return ('Error:'.$error.$/,undef);
124: }
125: }
126: if ($no_action_count) {
127: print $no_action_count.' lines did not require action.'.$/;
128: }
129: close(HISTFILE);
130: return (undef,\%db_to_store);
131: }
132:
133: sub write_hash {
134: my ($db_filename,$db_to_store) = @_;
135: #
136: # Write the gdbm file
137: my %db;
138: if (! tie(%db,'GDBM_File',$db_filename,&GDBM_WRCREAT(),0640)) {
139: warn "Unable to tie to $db_filename";
140: return "Unable to tie to $db_filename";
141: }
142: #
143: while (my ($k,$v) = each(%$db_to_store)) {
144: $db{$k}=$v;
145: }
146: #
147: untie(%db);
148: return undef;
149: }
150:
151: sub test_hash {
152: my ($db_filename,$my_db) = @_;
153: #
154: my %db;
155: if (! tie(%db,'GDBM_File',$db_filename,&GDBM_READER(),0640)) {
156: return "Unable to tie to $db_filename";;
157: }
158: my (%key_errors,%value_errors);
159: while (my ($k,$v) = each(%db)) {
160: if (exists($my_db->{$k})) {
161: if ($my_db->{$k} eq $v) {
162: delete($my_db->{$k});
163: } else {
164: $value_errors{$k}=$v;
165: }
166: } else {
167: $key_errors{$k}=$v;
168: }
169: }
170: untie(%db);
171: #
172: my $error;
173: my $extra_count = scalar(keys(%$my_db));
174: if ($extra_count) {
175: $error.=$extra_count.' extra key/value pairs found: '.$/;
176: while (my ($k,$v) = each(%$my_db)) {
177: $error .= ' "'.$k.'" => "'.$v.'"'.$/;
178: }
179: }
180: my $key_count = scalar(keys(%key_errors));
181: if ($key_count) {
182: $error.=$key_count.' missing keys found: '.$/;
183: while (my ($k,$v) = each(%key_errors)) {
184: $error .= ' "'.$k.'" => "'.$v.'"'.$/;
185: }
186: }
187: my $value_count = scalar(keys(%value_errors));
188: if ($value_count) {
189: $error.=$value_count.' missing values found: '.$/;
190: while (my ($k,$v) = each(%value_errors)) {
191: $error .= ' "'.$k.'" => "'.$v.'"'.$/;
192: }
193: }
194: #
195: return $error;
196: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>