be3a7e0781f152e03ac006f7f3329f3b4b489d87
[scilab.git] / scilab / modules / atoms / macros / atoms_internals / atomsInstallList.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 // Internal function
11
12 // Output arguments :
13
14 //   insList  : . List  of the changes caused by the installation of one or
15 //                more packages
16 //              . matrix ( n x 4 ) 
17 //              . mandatory
18 //              . example :
19 //                 !~  U  toolbox_4  1.0  !
20 //                 !                      !
21 //                 !~     toolbox_2  1.3  !
22 //                 !                      !
23 //                 !~     toolbox_1  1.9  !
24 //                 !                      !
25 //                 !~  U  toolbox_5  1.0  !
26
27 //   tree_out : . Dependency tree of the package (returned by atomsDepTreeFlat)
28 //              . struct
29 //              . mandatory
30 //              . Example :
31 //                   tree_out  = 
32 //                   toolbox_5 - 1.0: [1x1 struct]
33 //                   toolbox_4 - 1.0: [1x1 struct]
34 //                   toolbox_2 - 1.3: [1x1 struct]
35 //                   toolbox_1 - 1.9: [1x1 struct]
36
37 function [insList,depTree] = atomsInstallList(packages,section)
38         
39         insList = [];
40         depTree = struct();
41         
42         // Save the initial path
43         // =========================================================================
44         initialpath = pwd();
45         
46         // Get scilab version (needed for later)
47         // =========================================================================
48         sciversion = strcat(string(getversion("scilab")) + ".");
49         
50         // Check input parameters
51         // =========================================================================
52         
53         rhs = argn(2);
54         
55         if rhs > 2 then
56                 error(msprintf(gettext("%s: Wrong number of input arguments: at most %d expected.\n"),"atomsInstallList",2))
57         end
58         
59         // 1st input argument
60         
61         if type(packages) <> 10 then
62                 error(msprintf(gettext("%s: Wrong type for input argument #%d: String array expected.\n"),"atomsInstallList",1));
63         end
64         
65         packages = stripblanks(packages);
66         
67         // 2nd input input argument, section management :
68         //   - 1st case: section is not specified or is equal to "all", module 
69         //               is searched in both "user" and "allusers" section
70         //   - 2nd case: section is equal to "user", module is only searched in
71         //               the "user" section"
72         //   - 3rd case: section is equal to "allusers", module is only searched
73         //               in the "allusers" section"
74         
75         if rhs < 2 then
76                 section = "all";
77         
78         else
79                 
80                 if type(section) <> 10 then
81                         chdir(initialpath);
82                         error(msprintf(gettext("%s: Wrong type for input argument #%d: A single-string expected.\n"),"atomsInstallList",2));
83                 end
84                 
85                 if and(section<>["user","allusers","all"]) then
86                         chdir(initialpath);
87                         error(msprintf(gettext("%s: Wrong value for input argument #%d: ''user'' or ''allusers'' expected.\n"),"atomsInstallList",2));
88                 end
89                 
90         end
91         
92         // Loop on packages and to build the dependency tree
93         // =========================================================================
94         
95         for i=1:size(packages,"*")
96                 
97                 package = packages(i);
98                 
99                 if size(regexp(package,"/\s/") ,"*" ) > 1 then
100                         chdir(initialpath);
101                         error(msprintf(gettext("%s: Wrong value for input argument #%d: it must contain at most one space.\n"),"atomsInstallList",1));
102                 end
103                 
104                 if size(regexp(package,"/\s/") ,"*" ) == 0 then
105                         // install the most recent version of the package
106                         package_names(i)    = package;
107                         package_versions(i) = "";
108                 else
109                         // A version is specified
110                         space               = regexp(package,"/\s/");
111                         package_names(i)    = part(package,[1:space-1]);
112                         package_versions(i) = part(package,[space+1:length(package)]);
113                 end
114                 
115                 // Ok, The syntax is correct, Now check if it's a valid package
116                 if ~ atomsIsPackage(package_names(i),package_versions(i)) then
117                         if isempty(package_versions(i)) then
118                                 package_full_name = package_names(i);
119                         else
120                                 package_full_name = package_names(i)+" - "+package_versions(i);
121                         end
122                         chdir(initialpath);
123                         error(msprintf(gettext("%s: The package %s is not available.\n"),"atomsInstallList",package_full_name));
124                 end
125                 
126                 // Build the depencency tree
127                 [tree,version_out]  = atomsDepTreeFlat(package_names(i),package_versions(i));
128                 
129                 if (type(tree) == 4) & (~ tree) then
130                         chdir(initialpath);
131                         error(msprintf(gettext("%s: The dependency tree cannot be resolved.\n"),"atomsInstallList",1));
132                 end
133                 
134                 // Update the  package_versions(i) with the version returned by
135                 // atomsDepTreeFlat
136                 package_versions(i) = version_out;
137                 
138                 // Concatenate the tree with the existing one
139                 depTree = atomsCatTree( depTree , tree );
140         end
141         
142         // Add a field to detect later if it's the toolbox is automaticaly installed
143         // or if it's a user choice
144         // =========================================================================
145         
146         for i=1:size(package_names,"*")
147                 this_package_details                                = depTree(package_names(i)+" - "+package_versions(i));
148                 this_package_details("user_choice")                 = %T;
149                 depTree(package_names(i)+" - "+package_versions(i)) = this_package_details;
150         end
151         
152         // Now we have the list of package that have to be installed
153         // We have to check if
154         //  - each package is already installed
155         //  - If yes, Is it the most recent version
156         // =========================================================================
157         
158         mandatory_packages      = getfield(1,depTree);
159         mandatory_packages(1:2) = [];
160         
161         for i=1:size(mandatory_packages,"*")
162                 
163                 this_package_details = depTree(mandatory_packages(i));
164                 
165                 this_package_name    = this_package_details("Toolbox");
166                 this_package_version = this_package_details("Version");
167                 
168                 if isfield(this_package_details,"user_choice") & this_package_details("user_choice") then
169                         this_package_user_choice = "U"; // stand for User Choice
170                 else
171                         this_package_user_choice = ""; // stand for User Choice
172                 end
173                 
174                 to_install = %F;
175                 
176                 // Now, it's time to check if the module is installed or not :
177                 //   - 1st case: section is not specified or is equal to "all", module 
178                 //               is searched in both "user" and "allusers" section
179                 //   - 2nd case: section is equal to "user", module is only searched in
180                 //               the "user" section"
181                 //   - 3rd case: section is equal to "allusers", module is only searched
182                 //               in the "allusers" section"
183                 
184                 if atomsIsInstalled(this_package_name,[],section) then
185                         vers = atomsGetInstalledVers(this_package_name,section);
186                         if find( vers == this_package_version ) == [] then
187                                 to_install = %T;
188                         end
189                 else
190                         to_install = %T;
191                 end
192                 
193                 if to_install then
194                         insList = [ insList ; "+" this_package_user_choice this_package_name this_package_version ];
195                 else
196                         insList = [ insList ; "~" this_package_user_choice this_package_name this_package_version ];
197                 end
198                 
199         end
200         
201         // Go to the initial location
202         // =========================================================================
203         chdir(initialpath);
204         
205 endfunction