Better management of the dependencies
[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 = [];
17         
18         // Save the initial path
19         // =========================================================================
20         initialpath = pwd();
21         
22         // Get scilab version (needed for later)
23         // =========================================================================
24         sciversion = strcat(string(getversion("scilab")) + ".");
25         
26         // Check input parameters
27         // =========================================================================
28         
29         rhs = argn(2);
30         
31         if rhs < 1 | rhs > 2 then
32                 error(msprintf(gettext("%s: Wrong number of input arguments: %d to %d expected.\n"),"atomsInstall",1,2))
33         end
34         
35         if type(packages) <> 10 then
36                 error(msprintf(gettext("%s: Wrong type for input argument #%d: String array expected.\n"),"atomsInstall",1));
37         end
38         
39         packages = stripblanks(packages);
40         
41         // Operating system detection
42         // =========================================================================
43         
44         if ~MSDOS then
45                 OSNAME = unix_g('uname');
46                 MACOSX = (strcmpi(OSNAME,"darwin") == 0);
47                 LINUX  = (strcmpi(OSNAME,"linux") == 0);
48         else
49                 MACOSX = %F;
50                 LINUX  = %F;
51         end
52         
53         if MSDOS then
54                 OSNAME = "windows";
55         elseif LINUX then
56                 OSNAME = "linux";
57         elseif MACOSX then
58                 OSNAME = "macosx";
59         end
60         
61         // Verbose Mode ?
62         // =========================================================================
63         if strcmpi(atomsGetConfig("Verbose"),"True") == 0 then
64                 VERBOSE = %T;
65         else
66                 VERBOSE = %F;
67         end
68         
69         // Install for all users or just for me ?
70         // =========================================================================
71         
72         if rhs == 1 then
73                 // By default, install for all users (if we have write access of course !)
74                 if atomsAUWriteAccess() then
75                         allusers = %T; 
76                 else
77                         allusers = %F;
78                 end
79         
80         else
81                 // Just check if it's a boolean
82                 if type(allusers) <> 4 then
83                         chdir(initialpath);
84                         error(msprintf(gettext("%s: Wrong type for input argument #%d: A boolean expected.\n"),"atomsInstall",2));
85                 end
86                 
87                 // Check if we have the write access
88                 if allusers & ~ atomsAUWriteAccess() then
89                         chdir(initialpath);
90                         error(msprintf(gettext("%s: You haven''t write access on this directory : %s.\n"),"atomsInstall",2,pathconvert(SCI+"/.atoms")));
91                 end
92         end
93         
94         // Define the "archives" directory path
95         // Create it if it's not exist
96         // =========================================================================
97         
98         if allusers then
99                 archives_directory = pathconvert(SCI+"/contrib/archives");
100         else
101                 archives_directory = pathconvert(SCIHOME+"/atoms/archives");
102         end
103         
104         if ~ isdir( archives_directory ) then
105                 status = mkdir( archives_directory );
106                 if status <> 1 then
107                         chdir(initialpath);
108                         error(msprintf( ..
109                                 gettext("%s: The directory ""%s"" cannot been created, please check if you have write access on this directory.\n"),..
110                                 "atomsInstall", ..
111                                 archives_directory));
112                 end
113         end
114         
115         // Get the install list
116         // =========================================================================
117         [install_package_list,dependency_tree] = atomsInstallList(packages);
118         
119         // Loop on install_package_list to print if a package has to be installed
120         // or not
121         // =========================================================================
122         if VERBOSE 
123                 for i=1:size(install_package_list(:,1),"*")
124                         if install_package_list(i,1) == "+" then
125                                 mprintf("\t%s (%s) will be installed\n",install_package_list(i,3),install_package_list(i,4));
126                         elseif install_package_list(i,1) == "~" then
127                                 mprintf("\t%s (%s) is already installed and up-to-date\n",install_package_list(i,3),install_package_list(i,4));
128                         end
129                 end
130         end
131         
132         // Now really install the packages
133         // =========================================================================
134         
135         for i=1:size(install_package_list(:,1),"*")
136                 
137                 this_package_details = dependency_tree(install_package_list(i,3));
138                 this_package_name    = install_package_list(i,3);
139                 this_package_version = install_package_list(i,4);
140                 
141                 if install_package_list(i,1) <> "+" then
142                         continue;
143                 end
144                 
145                 if VERBOSE then
146                         mprintf("\tInstalling %s (%s) ...",this_package_name,this_package_version);
147                 end
148                 
149                 // Define the path of the directory where will be installed this toolbox
150                 // =====================================================================
151                 
152                 if allusers then
153                         atoms_directory = pathconvert(SCI+"/contrib/"+this_package_name);
154                 else
155                         atoms_directory = pathconvert(SCIHOME+"/atoms/"+this_package_name);
156                 end
157                 
158                 // Create the parent directory of this toolbox if it's not already exist
159                 // =====================================================================
160                 
161                 if ~ isdir( atoms_directory ) then
162                         status = mkdir( atoms_directory );
163                         if status <> 1 then
164                                 chdir(initialpath);
165                                 error(msprintf( ..
166                                         gettext("%s: The directory ""%s"" cannot been created, please check if you have write access on this directory.\n"),..
167                                         atoms_directory));
168                         end
169                 end
170                 
171                 // Define the path of the downloaded file
172                 // =====================================================================
173                 
174                 fileout = pathconvert(atoms_directory+this_package_details(OSNAME+"Name"),%F);
175                 filein  = this_package_details(OSNAME+"Url");
176                 
177                 // Get the package
178                 // =====================================================================
179                 
180                 if MSDOS then
181                         download_cmd =  getshortpathname(pathconvert(SCI+"/tools/curl/curl.exe",%F)) +" -s """+ filein + """ -o """ + fileout + """";
182                 else
183                         download_cmd = "wget "+ filein + " -O " + fileout;
184                 end
185                 
186                 [rep,stat,err] = unix_g(download_cmd)
187                 
188                 if stat ~= 0 then
189                         disp(download_cmd);
190                         disp(err);
191                 end
192                 
193                 // Check the md5sum
194                 // =====================================================================
195                 
196                 filemd5 = getmd5(fileout);
197                 
198                 if filemd5 <> this_package_details(OSNAME+"Md5") then
199                         chdir(initialpath);
200                         error(msprintf(gettext("%s: The downloaded binary file (%s) doesn''t check the MD5SUM.\n"),"atomsInstall",fileout));
201                 end
202                 
203                 // unarchive it
204                 // =====================================================================
205                 
206                 chdir( atoms_directory );
207                 
208                 // get the list of directories before unarchive
209                 dir_list_before = atomsListDir();
210                 
211                 if ( LINUX | MACOSX ) & regexp(fileout,"/\.tar\.gz$/","o") <> [] then
212                         
213                         extract_cmd = "tar xzf "+ fileout + " -C """+ atoms_directory + """";
214                         
215                 elseif regexp(fileout,"/\.zip$/","o") <> [] then
216                         
217                         if MSDOS then
218                                 extract_cmd = getshortpathname(pathconvert(SCI+"/tools/zip/unzip.exe",%F));
219                         else
220                                 extract_cmd = "unzip";
221                         end
222                         
223                         extract_cmd = extract_cmd + " -q """ + fileout + """ -d """ + pathconvert(atoms_directory,%F) +"""";
224                         
225                 else
226                         chdir(initialpath);
227                         error(msprintf(gettext("%s: internal error, the archive ""%s"" cannot be extracted on this operating system.\n"),"atomsInstall",fileout));
228                 
229                 end
230                 
231                 [rep,stat,err] = unix_g(extract_cmd);
232                 
233                 if stat ~= 0 then
234                         disp(extract_cmd);
235                         disp(err);
236                         chdir(initialpath);
237                         error(msprintf(gettext("%s: internal error, the extraction of the archive ""%s"" has failed.\n"),"atomsInstall",fileout));
238                 end
239                 
240                 // get the list of directories after unarchive
241                 dir_list_after = atomsListDir();
242                 
243                 // Get the name of the created directory
244                 // =====================================================================
245                 
246                 unarchive_dir = "";
247                 
248                 for j=1:size(dir_list_after,"*")
249                         if find(dir_list_after(j) == dir_list_before) == [] then
250                                 unarchive_dir = dir_list_after(j);
251                                 break;
252                         end
253                 end
254                 
255                 // Rename the created directory
256                 // =====================================================================
257                 
258                 if MSDOS then
259                         rename_cmd = "rename";
260                 else
261                         rename_cmd = "mv";
262                 end
263                 
264                 rename_cmd = rename_cmd+" """+unarchive_dir+""" """+this_package_version+"""";
265                 
266                 [rep,stat]=unix_g(rename_cmd)
267                 
268                 if stat <> 0 then
269                         disp(rename_cmd);
270                         chdir(initialpath);
271                         error(msprintf(gettext("%s: Error while creating the directory ''%s''.\n"),"atomsInstall",pathconvert(atoms_directory+this_package_version)));
272                 end
273                 
274                 // Register the successfully installed package
275                 // =====================================================================
276                 
277                 if install_package_list(i,2) == "U" then
278                         // Intentionnaly Installed
279                         this_package_status = "I";
280                 else
281                         // Automaticaly installed
282                         this_package_status = "A";
283                 end
284                 
285                 atomsInstallRegister(this_package_name,this_package_version,this_package_status,allusers);
286                 
287                 // Autoload the toolbox unless precised
288                 // =====================================================================
289                 
290                 if ~ (atomsGetConfig("autoload") == "False") then
291                         atomsAddAutoload(this_package_name,this_package_version,allusers);
292                 end
293                 
294                 // Move the archive file (.tar.gz or .zip file) to the archive directory
295                 // =====================================================================
296                 
297                 stat = copyfile( fileout , archives_directory );
298                 
299                 if stat <> 1 then
300                         chdir(initialpath);
301                         error(msprintf(gettext("%s: Error while copying the file ''%s'' to the directory ''%s''.\n"),"atomsInstall",fileout,archives_directory));
302                 end
303                 
304                 mdelete( fileout );
305                 
306                 // Fill the result matrix
307                 // =====================================================================
308                 
309                 result = [ result ; atomsGetInstalledDetails(this_package_name,this_package_version) ];
310                 
311                 // Sucess message if needed
312                 // =====================================================================
313                 
314                 if VERBOSE then
315                         mprintf(" success\n");
316                 end
317         end
318         
319         // Go to the initial location
320         // =========================================================================
321         chdir(initialpath);
322         
323 endfunction
324
325
326 // =============================================================================
327 // Just get the list of the directories present in the current directory
328 // =============================================================================
329
330 function result = atomsListDir()
331         
332         result = [];
333         
334         items  = listfiles();
335         
336         // Loop on items
337         for i=1:size(items,"*")
338                 if isdir(items(i)) then
339                         result = [ result ; items(i) ];
340                 end
341         end
342         
343 endfunction