[LON-CAPA-cvs] cvs: loncom /debugging_tools move_construction_spaces.pl

raeburn raeburn at source.lon-capa.org
Sun Oct 30 12:01:20 EDT 2011


raeburn		Sun Oct 30 16:01:20 2011 EDT

  Modified files:              
    /loncom/debugging_tools	move_construction_spaces.pl 
  Log:
  - Add fourth way to identifier domain for <user> with directory:
    /home/<user>/pubic_html
    - check for username = <user> in allusers table in loncapa mysql database.
  - Consistent order for entering action to take for users in multiple domains or
    without identified domain:
    1 - skip
    2 - exit
    >= 3 - choose domain.
  - Show which users were skipped at end of completed run. 
  
  
-------------- next part --------------
Index: loncom/debugging_tools/move_construction_spaces.pl
diff -u loncom/debugging_tools/move_construction_spaces.pl:1.5 loncom/debugging_tools/move_construction_spaces.pl:1.6
--- loncom/debugging_tools/move_construction_spaces.pl:1.5	Thu Oct 27 14:01:21 2011
+++ loncom/debugging_tools/move_construction_spaces.pl	Sun Oct 30 16:01:20 2011
@@ -5,7 +5,7 @@
 # Move Construction Spaces from /home/$user/public_html
 # to /home/httpd/html/priv/$domain/$user and vice versa
 #
-# $Id: move_construction_spaces.pl,v 1.5 2011/10/27 14:01:21 raeburn Exp $
+# $Id: move_construction_spaces.pl,v 1.6 2011/10/30 16:01:20 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -38,8 +38,9 @@
 use Apache::lonlocal;
 use File::Copy;
 use GDBM_File;
+use DBI;
 
-my ($lonusersdir,$londocroot,$londaemons);
+my ($lonusersdir,$londocroot,$londaemons,$lonsqlaccess);
 
 BEGIN {
     my $perlvar=&LONCAPA::Configuration::read_conf();
@@ -47,6 +48,7 @@
         $lonusersdir = $perlvar->{'lonUsersDir'};
         $londocroot = $perlvar->{'lonDocRoot'};
         $londaemons = $perlvar->{'lonDaemons'};
+        $lonsqlaccess = $perlvar->{'lonSqlAccess'};
     }
     undef($perlvar);
 }
@@ -395,6 +397,7 @@
 
 my @allskipped;
 my %allmoved;
+my ($dbh,$dbflag);
 
 # Iterate over directories in /home
 if (opendir(my $dir,"/home")) {
@@ -405,13 +408,39 @@
 # Is there a public_html-directory?
             if (-d "/home/$item/public_html") {
                 my $author = $item;
-                my ($domain,$skipped,$output);
+                my ($domain,$skipped,$output,$stopnow);
                 if (ref($allauthors{$author}) eq 'ARRAY') {
-                    ($domain,$skipped) = &choose_domain($action,$author,$allauthors{$author});
+                    ($domain,$skipped,$stopnow) = 
+                        &choose_domain($action,$author,$allauthors{$author});
+                    if ($stopnow) {
+                        if ($action ne 'dryrun') {
+                            my $output = &mt('Stopped by user at author: [_1].',
+                                             "'$author'")/"\n";
+                            &stop_logging($logfh,$output);
+                        }
+                        if ($dbflag == 1) {
+                            &disconnect_mysql($dbh);
+                        }
+                        print &mt('Stopped.')."\n";
+                        exit;
+                    }
                 }
                 if (($domain eq '') && (!$skipped)) {
                     if (ref($pubusers{$author}) eq 'ARRAY') {
-                        ($domain,$skipped) = &choose_domain($action,$author,$pubusers{$author});
+                        ($domain,$skipped,$stopnow) = 
+                            &choose_domain($action,$author,$pubusers{$author});
+                    }
+                    if ($stopnow) {
+                        if ($action ne 'dryrun') {
+                            my $output = &mt('Stopped by user at author: [_1].',
+                                             "'$author'")/"\n";
+                            &stop_logging($logfh,$output);
+                        }
+                        if ($dbflag == 1) {
+                            &disconnect_mysql($dbh);
+                        }
+                        print &mt('Stopped.')."\n";
+                        exit;
                     }
                 }
                 if (($domain eq '') && (!$skipped)) {
@@ -439,7 +468,44 @@
                         }
                     }
                     if (@foundauthor > 0) {
-                        ($domain,$skipped) = &choose_domain($action,$author,\@foundauthor);
+                        ($domain,$skipped,$stopnow) = 
+                             &choose_domain($action,$author,\@foundauthor);
+                        if ($stopnow) {
+                            if ($action ne 'dryrun') {
+                                my $output = &mt('Stopped by user at author: [_1].',
+                                                 "'$author'")/"\n";
+                                &stop_logging($logfh,$output);
+                            }
+                            if ($dbflag == 1) {
+                                &disconnect_mysql($dbh);
+                            }
+                            print &mt('Stopped.')."\n";
+                            exit;
+                        }
+                    }
+                }
+                if (($domain eq '') && (!$skipped)) {
+                    if (!$dbflag) {
+                        ($dbh,$dbflag) = &connect_mysql($lonsqlaccess);
+                    }
+                    if (defined($dbh)) {
+                        my $foundusers = &search_allusers($dbh,$author);
+                        if (ref($foundusers) eq 'HASH') {
+                            ($domain,$skipped,$stopnow) = 
+                                &choose_domain($action,$author,$foundusers);
+                        }
+                        if ($stopnow) {
+                            if ($action ne 'dryrun') {
+                                my $output = &mt('Stopped by user at author: [_1].',
+                                                 "'$author'")/"\n";
+                                &stop_logging($logfh,$output);
+                            }
+                            if ($dbflag == 1) {
+                                &disconnect_mysql($dbh);
+                            }
+                            print &mt('Stopped.')."\n";
+                            exit;
+                        }
                     }
                 }
                 my $source_path="/home/$author/public_html";
