- installed packages and dependencies are now saved into binary files
[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_name    = install_package_list(i,3);
138                 this_package_version = install_package_list(i,4);
139                 
140                 this_package_details = dependency_tree(this_package_name+" - "+this_package_version);
141                 
142                 if install_package_list(i,1) <> "+" then
143                         continue;
144                 end
145                 
146                 if VERBOSE then
147                         mprintf("\tInstalling %s (%s) ...",this_package_name,this_package_version);
148                 end
149                 
150                 // Define the path of the directory where will be installed this toolbox
151                 // =====================================================================
152                 
153                 if allusers then
154                         atoms_directory = pathconvert(SCI+"/contrib/"+this_package_name);
155                 else
156                         atoms_directory = pathconvert(SCIHOME+"/atoms/"+this_package_name);
157                 end
158                 
159                 // Create the parent directory of this toolbox if it's not already exist
160                 // =====================================================================
161                 
162                 if ~ isdir( atoms_directory ) then
163                         status = mkdir( atoms_directory );
164                         if status <> 1 then
165                                 chdir(initialpath);
166                                 error(msprintf( ..
167                                         gettext("%s: The directory ""%s"" cannot been created, please check if you have write access on this directory.\n"),..
168                                         atoms_directory));
169                         end
170                 end
171                 
172                 // Define the path of the downloaded file
173                 // =====================================================================
174                 
175                 fileout = pathconvert(atoms_directory+this_package_details(OSNAME+"Name"),%F);
176                 filein  = this_package_details(OSNAME+"Url");
177                 
178                 // Get the package
179                 // =====================================================================
180                 
181                 if MSDOS then
182                         download_cmd =  getshortpathname(pathconvert(SCI+"/tools/curl/curl.exe",%F)) +" -s """+ filein + """ -o """ + fileout + """";
183                 else
184                         download_cmd = "wget "+ filein + " -O " + fileout;
185                 end
186                 
187                 [rep,stat,err] = unix_g(download_cmd)
188                 
189                 if stat ~= 0 then
190                         disp(download_cmd);
191                         disp(err);
192                 end
193                 
194                 // Check the md5sum
195                 // =====================================================================
196                 
197                 filemd5 = getmd5(fileout);
198                 
199                 if filemd5 <> this_package_details(OSNAME+"Md5") then
200                         chdir(initialpath);
201                         error(msprintf(gettext("%s: The downloaded binary file (%s) doesn''t check the MD5SUM.\n"),"atomsInstall",fileout));
202                 end
203                 
204                 // unarchive it
205                 // =====================================================================
206                 
207                 chdir( atoms_directory );
208                 
209                 // get the list of directories before unarchive
210                 dir_list_before = atomsListDir();
211                 
212                 if ( LINUX | MACOSX ) & regexp(fileout,"/\.tar\.gz$/","o") <> [] then
213                         
214                         extract_cmd = "tar xzf "+ fileout + " -C """+ atoms_directory + """";
215                         
216                 elseif regexp(fileout,"/\.zip$/","o") <> [] then
217                         
218                         if MSDOS then
219                                 extract_cmd = getshortpathname(pathconvert(SCI+"/tools/zip/unzip.exe",%F));
220                         else
221                                 extract_cmd = "unzip";
222                         end
223                         
224                         extract_cmd = extract_cmd + " -q """ + fileout + """ -d """ + pathconvert(atoms_directory,%F) +"""";
225                         
226                 else
227                         chdir(initialpath);
228                         error(msprintf(gettext("%s: internal error, the archive ""%s"" cannot be extracted on this operating system.\n"),"atomsInstall",fileout));
229                 
230                 end
231                 
232                 [rep,stat,err] = unix_g(extract_cmd);
233                 
234                 if stat ~= 0 then
235                         disp(extract_cmd);
236                         disp(err);
237                         chdir(initialpath);
238                         error(msprintf(gettext("%s: internal error, the extraction of the archive ""%s"" has failed.\n"),"atomsInstall",fileout));
239                 end
240                 
241                 // get the list of directories after unarchive
242                 dir_list_after = atomsListDir();
243                 
244                 // Get the name of the created directory
245                 // =====================================================================
246                 
247                 unarchive_dir = "";
248                 
249                 for j=1:size(dir_list_after,"*")
250                         if find(dir_list_after(j) == dir_list_before) == [] then
251                                 unarchive_dir = dir_list_after(j);
252                                 break;
253                         end
254                 end
255                 
256                 // Rename the created directory
257                 // =====================================================================
258                 
259                 if MSDOS then
260                         rename_cmd = "rename";
261                 else
262                         rename_cmd = "mv";
263                 end
264                 
265                 rename_cmd = rename_cmd+" """+unarchive_dir+""" """+this_package_version+"""";
266                 
267                 [rep,stat]=unix_g(rename_cmd)
268                 
269                 if stat <> 0 then
270                         disp(rename_cmd);
271                         chdir(initialpath);
272                         error(msprintf(gettext("%s: Error while creating the directory ''%s''.\n"),"atomsInstall",pathconvert(atoms_directory+this_package_version)));
273                 end
274                 
275                 // Register the successfully installed package
276                 // =====================================================================
277                 
278                 if install_package_list(i,2) == "U" then
279                         // Intentionnaly Installed
280                         this_package_status = "I";
281                 else
282                         // Automaticaly installed
283                         this_package_status = "A";
284                 end
285                 
286                 atomsInstallRegister(this_package_name,this_package_version,this_package_status,allusers);
287                 
288                 // Autoload the toolbox unless precised
289                 // =====================================================================
290                 
291                 if ~ (atomsGetConfig("autoload") == "False") then
292                         atomsAddAutoload(this_package_name,this_package_version,allusers);
293                 end
294                 
295                 // Move the archive file (.tar.gz or .zip file) to the archive directory
296                 // =====================================================================
297                 
298                 stat = copyfile( fileout , archives_directory );
299                 
300                 if stat <> 1 then
301                         chdir(initialpath);
302                         error(msprintf(gettext("%s: Error while copying the file ''%s'' to the directory ''%s''.\n"),"atomsInstall",fileout,archives_directory));
303                 end
304                 
305                 mdelete( fileout );
306                 
307                 // Fill the result matrix
308                 // =====================================================================
309                 
310                 result = [ result ; atomsGetInstalledDetails(this_package_name,this_package_version) ];
311                 
312                 // Sucess message if needed
313                 // =====================================================================
314                 
315                 if VERBOSE then
316                         mprintf(" success\n");
317                 end
318         end
319         
320         // Go to the initial location
321         // =========================================================================
322         chdir(initialpath);
323         
324 endfunction
325
326
327 // =============================================================================
328 // Just get the list of the directories present in the current directory
329 // =============================================================================
330
331 function result = atomsListDir()
332         
333         result = [];
334         
335         items  = listfiles();
336         
337         // Loop on items
338         for i=1:size(items,"*")
339                 if isdir(items(i)) then
340                         result = [ result ; items(i) ];
341                 end
342         end
343         
344 endfunction