7c0def22e51e21ed99ca81f8ddae64e9338a051e
[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 = 86400;
121     if (packages_path_info == []) ..
122         | (getdate("s") - packages_path_info(6) > TIME_BEFORE_NEW_UPDATE) ..
123         | (rhs == 1 & update) then
124
125         // Initialize
126         packages     = struct();
127         categories_flat  = struct();
128         categories     = struct();
129
130         description          = struct();
131         description("packages")    = packages;
132         description("categories")    = categories;
133         description("categories_flat") = categories_flat;
134
135         // Operating system detection + Architecture detection
136         // =========================================================================
137         [OSNAME, ARCH, LINUX, MACOSX, SOLARIS, BSD] = atomsGetPlatform();
138
139         description_files = [ ..
140         atomsPath("system","allusers") + "DESCRIPTION_installed" "" ; ..
141         atomsPath("system","user")   + "DESCRIPTION_installed" "" ; ..
142         atomsPath("system","session")  + "DESCRIPTION_installed" "" ; ..
143         atomsPath("system","session")  + "DESCRIPTION_archives"  "" ];
144
145         // 1st step : Loop on available repositories
146         // ---------------------------------------------------------------------
147
148         repositories = atomsRepositoryList();
149         repositories = repositories(:,1);
150
151         atoms_tmp_directory = atomsPath("system","session");
152
153         if ~isdir(atoms_tmp_directory) then
154             mkdir(atoms_tmp_directory);
155         end
156
157         for i=1:size(repositories,"*")
158             // Building url & file_out
159             // ----------------------------------------
160             url      = repositories(i)+"/TOOLBOXES/"+ARCH+"/"+OSNAME+".gz";
161             file_out     = pathconvert(getshortpathname(fullpath(atoms_tmp_directory))+string(i)+"_TOOLBOXES.gz",%f);
162
163             // Remove the existing file
164             // ----------------------------------------
165             if fileinfo(file_out) <> [] then
166                 mdelete(file_out);
167             end
168
169             // Launch the download
170             // ----------------------------------------
171             atomsDownload(url, file_out);
172
173             // We check that file_out exists
174             // ----------------------------------------
175             if ~isfile(file_out) then
176                 error(msprintf(gettext("%s: DESCRIPTION file (''%s'') does not exist.\n"),"atomsDESCRIPTIONget", file_out));
177             end
178
179             // Extract It
180             // ----------------------------------------
181             if LINUX | MACOSX | SOLARIS | BSD then
182                 extract_cmd = "gunzip "+ file_out;
183             else
184                 gzip_path = getshortpathname(fullpath(pathconvert(SCI+"/tools/gzip/gzip.exe",%F)));
185                 if ~isfile(gzip_path) then
186                     error(msprintf(gettext("%s: gzip not found.\n"), "atomsDESCRIPTIONget"));
187                 end
188                 extract_cmd = """" + gzip_path + """" + " -d """ + file_out + """";
189             end
190
191             [rep, stat ,err] = unix_g(extract_cmd);
192
193             if stat ~= 0 then
194                 disp(err);
195                 error(msprintf(gettext("%s: Extraction of the DESCRIPTION file (''%s'') has failed.\n"),"atomsDESCRIPTIONget",file_out));
196             end
197
198             description_files = [ description_files ; strsubst(file_out,"/\.gz$/","","r") repositories(i) ];
199
200         end
201
202         // 2nd step : Loop on available Description files
203         // ---------------------------------------------------------------------
204
205         for i=1:size(description_files(:,1),"*")
206
207             file_out    = description_files(i,1);
208             this_repository = description_files(i,2);
209
210             if ~isfile(file_out) then
211                 continue;
212             end
213
214             // Read the download description file
215             // ----------------------------------------
216             if this_repository <> "" then
217                 additional("repository") = this_repository;
218             else
219
220                 additional = struct();
221             end
222
223             this_description = atomsDESCRIPTIONread(file_out,additional);
224
225             // Add information about the repository
226             // ----------------------------------------
227             if this_repository <> "" then
228                 this_description = atomsDESCRIPTIONaddField(this_description,"*","*","repository",this_repository);
229                 this_description = atomsDESCRIPTIONaddField(this_description,"*","*","fromRepository","1");
230             end
231
232             // concatenate the description with the
233             // global struct
234             // ----------------------------------------
235             description  = atomsDESCRIPTIONcat(description,this_description);
236
237             // file_out is no more needed, delete it
238             // but only if it's named TOOLBOXES
239             // DESCRIPTION_installed must be keeped
240             // ----------------------------------------
241             if regexp( file_out , "/TOOLBOXES$/" , "o" ) <> [] then
242                 mdelete(file_out);
243             end
244
245         end
246
247         // Save the "packages" variable in a file
248         // ---------------------------------------------------------------------
249
250         if ~isdir(atomsPath("system","user")) then
251             mkdir(atomsPath("system","user"));
252         end
253
254         packages     = description("packages");
255         categories     = description("categories");
256         categories_flat  = description("categories_flat");
257
258         wMode = warning("query");
259         warning("off");
260         commandToExec = "save(packages_path, packages, categories, categories_flat)";
261         ierr = execstr(commandToExec, "errcatch");
262         warning(wMode);
263         if ierr <> 0 then
264             error(msprintf(gettext("%s: save (''%s'') has failed.\n"),"atomsDESCRIPTIONget", packages_path));
265         end
266         clearglobal %_atoms_cache;
267
268         // Just load from file
269         // =========================================================================
270
271     else
272         global %_atoms_cache; // /!\ Do not rename this variable. Name is tracked/ignored by Variable Browser
273         if isempty(%_atoms_cache) then
274             load(packages_path,"packages","categories","categories_flat");
275             %_atoms_cache = list(packages, categories, categories_flat);
276         else
277             packages = %_atoms_cache(1);
278             categories = %_atoms_cache(2);
279             categories_flat = %_atoms_cache(3);
280         end
281     end
282
283 endfunction