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

raeburn raeburn at source.lon-capa.org
Wed Oct 26 13:35:10 EDT 2011


raeburn		Wed Oct 26 17:35:10 2011 EDT

  Modified files:              
    /loncom/debugging_tools	move_construction_spaces.pl 
  Log:
  - Early out if not run as root.
  - &mt().
  
  
-------------- next part --------------
Index: loncom/debugging_tools/move_construction_spaces.pl
diff -u loncom/debugging_tools/move_construction_spaces.pl:1.1 loncom/debugging_tools/move_construction_spaces.pl:1.2
--- loncom/debugging_tools/move_construction_spaces.pl:1.1	Wed Oct 26 14:19:42 2011
+++ loncom/debugging_tools/move_construction_spaces.pl	Wed Oct 26 17:35:10 2011
@@ -12,28 +12,45 @@
 use File::Copy;
 use GDBM_File;
 
-
-my ($parameter)=(@ARGV);
 my $lang = &Apache::lonlocal::choose_language();
 &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.
-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".
           &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('undo - to reverse those changes and move Construction Spaces back from: /home/httpd/html/priv/domain to /home')."\n".
+          &mt("move - to move authors' Construction Spaces from: [_1] to [_2].",
+              "'/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";
     exit;
 }
 
-print "\nMoving authors' Construction Spaces\n".
+print "\n".&mt("Moving authors' Construction Spaces.")."\n".
       "-----------------------------\n\n".
-      "If run without an argument, the script will report what it would do\n".
-      "when moving Construction Spaces from /home to /home/httpd/html/priv/.\n\n".
-      "If there are ambiguities (i.e., the same username belongs to two domains)\n".
-      "this will be flagged, and you will be able to decide how to proceed.\n";
+      &mt('If run without an argument, the script will report what it would do when moving Construction Spaces from [_1] to [_2].',
+          "'/home'","'$londocroot/priv/'")."\n\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";
 
 my $perlvar=&LONCAPA::Configuration::read_conf();
 my ($lonuserdir,$londocroot);
@@ -43,9 +60,6 @@
 }
 undef($perlvar);
 
-my $parameter=$ARGV[0];
-$parameter =~ s/^\s+//;
-$parameter =~ s/\s+$//;
 my (undef,undef,$uid,$gid) = getpwnam('www');
 my ($action) = ($parameter=~/^(move|undo)$/);
 if ($action eq '') {
@@ -53,20 +67,28 @@
 }
 
 if ($action eq 'dryrun') {
-    print "\nRunning in exploratory mode.\n".
-          "Run with parameter 'move' to actually move the author spaces, i.e. \n".
-          "move_construction_spaces.pl move\n\n".
-          "Run with parameter 'undo' to move author spaces back to /home, i.e. \n".
-          "move_construction_spaces.pl undo\n\n";
+    print "\n".
+          &mt('Running in exploratory mode.')."\n".
+          &mt('Run with argument [_1] to actually move Construction Spaces to [_2], i.e., [_3]',
+              "'move'","'$londocroot/priv'","\nperl move_construction_spaces.pl move")."\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 {
-    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') {
-        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 {
-        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~] ');
-
     if (!&get_user_selection()) {
         exit;
     }
@@ -78,19 +100,25 @@
 
 if ($action eq 'move') {
     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 {
-        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 (chown($uid,$gid,"$londocroot/priv")) {
-                print "Creation Successful\n";
+                print &mt('Creation Successful')."\n";
             } 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;
             }
         } else {
-            print "Failed to create directory\n";
-            exit; 
+            print &mt('Failed to create directory [_1].',"'$londocroot/priv'")."\n".
+                  &mt('Stopping')."\n";
+            exit;
         }
     }
 }
@@ -112,13 +140,14 @@
                         if (!-e $dom_target) {
                             if (mkdir($dom_target,0755)) {
                                 chown($uid,$gid,$dom_target);
-                                print "Made $dom_target\n";
+                                print &mt('Made [_1].',"'$dom_target'")."\n";
                             } else {
-                                print "Failed to make $dom_target. Stopping\n";
+                                print &mt('Failed to make [_1].',"'$dom_target'")."\n".
+                                      &mt('Stopping')."\n";
                                 exit;
                             }
                         } elsif ($action eq 'dryrun') {
-                            print "Would make $dom_target\n";
+                            print &mt('Would make [_1].',"'$dom_target'")."\n";
                         }
                     }
                     my %authors=();
@@ -128,7 +157,7 @@
                         $dbref=&LONCAPA::locking_hash_tie($fname,&GDBM_READER());
                     }
                     if (!$dbref) {
-                        print "Unable to tie to $fname";
+                        print &mt('Unable to tie to [_1].',"'$fname'")."\n";
                     } elsif (ref($dbref) eq 'HASH') {
                         foreach my $key (keys(%{$dbref})) {
                             $key = &unescape($key);
@@ -143,7 +172,8 @@
         }
         closedir($dir);
     } else {
-        print "Could not open $lonuserdir.  Stopping\n";
+        print &mt('Could not open [_1].',"'$lonuserdir'")."\n".
+              &mt('Stopping')."\n";
         exit;
     }
 }
