From 221fddf796b6b5205368e1f00af53d8df14e09bc Mon Sep 17 00:00:00 2001
From: Nicola Vigano <nicola.vigano@esrf.fr>
Date: Mon, 9 Jul 2012 14:15:43 +0000
Subject: [PATCH] OAR: fixed a bug when deleting arrays. updateOar still needs
 some commenting!!

Signed-off-by: Nicola Vigano <nicola.vigano@esrf.fr>

git-svn-id: https://svn.code.sf.net/p/dct/code/trunk@651 4c865b51-4357-4376-afb4-474e03ccb993
---
 zUtil_OAR/updateOar.m | 72 +++++++++++++++++++++----------------------
 1 file changed, 36 insertions(+), 36 deletions(-)

diff --git a/zUtil_OAR/updateOar.m b/zUtil_OAR/updateOar.m
index 08589720..59beb08a 100644
--- a/zUtil_OAR/updateOar.m
+++ b/zUtil_OAR/updateOar.m
@@ -35,7 +35,7 @@ else
 end
 
 if ~isempty(fieldnames(oarparameters))
-    names = fieldnames(oarparameters);
+    fnames = fieldnames(oarparameters);
 
     error_ids.terminated = [];
     error_ids.error      = [];
@@ -46,15 +46,18 @@ if ~isempty(fieldnames(oarparameters))
         %%%%%%%%%%%%%%%%%%%%%%%%%%
         if (~isempty(funcname))
             disp(['Looking for function named ' funcname ' ...'])
-            match = strfind(names, funcname);
+            match = strfind(fnames, funcname);
             jj = 0;
-            while (~isempty(match) && (jj < length(names)))
+            % XXX - Laura, what is this doing?? please comment!! looks like
+            % something very dirty. If you can avoid this kind of loops with
+            % while and jumping indexes, please do it! It gets very difficult to
+            % debug, in case of problems
+            while (~isempty(match) && (jj < length(fnames)))
                 jj = jj + 1;
 
                 if isempty(match{jj})
-                    names(jj) = [];
-                    k = jj;
-                    match(k) = [];
+                    fnames(jj) = [];
+                    match(jj) = [];
                     jj = 0;
                 end
             end
@@ -62,17 +65,17 @@ if ~isempty(fieldnames(oarparameters))
 
         if (strcmpi(action, 'delete'))
             % loop over functions
-            for jj = 1:length(names)
-                disp(['Deleting function ' names{jj} '...']);
-                arrayField = fieldnames(oarparameters.(names{jj}));
+            for jj = 1:length(fnames)
+                disp(['Deleting function ' fnames{jj} '...']);
+                arrayField = fieldnames(oarparameters.(fnames{jj}));
 
-                questionStr = ['Are you sure to remove the function ' names{jj} ' from OAR and oarparameters.mat? [y/n]'];
+                questionStr = ['Are you sure to remove the function ' fnames{jj} ' from OAR and oarparameters.mat? [y/n]'];
                 check = inputwdefault(questionStr, 'y');
                 if strcmpi(check, 'y')
                     fprintf('Deleting jobs from OAR... ')
                     cellfun(@(array) deleteOarArray(array(6:end)), arrayField);
                     fprintf('Done.\nRemoving information about the jobs... ')
-                    oarparameters = rmfield(oarparameters, names{jj});
+                    oarparameters = rmfield(oarparameters, fnames{jj});
                     save('oarparameters.mat', 'oarparameters', '-v7.3');
                     fprintf('Done.\n')
                 end
@@ -82,12 +85,12 @@ if ~isempty(fieldnames(oarparameters))
         elseif (strcmpi(action, 'update'))
 
             % loop over functions
-            for jj = 1:length(names)
-                arrays = fieldnames(oarparameters.(names{jj}));
-                arrayName = names{jj};
+            for jj = 1:length(fnames)
+                arrays = fieldnames(oarparameters.(fnames{jj}));
+                arrayName = fnames{jj};
 
-                for k = 1:length(arrays)
-                    arrayField = arrays{k};
+                for kk = 1:length(arrays)
+                    arrayField = arrays{kk};
                     [oarparameters, error_ids] = updateArray(oarparameters, arrayName, arrayField, error_ids);
                 end % end for k
             end % end for j
@@ -99,54 +102,52 @@ if ~isempty(fieldnames(oarparameters))
             print_structure(output,'output',false,true);
 
         else
-            disp(['Action ' action ' is not valid! Exiting...'])
-            return;
+            % Fail :)
+            error( 'OAR:wrong_argument', ...
+                    'Action %s is not valid!', action)
         end
 
     else
         %%%%%%%%%%%%%%%%%%%%%%%%%
         % look for array number %
         %%%%%%%%%%%%%%%%%%%%%%%%%
-
         if any( cellfun(@(logarray) strcmpi(action, logarray), {'err','out','log','params','oar'}) )
-            disp(['Looking for job number ' num2str(arrayid) '...'])
+            fprintf('Looking for job number "%d"...\n', arrayid)
             output = logviewOar(arrayid, action);
-            print_structure(output,'output',false,true);
-
+            print_structure(output, 'output', false, true);
         else
             % loop over functions
-            disp(['Looking for array number ' num2str(arrayid) '...'])
+            fprintf('Looking for array number "%d"...\n', arrayid)
 
-            for jj = 1:length(names)
-                arrayName = names{jj};
+            for jj = 1:length(fnames)
+                arrayName = fnames{jj};
 
                 % search array id(s) for each function
                 arrays = logviewOar(arrayName, [], false);
                 % check if arrayid exists
                 if ~isempty(find(arrays.(arrayName).arrays == arrayid, 1))
-
-                    arrayField = sprintf('array%d', arrayid);
+                    % Do we want to delete?
                     if strcmpi(action, 'delete')
-
-                        disp(['Deleting array n. ' arrayField '...']);
+                        disp(['Deleting array n. ' num2str(arrayid) '...']);
                         questionStr = ['Are you sure to remove array ' num2str(arrayid) ' from oarparameters.mat? [y/n]'];
                         check = inputwdefault(questionStr, 'y');
                         if strcmpi(check, 'y')
                             fprintf('Deleting jobs from OAR... ')
-                            deleteOarArray(arrayField);
+                            deleteOarArray(arrayid);
                             fprintf('Done.\nRemoving information about the jobs... ')
+
+                            arrayField = sprintf('array%d', arrayid);
                             oarparameters.(arrayName) = rmfield(oarparameters.(arrayName), arrayField);
                             save('oarparameters.mat', 'oarparameters', '-v7.3');
                             fprintf('Done.\n')
                         end
-
+                    % Do we want some update?
                     elseif strcmpi(action, 'update')
-
                         [oarparameters, error_ids] = updateArray(oarparameters, arrayName, arrayField, error_ids);
-
                     else
-                        disp(['Action ' action ' is not valid! Exiting...'])
-                        return;
+                        % Fail :)
+                        error( 'OAR:wrong_argument', ...
+                                'Action %s is not valid!', action)
                     end
                 end % end if arrayid exists
 
@@ -230,7 +231,6 @@ else
         elseif strcmpi(state, 'Error')
             error_ids.error = [error_ids.error; jobid];
         end
-
     end
 
     oarparameters.(arrayName).(arrayField).job = newJobs;
-- 
GitLab