2b98209633b53765aca4abbdbffb73c17916710a
[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         
47         rhs = argn(2);
48         
49         if rhs <> 2 then
50                 error(msprintf(gettext("%s: Wrong number of input arguments: %d expected.\n"),"atomsRemoveList",2))
51         end
52         
53         if type(packages) <> 10 then
54                 error(msprintf(gettext("%s: Wrong type for input argument #%d: String array expected.\n"),"atomsRemoveList",1));
55         end
56         
57         // Process the 2nd input argument : section
58         // Allusers can be equal to "user" or "allusers"
59         
60         if type(section) <> 10 then
61                 error(msprintf(gettext("%s: Wrong type for input argument #%d: A boolean or a single string expected.\n"),"atomsRemoveList",2));
62         end
63         
64         if and(section<>["user","allusers","all"]) then
65                 error(msprintf(gettext("%s: Wrong value for input argument #%d: ''user'',''allusers'' or ''all'' expected.\n"),"atomsRemoveList",2));
66         end
67         
68         // Loop on packages and to build the list of package to uninstall
69         // =========================================================================
70         
71         packages = stripblanks(packages);
72         
73         // First step : List the packages wanted by the user
74         
75         for i=1:size(packages,"*")
76                 
77                 package = packages(i);
78                 
79                 if size(regexp(package,"/\s/") ,"*" ) == 0 then
80                         // Just the toolbox name is specified
81                         package_names(i)    = package;
82                         package_versions(i) = "";
83                 else
84                         // A version is specified
85                         space               = regexp(package,"/\s/");
86                         package_names(i)    = part(package,[1:space-1]);
87                         package_versions(i) = part(package,[space+1:length(package)]);
88                 end
89                 
90                 // get the list of the versions of this package to uninstall
91                 
92                 if isempty(package_versions(i)) then
93                         // uninstall all version of this toolbox (if we have the right, of
94                         // course)
95                         this_package_versions = atomsGetInstalledVers(package_names(i),section);
96                 else
97                         // Just uninstall the specified version
98                         this_package_versions = package_versions(i);
99                 end
100                 
101                 for j=1:size(this_package_versions,"*")
102                         
103                         if section == "all" then
104                                 if atomsIsInstalled(package_names(i),this_package_versions(j),"allusers") then
105                                         remList = [ remList ; "-" "U" package_names(i) this_package_versions(j) "allusers" ];
106                                 end
107                                 if atomsIsInstalled(package_names(i),this_package_versions(j),"user") then
108                                         remList = [ remList ; "-" "U" package_names(i) this_package_versions(j) "user" ];
109                                 end
110                         else
111                                 remList = [ remList ; "-" "U" package_names(i) this_package_versions(j) section ];
112                         end
113                         
114                 end
115                 
116         end
117         
118         // Second Step : List the packages that depends of the uninstalled packages
119         // =========================================================================
120         
121         packages = remList;
122         for i=1:size(packages(:,1),"*")
123                 
124                 this_package_name    = packages(i,3);
125                 this_package_version = packages(i,4);
126                 this_package_section = packages(i,5);
127         
128                 // Get the parents of this toolbox
129                 // (inevitably removed, unless we have not the right)
130                 // ----------------------------------------------------
131                 
132                 this_package_parents = atomsGetDepParents(this_package_name,this_package_version);
133                 
134                 for j=1:size(this_package_parents(:,1),"*")
135                         
136                         this_parent_name    = this_package_parents(j,1);
137                         this_parent_version = this_package_parents(j,2);
138                         
139                         // Check if we have the right to remove this package
140                         // If not, tag it as Broken (for later)
141                         if section=="user" then
142                                 details = atomsGetInstalledDetails(this_parent_name,this_parent_version,section);
143                                 if details(1,3) == "allusers" then
144                                         remList = [ remList ; "~" "B" this_parent_name this_parent_version this_package_section ]; // B stands for "Broken"
145                                         continue
146                                 end
147                         end
148                         
149                         // Add this parent to the list
150                         if find(remList(:,3)+" - "+remList(:,4) == this_parent_name+" - "+this_parent_version) == [] then
151                                 remList = [ remList ; "-" "P" this_parent_name this_parent_version this_package_section ];  // P stands for "Parent"
152                         end
153                         
154                 end
155                 
156                 // Get the childs of this toolbox
157                 // ----------------------------------------------
158                 
159                 this_package_childs = atomsGetDepChilds(this_package_name,this_package_version);
160                 
161                 for j=1:size(this_package_childs(:,1),"*")
162                         
163                         this_child_name    = this_package_childs(j,1);
164                         this_child_version = this_package_childs(j,2);
165                         
166                         // Check if we have the right to remove this package
167                         // If not, Do not add it to the list
168                         if section=="user" then
169                                 details = atomsGetInstalledDetails(this_child_name,this_child_version,section);
170                                 if details(3) == "allusers" then
171                                         continue
172                                 end
173                         end
174                         
175                         if find(remList(:,3)+" - "+remList(:,4) == this_child_name+" - "+this_child_version) == [] then
176                                 remList = [ remList ; "-" "C" this_child_name this_child_version this_package_section ]; // C stands for "Child" 
177                         end
178                         
179                 end
180                 
181         end
182         
183         // Third Step : Loop on childs check if we uninstall it or not
184         // =========================================================================
185         
186         packages = remList(find(remList(:,2)=="C"),:);
187         
188         for i=1:size(remList(:,1),"*")
189                 
190                 // This is not a Child package :
191                 // => continue
192                 // ----------------------------------------------
193                 
194                 if remList(i,2) <> "C" then
195                         continue;
196                 end
197                 
198                 this_package_name    = remList(i,3);
199                 this_package_version = remList(i,4);
200                 
201                 // The package has been intentionnaly installed :
202                 // => Do not install it
203                 // ----------------------------------------------
204                 
205                 if atomsGetInstalledStatus(this_package_name,this_package_version,section) == "I" then
206                         remList(i,1) = "~";
207                 end
208                 
209                 // Get the parents of this toolbox
210                 // ----------------------------------------------
211                 
212                 this_package_parents = atomsGetDepParents(this_package_name,this_package_version);
213                 
214                 for j=1:size(this_package_parents(:,1),"*")
215                         
216                         if find(remList(:,3)+" - "+remList(:,4) == this_package_parents(j,1)+" - "+this_package_parents(j,2)) == [] then
217                                 // One of the parent is not the remove list
218                                 // => do not install it
219                                 remList(i,1) = "~";
220                                 continue;
221                         end
222                 end
223                 
224         end
225         
226 endfunction