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

matthew lon-capa-cvs@mail.lon-capa.org
Mon, 23 Aug 2004 19:47:39 -0000


matthew		Mon Aug 23 15:47:39 2004 EDT

  Modified files:              
    /loncom/debugging_tools	modify_config_files.pl 
  Log:
  Modified &modify_config_file to return a 1 or 0 if it modified or did
  not modify the configuration file.  Modified &update_file to return a
  1 or 0 if &modify_config_file indicated any change in the file (and we
  skip writing the config file if there were no changes made).  Modified
  the routine to return 0 or 1, where 1 indicates the global MySQL file
  was modified.
  
  
Index: loncom/debugging_tools/modify_config_files.pl
diff -u loncom/debugging_tools/modify_config_files.pl:1.3 loncom/debugging_tools/modify_config_files.pl:1.4
--- loncom/debugging_tools/modify_config_files.pl:1.3	Wed Aug 11 13:32:53 2004
+++ loncom/debugging_tools/modify_config_files.pl	Mon Aug 23 15:47:39 2004
@@ -2,7 +2,7 @@
 #
 # The LearningOnline Network
 #
-# $Id: modify_config_files.pl,v 1.3 2004/08/11 17:32:53 matthew Exp $
+# $Id: modify_config_files.pl,v 1.4 2004/08/23 19:47:39 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -53,7 +53,8 @@
 use LONCAPA::Configuration;
 my $loncapa_config=LONCAPA::Configuration::read_conf('loncapa.conf');
 
-&update_file('/etc/yum.conf',
+my $yum_status = 
+    &update_file('/etc/yum.conf',
              [{section => 'loncapa-updates-i386',
                key     => 'name=',
                value   => 'Fedora Core $releasever LON-CAPA i386 Updates',
@@ -70,18 +71,19 @@
                    '$releasever/noarch',
            }]);
 
-&update_file('/etc/my.cnf',
+my $mysql_global_status =
+    &update_file('/etc/my.cnf',
              [{section =>'mysqld',
                key     =>'set-variable=wait_timeout=',
                value   =>'31536000', }]);
 
-
 my $local_my_cnf = '/home/www/.my.cnf';
 if (! -e $local_my_cnf) {
-#    # Create a file so we can do something with it...
+    # Create a file so we can do something with it...
     system("touch $local_my_cnf");
 }
-&update_file($local_my_cnf,
+my $mysql_www_status =
+    &update_file($local_my_cnf,
              [{section =>'client',
                key     =>'user=',
                value   =>'www',},
@@ -89,7 +91,11 @@
                key     =>'password=',
                value   =>$loncapa_config->{'lonSqlAccess'}},]);
 
-exit;
+my $exitvalue = 0;
+
+if ($mysql_global_status) { $exitvalue = 1; }
+
+exit $exitvalue;
 
 
 
@@ -109,20 +115,24 @@
     return 1 if (! -e $file);
     my $backup = $file.'.backup';
     if (! copy($file,$backup)) {
-        warn "Error: Unable to make backup of $file";
+        warn "**** Error: Unable to make backup of $file";
         return 0;
     }
     my ($filedata) = &parse_config_file($file);
-    if (! ref($filedata)) { warn "Error: $filedata"; return 0;}
+    if (! ref($filedata)) { warn "**** Error: $filedata"; return 0;}
+    my $modified = 0;
     foreach my $data (@$newdata) {
         my $section = $data->{'section'};
         my $key = $data->{'key'};
         my $value = $data->{'value'};
-        &modify_config_file($filedata,$section,$key,$value)
+        my $result = &modify_config_file($filedata,$section,$key,$value);
+        if ($result) { $modified = 1; }
+    }
+    if ($modified) {
+        my $result = &write_config_file($file,$filedata);
+        if (defined($result)) { warn 'Error:'.$result; return 0; }
     }
-    my $result = &write_config_file($file,$filedata);
-    if (defined($result)) { warn 'Error:'.$result; return 0; }
-    return 1;
+    return $modified;
 }
 
 #################################################################
@@ -155,6 +165,7 @@
             my $section_id = $1;
             push(@Structure,'__section__'.$section_id);
             while ($line = shift(@Input)) {
+                chomp($line);
                 if ($line =~ /^\[([^\]]*)\]/) {
                     unshift(@Input,$line);
                     last;
@@ -237,12 +248,16 @@
 $newkey: A line which matches this will be replaced with $newkey.$newvalue
 $newvalue: The new value to be placed with the new key.
 
+Returns: 0 or 1, indicating if the file was modified(1) or not(0).
+
+
 =cut
 
 #################################################################
 #################################################################
 sub modify_config_file {
     my ($filedata,$section,$newkey,$newvalue)=@_;
+    my $modified = 0;    # returned value - set to true if the file is modified
     my ($structure,$sections) = @$filedata;
     if (! defined($newvalue)) {
         $newvalue = '';
@@ -264,9 +279,11 @@
     # Put the item in or update it.
     my $key_is_new = 1;
     for (my $i=0;$i<scalar(@$target);$i++) {
-        
         if ($target->[$i] =~/^$newkey/) {
-            $target->[$i]=$newline;
+            if ($target->[$i] ne $newline) {
+                $target->[$i]=$newline;
+                $modified = 1;
+            }
             $key_is_new = 0;
             last;
         }
@@ -278,12 +295,14 @@
             # No need to put things after a blank line.
             if (defined($target->[-1]) && $target->[-1] =~ /^\s*$/) {
                 $target->[-1] = $newline;
+                $modified = 1;
             } else {
                 push(@$target,$newline);
+                $modified = 1;
             }
         }
     }
-    return ($filedata);
+    return $modified;
 }