Diff for /loncom/debugging_tools/move_construction_spaces.pl between versions 1.1 and 1.2

version 1.1, 2011/10/26 14:19:42 version 1.2, 2011/10/26 17:35:10
Line 12  use Apache::lonlocal; Line 12  use Apache::lonlocal;
 use File::Copy;  use File::Copy;
 use GDBM_File;  use GDBM_File;
   
   
 my ($parameter)=(@ARGV);  
 my $lang = &Apache::lonlocal::choose_language();  my $lang = &Apache::lonlocal::choose_language();
 &Apache::lonlocal::get_language_handle(undef,$lang);  &Apache::lonlocal::get_language_handle(undef,$lang);
 print"\n";  
   if ($< != 0) {
       print(&mt('You must be root in order to move Construction Spaces.').
            "\n");
       exit;
   }
   
   my $perlvar=&LONCAPA::Configuration::read_conf();
   my ($lonuserdir,$londocroot);
   if (ref($perlvar) eq 'HASH') {
       $lonuserdir = $perlvar->{'lonUsersDir'};
       $londocroot = $perlvar->{'lonDocRoot'};
   }
   undef($perlvar);
   
 # Abort if more than one argument.  # Abort if more than one argument.
 if (@ARGV > 1) {  
   my $parameter=$ARGV[0];
   $parameter =~ s/^\s+//;
   $parameter =~ s/\s+$//;
   
   if ((@ARGV > 1) || (($parameter ne '') && ($parameter !~ /^(move|undo)$/))) {
     print &mt('usage: [_1]','move_construction_spaces.pl [move|undo]')."\n\n".      print &mt('usage: [_1]','move_construction_spaces.pl [move|undo]')."\n\n".
           &mt('You should enter either no arguments, or just one argument -- either move or undo.')."\n".            &mt('You should enter either no arguments, or just one argument -- either move or undo.')."\n".
           &mt("move - to move authors' Construction Spaces from: /home to /home/httpd/html/priv/domain")."\n".            &mt("move - to move authors' Construction Spaces from: [_1] to [_2].",
           &mt('undo - to reverse those changes and move Construction Spaces back from: /home/httpd/html/priv/domain to /home')."\n".                "'/home'","'$londocroot/priv/'")."\n".
             &mt('undo - to reverse those changes and move Construction Spaces back from: [_1] to [_2].',
                 "'$londocroot/priv/'","'/home'")."\n".
           &mt('no argument to do a dry run of the move option, without actually moving anything.')."\n";            &mt('no argument to do a dry run of the move option, without actually moving anything.')."\n";
     exit;      exit;
 }  }
   
 print "\nMoving authors' Construction Spaces\n".  print "\n".&mt("Moving authors' Construction Spaces.")."\n".
       "-----------------------------\n\n".        "-----------------------------\n\n".
       "If run without an argument, the script will report what it would do\n".        &mt('If run without an argument, the script will report what it would do when moving Construction Spaces from [_1] to [_2].',
       "when moving Construction Spaces from /home to /home/httpd/html/priv/.\n\n".            "'/home'","'$londocroot/priv/'")."\n\n".
       "If there are ambiguities (i.e., the same username belongs to two domains)\n".        &mt('If there are ambiguities (i.e., the same username belongs to two domains), this will be flagged, and you will be able to decide how to proceed.')."\n";
       "this will be flagged, and you will be able to decide how to proceed.\n";  
   
 my $perlvar=&LONCAPA::Configuration::read_conf();  my $perlvar=&LONCAPA::Configuration::read_conf();
 my ($lonuserdir,$londocroot);  my ($lonuserdir,$londocroot);