@@ -496,7 +562,7 @@
                     }
                 } else {
                     print '*** '.&mt('WARNING: [_1] has no domain.',"'$author'")."\n".
-                          &mt('Enter [_1]: do nothing, continue.','1')."\n".
+                          &mt('Enter [_1]: skip this user.','1')."\n".
                           &mt('Enter [_1]: stop.','2')."\n".
                           &mt('or enter domain for user to be placed into')."\n".
                           &mt('Your input: ');
@@ -520,13 +586,16 @@
                                              "'$author'")/"\n";
                             &stop_logging($logfh,$output); 
                         }
+                        if ($dbflag == 1) {
+                            &disconnect_mysql($dbh);
+                        } 
                         exit;
                     } elsif ($choice =~ /^$match_domain$/) {
                         print &mt('You entered:')." $choice\n".
                               &mt('Is this ok? ~[Y/n~] ');
                         if (!&get_user_selection(1)) {
                             print &mt('Try again ...')."\n".
-                                  &mt('Enter [_1]: do nothing, continue.','1')."\n".
+                                  &mt('Enter [_1]: skip this user.','1')."\n".
                                   &mt('Enter [_1]: stop.','2')."\n".
                                   &mt('or enter domain for user to be placed into')."\n".
                                   &mt('Your input: ');
@@ -550,6 +619,9 @@
                                                      "'$author'")/"\n";
                                     &stop_logging($logfh,$output);
                                 }
