* Bug #13108 fixed - Atoms: updates spread and configurable
[scilab.git] / scilab / modules / atoms / macros / atoms_internals / atomsDESCRIPTIONget.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 // Copyright (C) 2011-2012 - DIGITEO - Allan CORNET
4 // Copyright (C) 2012 - Samuel GOUGEON
5 //
6 // This file must be used under the terms of the CeCILL.
7 // This source file is licensed as described in the file COPYING, which
8 // you should have received as part of this distribution.  The terms
9 // are also available at
10 // http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
11
12 // Internal function
13
14 // Return the full description of the packages present in the differents
15 // repositories and the installed packages
16
17 function [packages,categories_flat,categories] = atomsDESCRIPTIONget(update)
18
19     // Check input parameters
20     // =========================================================================
21
22     rhs  = argn(2);
23
24     if rhs > 1 then
25         error(msprintf(gettext("%s: Wrong number of input argument: at most %d expected.\n"),"atomsDESCRIPTIONget",1));
26     end
27
28     if (rhs == 1) & (type(update) <> 4) then
29         error(msprintf(gettext("%s: Wrong type for input argument #%d: Boolean expected.\n"),"atomsDESCRIPTIONget",1));
30     end
31
32     // packages file path definition
33     // =========================================================================
34     atoms_path_all_users = atomsPath("system", "allusers");
35     atoms_path_user = atomsPath("system", "user");
36
37     filename_test_rw = "__ATOMS_RW__";
38     if isfile(atoms_path_all_users + filename_test_rw) then
39         mdelete(atoms_path_all_users + filename_test_rw);
40     end
41
42     // Have the user write access to the Scilab distribution ?
43     [id, err] = mopen(atoms_path_all_users + filename_test_rw, "wt");
44     if err == 0 then // The user has write access to the Scilab distribution
45         mclose(id);
46         mdelete(atoms_path_all_users + filename_test_rw);
47
48         packages_path = atoms_path_all_users + "packages";
49         packages_path_user = atoms_path_user + "packages";
50
51         // Then, we must test if there is also a user defined packages file
52         if isfile(packages_path_user) then  // We use it if it is newer
53             kdiff = newest([packages_path, packages_path_user])
54             if ~isfile(packages_path) | kdiff == 2 then
55                 copyfile(packages_path_user, packages_path)
56             end
57         end
58     else  // The user is not a Scilab admin => default user profile used
59         packages_path = atoms_path_user + "packages";
60     end
61
62     categories_path  = atoms_path_user + "categories";
63     packages_path_info = fileinfo(packages_path);
64
65
66     // in offline mode we use only DESCRIPTION file of the module
67     if (atomsGetConfig("offLine") == "True" | atomsGetConfig("offline") == "True") then
68         if isfile(atomsPath("system", "session") + "/DESCRIPTION_archives") then
69             this_description = atomsDESCRIPTIONread(atomsPath("system","session") + "/DESCRIPTION_archives");
70
71             inst_description_files = [ ..
72             atomsPath("system","allusers") + "DESCRIPTION_installed" "" ; ..
73             atomsPath("system","user")   + "DESCRIPTION_installed" "" ; ..
74             atomsPath("system","session")  + "DESCRIPTION_installed" "" ; ..
75             atomsPath("system","session")  + "DESCRIPTION_archives"  "" ];
76
77             for i=1:size(inst_description_files(:,1),"*")
78
79                 file_out    = inst_description_files(i,1);
80                 this_repository = inst_description_files(i,2);
81
82                 if ~isfile(file_out) then
83                     continue;
84                 end
85
86                 // Read the download description file
87                 // ----------------------------------------
88                 if this_repository <> "" then
89                     additional("repository") = this_repository;
90                 else
91                     additional = struct();
92                 end
93
94                 installed_description = atomsDESCRIPTIONread(file_out,additional);
95
96                 this_description  = atomsDESCRIPTIONcat(installed_description,this_description);
97
98             end
99
100             packages = this_description("packages");
101             categories_flat = this_description("categories_flat");
102             categories = this_description("categories");
103
104             global %_atoms_cache; // /!\ Do not rename this variable. Name is tracked/ignored by Variable Browser
105             %_atoms_cache(1) = packages;
106             %_atoms_cache(2) = categories;
107             %_atoms_cache(3) = categories_flat;
108
109         else
110             load(packages_path, "packages", "categories", "categories_flat");
111
112         end
113
114         return
115     end
116
117
118     // If necessary, rebuild the struct
119     // =========================================================================
120     TIME_BEFORE_NEW_UPDATE = strtod(atomsGetConfig("updateTime"))*86400;
121     if isnan(TIME_BEFORE_NEW_UPDATE) then // Value not found in config file, give the default one
122         TIME_BEFORE_NEW_UPDATE = 86400*30; // One month, in seconds
123     end
124     if (packages_path_info == []) ..
125         | (getdate("s") - packages_path_info(6) > TIME_BEFORE_NEW_UPDATE) ..
126         | (rhs == 1 & update) then
127
128         // Initialize
129         packages     = struct();
130         categories_flat  = struct();
131         categories     = struct();
132
133         description          = struct();
134         description("packages")    = packages;
135         description("categories")    = categories;
136         description("categories_flat") = categories_flat;
137
138         // Operating system detection + Architecture detection
139         // =========================================================================
140         [OSNAME, ARCH, LINUX, MACOSX, SOLARIS, BSD] = atomsGetPlatform();
141
142         description_files = [ ..
143         atomsPath("system","allusers") + "DESCRIPTION_installed" "" ; ..
144         atomsPath("system","user")   + "DESCRIPTION_installed" "" ; ..
145         atomsPath("system","session")  + "DESCRIPTION_installed" "" ; ..
146         atomsPath("system","session")  + "DESCRIPTION_archives"  "" ];
147
148         // 1st step : Loop on available repositories
149         // ---------------------------------------------------------------------
150
151         repositories = atomsRepositoryList();
152         repositories = repositories(:,1);
153
154         atoms_tmp_directory = atomsPath("system","session");
155
156         if ~isdir(atoms_tmp_directory) then
157             mkdir(atoms_tmp_directory);
158         end
159
160         for i=1:size(repositories,"*")
161             // Building url & file_out
162             // ----------------------------------------
163             url      = repositories(i)+"/TOOLBOXES/"+ARCH+"/"+OSNAME+".gz";
164             file_out     = pathconvert(getshortpathname(fullpath(atoms_tmp_directory))+string(i)+"_TOOLBOXES.gz",%f);
165
166             // Remove the existing file
167             // ----------------------------------------
168             if fileinfo(file_out) <> [] then
169                 mdelete(file_out);
170             end
171
172             // Launch the download
173             // ----------------------------------------
174             atomsDownload(url, file_out);
175
176             // We check that file_out exists
177             // ----------------------------------------
178             if ~isfile(file_out) then
179                 error(msprintf(gettext("%s: DESCRIPTION file (''%s'') does not exist.\n"),"atomsDESCRIPTIONget", file_out));
180             end
181
182             // Extract It
183             // ----------------------------------------
184             if LINUX | MACOSX | SOLARIS | BSD then
185                 extract_cmd = "gunzip "+ file_out;
186             else
187                 gzip_path = getshortpathname(fullpath(pathconvert(SCI+"/tools/gzip/gzip.exe",%F)));
188                 if ~isfile(gzip_path) then
189                     error(msprintf(gettext("%s: gzip not found.\n"), "atomsDESCRIPTIONget"));
190                 end
191                 extract_cmd = """" + gzip_path + """" + " -d """ + file_out + """";
192             end
193
194             [rep, stat ,err] = unix_g(extract_cmd);
195
196             if stat ~= 0 then
197                 disp(err);
198                 error(msprintf(gettext("%s: Extraction of the DESCRIPTION file (''%s'') has failed.\n"),"atomsDESCRIPTIONget",file_out));
199             end
200
201             description_files = [ description_files ; strsubst(file_out,"/\.gz$/","","r") repositories(i) ];
202
203         end
204
205         // 2nd step : Loop on available Description files
206         // ---------------------------------------------------------------------
207
208         for i=1:size(description_files(:,1),"*")
209
210             file_out    = description_files(i,1);
211             this_repository = description_files(i,2);
212
213             if ~isfile(file_out) then
214                 continue;
215             end
216
217             // Read the download description file
218             // ----------------------------------------
219             if this_repository <> "" then
220                 additional("repository") = this_repository;
221             else
222
223                 additional = struct();
224             end
225
226             this_description = atomsDESCRIPTIONread(file_out,additional);
227
228             // Add information about the repository
229             // ----------------------------------------
230             if this_repository <> "" then
231                 this_description = atomsDESCRIPTIONaddField(this_description,"*","*","repository",this_repository);
232                 this_description = atomsDESCRIPTIONaddField(this_description,"*","*","fromRepository","1");
233             end
234
235             // concatenate the description with the
236             // global struct
237             // ----------------------------------------
238             description  = atomsDESCRIPTIONcat(description,this_description);
239
240             // file_out is no more needed, delete it
241             // but only if it's named TOOLBOXES
242             // DESCRIPTION_installed must be keeped
243             // ----------------------------------------
244             if regexp( file_out , "/TOOLBOXES$/" , "o" ) <> [] then
245                 mdelete(file_out);
246             end
247
248         end
249
250         // Save the "packages" variable in a file
251         // ---------------------------------------------------------------------
252
253         if ~isdir(atomsPath("system","user")) then
254             mkdir(atomsPath("system","user"));
255         end
256
257         packages     = description("packages");
258         categories     = description("categories");
259         categories_flat  = description("categories_flat");
260
261         wMode = warning("query");
262         warning("off");
263         commandToExec = "save(packages_path, packages, categories, categories_flat)";
264         ierr = execstr(commandToExec, "errcatch");
265         warning(wMode);
266         if ierr <> 0 then
267             error(msprintf(gettext("%s: save (''%s'') has failed.\n"),"atomsDESCRIPTIONget", packages_path));
268         end
269         clearglobal %_atoms_cache;
270
271         // Just load from file
272         // =========================================================================
273
274     else
275         global %_atoms_cache; // /!\ Do not rename this variable. Name is tracked/ignored by Variable Browser
276         if isempty(%_atoms_cache) then
277             load(packages_path,"packages","categories","categories_flat");
278             %_atoms_cache = list(packages, categories, categories_flat);
279         else
280             packages = %_atoms_cache(1);
281             categories = %_atoms_cache(2);
282             categories_flat = %_atoms_cache(3);
283         end
284     end
285
286 endfunction