Line 43  if (ref($perlvar) eq 'HASH') { Line 60  if (ref($perlvar) eq 'HASH') {
 }  }
 undef($perlvar);  undef($perlvar);
   
 my $parameter=$ARGV[0];  
 $parameter =~ s/^\s+//;  
 $parameter =~ s/\s+$//;  
 my (undef,undef,$uid,$gid) = getpwnam('www');  my (undef,undef,$uid,$gid) = getpwnam('www');
 my ($action) = ($parameter=~/^(move|undo)$/);  my ($action) = ($parameter=~/^(move|undo)$/);
 if ($action eq '') {  if ($action eq '') {
Line 53  if ($action eq '') { Line 67  if ($action eq '') {
 }  }
   
 if ($action eq 'dryrun') {  if ($action eq 'dryrun') {
     print "\nRunning in exploratory mode.\n".      print "\n".
           "Run with parameter 'move' to actually move the author spaces, i.e. \n".            &mt('Running in exploratory mode.')."\n".
           "move_construction_spaces.pl move\n\n".            &mt('Run with argument [_1] to actually move Construction Spaces to [_2], i.e., [_3]',
           "Run with parameter 'undo' to move author spaces back to /home, i.e. \n".                "'move'","'$londocroot/priv'","\nperl move_construction_spaces.pl move")."\n\n".
           "move_construction_spaces.pl undo\n\n";            &mt('Run with argument [_1] to move Construction spaces back to [_2], i.e., [_3]',
                 "'undo'","'/home'","\nperl move_construction_spaces.pl undo")."\n\n".
             &mt('Continue? ~[y/N~] ');
       if (!&get_user_selection()) {
           exit;
       }
 } else {  } else {
     print "\n *** Running in a mode where changes will be made.\n";      print "\n ***".&mt('Running in a mode where changes will be made.')."\n";
     if ($action eq 'move') {      if ($action eq 'move') {
         print "\nMode is $action -- directories will be moved to $londocroot/priv\n";          print "\n".
                 &mt('Mode is [_1] -- directories will be moved to [_2].',
                     "'$action'","'$londocroot/priv'")."\n";
     } else {      } else {
         print "\nMode is $action -- directories will be moved back to /home\n";          print "\n".
                 &mt('Mode is [_1] -- directories will be moved back to [_2].',
                     "'$action'","'/home'")."\n";
     }      }
     print &mt('Continue? ~[y/N~] ');      print &mt('Continue? ~[y/N~] ');
   
     if (!&get_user_selection()) {      if (!&get_user_selection()) {
         exit;          exit;
     }      }
Line 78  my %pubusers; Line 100  my %pubusers;
   
 if ($action eq 'move') {  if ($action eq 'move') {
     if (-d "$londocroot/priv") {      if (-d "$londocroot/priv") {
         print "New Construction Spaces directory: '$londocroot/priv' already exists.\n";          print "\n".
                 &mt('New Construction Spaces directory: [_1] already exists.',
                     "'$londocroot/priv'")."\n";
     } else {      } else {
         print "\nCreating new directory: '$londocroot/priv' for Construction Spaces.\n";          print "\n".
                 &mt('Creating new directory: [_1] for Construction Spaces.',
                     "'$londocroot/priv'")."\n";
         if (mkdir("$londocroot/priv",0755)) {          if (mkdir("$londocroot/priv",0755)) {
             if (chown($uid,$gid,"$londocroot/priv")) {              if (chown($uid,$gid,"$londocroot/priv")) {
                 print "Creation Successful\n";                  print &mt('Creation Successful')."\n";
             } else {              } else {
                 print "Failed to changer ownership to $uid:$gid\n";                  print &mt('Failed to change ownership to [_1].',"'$uid:$gid'")."\n".
                   &mt('Stopping')."\n";
                 exit;                  exit;
             }              }
         } else {          } else {
             print "Failed to create directory\n";              print &mt('Failed to create directory [_1].',"'$londocroot/priv'")."\n".
             exit;                     &mt('Stopping')."\n";
               exit;
         }          }
     }      }
 }  }
Line 112  if ($lonuserdir) { Line 140  if ($lonuserdir) {
                         if (!-e $dom_target) {                          if (!-e $dom_target) {
                             if (mkdir($dom_target,0755)) {                              if (mkdir($dom_target,0755)) {
                                 chown($uid,$gid,$dom_target);                                  chown($uid,$gid,$dom_target);
                                 print "Made $dom_target\n";                                  print &mt('Made [_1].',"'$dom_target'")."\n";
                             } else {                              } else {
                                 print "Failed to make $dom_target. Stopping\n";                                  print &mt('Failed to make [_1].',"'$dom_target'")."\n".
                                         &mt('Stopping')."\n";
                                 exit;                                  exit;
                             }                              }
                         } elsif ($action eq 'dryrun') {                          } elsif ($action eq 'dryrun') {
                             print "Would make $dom_target\n";                              print &mt('Would make [_1].',"'$dom_target'")."\n";
                         }                          }
                     }                      }
                     my %authors=();                      my %authors=();
Line 128  if ($lonuserdir) { Line 157  if ($lonuserdir) {
                         $dbref=&LONCAPA::locking_hash_tie($fname,&GDBM_READER());                          $dbref=&LONCAPA::locking_hash_tie($fname,&GDBM_READER());
                     }                      }
                     if (!$dbref) {                      if (!$dbref) {
                         print "Unable to tie to $fname";                          print &mt('Unable to tie to [_1].',"'$fname'")."\n";
                     } elsif (ref($dbref) eq 'HASH') {                      } elsif (ref($dbref) eq 'HASH') {
                         foreach my $key (keys(%{$dbref})) {                          foreach my $key (keys(%{$dbref})) {
                             $key = &unescape($key);                              $key = &unescape($key);
Line 143  if ($lonuserdir) { Line 172  if ($lonuserdir) {
         }          }
         closedir($dir);          closedir($dir);
     } else {      } else {
         print "Could not open $lonuserdir.  Stopping\n";          print &mt('Could not open [_1].',"'$lonuserdir'")."\n".
                 &mt('Stopping')."\n";
         exit;          exit;
     }      }
 }  }
Line 193  if ($action eq 'undo') { Line 223  if ($action eq 'undo') {
     foreach my $uname (keys(%privspaces)) {      foreach my $uname (keys(%privspaces)) {
         if (ref($privspaces{$uname}) eq 'ARRAY') {          if (ref($privspaces{$uname}) eq 'ARRAY') {
             if (@{$privspaces{$uname}} > 1) {              if (@{$privspaces{$uname}} > 1) {
                 print "Same username used for authors in multiple domains\n".                  my $displaydoms = join(', ',@{$privspaces{$uname}});
                       "This configuration is not supported where Construction Spaces are located in /home.\n".                  print &mt('Same username used for authors in multiple domains.')."\n".
                       "You will be able to move files for just one of the domains, choose which one: \n".                        &mt('This configuration is not supported where Construction Spaces are located in [_1].','/home').".\n".
                       "The domains to choose from are: ".join(', ',@{$privspaces{$uname}})."\n".                        &mt('You will be able to move files for just one of the domains, choose which one.')."\n".
                       "Enter choice: ";                        &mt('The domains to choose from are: [_1].',"'$displaydoms'")."\n".
                         &mt('Enter choice: ');
                 my $choice=<STDIN>;                  my $choice=<STDIN>;
                 chomp($choice);                  chomp($choice);
                 if (grep(/^\Q$choice\E$/,@{$privspaces{$uname}})) {                  if (grep(/^\Q$choice\E$/,@{$privspaces{$uname}})) {
                     &move_priv_to_home($londocroot,$uname,$choice);                      &move_priv_to_home($londocroot,$uname,$choice);
                 } else {                  } else {
                     print "Invalid choice of domain: $choice\n".                      print &mt('Invalid choice of domain:')." $choice\n".
                           "Skipping this user: $uname\n";                            &mt('Skipping this user: [_1].',"'$uname'")."\n";
                     next;                      next;
                 }                  }
             } elsif (@{$privspaces{$uname}} == 1) {              } elsif (@{$privspaces{$uname}} == 1) {
                     &move_priv_to_home($londocroot,$uname,$privspaces{$uname}[0]);                      &move_priv_to_home($londocroot,$uname,$privspaces{$uname}[0]);
             } else {              } else {
                 print "User $uname found in $londocroot/priv was not within a domain\n";                  print &mt('Username [_1] found in [_2] was not within a domain',
                             "'$uname'","'$londocroot/priv'")."\n";
             }              }
         }          }
     }      }
     print "Done\n";      print &mt('Done')."\n";
     exit;      exit;
 }  }
   
Line 242  if (opendir(my $dir,"/home")) { Line 274  if (opendir(my $dir,"/home")) {
                         move($source_path,$target_path);                          move($source_path,$target_path);
                             chown($uid,$gid,$target_path);                              chown($uid,$gid,$target_path);
                             chmod($target_path,0755);                              chmod($target_path,0755);
                         print "Moved $source_path to $target_path\n";                          print &mt('Moved [_1] to [_2].',"'$source_path'","'$target_path'")."\n";
                     } elsif ($action eq 'dryrun') {                      } elsif ($action eq 'dryrun') {
                         print "Would move $source_path to $target_path\n";                          print &mt('Would move [_1] to [_2].',"'$source_path'","'$target_path'")."\n";
                     }                      }
                 } elsif (!$skipped) {                  } elsif (!$skipped) {
                     print "*** WARNING: $author has no domain.\n".                      print '*** '.&mt('WARNING: [_1] has no domain.',"'$author'")."\n".
                           "Enter 1: do nothing, continue\n".                            &mt('Enter [_1]: do nothing, continue.','1')."\n".
                           "Enter 2: stop\n".                            &mt('Enter [_2]: stop.','2')."\n".
                           "or enter domain for user to be placed into\n".                            &mt('or enter domain for user to be placed into')."\n".
                           "Your input: ";                            &mt('Your input: ');
                     my $choice=<STDIN>;                      my $choice=<STDIN>;
                     chomp($choice);                      chomp($choice);
                     next if ($choice ==1);                      next if ($choice ==1);
                     if ($choice == 2) { print "Stopped.\n"; exit; }                       if ($choice == 2) {
                           print &mt('Stopped.')."\n";
                           exit;
                       } 
                     if ($choice =~ /^$match_domain$/) {                      if ($choice =~ /^$match_domain$/) {
                         my $dompath="$londocroot/priv/$choice";                          my $dompath="$londocroot/priv/$choice";
                         my $newpath="$londocroot/priv/$choice/$author";                          my $newpath="$londocroot/priv/$choice/$author";
                         unless (-e $dompath) {                          unless (-e $dompath) {
                             print "*** WARNING: $dompath does not yet exist.\n";                              print '*** '.&mt('WARNING: [_1] does not yet exist.',"'$dompath'")."\n";
                         }                          }
                         if ($action eq 'move') {                          if ($action eq 'move') {
                             print "Making author $author in domain $choice\n";                              print &mt('Making author [_1] in domain [_2].',"'$author'","'$choice'")."\n";
                             unless (-e $dompath) {                              unless (-e $dompath) {
                                 print "Making $dompath\n";                                  print &mt('Making [_1].',"'$dompath'")."\n";
                                 mkdir($dompath,0755);                                  mkdir($dompath,0755);
                                 chown($uid,$gid,$dompath);                                  chown($uid,$gid,$dompath);
                             }                              }
                             print "Making $newpath\n";                              print &mt('Making [_1].',"'$newpath'")."\n";
                             mkdir($newpath,0755);                              mkdir($newpath,0755);
                             chown($uid,$gid,$newpath);                              chown($uid,$gid,$newpath);
                         } elsif ($action eq 'dryrun') {                          } elsif ($action eq 'dryrun') {
                            print "Would make author $author in domain $choice\n";                             print &mt('Would make author [_1] in domain [_2].',"'$author'","'$choice'")."\n";
                            unless (-e $dompath) {                             unless (-e $dompath) {
                                print "Would make $dompath\n";                                 print &mt('Would make [_1].',"'$dompath'")."\n";
                            }                             }
                            print "Would make $newpath\n";                             print &mt('Would make [_1].',"'$newpath'")."\n";
                         }                          }
                     }                      }
                 }                  }
Line 292  sub choose_domain { Line 327  sub choose_domain {
     my ($domain,$skipped);      my ($domain,$skipped);
     if (ref($domarrayref) eq 'ARRAY') {      if (ref($domarrayref) eq 'ARRAY') {
          if (@{$domarrayref} > 1) {           if (@{$domarrayref} > 1) {
              print "*** ERROR: $author found in multiple domains\n".               print '*** '.&mt('ERROR: [_1] found in multiple domains.',"'$author'")."\n".
                    "Enter a number to choose what action to take\n";                     &mt('Enter a number to choose what action to take.')."\n";
              my $num = 1;               my $num = 1;
              for (my $i=0; $i<@{$domarrayref}; $i++) {               for (my $i=0; $i<@{$domarrayref}; $i++) {
                  print "To use: $domarrayref->[$i] enter $num\n";                   print &mt('To use: [_1] enter [_2].',$domarrayref->[$i],$num)."\n";
                  $num ++;                   $num ++;
              }               }
              print "To skip this user enter: $num\n".               print &mt('To skip this user enter: [_1].',$num)."\n".
                    "Your choice: ";                     &mt('Your choice:').' ';
              my $choice=<STDIN>;               my $choice=<STDIN>;
              chomp($choice);               chomp($choice);
              if ($choice =~ /^\d+$/) {               if ($choice =~ /^\d+$/) {
Line 309  sub choose_domain { Line 344  sub choose_domain {
                  } elsif (($choice < $num) && ($choice > 0)) {                   } elsif (($choice < $num) && ($choice > 0)) {
                      $domain = $domarrayref->[$choice-1];                       $domain = $domarrayref->[$choice-1];
                  } else {                   } else {
                      print "Invalid choice\n";                       print &mt('Invalid choice:')." $choice\n";
                      $skipped = 1;                       $skipped = 1;
                  }                   }
              } else {               } else {
                  print "Invalid choice\n";                   print &mt('Invalid choice:')." $choice\n";
                  $skipped = 1;                   $skipped = 1;
              }               }
          } elsif (@{$domarrayref} == 1) {           } elsif (@{$domarrayref} == 1) {
              $domain = $domarrayref->[0];               $domain = $domarrayref->[0];
              if ($action eq 'dryrun') {  
                  print "Would use domain: $domain for author: $author\n";  
              }  
          }           }
     }      }
     return ($domain,$skipped);      return ($domain,$skipped);
Line 335  sub move_priv_to_home { Line 367  sub move_priv_to_home {
             if (mkdir("/home/$uname",0755)) {              if (mkdir("/home/$uname",0755)) {
                 chown($uid,$gid,"/home/$uname");                  chown($uid,$gid,"/home/$uname");
             } else {              } else {
                 print "Failed to create directory /home/$uname -- not moving $source_path\n";                  print &mt('Failed to create directory [_1] -- not moving [_2].',
                             "'/home/$uname'","'$source_path'")."\n";
             }              }
         }          }
         if (!-e $target_path) {          if (!-e $target_path) {
              move($source_path,$target_path);               move($source_path,$target_path);
              chown($uid,$gid,$target_path);               chown($uid,$gid,$target_path);
              chmod($target_path,0755);               chmod($target_path,0755);
              print "Moved $source_path to $target_path\n";               print &mt('Moved [_1] to [_2].',"'$source_path'","'$target_path'")."\n";
         } else {          } else {
              print "Directory $target_path already exists -- not moving $source_path\n";               print &mt('Directory [_1] already exists -- not moving [_2].',
                          "'$target_path'","'$source_path'")."\n";
         }          }
     }      }
     return;      return;

Removed from v.1.1  
changed lines
  Added in v.1.2


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