+                                if ($dbflag == 1) {
+                                    &disconnect_mysql($dbh);
+                                }
                                 exit;
                             } elsif ($choice !~ /^$match_domain$/) {
                                 print &mt('Invalid domain entered:')." $choice\n";
@@ -630,50 +702,67 @@
 print "\n";
 if ($action ne 'dryrun') {
     my $output = &mt('You skipped: [_1].',$skipcount)."\n".
+                 join("\n",sort(@allskipped))."\n\n".
                  &mt('Moved ... [_1]',$moveinfo);
     print $output;
     print $logfh $output;
     &stop_logging($logfh);
 } else {
     print &mt('You would have skipped: [_1].',$skipcount)."\n".
+          join("\n",sort(@allskipped))."\n\n".
           &mt('You would have moved ... [_1]',$moveinfo);
 }
 print "\n\n".&mt('Done.')."\n";
 
-sub choose_domain { 
-    my ($action,$author,$domarrayref) = @_;
-    my ($domain,$skipped);
-    if (ref($domarrayref) eq 'ARRAY') {
-         if (@{$domarrayref} > 1) {
-             print '*** '.&mt('ERROR: [_1] found in multiple domains.',"'$author'")."\n".
-                   &mt('Enter a number to choose what action to take.')."\n";
-             my $num = 1;
-             for (my $i=0; $i<@{$domarrayref}; $i++) {
-                 print &mt('To use: [_1] enter [_2].',$domarrayref->[$i],$num)."\n";
-                 $num ++;
-             }
-             print &mt('To skip this user enter: [_1].',$num)."\n".
-                   &mt('Your choice:').' ';
-             my $choice=<STDIN>;
-             chomp($choice);
-             if ($choice =~ /^\d+$/) {
-                 if (($choice == $num) || ($choice > $num)) {
-                     $skipped = 1;       
-                 } elsif (($choice < $num) && ($choice > 0)) {
-                     $domain = $domarrayref->[$choice-1];
-                 } else {
-                     print &mt('Invalid choice:')." $choice\n";
-                     $skipped = 1;
-                 }
-             } else {
-                 print &mt('Invalid choice:')." $choice\n";
-                 $skipped = 1;
-             }
-         } elsif (@{$domarrayref} == 1) {
-             $domain = $domarrayref->[0];
-         }
+sub choose_domain {
+    my ($action,$author,$domref) = @_;
+    my ($domain,$skipped,$stopnow, at domains);
+    if (ref($domref) eq 'ARRAY') {
+        @domains = @{$domref};
+    } elsif (ref($domref) eq 'HASH') {
+        @domains = sort(keys(%{$domref}));
+    }
+    if (@domains > 1) {
+        print '*** '.&mt('ERROR: [_1] found in multiple domains.',"'$author'")."\n".
+               &mt('Enter a number to choose what action to take.')."\n";
+        my $num = 1;
+        print &mt('Enter [_1]: skip this user.',$num)."\n";
+        for (my $i=0; $i<@domains; $i++) {
+            my $shown = $domains[$i];
+            if (ref($domref) eq 'HASH') {
+                if ($domref->{$shown} ne '') {
+                    $shown .= ' ('.$domref->{$shown}.') ';
+                }
+            }
+            $num ++; 
+            print &mt('Enter [_1]: use domain - [_2].',$num,$shown)."\n";
+        }
+        $num ++;
+        print &mt('Enter [_1]: stop.',$num)."\n";
+        print &mt('Your choice:').' ';
+        my $choice=<STDIN>;
+        chomp($choice);
+        if ($choice =~ /^\d+$/) {
+            if ($choice == 1) {
+                $skipped = 1;       
+            } elsif (($choice < $num) && ($choice > 1)) {
+                $domain = $domains[$choice-2];
+            } elsif ($choice == $num) {
+                $stopnow = 1;
+            } else {
+                print &mt('Invalid choice:')." $choice\n".
+                      &mt('Skipping this user.')."\n";
+                $skipped = 1;
+            }
+        } else {
+            print &mt('Invalid choice:')." $choice\n".
+                  &mt('Skipping this user.')."\n";
+            $skipped = 1;
+        }
+    } elsif (@domains == 1) {
+        $domain = $domains[0];
     }
-    return ($domain,$skipped);
+    return ($domain,$skipped,$stopnow);
 }
 
 sub move_priv_to_home {
@@ -791,3 +880,46 @@
     return;
 }
 
+sub connect_mysql {
+    my ($lonsqlaccess) = @_;
+    my ($dbh,$dbflag);
+    eval { $dbh = DBI->connect("DBI:mysql:loncapa","www",
+                               $lonsqlaccess,
+                               {RaiseError =>0,PrintError=>0}); 
+    };
+    if ($@) {
+        $dbflag = -1;
+    } else {
+        if (defined($dbh)) {
+            $dbflag = 1;
+        }
+    }
+    return ($dbh,$dbflag);
+}
+
+sub disconnect_mysql {
+    my ($dbh) = @_;
+    if (ref($dbh)) {
+        $dbh->disconnect;
+    }
+    return;
+}
+
+sub search_allusers {
+    my ($dbh,$author) = @_;
+    my %fullnames;
+    if ((ref($dbh)) && ($author ne '')) {
+        eval {
+            my $statement = "SELECT domain, lastname, firstname FROM allusers WHERE username='$author'";
+            my $sth = $dbh->prepare($statement);
+            $sth->execute();
+            while ( my ($dom,$last,$first) = $sth->fetchrow_array()) {
+                if ($dom ne '') {
+                    $fullnames{$dom} = "$first $last";
+                }
+            }
+            $sth->finish;
+        };
+    }
+    return \%fullnames;
+}


More information about the LON-CAPA-cvs mailing list