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