Better management of the dependencies
[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 // Return a matrix that list the changes caused by the installation of one or
13 // more packages
14
15 // !~  U  toolbox_4  1.0  !
16 // !                      !
17 // !~     toolbox_2  1.3  !
18 // !                      !
19 // !~     toolbox_1  1.9  !
20 // !                      !
21 // !~  U  toolbox_5  1.0  !
22
23
24 function [insList,depTree] = atomsInstallList(packages)
25         
26         insList = [];
27         depTree = struct();
28         
29         // Save the initial path
30         // =========================================================================
31         initialpath = pwd();
32         
33         // Get scilab version (needed for later)
34         // =========================================================================
35         sciversion = strcat(string(getversion("scilab")) + ".");
36         
37         // Check input parameters
38         // =========================================================================
39         
40         rhs = argn(2);
41         
42         if rhs <> 1 then
43                 error(msprintf(gettext("%s: Wrong number of input arguments: %d expected.\n"),"atomsInstallList",1))
44         end
45         
46         if type(packages) <> 10 then
47                 error(msprintf(gettext("%s: Wrong type for input argument #%d: String array expected.\n"),"atomsInstallList",1));
48         end
49         
50         packages = stripblanks(packages);
51         
52         // Loop on packages and to build the dependency tree
53         // =========================================================================
54         
55         for i=1:size(packages,"*")
56                 
57                 package = packages(i);
58                 
59                 if size(regexp(package,"/\s/") ,"*" ) > 1 then
60                         chdir(initialpath);
61                         error(msprintf(gettext("%s: Wrong value for input argument #%d: it must contain at most one space.\n"),"atomsInstallList",1));
62                 end
63                 
64                 if size(regexp(package,"/\s/") ,"*" ) == 0 then
65                         // install the most recent version of the package
66                         package_names(i)    = package;
67                         package_versions(i) = "";
68                 else
69                         // A version is specified
70                         space               = regexp(package,"/\s/");
71                         package_names(i)    = part(package,[1:space-1]);
72                         package_versions(i) = part(package,[space+1:length(package)]);
73                 end
74                 
75                 // Ok, The syntax is correct, Now check if it's a valid package
76                 if ~ atomsIsPackage(package_names(i),package_versions(i)) then
77                         if isempty(package_versions(i)) then
78                                 package_full_name = package_names(i);
79                         else
80                                 package_full_name = package_names(i)+" - "+package_versions(i);
81                         end
82                         chdir(initialpath);
83                         error(msprintf(gettext("%s: The package %s is not available.\n"),"atomsInstallList",package_full_name));
84                 end
85                 
86                 // Build the depencency tree
87                 tree = atomsDependencyTree(package_names(i),package_versions(i));
88                 
89                 if (type(depTree) == 4) & (~ depTree) then
90                         chdir(initialpath);
91                         error(msprintf(gettext("%s: The dependency tree cannot be resolved.\n"),"atomsInstallList",1));
92                 end
93                 
94                 // Concatenate the tree with the existing one
95                 depTree = atomsCatTree( depTree , tree );
96         end
97         
98         // Add a field to detect later if it's the toolbox is automaticaly installed
99         // or if it's a user choice
100         // =========================================================================
101         
102         for i=1:size(package_names,"*")
103                 this_package_details                = depTree(package_names(i));
104                 this_package_details("user_choice") = %T;
105                 depTree(package_names(i))           = this_package_details;
106         end
107         
108         // Now we have the list of package that have to be installed
109         // We have to check if
110         //  - each package is already installed
111         //  - If yes, Is it the most recent version
112         // =========================================================================
113         
114         mandatory_packages      = getfield(1,depTree);
115         mandatory_packages(1:2) = [];
116         
117         for i=1:size(mandatory_packages,"*")
118                 this_package_details = depTree(mandatory_packages(i));
119                 
120                 this_package_name    = this_package_details("Toolbox");
121                 this_package_version = this_package_details("Version");
122                 
123                 if isfield(this_package_details,"user_choice") & this_package_details("user_choice") then
124                         this_package_user_choice = "U"; // stand for User Choice
125                 else
126                         this_package_user_choice = ""; // stand for User Choice
127                 end
128                 
129                 to_install = %F; 
130                 
131                 if atomsIsInstalled(this_package_name) then
132                         vers = atomsGetInstalledVers(mandatory_packages(i));
133                         if find( vers == this_package_version ) == [] then
134                                 to_install = %T;
135                         end
136                 else
137                         to_install = %T;
138                 end
139                 
140                 if to_install then
141                         insList = [ insList ; "+" this_package_user_choice this_package_name this_package_version ];
142                 else
143                         insList = [ insList ; "~" this_package_user_choice this_package_name this_package_version ];
144                 end
145                 
146         end
147         
148         // Go to the initial location
149         // =========================================================================
150         chdir(initialpath);
151         
152 endfunction