Atoms 0.2
[scilab.git] / scilab / modules / atoms / macros / atomsInstall.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 // Installation of a toolbox
13
14 function result = atomsInstall(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"),"atomsInstall",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"),"atomsInstall",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         // Install for all users or just for me ?
58         // =========================================================================
59         
60         if rhs == 1 then
61                 // By default, install for all users (if we have write access of course !)
62                 if atomsAUWriteAccess() then
63                         allusers = %T; 
64                 else
65                         allusers = %F;
66                 end
67         
68         else
69                 // Just check if it's a boolean
70                 if type(allusers) <> 4 then
71                         error(msprintf(gettext("%s: Wrong type for input argument #%d: A boolean expected.\n"),"atomsInstall",2));
72                 end
73                 
74                 // Check if we have the write access
75                 if allusers & ~ atomsAUWriteAccess() then
76                         error(msprintf(gettext("%s: You haven''t write access on this directory : %s.\n"),"atomsInstall",2,pathconvert(SCI+"/.atoms")));
77                 end
78         end
79         
80         // We are installing new package, so we have a net connection
81         // => update the package list
82         // =========================================================================
83         
84         atomsGetTOOLBOXES(%T);
85         
86         // Loop on packages and to build the dependency tree
87         // =========================================================================
88         
89         dependency_tree = struct();
90         
91         for i=1:size(packages,"*")
92                 
93                 package = packages(i);
94                 
95                 if size(regexp(package,"/\s/") ,"*" ) > 1 then
96                         error(msprintf(gettext("%s: Wrong value for input argument #%d: it must contain at most one space.\n"),"atomsInstall",1));
97                 end
98                 
99                 if size(regexp(package,"/\s/") ,"*" ) == 0 then
100                         // install the most recent version of the package
101                         package_names(i)    = package;
102                         package_versions(i) = "";
103                 else
104                         // A version is specified
105                         space               = regexp(package,"/\s/");
106                         package_names(i)    = part(package,[1:space-1]);
107                         package_versions(i) = part(package,[space+1:length(package)]);
108                 end
109                 
110                 // Ok, The syntax is correct, Now check if it's a valid package
111                 if ~ atomsIsPackage(package_names(i),package_versions(i)) then
112                         if isempty(package_versions(i)) then
113                                 package_full_name = package_names(i);
114                         else
115                                 package_full_name = package_names(i)+" - "+package_versions(i);
116                         end
117                         error(msprintf(gettext("%s: The package %s is not available.\n"),"atomsInstall",1,package_full_name));
118                 end
119                 
120                 // Build the depencency tree
121                 tree = atomsDependencyTree(package_names(i),package_versions(i));
122                 
123                 if (type(dependency_tree) == 4) & (~ dependency_tree) then
124                         error(msprintf(gettext("%s: The dependency tree cannot be resolved.\n"),"atomsInstall",1));
125                 end
126                 
127                 // Concatenate the tree with the existing one
128                 dependency_tree = atomsCatTree( dependency_tree , tree );
129                 
130         end
131         
132         // Now we have the list of package that have to be installed
133         // We have to check if
134         //  - each package is already installed
135         //  - If yes, Is it the most recent version
136         // =========================================================================
137         
138         mandatory_packages      = getfield(1,dependency_tree);
139         mandatory_packages(1:2) = [];
140         
141         for i=1:size(mandatory_packages,"*")
142                 
143                 this_package_details = dependency_tree(mandatory_packages(i));
144                 
145                 this_package_name    = this_package_details("Toolbox");
146                 this_package_version = this_package_details("Version");
147                 
148                 this_package_details("to_install") = %F; 
149                 
150                 if atomsIsInstalled(this_package_name) then
151                         vers = atomsGetInstalledVers(mandatory_packages(i));
152                         if atomsVersionCompare( vers(1) , this_package_version ) < 0 then
153                                 this_package_details("to_install") = %T;
154                         end
155                 else
156                         this_package_details("to_install") = %T;
157                 end
158                 
159                 dependency_tree(mandatory_packages(i)) = this_package_details;
160                 
161         end
162         
163         // Now really install the packages
164         // =========================================================================
165         
166         for i=1:size(mandatory_packages,"*")
167                 
168                 if ~ this_package_details("to_install") then
169                         continue;
170                 end
171                 
172                 this_package_details = dependency_tree(mandatory_packages(i));
173                 this_package_name    = this_package_details("Toolbox");
174                 this_package_version = this_package_details("Version");
175                 
176                 // Define the path of the directory where will be installed this toolbox
177                 // =====================================================================
178                 
179                 if allusers then
180                         atoms_directory = pathconvert(SCI+"/contrib/"+this_package_name);
181                 else
182                         atoms_directory = pathconvert(SCIHOME+"/atoms/"+this_package_name);
183                 end
184                 
185                 // Create the parent directory of this toolbox if it's not already exist
186                 // =====================================================================
187                 
188                 if ~ isdir( atoms_directory ) then
189                         status = mkdir( atoms_directory );
190                         if status <> 1 then
191                                 error(msprintf( ..
192                                         gettext("%s: The directory ""%s"" cannot been created, please check if you have write access on this directory.\n"),..
193                                         atoms_directory));
194                         end
195                 end
196                 
197                 // Define the path of the downloaded file
198                 // =====================================================================
199                 
200                 fileout = pathconvert(atoms_directory+this_package_details(OSNAME+"Name"),%F);
201                 filein  = this_package_details(OSNAME+"Url");
202                 
203                 // Get the package
204                 // =====================================================================
205                 
206                 [rep,stat,err] = unix_g("wget "+filein + " -O " + fileout)
207                 
208                 if stat ~= 0 then
209                         disp("wget "+ filein + " -O " + fileout);
210                         disp(err);
211                 end
212                 
213                 // Check the md5sum
214                 // =====================================================================
215                 
216                 filemd5 = getmd5(fileout);
217                 
218                 if filemd5 <> this_package_details(OSNAME+"Md5") then
219                         error(msprintf(gettext("%s: The downloaded binary file (%s) doesn''t check the MD5SUM.\n"),"atomsInstall",fileout));
220                 end
221                 
222                 // unarchive it
223                 // =====================================================================
224                 
225                 chdir( atoms_directory );
226                 
227                 // get the list of directories before unarchive
228                 dir_list_before = atomsListDir();
229                 
230                 if regexp(fileout,"/\.tar\.gz$/","o") <> [] then
231                         
232                         [rep,stat,err] = unix_g("tar xzf "+ fileout + " -C "+ atoms_directory );
233                         
234                         if stat ~= 0 then
235                                 disp("tar xzf "+ fileout + " -C "+ atoms_directory);
236                                 disp(err);
237                         end
238                         
239                 elseif regexp(fileout,"/\.zip$/","o") <> [] then
240                         
241                         [rep,stat,err] = unix_g("unzip "+ fileout);
242                         
243                         if stat ~= 0 then
244                                 disp("unzip "+ fileout);
245                                 disp(err);
246                         end
247                         
248                 end
249                 
250                 // get the list of directories after unarchive
251                 dir_list_after = atomsListDir();
252                 
253                 // Get the name of the created directory
254                 // =====================================================================
255                 
256                 unarchive_dir = "";
257                 
258                 for i=1:size(dir_list_after,"*")
259                         if find(dir_list_after(i) == dir_list_before) == [] then
260                                 unarchive_dir = dir_list_after(i);
261                                 break;
262                         end
263                 end
264                 
265                 // Rename the created directory
266                 // =====================================================================
267                 
268                 if MSDOS then
269                         rename_cmd = "rename";
270                 else
271                         rename_cmd = "mv";
272                 end
273                 
274                 rename_cmd = rename_cmd+" """+unarchive_dir+""" """+this_package_version+"""";
275                 
276                 [rep,stat]=unix_g(rename_cmd)
277                 
278                 if stat <> 0 then
279                         disp(rename_cmd);
280                         error(msprintf(gettext("%s: Error while creating the directory ''%s''.\n"),"atomsInstall",pathconvert(atoms_directory+this_package_version)));
281                 end
282                 
283                 // Register the successfully installed package
284                 // =====================================================================
285                 
286                 if find(package_names == this_package_name) == [] then
287                         // Automaticaly installed
288                         this_package_status = "A";
289                 else
290                         // Volontary Installed
291                         this_package_status = "I";
292                 end
293                 
294                 atomsInstallRegister(this_package_name,this_package_version,this_package_status,allusers);
295                 
296         end
297         
298         result = dependency_tree;
299         
300 endfunction
301
302
303 // =============================================================================
304 // Just get the list of the directories present in the current directory
305 // =============================================================================
306
307 function result = atomsListDir()
308         
309         result = [];
310         
311         items  = listfiles();
312         
313         // Loop on items
314         for i=1:size(items,"*")
315                 if isdir(items(i)) then
316                         result = [ result ; items(i) ];
317                 end
318         end
319         
320 endfunction