Atoms 0.2
[scilab.git] / scilab / modules / atoms / macros / atomsUninstall.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 // End user function
11
12 // Uninstallation of a toolbox
13
14 function result = atomsUninstall(packages,allusers)
15         
16         result = %F;
17         
18         // Get scilab version (needed for later)
19         // =========================================================================
20         sciversion = strcat(string(getversion('scilab')) + ".");
21         
22         // Check input parameters
23         // =========================================================================
24         
25         rhs = argn(2);
26         
27         if rhs < 1 | rhs > 2 then
28                 error(msprintf(gettext("%s: Wrong number of input arguments: %d to %d expected.\n"),"atomsUninstall",1,2))
29         end
30         
31         if type(packages) <> 10 then
32                 error(msprintf(gettext("%s: Wrong type for input argument #%d: String array expected.\n"),"atomsUninstall",1));
33         end
34         
35         packages = stripblanks(packages);
36         
37         // Operating system detection
38         // =========================================================================
39         
40         if ~MSDOS then
41                 OSNAME = unix_g('uname');
42                 MACOSX = (strcmpi(OSNAME,"darwin") == 0);
43                 LINUX  = (strcmpi(OSNAME,"linux") == 0);
44         else
45                 MACOSX = %F;
46                 LINUX  = %F;
47         end
48         
49         if MSDOS then
50                 OSNAME = "windows";
51         elseif LINUX then
52                 OSNAME = "linux";
53         elseif MACOSX then
54                 OSNAME = "macosx";
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                         error(msprintf(gettext("%s: Wrong type for input argument #%d: A boolean expected.\n"),"atomsUninstall",2));
73                 end
74                 
75                 // Check if we have the write access
76                 if allusers & ~ atomsAUWriteAccess() then
77                         error(msprintf(gettext("%s: You haven''t write access on this directory : %s.\n"),"atomsUninstall",2,pathconvert(SCI+"/.atoms")));
78                 end
79         end 
80         
81         // Loop on packages and to build the list of package to uninstall
82         // =========================================================================
83         
84         packagesToUninstall_W = []; // Wanted packages
85         packagesToUninstall_D = []; // Dependency packages
86         packagesToUninstall_B = []; // Broken packages
87         
88         for i=1:size(packages,"*")
89                 
90                 package = packages(i);
91                 
92                 if size(regexp(package,"/\s/") ,"*" ) > 1 then
93                         error(msprintf(gettext("%s: Wrong value for input argument #%d: it must contain at most one space.\n"),"atomsInstall",1));
94                 end
95                 
96                 if size(regexp(package,"/\s/") ,"*" ) == 0 then
97                         // Just the toolbox name is specified
98                         package_names(i)    = package;
99                         package_versions(i) = "";
100                 else
101                         // A version is specified
102                         space               = regexp(package,"/\s/");
103                         package_names(i)    = part(package,[1:space-1]);
104                         package_versions(i) = part(package,[space+1:length(package)]);
105                 end
106                 
107                 // Ok, The syntax is correct, Now check if the package is really installed
108                 if ~ atomsIsInstalled(package_names(i),package_versions(i)) then
109                         continue;
110                 end
111                 
112                 // get the list of the versions of this package to uninstall
113                 
114                 if isempty(package_versions(i)) then
115                         // uninstall all version of this toolbox
116                         this_package_versions = atomsGetInstalledVers(package_names(i));
117                 else
118                         // Just uninstall the specified version
119                         this_package_versions = package_versions(i);
120                 end
121                 
122                 // Loop on this version list
123                 
124                 for j=1:size(this_package_versions,"*")
125                         
126                         packagesToUninstall_W = [ packagesToUninstall_W ; atomsGetInstalledDetails( package_names(i),this_package_versions(j)) ];
127                         
128                         // Establish the dependency list to uninstall
129                         // =================================================================
130                         
131                         // Get the dependencies of this package
132                         this_package_child_deps = atomsGetDepChilds(package_names(i),this_package_versions(j));
133                         
134                         // Loop on dependency childs
135                         for k=1:size(this_package_child_deps(:,1),"*")
136                                 
137                                 // If this dependence is automatically installed, add it to the
138                                 // list of package to uninstall
139                                 
140                                 if atomsGetInstalledStatus( this_package_child_deps(k,1) , this_package_child_deps(k,2) ) == "A" then
141                                         packagesToUninstall_D = [ packagesToUninstall_D ; atomsGetInstalledDetails(this_package_child_deps(k,1),this_package_child_deps(k,2)) ];
142                                 end
143                                 
144                         end
145                         
146                         // Establish the broken list to uninstall
147                         // =================================================================
148                         
149                         // Get the dependencies of this package
150                         this_package_parent_deps = atomsGetDepParents(package_names(i),this_package_versions(j));
151                         
152                         // Loop on dependency parents
153                         for k=1:size(this_package_parent_deps(:,1),"*")
154                                 
155                                 // If this dependence is automatically installed, add it to the
156                                 // list of package to uninstall
157                                 packagesToUninstall_B = [ packagesToUninstall_B ; atomsGetInstalledDetails(this_package_deps(k,1) , this_package_deps(k,2)) ];
158                                 
159                         end
160                         
161                 end
162                 
163         end
164         
165         // Loop on packages to filter the list of package to uninstall
166         // =========================================================================
167         
168         // All wanted packages must be uninstalled
169         packagesToUninstall = packagesToUninstall_W;
170         
171         // For the moment, All dependency packages must be uninstalled
172         packagesToUninstall = [ packagesToUninstall ; packagesToUninstall_D ];
173         
174         // Now we have the list of package that have to be uninstalled
175         // =========================================================================
176         
177         for i=1:size(packagesToUninstall(:,1),"*")
178                 
179                 this_package_name      = packagesToUninstall(i,1);
180                 this_package_version   = packagesToUninstall(i,2);
181                 this_package_directory = packagesToUninstall(i,4);
182                 
183                 // Check if this_package_directory start with SCI or SCIHOME
184                 
185                 if (grep(this_package_directory,pathconvert(SCI)) == []) & ..
186                         (grep(this_package_directory,pathconvert(SCIHOME)) == []) then
187                         
188                         error(msprintf(gettext("%s: The directory of this package (%s-%s) is located neither in SCI nor in SCIHOME. For security resaon, ATOMS refuse to delete this directory.\n"),"atomsUninstall",packagesToUninstall(1,1),packagesToUninstall(1,2)));
189                 end
190                 
191                 uninstall_status = rmdir(this_package_directory,"s");
192                 
193                 if uninstall_status<>1 then
194                         error(msprintf( ..
195                                 gettext("%s: The directory of this package (%s-%s) cannot been deleted, please check if you have write access on this directory : %s.\n"),..
196                                 "atomsUninstall", ..
197                                 this_package_name, ..
198                                 this_package_version, ..
199                                 this_package_directory));
200                 end
201                 
202                 atomsInstallUnregister(this_package_name,this_package_version,allusers);
203                 
204         end
205         
206         result = %T;
207         
208 endfunction