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