ATOMS: homogenize function's profile
[scilab.git] / scilab / modules / atoms / macros / atoms_internals / atomsRemoveList.sci
1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2009 - DIGITEO - Pierre MARECHAL <pierre.marechal@scilab.org>
3 //
4 // This file must be used under the terms of the CeCILL.
5 // This source file is licensed as described in the file COPYING, which
6 // you should have received as part of this distribution.  The terms
7 // are also available at
8 // http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
9
10 // Internal function
11
12 // Return a matrix that list the changes caused by the uninstallation of one or
13 // more packages
14
15 // -->A = atomsRemoveList('toolbox_2',%F)
16 //  A  =
17 //  
18 // !-  U  toolbox_2  1.5  !
19 // !                      !
20 // !-  U  toolbox_2  1.3  !
21 // !                      !
22 // !-  B  toolbox_3  1.6  !
23 // !                      !
24 // !-  B  toolbox_5  1.0  !
25 // !                      !
26 // !-  B  toolbox_4  1.0  !
27
28 // -  : The package will be removed
29 // ~  : The package will be keeped
30
31 // U  : The package is intentionnaly removed
32 // P  : The package is a parent of one package
33 // C  : The package is a child of one package
34 // B  : The package will be broken (It's a parent but cannot be uninstall)
35
36 function remList = atomsRemoveList(packages,section)
37         
38         remList = [];
39         
40         // Save the initial path
41         // =========================================================================
42         initialpath = pwd();
43         
44         // Check input parameters
45         // =========================================================================
46         rhs = argn(2);
47         
48         // Input argument number
49         // =========================================================================
50         
51         if rhs <> 2 then
52                 error(msprintf(gettext("%s: Wrong number of input arguments: %d expected.\n"),"atomsRemoveList",2))
53         end
54         
55         // 1st input argument
56         // =========================================================================
57         
58         if type(packages) <> 10 then
59                 error(msprintf(gettext("%s: Wrong type for input argument #%d: String array expected.\n"),"atomsRemoveList",1));
60         end
61         
62         if size(packages(1,:),"*") > 2 then
63                 error(msprintf(gettext("%s: Wrong size for input argument #%d: mx1 or mx2 string matrix expected.\n"),"atomsRemoveList",1));
64         end
65         
66         // 2nd input argument
67         // =========================================================================
68         
69         if type(section) <> 10 then
70                 error(msprintf(gettext("%s: Wrong type for input argument #%d: A boolean or a single string expected.\n"),"atomsRemoveList",2));
71         end
72         
73         if and(section<>["user","allusers","all"]) then
74                 error(msprintf(gettext("%s: Wrong value for input argument #%d: ''user'',''allusers'' or ''all'' expected.\n"),"atomsRemoveList",2));
75         end
76         
77         // Remove leading and trailing whitespace
78         // =========================================================================
79         packages = stripblanks(packages);
80         
81         // If mx1 matrix, add a 2nd column with empty versions
82         // =========================================================================
83         if size(packages(1,:),"*") == 1 then
84                 packages = [ packages emptystr(size(packages(:,1),"*"),1) ];
85         end
86         
87         // Loop on packages and to build the list of package to remove
88         // =========================================================================
89         
90         // First step : List the packages wanted by the user
91         
92         for i=1:size(packages(:,1),"*")
93                 
94                 package_names(i)    = packages(i,1);
95                 package_versions(i) = packages(i,2);
96                 
97                 // get the list of the versions of this package to uninstall
98                 
99                 if isempty(package_versions(i)) then
100                         // uninstall all version of this toolbox (if we have the right, of
101                         // course)
102                         this_package_versions = atomsGetInstalledVers(package_names(i),section);
103                 else
104                         // Just uninstall the specified version
105                         this_package_versions = package_versions(i);
106                 end
107                 
108                 for j=1:size(this_package_versions,"*")
109                         
110                         if section == "all" then
111                                 if atomsIsInstalled([package_names(i) this_package_versions(j)],"allusers") then
112                                         remList = [ remList ; "-" "U" package_names(i) this_package_versions(j) "allusers" ];
113                                 end
114                                 if atomsIsInstalled([package_names(i) this_package_versions(j)],"user") then
115                                         remList = [ remList ; "-" "U" package_names(i) this_package_versions(j) "user" ];
116                                 end
117                         else
118                                 remList = [ remList ; "-" "U" package_names(i) this_package_versions(j) section ];
119                         end
120                         
121                 end
122                 
123         end
124         
125         // Second Step : List the packages that depends of the uninstalled packages
126         // =========================================================================
127         
128         packages = remList;
129         for i=1:size(packages(:,1),"*")
130                 
131                 this_package_name    = packages(i,3);
132                 this_package_version = packages(i,4);
133                 this_package_section = packages(i,5);
134         
135                 // Get the parents of this toolbox
136                 // (inevitably removed, unless we have not the right)
137                 // ----------------------------------------------------
138                 
139                 this_package_parents = atomsGetDepParents([this_package_name this_package_version]);
140                 
141                 for j=1:size(this_package_parents(:,1),"*")
142                         
143                         this_parent_name    = this_package_parents(j,1);
144                         this_parent_version = this_package_parents(j,2);
145                         
146                         // Check if we have the right to remove this package
147                         // If not, tag it as Broken (for later)
148                         if section=="user" then
149                                 details = atomsGetInstalledDetails(this_package_parents(j,:),section);
150                                 if details(1,3) == "allusers" then
151                                         remList = [ remList ; "~" "B" this_parent_name this_parent_version this_package_section ]; // B stands for "Broken"
152                                         continue
153                                 end
154                         end
155                         
156                         // Add this parent to the list
157                         if find(remList(:,3)+" - "+remList(:,4) == this_parent_name+" - "+this_parent_version) == [] then
158                                 remList = [ remList ; "-" "P" this_parent_name this_parent_version this_package_section ];  // P stands for "Parent"
159                         end
160                         
161                 end
162                 
163                 // Get the childs of this toolbox
164                 // ----------------------------------------------
165                 
166                 this_package_childs = atomsGetDepChilds([this_package_name this_package_version]);
167                 
168                 for j=1:size(this_package_childs(:,1),"*")
169                         
170                         this_child_name    = this_package_childs(j,1);
171                         this_child_version = this_package_childs(j,2);
172                         
173                         // Check if we have the right to remove this package
174                         // If not, Do not add it to the list
175                         if section=="user" then
176                                 details = atomsGetInstalledDetails(this_package_childs(j,:),section);
177                                 if details(3) == "allusers" then
178                                         continue
179                                 end
180                         end
181                         
182                         if find(remList(:,3)+" - "+remList(:,4) == this_child_name+" - "+this_child_version) == [] then
183                                 remList = [ remList ; "-" "C" this_child_name this_child_version this_package_section ]; // C stands for "Child" 
184                         end
185                         
186                 end
187                 
188         end
189         
190         // Third Step : Loop on childs check if we uninstall it or not
191         // =========================================================================
192         
193         packages = remList(find(remList(:,2)=="C"),:);
194         
195         for i=1:size(remList(:,1),"*")
196                 
197                 // This is not a Child package :
198                 // => continue
199                 // ----------------------------------------------
200                 
201                 if remList(i,2) <> "C" then
202                         continue;
203                 end
204                 
205                 this_package_name    = remList(i,3);
206                 this_package_version = remList(i,4);
207                 
208                 // The package has been intentionnaly installed :
209                 // => Do not install it
210                 // ----------------------------------------------
211                 
212                 if atomsGetInstalledStatus([this_package_name this_package_version],section) == "I" then
213                         remList(i,1) = "~";
214                 end
215                 
216                 // Get the parents of this toolbox
217                 // ----------------------------------------------
218                 
219                 this_package_parents = atomsGetDepParents([this_package_name this_package_version]);
220                 
221                 for j=1:size(this_package_parents(:,1),"*")
222                         
223                         if find(remList(:,3)+" - "+remList(:,4) == this_package_parents(j,1)+" - "+this_package_parents(j,2)) == [] then
224                                 // One of the parent is not the remove list
225                                 // => do not install it
226                                 remList(i,1) = "~";
227                                 continue;
228                         end
229                 end
230                 
231         end
232         
233 endfunction