#!/usr/bin/env perl #################################################################### # Copyright @ 1995-1999 University of Rochester # All Rights Reserved #################################################################### BEGIN { my $useLibDir = '.'; if ($0 =~ m|^(/.*)/|) { $useLibDir = $1; } elsif ($0 =~ m|^(\..*)/|) { $useLibDir = $1; } elsif ($0 =~ m|^(.+)/|) { $useLibDir = "./$1"; } unshift @INC, $useLibDir; } use webworkInit; # WeBWorKInitLine use Global; use Auth; use strict; if (@ARGV != 2) { print "\nSyntax is import_permission-database.pl courseID textDatabaseFile\n"; print " (e.g. import_permission-database.pl demoCourse textDataBase)\n\n"; exit(0); } my $course = $ARGV[0]; my $textDatabaseFile = $ARGV[1]; # establish environment for this script &Global::getCourseEnvironment($course); # Directory paths my $databaseDirectory = getCourseDatabaseDirectory(); my $courseScriptsDirectory = getWebworkScriptDirectory(); my $perm_file = &Global::getCoursePermissionsFile($course); # File names require "${courseScriptsDirectory}$Global::DBglue_pl"; require "${courseScriptsDirectory}$Global::HTMLglue_pl"; require "${courseScriptsDirectory}$Global::FILE_pl"; # get all psvn's for set my ($line); my @permission_array = (); open (INFILE, "$textDatabaseFile") or wwerror("$0","can't open $textDatabaseFile for reading"); while (defined ($line = )) { chomp($line); unless ($line =~ /\S/) {next;} ## skip blank lines if ($line =~ /^[^\[]\s*(.*)/) { ## non bracketed line push @permission_array, $1;} else { ## bracketed line $line =~ /^\[(.*)\]$/; push @permission_array, $1; } } close (INFILE); ## backup existing db my $message = ''; if ( -e "$perm_file" ) { # $message .= "Backing up current webwork data base: $perm_file\n Two backups are kept.\n\n"; print "Backing up current webwork data base: $perm_file\n Two backups are kept.\n\n"; &backup($perm_file); } ## Create new database #$message .= "Creating new data base $perm_file .\n"; print "Creating new data base $perm_file .\n"; create_db($perm_file, $Global::webwork_database_permission); if ( -e $perm_file ) { chmod($Global::webwork_database_permission,$perm_file) || wwerror($0, "Can't do chmod($Global::webwork_database_permission,$perm_file)"); chown(-1,$Global::numericalGroupID,$perm_file) || wwerror($0,"Can't do chown(-1,$Global::numericalGroupID,$perm_file)"); # $message .= "New data base created\n"; print "New (blank) data base created\n"; } else { # $message .= "New data base could not be created\n"; print "New data base could not be created\n"; } # load up new database my $wwDbObj; # Object for referencing the database my %hash; &Global::tie_hash('PER_FH', \$wwDbObj, \%hash, $perm_file, 'W', $Global::standard_tie_permission); %hash = @permission_array; &Global::untie_hash('PER_FH', \$wwDbObj, \%hash, $perm_file); # $message .= "The new data base $perm_file could not be created\n"; print "The new data base $perm_file has been filled with data\n"; sub backup { ## takes as a parameter the full path name ## makes upto two backups of the file with _bak1, or _bak2 ## appended to filename where _bak1 is the most recent backup my $fileName =$_[0]; if (-e "${fileName}_bak1") { rename("${fileName}_bak1","${fileName}_bak2") or &wwerror("$0","can't rename ${fileName}_bak1"); } if (-e "${fileName}") { rename("${fileName}","${fileName}_bak1") or &wwerror("$0","can't rename ${fileName}"); } }