ATOMS: homogenize function's profile
[scilab.git] / scilab / modules / atoms / macros / atomsLoad.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 // Load one or several toolboxes
11
12 function result = atomsLoad(packages,section)
13         
14         // Load Atoms Internals lib if it's not already loaded
15         // =========================================================================
16         if ~ exists("atomsinternalslib") then
17                 load("SCI/modules/atoms/macros/atoms_internals/lib");
18         end
19         
20         // Init the output argument
21         // =========================================================================
22         result = [];
23         
24         // Check input parameters
25         // =========================================================================
26         
27         rhs = argn(2);
28         
29         if rhs < 1 | rhs > 2 then
30                 error(msprintf(gettext("%s: Wrong number of input arguments: %d to %d expected.\n"),"atomsLoad",1,2))
31         end
32         
33         if type(packages) <> 10 then
34                 error(msprintf(gettext("%s: Wrong type for input argument #%d: String array expected.\n"),"atomsLoad",1));
35         end
36         
37         if size(packages(1,:),"*") > 2 then
38                 error(msprintf(gettext("%s: Wrong size for input argument #%d: mx1 or mx2 string matrix expected.\n"),"atomsLoad",1));
39         end
40         
41         packages = stripblanks(packages);
42         
43         // Allusers/user management
44         // =========================================================================
45         
46         if rhs < 2 then
47                 section = "all";
48         
49         else
50                 
51                 // Process the 2nd input argument : allusers
52                 // Allusers can be a boolean or equal to "user" or "allusers"
53                 
54                 if type(section) <> 10 then
55                         error(msprintf(gettext("%s: Wrong type for input argument #%d: A boolean or a single string expected.\n"),"atomsLoad",2));
56                 end
57                 
58                 if and(section<>["user","allusers","all"]) then
59                         error(msprintf(gettext("%s: Wrong value for input argument #%d: ''user'' or ''allusers'' or ''all'' expected.\n"),"atomsLoad",2));
60                 end
61                 
62         end
63         
64         // If package is a mx1 matrix, add a 2nd column with empty versions
65         // =========================================================================
66         
67         if size(packages(1,:),"*") == 1 then
68                 packages = [ packages emptystr(size(packages(:,1),"*"),1) ];
69         end
70         
71         // If only one input argument, define the version (The Most Recent Version)
72         // =========================================================================
73         
74         
75         for i=1:size(packages(:,1),"*")
76                 
77                 if isempty(packages(i,2)) then
78                         
79                         this_module_versions = atomsGetInstalledVers(packages(i,1),section);
80                         
81                         if isempty(this_module_versions) then
82                                 error(msprintf(gettext("%s: No version of the module ''%s'' is installed.\n"),"atomsLoad",packages(i,1)));
83                         else
84                                 packages(i,2) = this_module_versions(1);
85                         end
86                 
87                 else
88                         if ~atomsIsInstalled([packages(i,1) packages(i,2)],section) then
89                                 error(msprintf(gettext("%s: the module ''%s - %s'' is not installed.\n"),"atomsLoad",packages(i,1),packages(i,2)));
90                         end
91                 
92                 end
93                 
94         end
95         
96         // Get path of the toolboxes
97         // =========================================================================
98         path = atomsGetInstalledPath(packages);
99         
100         // Create the TMPDIR/atoms directory
101         // =========================================================================
102         if ~ isdir(TMPDIR+"/atoms") then
103                 status = mkdir( TMPDIR+"/atoms" );
104                 if status <> 1 then
105                         error(msprintf( ..
106                                 gettext("%s: The directory ""%s"" cannot been created, please check if you have write access on this directory.\n"), ..
107                                 "atomsLoad", ..
108                                 TMPDIR+"/atoms"));
109                 end
110         end
111         
112         // Verbose Mode ?
113         // =========================================================================
114         if strcmpi(atomsGetConfig("Verbose"),"True") == 0 then
115                 ATOMSVERBOSE = %T;
116         else
117                 ATOMSVERBOSE = %F;
118         end
119         
120         // Define the path of the loaded file
121         // =========================================================================
122         loaded_file = pathconvert(TMPDIR+"/atoms/loaded",%F);
123         
124         // Does the loaded file exist, if yes load it
125         // =========================================================================
126         nbAdd = 0;
127         if fileinfo(loaded_file) <> [] then
128                 loaded = mgetl(loaded_file);
129         else
130                 loaded = [];
131         end
132         
133         // Loop on packages gived by the user
134         // =========================================================================
135         
136         mandatory_packages      = struct();
137         mandatory_packages_name = struct();
138         mandatory_packages_mat  = [];
139         
140         for i=1:size(packages(:,1),"*")
141                 
142                 this_package_name    = packages(i,1);
143                 this_package_version = packages(i,2);
144                 this_package_path    = path(i);
145         
146                 // Check if the user try to load 2 versions of the same toolbox at the
147                 // same time
148                 // =====================================================================
149                 if size( find( this_package_name == packages(:,1) ) > 1 ) then
150                         this_versions = packages( find( this_package_name == packages(:,1) ) , 2 );
151                         for j=2:size(this_versions,"*")
152                                 if this_versions(j) <> this_versions(1) then
153                                         mprintf(gettext("%s: Several versions of a package (%s) cannot be loaded at the same scilab session :\n"),"atomsLoad",this_package_name);
154                                         mprintf(gettext("\t - You''ve asked ''%s - %s''\n"),this_package_name,this_versions(1));
155                                         mprintf(gettext("\t - You''ve asked ''%s - %s''\n"),this_package_name,this_versions(j));
156                                         mprintf("\n");
157                                         error("");
158                                 end
159                         end
160                 end
161                 
162                 // Check if this toolbox is already loaded
163                 // =====================================================================
164                 if atomsIsLoaded(packages(i,:)) then
165                         atomsDisp(msprintf("\tThe package %s (%s) is already loaded\n\n",this_package_name,this_package_version));
166                         continue;
167                 end
168                 
169                 // Check if another version of this toolbox is already loaded
170                 // =====================================================================
171                 [is_loaded,loaded_version] =  atomsIsLoaded(this_package_name);
172                 if is_loaded then
173                         error(msprintf(gettext("%s: Another version of the package %s is already loaded : %s\n"),"atomsLoad",this_package_name,loaded_version));
174                         continue;
175                 end
176                 
177                 mandatory_packages(this_package_name+" - "+this_package_version) = "asked_by_user";
178                 mandatory_packages_name(this_package_name) = this_package_version;
179                 mandatory_packages_mat = [ mandatory_packages_mat ; this_package_name this_package_version this_package_path ];
180                 
181         end
182         
183         // Fill the list of package to load
184         // =========================================================================
185         
186         for i=1:size(packages(:,1),"*")
187                 
188                 childs = atomsGetDepChilds(packages(i,:));
189                 
190                 for j=1:size( childs(:,1) , "*")
191                         
192                         // Check if it is already loaded
193                         // -------------------------------------------------------
194                         if atomsIsLoaded(childs(j,:)) then
195                                 continue;
196                         end
197                         
198                         // Check if another version of this package is already loaded
199                         // -------------------------------------------------------
200                         [is_loaded,loaded_version] =  atomsIsLoaded(childs(j,1));
201                         if is_loaded then
202                                 mprintf(gettext("%s: Several versions of a package (%s) cannot be loaded at the same scilab session :\n"),"atomsLoad",childs(j,1));
203                                 mprintf(gettext("\t - ''%s - %s'' is already loaded\n"),childs(j,1),loaded_version);
204                                 mprintf(gettext("\t - ''%s - %s'' is needed by ''%s - %s''\n"),childs(j,1),childs(j,2),packages(i,1),packages(i,2));
205                                 mprintf("\n");
206                                 error("");
207                         end
208                         
209                         // Check if it is already in the list
210                         // -------------------------------------------------------
211                         if isfield( mandatory_packages , childs(j,1)+" - "+childs(j,2) ) then
212                                 continue;
213                         end
214                         
215                         // Check if another version is already in the list
216                         // -------------------------------------------------------
217                         if isfield( mandatory_packages_name , childs(j,1) ) then
218                                 
219                                 // if it's not the name version => error
220                                 if mandatory_packages_name(childs(j,1)) <> childs(j,2) then
221                                         
222                                         mprintf(gettext("%s: Several versions of a package (%s) cannot be loaded at the same scilab session :\n"),"atomsLoad",childs(j,1));
223                                         mprintf(gettext("\t - ''%s - %s'' is needed by ''%s - %s''\n"),childs(j,1),childs(j,2),packages(i,1),packages(i,2));
224                                         
225                                         // The other version of the package is asked by the user
226                                         if mandatory_packages(childs(j,1)+" - "+mandatory_packages_name(childs(j,1))) == "asked_by_user" then
227                                                 mprintf(gettext("\t - You''ve asked ''%s - %s''\n"),childs(j,1),mandatory_packages_name(childs(j,1)));
228                                         
229                                         // The other version of the package is a need by another package
230                                         else
231                                                 mprintf(gettext("\t - ''%s - %s'' is needed by ''%s''\n"), .. 
232                                                         childs(j,1), .. // name
233                                                         mandatory_packages_name(childs(j,1)), .. // version
234                                                         mandatory_packages(childs(j,1)+" - "+mandatory_packages_name(childs(j,1))) .. // name - version
235                                                         );
236                                         end
237                                         
238                                         mprintf("\n");
239                                         error("");
240                                 end
241                         end
242                         
243                         // The child has passed the test, add it to the mandatory
244                         // packages to load
245                         // -------------------------------------------------------
246                         
247                         mandatory_packages(childs(j,1)+" - "+childs(j,2)) = packages(i,1)+" - "+packages(i,2);
248                         mandatory_packages_name(childs(j,1)) = childs(j,2);
249                         mandatory_packages_mat = [ mandatory_packages_mat ; childs(j,1) childs(j,2) atomsGetInstalledPath(childs(j,:),section) ];
250                         
251                 end
252         end
253         
254         // Libraries to resume
255         // =========================================================================
256         libs_resume = [];
257         
258         if ~ isempty(mandatory_packages_mat) then
259                 mprintf("\n");
260         end
261         
262         for i=1:size(mandatory_packages_mat(:,1),"*")
263                 
264                 this_package_name    = mandatory_packages_mat(i,1);
265                 this_package_version = mandatory_packages_mat(i,2);
266                 this_package_path    = mandatory_packages_mat(i,3);
267                 
268                 // Get the list of lib
269                 // =====================================================================
270                 libs_before = librarieslist();
271                 
272                 // Exec the loader
273                 // =====================================================================
274                 
275                 loader_file = pathconvert(this_package_path) + "loader.sce";
276                 
277                 if fileinfo(loader_file)==[] then
278                         error(msprintf(gettext("%s: The file ''%s'' doesn''t exist or is not read accessible.\n"),"atomsLoad",loader_file));
279                 end
280                 
281                 exec( loader_file );
282                 mprintf("\n");
283                 
284                 // Get the list of libraries (macros)
285                 // =====================================================================
286                 libs_after = librarieslist();
287                 
288                 // Loop on libs_after
289                 // =====================================================================
290                 for j=1:size(libs_after,"*")
291                         
292                         if find(libs_after(j) == libs_before) == [] then
293                                 libs_resume = [ libs_resume ; libs_after(j) ];
294                         end
295                 end
296                 
297                 // Fill the output argument
298                 // =====================================================================
299                 result = [ result ; mandatory_packages_mat(i,:) ];
300                 
301                 // fill the loaded matrix
302                 // =====================================================================
303                 if find(loaded == this_package_name + " - " + this_package_version) == [] then
304                         nbAdd  = nbAdd + 1;
305                         loaded = [ loaded ; this_package_name + " - " + this_package_version ];
306                 end
307                 
308         end
309         
310         // Apply changes
311         // =========================================================================
312         if nbAdd > 0 then
313                 mputl(loaded,loaded_file);
314         end
315         
316         // If libs_resume is empty, the job is done
317         // =========================================================================
318         if isempty(libs_resume) then
319                 return;
320         end
321         
322         // Build the resume cmd
323         // =========================================================================
324         
325         resume_cmd = "[";
326         
327         for i=1:size(libs_resume,"*")
328                 resume_cmd = resume_cmd + libs_resume(i);
329                 if i<size(libs_resume,"*") then
330                         resume_cmd = resume_cmd + ",";
331                 else
332                         resume_cmd = resume_cmd + "] = resume(";
333                 end
334         end
335         
336         for i=1:size(libs_resume,"*")
337                 resume_cmd = resume_cmd + libs_resume(i);
338                 if i<size(libs_resume,"*") then
339                         resume_cmd = resume_cmd + ",";
340                 else
341                         resume_cmd = resume_cmd + ");";
342                 end
343         end
344         
345         // Exec the resume cmd
346         // =========================================================================
347         execstr(resume_cmd);
348         
349 endfunction