@@ -193,28 +223,30 @@
     foreach my $uname (keys(%privspaces)) {
         if (ref($privspaces{$uname}) eq 'ARRAY') {
             if (@{$privspaces{$uname}} > 1) {
-                print "Same username used for authors in multiple domains\n".
-                      "This configuration is not supported where Construction Spaces are located in /home.\n".
-                      "You will be able to move files for just one of the domains, choose which one: \n".
-                      "The domains to choose from are: ".join(', ',@{$privspaces{$uname}})."\n".
-                      "Enter choice: ";
+                my $displaydoms = join(', ',@{$privspaces{$uname}});
+                print &mt('Same username used for authors in multiple domains.')."\n".
+                      &mt('This configuration is not supported where Construction Spaces are located in [_1].','/home').".\n".
+                      &mt('You will be able to move files for just one of the domains, choose which one.')."\n".
+                      &mt('The domains to choose from are: [_1].',"'$displaydoms'")."\n".
+                      &mt('Enter choice: ');
                 my $choice=<STDIN>;
                 chomp($choice);
                 if (grep(/^\Q$choice\E$/,@{$privspaces{$uname}})) {
                     &move_priv_to_home($londocroot,$uname,$choice);
                 } else {
-                    print "Invalid choice of domain: $choice\n".
-                          "Skipping this user: $uname\n";
+                    print &mt('Invalid choice of domain:')." $choice\n".
+                          &mt('Skipping this user: [_1].',"'$uname'")."\n";
                     next;
                 }
             } elsif (@{$privspaces{$uname}} == 1) {
                     &move_priv_to_home($londocroot,$uname,$privspaces{$uname}[0]);
             } 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;
 }
 
@@ -242,42 +274,45 @@
                         move($source_path,$target_path);
                             chown($uid,$gid,$target_path);
                             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') {
-                        print "Would move $source_path to $target_path\n";
+                        print &mt('Would move [_1] to [_2].',"'$source_path'","'$target_path'")."\n";
                     }
                 } elsif (!$skipped) {
-                    print "*** WARNING: $author has no domain.\n".
-                          "Enter 1: do nothing, continue\n".
-                          "Enter 2: stop\n".
-                          "or enter domain for user to be placed into\n".
-                          "Your input: ";
+                    print '*** '.&mt('WARNING: [_1] has no domain.',"'$author'")."\n".
+                          &mt('Enter [_1]: do nothing, continue.','1')."\n".
+                          &mt('Enter [_2]: stop.','2')."\n".
+                          &mt('or enter domain for user to be placed into')."\n".
+                          &mt('Your input: ');
                     my $choice=<STDIN>;
                     chomp($choice);
                     next if ($choice ==1);
-                    if ($choice == 2) { print "Stopped.\n"; exit; } 
+                    if ($choice == 2) {
+                        print &mt('Stopped.')."\n";
+                        exit;
+                    } 
                     if ($choice =~ /^$match_domain$/) {
                         my $dompath="$londocroot/priv/$choice";
                         my $newpath="$londocroot/priv/$choice/$author";
                         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') {
-                            print "Making author $author in domain $choice\n";
+                            print &mt('Making author [_1] in domain [_2].',"'$author'","'$choice'")."\n";
                             unless (-e $dompath) {
-                                print "Making $dompath\n";
+                                print &mt('Making [_1].',"'$dompath'")."\n";
                                 mkdir($dompath,0755);
                                 chown($uid,$gid,$dompath);
                             }
-                            print "Making $newpath\n";
+                            print &mt('Making [_1].',"'$newpath'")."\n";
                             mkdir($newpath,0755);
                             chown($uid,$gid,$newpath);
                         } 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) {
-                               print "Would make $dompath\n";
+                               print &mt('Would make [_1].',"'$dompath'")."\n";
                            }
-                           print "Would make $newpath\n";
+                           print &mt('Would make [_1].',"'$newpath'")."\n";
                         }
                     }
                 }
@@ -292,15 +327,15 @@
     my ($domain,$skipped);
     if (ref($domarrayref) eq 'ARRAY') {
          if (@{$domarrayref} > 1) {
-             print "*** ERROR: $author found in multiple domains\n".
-                   "Enter a number to choose what action to take\n";
+             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 "To use: $domarrayref->[$i] enter $num\n";
+                 print &mt('To use: [_1] enter [_2].',$domarrayref->[$i],$num)."\n";
                  $num ++;
              }
-             print "To skip this user enter: $num\n".
-                   "Your choice: ";
+             print &mt('To skip this user enter: [_1].',$num)."\n".
+                   &mt('Your choice:').' ';
              my $choice=<STDIN>;
              chomp($choice);
              if ($choice =~ /^\d+$/) {
@@ -309,18 +344,15 @@
                  } elsif (($choice < $num) && ($choice > 0)) {
                      $domain = $domarrayref->[$choice-1];
                  } else {
-                     print "Invalid choice\n";
+                     print &mt('Invalid choice:')." $choice\n";
                      $skipped = 1;
                  }
              } else {
-                 print "Invalid choice\n";
+                 print &mt('Invalid choice:')." $choice\n";
                  $skipped = 1;
              }
          } elsif (@{$domarrayref} == 1) {
              $domain = $domarrayref->[0];
-             if ($action eq 'dryrun') {
-                 print "Would use domain: $domain for author: $author\n";
-             }
          }
     }
     return ($domain,$skipped);
@@ -335,16 +367,18 @@
             if (mkdir("/home/$uname",0755)) {
                 chown($uid,$gid,"/home/$uname");
             } 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) {
              move($source_path,$target_path);
              chown($uid,$gid,$target_path);
              chmod($target_path,0755);
-             print "Moved $source_path to $target_path\n";
+             print &mt('Moved [_1] to [_2].',"'$source_path'","'$target_path'")."\n";
         } 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;


More information about the LON-CAPA-cvs mailing list