d811b285d17aa1a7dbf8b4de0ce9cde0e09aa0e9
[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,section)
15         
16         // Load Atoms Internals lib if it's not already loaded
17         // =========================================================================
18         if ~ exists("atomsinternalslib") then
19                 load("SCI/modules/atoms/macros/atoms_internals/lib");
20         end
21         
22         result = [];
23         
24         // Save the initial path
25         // =========================================================================
26         initialpath = pwd();
27         
28         // Get scilab version (needed for later)
29         // =========================================================================
30         sciversion = strcat(string(getversion("scilab")) + ".");
31         
32         // Check input parameters
33         // =========================================================================
34         
35         rhs = argn(2);
36         
37         if rhs < 1 | rhs > 2 then
38                 error(msprintf(gettext("%s: Wrong number of input arguments: %d to %d expected.\n"),"atomsInstall",1,2))
39         end
40         
41         if type(packages) <> 10 then
42                 error(msprintf(gettext("%s: Wrong type for input argument #%d: String array expected.\n"),"atomsInstall",1));
43         end
44         
45         packages = stripblanks(packages);
46         
47         // Operating system detection
48         // =========================================================================
49         
50         if ~MSDOS then
51                 OSNAME = unix_g('uname');
52                 MACOSX = (strcmpi(OSNAME,"darwin") == 0);
53                 LINUX  = (strcmpi(OSNAME,"linux") == 0);
54         else
55                 MACOSX = %F;
56                 LINUX  = %F;
57         end
58         
59         if MSDOS then
60                 OSNAME = "windows";
61         elseif LINUX then
62                 OSNAME = "linux";
63         elseif MACOSX then
64                 OSNAME = "macosx";
65         end
66         
67         // Architecture detection
68         // =========================================================================
69         
70         [dynamic_info,static_info] = getdebuginfo();
71         arch_info  = static_info(grep(static_info,"/^Compiler Architecture:/","r"))
72         
73         if ~isempty(arch_info) & (regexp(arch_info,"/\sX64$/","o") <> []) then
74                 ARCH = "64";
75         else
76                 ARCH = "32";
77         end
78         
79         // Verbose Mode ?
80         // =========================================================================
81         if strcmpi(atomsGetConfig("Verbose"),"True") == 0 then
82                 ATOMSVERBOSE = %T;
83         else
84                 ATOMSVERBOSE = %F;
85         end
86         
87         // Allusers/user management
88         //   - If Allusers is equal to "allusers", packages will installed in the "allusers" section :
89         //       → SCI/contrib    : location of the modules
90         //       → SCI/.atoms     : ATOMS system files
91         //   - Otherwise, packages will installed in the "user" section :
92         //       → SCIHOME/atoms  : location of the modules
93         //       → SCIHOME/.atoms : location of the modules & ATOMS system files
94         // =========================================================================
95         
96         if rhs <= 1 then
97                 
98                 // By default: 
99                 //  → Install in the "allusers" section if we have the write access to
100                 //    SCI directory
101                 //  → Install in the "user" otherwise
102                 
103                 if atomsAUWriteAccess() then
104                         section = "allusers"; 
105                 else
106                         section = "user";
107                 end
108                 
109         else
110                 
111                 // Process the 2nd input argument : section
112                 // Allusers can be a boolean or equal to "user" or "allusers"
113                 
114                 if type(section) <> 10 then
115                         chdir(initialpath);
116                         error(msprintf(gettext("%s: Wrong type for input argument #%d: A single-string expected.\n"),"atomsInstall",2));
117                 end
118                 
119                 if and(section<>["user","allusers"]) then
120                         chdir(initialpath);
121                         error(msprintf(gettext("%s: Wrong value for input argument #%d: ''user'' or ''allusers'' expected.\n"),"atomsInstall",2));
122                 end
123                 
124                 // Check if we have the write access
125                 if (section=="allusers") & ~ atomsAUWriteAccess() then
126                         chdir(initialpath);
127                         error(msprintf(gettext("%s: You haven''t write access on this directory : %s.\n"),"atomsInstall",2,pathconvert(SCI+"/.atoms")));
128                 end
129         end
130         
131         // Create needed directories
132         // =========================================================================
133         atoms_system_directory  = atomsPath("system" ,section);
134         atoms_install_directory = atomsPath("install",section);
135         atoms_tmp_directory     = atomsPath("system" ,"session");
136         
137         if ~ isdir( atoms_system_directory ) & (mkdir( atoms_system_directory ) <> 1) then
138                 error(msprintf( ..
139                         gettext("%s: The directory ''%s'' cannot been created, please check if you have write access on this directory.\n"),..
140                         atoms_system_directory));
141         end
142         
143         if ~ isdir( atoms_install_directory ) & (mkdir( atoms_install_directory ) <> 1) then
144                 error(msprintf( ..
145                         gettext("%s: The directory ''%s'' cannot been created, please check if you have write access on this directory.\n"),..
146                         atoms_install_directory));
147         end
148         
149         if ~ isdir(atoms_tmp_directory) & (mkdir(atoms_tmp_directory) <> 1) then
150                 error(msprintf( ..
151                         gettext("%s: The directory ''%s'' cannot been created, please check if you have write access on this directory.\n"),..
152                         atoms_tmp_directory));
153         end
154         
155         // Define the "archives" directory path
156         // Create it if it's not exist
157         // =========================================================================
158         archives_directory = atoms_install_directory + "archives";
159         
160         if ~ isdir( archives_directory ) & (mkdir( archives_directory ) <> 1) then
161                 error(msprintf( ..
162                         gettext("%s: The directory ''%s'' cannot been created, please check if you have write access on this directory.\n"),..
163                         "atomsInstall", ..
164                         archives_directory));
165         end
166         
167         // "Archive" installation
168         // =========================================================================
169         
170         for i=1:size(packages,"*")
171                 
172                 this_package = packages(i);
173                 
174                 if ~ isempty(regexp(this_package,"/(\.tar\.gz|\.tgz|\.zip)$/","o")) then
175                         
176                         if fileinfo( this_package ) then
177                                 error(msprintf(gettext("%s: The file ''%s'' doesn''t exist or is not read accessible\n"),"atomsInstall",this_package));
178                         end
179                         
180                         tmp_dir = atomsExtract(this_package,atoms_tmp_directory);
181                         tmp_dir = pathconvert(atoms_tmp_directory+tmp_dir);
182                         
183                         if fileinfo( tmp_dir + "DESCRIPTION" ) then
184                                 error(msprintf(gettext("%s: DESCRIPTION file cannot be found in the package ''%s''\n"),"atomsInstall",this_package));
185                         end
186                         
187                         this_package_description = atomsDESCRIPTIONread(tmp_dir + "DESCRIPTION");
188                         
189                         // Get package name and version
190                         // -----------------------------------------------------------------
191                         
192                         this_package_name    = getfield(1,this_package_description);
193                         this_package_name    = this_package_name(3);
194                         
195                         this_package_version = getfield(1,this_package_description(this_package_name));
196                         this_package_version = this_package_version(3);
197                         
198                         // Save the extracted directory
199                         // -----------------------------------------------------------------
200                         
201                         this_package_description = atomsDESCRIPTIONaddField( .. 
202                                 this_package_description, ..
203                                 this_package_name,        ..
204                                 this_package_version,     ..
205                                 "extractedDirectory",     ..
206                                 tmp_dir);
207                         
208                         this_package_description = atomsDESCRIPTIONaddField( .. 
209                                 this_package_description, ..
210                                 this_package_name,        ..
211                                 this_package_version,     ..
212                                 "archiveFile",            ..
213                                 this_package);
214                                 
215                         this_package_description = atomsDESCRIPTIONaddField( .. 
216                                 this_package_description, ..
217                                 this_package_name,        ..
218                                 this_package_version,     ..
219                                 "fromRepository",         ..
220                                 "0");
221                         
222                         // Save the DESCRIPTION_archives
223                         // -----------------------------------------------------------------
224                         
225                         if fileinfo( atoms_tmp_directory + "DESCRIPTION_archives" )<>[] then
226                                 packages_description = atomsDESCRIPTIONread(atoms_tmp_directory+"DESCRIPTION_archives");
227                                 packages_description = atomsDESCRIPTIONcat(packages_description,this_package_description);
228                         else
229                                 packages_description = this_package_description;
230                         end
231                         
232                         atomsDESCRIPTIONwrite(packages_description,atoms_tmp_directory+"DESCRIPTION_archives");
233                         
234                         // change the packages var
235                         // -----------------------------------------------------------------
236                         packages(i) = this_package_name+" "+this_package_version;
237                         
238                         
239                 end
240                 
241         end
242         
243         // Force update the system informations
244         // =========================================================================
245         atomsGetTOOLBOXES(%T)
246         
247         // Get the install list
248         // =========================================================================
249         [install_package_list,dependency_tree] = atomsInstallList(packages,section);
250         
251         // Loop on install_package_list to print if a package has to be installed
252         // or not
253         // =========================================================================
254         for i=1:size(install_package_list(:,1),"*")
255                 if install_package_list(i,1) == "+" then
256                         atomsDisp(msprintf("\t%s (%s) will be installed\n\n",install_package_list(i,3),install_package_list(i,4)));
257                 elseif install_package_list(i,1) == "~" then
258                         atomsDisp(msprintf("\t%s (%s) is already installed and up-to-date\n\n",install_package_list(i,3),install_package_list(i,4)));
259                 end
260         end
261         
262         // Now really install the packages
263         // =========================================================================
264         
265         for i=1:size(install_package_list(:,1),"*")
266                 
267                 this_package_name    = install_package_list(i,3);
268                 this_package_version = install_package_list(i,4);
269                 
270                 this_package_details = dependency_tree(this_package_name+" - "+this_package_version);
271                 
272                 if install_package_list(i,1) <> "+" then
273                         continue;
274                 end
275                 
276                 atomsDisp(msprintf("\tInstalling %s (%s) ...",this_package_name,this_package_version));
277                 
278                 // Define the path of the directory where will be installed this toolbox
279                 // =====================================================================
280                 this_package_directory = atomsPath("install",section) + this_package_name + filesep();
281                 
282                 // Create the parent directory of this toolbox if it's not already exist
283                 // =====================================================================
284                 
285                 if ~isdir(this_package_directory) & (mkdir(this_package_directory)<>1) then
286                         chdir(initialpath);
287                         error(msprintf( ..
288                                 gettext("%s: The directory ""%s"" cannot been created, please check if you have write access on this directory.\n"),..
289                                 this_package_directory));
290                 end
291                 
292                 // "Repository" installation ; Download and Extract
293                 // =====================================================================
294                 
295                 if this_package_details("fromRepository") == "1" then
296                         
297                         // Define the path of the downloaded file
298                         // =================================================================
299                         
300                         if isfield(this_package_details,OSNAME+ARCH+"Name") then
301                                 OSTYPE = OSNAME+ARCH;
302                         else
303                                 OSTYPE = OSNAME;
304                         end
305                         
306                         fileout = pathconvert(this_package_directory+this_package_details(OSTYPE+"Name"),%F);
307                         filein  = this_package_details(OSTYPE+"Url");
308                         filemd5 = this_package_details(OSTYPE+"Md5");
309                         
310                         // Launch the download
311                         // =================================================================
312                         atomsDownload(filein,fileout,filemd5);
313                         
314                         // unarchive it
315                         // =================================================================
316                         this_package_details("extractedDirectory") = this_package_directory + atomsExtract(fileout,this_package_directory);
317                 end
318                 
319                 // Rename the created directory
320                 // =====================================================================
321                 
322                 if MSDOS then
323                         rename_cmd = "rename """+this_package_details("extractedDirectory")+""" """+this_package_version+"""";
324                 else
325                         rename_cmd = "mv """+this_package_details("extractedDirectory")+""" """+this_package_directory+this_package_version+"""";
326                 end
327                 
328                 [rep,stat]=unix_g(rename_cmd)
329                 
330                 if stat <> 0 then
331                         disp(rename_cmd);
332                         chdir(initialpath);
333                         error(msprintf(gettext("%s: Error while creating the directory ''%s''.\n"),"atomsInstall",pathconvert(this_package_directory+this_package_version)));
334                 end
335                 
336                 // Register the successfully installed package
337                 // =====================================================================
338                 
339                 if install_package_list(i,2) == "U" then
340                         // Intentionnaly Installed
341                         this_package_status = "I";
342                 else
343                         // Automaticaly installed
344                         this_package_status = "A";
345                 end
346                 
347                 atomsInstallRegister(this_package_name,this_package_version,this_package_status,section);
348                 
349                 // Autoload the toolbox unless precised
350                 // =====================================================================
351                 
352                 if ~ (atomsGetConfig("autoload") == "False") then
353                         // Add a package to the autoload list only if it's intentionnaly
354                         // installed
355                         if this_package_status=="I" then
356                                 atomsAutoloadAdd(this_package_name,this_package_version,section);
357                         end
358                 end
359                 
360                 // Move the archive file (.tar.gz or .zip file) to the archive directory
361                 // =====================================================================
362                 
363                 if this_package_details("fromRepository")=="1" then
364                         this_package_archive = fileout;
365                 else
366                         this_package_archive = this_package_details("archiveFile");
367                 end
368                 
369                 if copyfile( this_package_archive , archives_directory ) <> 1 then
370                         chdir(initialpath);
371                         error(msprintf(gettext("%s: Error while copying the file ''%s'' to the directory ''%s''.\n"),"atomsInstall",this_package_archive,archives_directory));
372                 end
373                 
374                 if this_package_details("fromRepository")=="1" then
375                         mdelete( fileout );
376                 end
377                 
378                 // Fill the result matrix
379                 // =====================================================================
380                 result = [ result ; atomsGetInstalledDetails(this_package_name,this_package_version) ];
381                 
382                 // "archive" installation : Save the description
383                 // =====================================================================
384                 
385                 if this_package_details("fromRepository")=="0" then
386                         
387                         DESCRIPTION_file = atoms_directory+"DESCRIPTION_archives";
388                         
389                         if isempty(fileinfo(atoms_directory+"DESCRIPTION_archives")) then
390                                 DESCRIPTION = struct();
391                         else
392                                 DESCRIPTION = atomsDESCRIPTIONread(DESCRIPTION_file);
393                         end
394                         
395                         DESCRIPTION = atomsDESCRIPTIONadd(DESCRIPTION,this_package_name,this_package_version,this_package_details);
396                         atomsDESCRIPTIONwrite(DESCRIPTION,DESCRIPTION_file);
397                         
398                 end
399                 
400                 // Sucess message if needed
401                 // =====================================================================
402                 
403                 atomsDisp(msprintf(" success\n\n"));
404                 
405         end
406         
407         // The TMPDIR DESCRIPTION_archives is no more needed
408         // =========================================================================
409         
410         if ~ isempty(fileinfo(atoms_tmp_directory + "DESCRIPTION_archives")) then
411                 mdelete(atoms_tmp_directory + "DESCRIPTION_archives");
412         end
413         
414         // Update the dependencies of packages that use another version of packages
415         // that have been installed
416         // =========================================================================
417         
418         for i=1:size( result(:,1) , "*" )
419                 
420                 packages_out = atomsUpdateDeps(result(i,1),result(i,2),section);
421                 
422                 if ATOMSVERBOSE then
423                         for j=1:size(packages_out(:,1),"*")
424                                 atomsDisp(msprintf("\t%s (%s) will now use the version %s of the package %s\n\n",packages_out(j,1),packages_out(j,2),result(i,1),result(i,2)));
425                         end
426                 end
427                 
428         end
429         
430         // Remove orphan packages
431         // =========================================================================
432         
433         orphan_list = atomsOrphanList(section);
434         for i=1:size( orphan_list(:,1) , "*" )
435                 atomsRemove( orphan_list(i,1) + " " + orphan_list(i,2) );
436         end
437         
438         // Go to the initial location
439         // =========================================================================
440         chdir(initialpath);
441         
442 endfunction