[LON-CAPA-cvs] cvs: modules /raeburn builddb_fromtext.pl

raeburn raeburn@source.lon-capa.org
Tue, 27 Jan 2009 06:12:45 -0000


raeburn		Tue Jan 27 06:12:45 2009 EDT

  Added files:                 
    /modules/raeburn	builddb_fromtext.pl 
  Log:
  - Builds GDBM .db files from text files (previously generated by dbdump_totext.pl.
    - given a file containing full paths to *.db.txt files will convert to .db files.
  - Useful for migration between architectures which use different byte order.
  
  

Index: modules/raeburn/builddb_fromtext.pl
+++ modules/raeburn/builddb_fromtext.pl
#!/usr/bin/perl
#
# The LearningOnline Network
#
# builddb_fromtext.pl Rebuild a *.db file from a prior dump of a .db file to a
#                     text file. Allows db files to be transitioned between
#                     architectures which use different byte order
#
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#
#################################################
use strict;
use lib '/home/httpd/lib/perl';
use Getopt::Long;
use GDBM_File;
use LONCAPA;
use Apache::lonnet;

#
# Options
my ($help,$overwrite) = (0,0);
GetOptions("help"   => \$help,
           "o"      => \$overwrite);

# Help them out if they ask for it
if ($help) {
    print <<END;
builddb_to_text.pl - build GDBM_File databases from text files
- Specify location of a data file containing fullpaths to text files 
  (extensions .db.txt) which are to be converted to GDBM files.
- Will append to a log file "builddbfromtext.log" in the directory where
  this script is run.

Options:
   --help  Display this help.
   -o      Overwrite any existing .db files (instead of renaming to *.db.save) 
 
Examples:
   .\builddb_to_text.pl db_to_text.dat
END
    exit;
}

#  Make sure this process is running from user=www
my $wwwid=getpwnam('www');
if ($wwwid!=$<) {
    print "User ID mismatch. builddb_to_text.pl must be run as user www\n";
    exit;
}

if (@ARGV) {
    if (-e $ARGV[0]) {
        if (open(my $fh,"<$ARGV[0]")) {
            my $log;
            if (!open($log,">>builddbfromtext.log")) {
                print "Could not open logfile: builddbfromtext.log for appending\n";
                exit;
            }
            print $log "\n".localtime(time)." Starting creation of db files listed in $ARGV[0]\n";
            while(<$fh>) {
                my $file = $_;
                chomp($file);
                if (-e $file) {
                    if ($file =~ /^(.+\.db)\.txt$/) {
                        my $dbfile = $1;
                        if (-e $dbfile && !$overwrite) {
                            if (!(rename ($dbfile,"$dbfile.save"))) {
                                print "Existing file: $dbfile. Renaming to $dbfile.save failed .. skipping conversion of $file\n";
                                print $log "Conversion of $dbfile.txt skipped - could not rename existing $dbfile to $dbfile.save\n";
                                next;
                            } else {
                                print $log "Existing file:$dbfile renamed $dbfile.save\n";
                            }
                        }
                        if (open(my $fhin,"<$file")) {  
                            my %db_to_store;
                            my $count = 0;
                            while(<$fhin>) {
                                chomp();
                                my ($key,$val) = split(/\s+=>\s+/,$_,2);
                                $db_to_store{$key} = $val;
                                $count++;
                            }
                            close($fhin);
                            &write_hash($dbfile,\%db_to_store);
                            print $log "Built $dbfile from text - $count key=>value pairs\n"; 
                        }
                    }
                } 
            }
            print $log localtime(time)." Ending creation of db files\n";
            close($log);
        } else {
            print "No file contain paths to text files from which to build db files\n";
        }
    } else {
        print "Usage: ./builddb_from_text.pl <file_of_paths_to_text_files>\n";
    }
} else {
    print "Usage: ./builddb_from_text.pl <file_of_paths_to_text_files>\n";
}

sub write_hash {
    my ($db_filename,$db_to_store) = @_;
    #
    # Write the gdbm file
    my %db;
    if (! tie(%db,'GDBM_File',$db_filename,&GDBM_WRCREAT(),0640)) {
        warn "Unable to tie to $db_filename";
        return "Unable to tie to $db_filename";
    }
    #
    while (my ($k,$v) = each(%$db_to_store)) {
        $db{$k}=$v;
    }
    #
    untie(%db);
    return undef;
}