5801
|
1 ## Copyright (C) 2005 S�ren Hauberg |
|
2 ## |
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2 of the License, or |
|
6 ## (at your option) any later version. |
|
7 ## |
|
8 ## This program is distributed in the hope that it will be useful, |
|
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 ## GNU General Public License for more details. |
|
12 ## |
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this program; if not, write to the Free Software |
|
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
16 |
|
17 ## -*- texinfo -*- |
6032
|
18 ## @deftypefn {Command} pkg @var{command} @var{pkg_name} |
|
19 ## @deftypefnx {Command} pkg @var{command} @var{option} @var{pkg_name} |
|
20 ## This command interacts with the package manager. Different actions will |
6378
|
21 ## be taken depending on the value of @var{command}. |
6032
|
22 ## |
|
23 ## @table @samp |
|
24 ## @item install |
6070
|
25 ## Install named packages. For example, |
|
26 ## @example |
|
27 ## pkg install image-1.0.0.tar.gz |
|
28 ## @end example |
|
29 ## @noindent |
|
30 ## installs the package found in the file @code{image-1.0.0.tar.gz}. |
|
31 ## |
|
32 ## If @var{option} is @code{-nodeps} the package manager will disable the |
|
33 ## dependency checking. That way it is possible to install a package even |
|
34 ## if it depends on another package that's not installed on the system. |
|
35 ## @strong{Use this option with care.} |
6258
|
36 ## |
|
37 ## If @var{option} is @code{-noauto} the package manager will not |
|
38 ## automatically load the installed package when starting Octave, |
|
39 ## even if the package requests that it is. |
|
40 ## |
|
41 ## If @var{option} is @code{-auto} the package manager will |
|
42 ## automatically load the installed package when starting Octave, |
|
43 ## even if the package requests that it isn't. |
6032
|
44 ## @item uninstall |
6070
|
45 ## Uninstall named packages. For example, |
|
46 ## @example |
|
47 ## pkg uninstall image |
|
48 ## @end example |
|
49 ## @noindent |
|
50 ## removes the @code{image} package from the system. If another installed |
|
51 ## package depends on the @code{image} package an error will be issued. |
|
52 ## The package can be uninstalled anyway by using the @code{-nodeps} option. |
6032
|
53 ## @item load |
6070
|
54 ## Add named packages to the path. After loading a package it is |
|
55 ## possible to use the functions provided by the package. For example, |
|
56 ## @example |
|
57 ## pkg load image |
|
58 ## @end example |
|
59 ## @noindent |
|
60 ## adds the @code{image} package to the path. It is possible to load all |
|
61 ## installed packages at once with the command |
|
62 ## @example |
|
63 ## pkg load all |
|
64 ## @end example |
6203
|
65 ## @item unload |
|
66 ## Removes named packages from the path. After unloading a package it is |
|
67 ## no longer possible to use the functions provided by the package. |
|
68 ## This command behaves like the @code{load} command. |
6032
|
69 ## @item list |
6070
|
70 ## Show a list of the currently installed packages. By requesting one or two |
|
71 ## output argument it is possible to get a list of the currently installed |
|
72 ## packages. For example, |
|
73 ## @example |
|
74 ## installed_packages = pkg list; |
|
75 ## @end example |
|
76 ## @noindent |
|
77 ## returns a cell array containing a structure for each installed package. |
|
78 ## The command |
|
79 ## @example |
|
80 ## [@var{user_packages}, @var{system_packages}] = pkg list |
|
81 ## @end example |
|
82 ## @noindent |
|
83 ## splits the list of installed packages into those who are installed by |
|
84 ## the current user, and those installed by the system administrator. |
6034
|
85 ## @item prefix |
6070
|
86 ## Set the installation prefix directory. For example, |
|
87 ## @example |
|
88 ## pkg prefix ~/my_octave_packages |
|
89 ## @end example |
|
90 ## @noindent |
|
91 ## sets the installation prefix to @code{~/my_octave_packages}. |
|
92 ## Packages will be installed in this directory. |
6034
|
93 ## |
6070
|
94 ## It is possible to get the current installation prefix by requesting an |
|
95 ## output argument. For example, |
|
96 ## @example |
|
97 ## p = pkg prefix |
|
98 ## @end example |
6189
|
99 ## @item local_list |
|
100 ## Set the file in which to look for information on the locally |
|
101 ## installed packages. Locally installed packages are those that are |
|
102 ## typically available only to the current user. For example |
|
103 ## @example |
|
104 ## pkg local_list ~/.octave_packages |
|
105 ## @end example |
|
106 ## It is possible to get the current value of local_list with the following |
|
107 ## @example |
|
108 ## pkg local_list |
|
109 ## @end example |
|
110 ## @item global_list |
|
111 ## Set the file in which to look for, for information on the globally |
|
112 ## installed packages. Globally installed packages are those that are |
|
113 ## typically available to all users. For example |
|
114 ## @example |
|
115 ## pkg global_list /usr/share/octave/octave_packages |
|
116 ## @end example |
|
117 ## It is possible to get the current value of global_list with the following |
|
118 ## @example |
|
119 ## pkg global_list |
|
120 ## @end example |
6032
|
121 ## @end table |
5801
|
122 ## @end deftypefn |
5947
|
123 |
|
124 ## PKG_ADD: mark_as_command pkg |
6258
|
125 ## PKG_ADD: pkg ("load", "auto"); |
5947
|
126 |
5801
|
127 function [local_packages, global_packages] = pkg(varargin) |
6233
|
128 ## Installation prefix (XXX: what should these be on windows?) |
6034
|
129 persistent prefix = -1; |
6364
|
130 persistent local_list = tilde_expand(fullfile("~", ".octave_packages")); |
|
131 persistent global_list = fullfile (OCTAVE_HOME (), "share", "octave", "octave_packages"); |
6233
|
132 mlock; |
6189
|
133 |
6034
|
134 if (prefix == -1) |
|
135 if (issuperuser()) |
6364
|
136 prefix = fullfile (OCTAVE_HOME (), "share", "octave", "packages"); |
6034
|
137 else |
6364
|
138 prefix = fullfile ("~", "octave"); |
6034
|
139 endif |
|
140 endif |
|
141 prefix = tilde_expand(prefix); |
6189
|
142 |
5801
|
143 ## Handle input |
|
144 if (length(varargin) == 0 || !iscellstr(varargin)) |
5928
|
145 print_usage(); |
5801
|
146 endif |
|
147 files = {}; |
|
148 deps = true; |
6258
|
149 auto = 0; |
5801
|
150 action = "none"; |
|
151 for i = 1:length(varargin) |
|
152 switch (varargin{i}) |
|
153 case "-nodeps" |
|
154 deps = false; |
6258
|
155 case "-noauto" |
|
156 auto = -1; |
|
157 case "-auto" |
|
158 auto = 1; |
6203
|
159 case {"list", "install", "uninstall", "load", "unload", ... |
|
160 "prefix", "local_list", "global_list"} |
5801
|
161 action = varargin{i}; |
|
162 otherwise |
|
163 files{end+1} = varargin{i}; |
|
164 endswitch |
|
165 endfor |
|
166 |
|
167 ## Take action |
|
168 switch (action) |
|
169 case "list" |
|
170 if (nargout == 0) |
6189
|
171 installed_packages(local_list, global_list); |
5801
|
172 elseif (nargout == 1) |
6189
|
173 local_packages = installed_packages(local_list, global_list); |
5801
|
174 elseif (nargout == 2) |
6189
|
175 [local_packages, global_packages] = installed_packages(local_list, global_list); |
5801
|
176 else |
6042
|
177 error("Too many output arguments requested."); |
5801
|
178 endif |
|
179 case "install" |
|
180 if (length(files) == 0) |
6042
|
181 error("You must specify at least one filename when calling 'pkg install'"); |
5801
|
182 endif |
6258
|
183 install(files, deps, auto, prefix, local_list, global_list); |
5801
|
184 case "uninstall" |
|
185 if (length(files) == 0) |
6042
|
186 error("You must specify at least one package when calling 'pkg uninstall'"); |
5801
|
187 endif |
6189
|
188 uninstall(files, deps, local_list, global_list); |
5801
|
189 case "load" |
|
190 if (length(files) == 0) |
6258
|
191 error("You must specify at least one package, 'all' or 'auto' when calling 'pkg load'"); |
5801
|
192 endif |
6189
|
193 load_packages(files, deps, local_list, global_list); |
6203
|
194 case "unload" |
|
195 if (length(files) == 0) |
|
196 error("You must specify at least one package or 'all' when calling 'pkg unload'"); |
|
197 endif |
|
198 unload_packages(files, deps, local_list, global_list); |
6034
|
199 case "prefix" |
|
200 if (length(files) == 0 && nargout == 0) |
6035
|
201 disp(prefix); |
6034
|
202 elseif (length(files) == 0 && nargout == 1) |
|
203 local_packages = prefix; |
|
204 elseif (length(files) == 1 && nargout == 0 && ischar(files{1})) |
|
205 prefix = files{1}; |
6364
|
206 #if (!strcmp(prefix(end), filesep)) prefix(end+1) = filesep; endif |
6034
|
207 else |
6189
|
208 error("You must specify a prefix directory, or request an output argument"); |
|
209 endif |
|
210 case "local_list" |
|
211 if (length(files) == 0 && nargout == 0) |
|
212 disp(local_list); |
|
213 elseif (length(files) == 0 && nargout == 1) |
|
214 local_packages = local_list; |
|
215 elseif (length(files) == 1 && nargout == 0 && ischar(files{1})) |
|
216 local_list = files{1}; |
|
217 else |
|
218 error("You must specify a local_list file, or request an output argument"); |
|
219 endif |
|
220 case "global_list" |
|
221 if (length(files) == 0 && nargout == 0) |
|
222 disp(global_list); |
|
223 elseif (length(files) == 0 && nargout == 1) |
|
224 local_packages = global_list; |
|
225 elseif (length(files) == 1 && nargout == 0 && ischar(files{1})) |
|
226 global_list = files{1}; |
|
227 else |
|
228 error("You must specify a global_list file, or request an output argument"); |
6034
|
229 endif |
5801
|
230 otherwise |
6042
|
231 error("You must specify a valid action for 'pkg'. See 'help pkg' for details"); |
5801
|
232 endswitch |
|
233 endfunction |
|
234 |
6258
|
235 function auto = isautoload(desc) |
|
236 auto = false; |
|
237 if (isfield(desc{1},"autoload")) |
|
238 a = desc{1}.autoload; |
|
239 if ((isnumeric(a) && a > 0) || |
|
240 (ischar(a) && (strcmp(tolower(a),"true") || |
|
241 strcmp(tolower(a),"on") || |
|
242 strcmp(tolower(a),"yes") || |
|
243 strcmp(tolower(a),"1")))) |
|
244 auto = true; |
|
245 endif |
|
246 endif |
|
247 endfunction |
|
248 |
|
249 function install(files, handle_deps, autoload, prefix, local_list, global_list) |
6034
|
250 global_install = issuperuser(); |
5801
|
251 |
|
252 # Check that the directory in prefix exist. If it doesn't: create it! |
|
253 if (!exist(prefix, "dir")) |
|
254 warning("Creating installation directory %s", prefix); |
|
255 [status, msg] = mkdir(prefix); |
|
256 if (status != 1) |
6042
|
257 error("Could not create installation directory: %s", msg); |
5801
|
258 endif |
|
259 endif |
|
260 |
|
261 ## Get the list of installed packages |
6189
|
262 [local_packages, global_packages] = installed_packages(local_list, |
|
263 global_list); |
|
264 installed_packages = {local_packages{:}, global_packages{:}}; |
5801
|
265 |
|
266 if (global_install) |
|
267 packages = global_packages; |
|
268 else |
|
269 packages = local_packages; |
|
270 endif |
|
271 |
|
272 ## Uncompress the packages and read the DESCRIPTION files |
5947
|
273 tmpdirs = packdirs = descriptions = {}; |
5801
|
274 try |
|
275 ## Unpack the package files and read the DESCRIPTION files |
6234
|
276 files = glob(files); |
5801
|
277 packages_to_uninstall = []; |
|
278 for i = 1:length(files) |
|
279 tgz = files{i}; |
|
280 |
|
281 ## Create a temporary directory |
|
282 tmpdir = tmpnam(); |
5947
|
283 tmpdirs{end+1} = tmpdir; |
5801
|
284 [status, msg] = mkdir(tmpdir); |
|
285 if (status != 1) |
6042
|
286 error("Couldn't create temporary directory: %s", msg); |
5801
|
287 endif |
|
288 |
|
289 ## Uncompress the package |
|
290 untar(tgz, tmpdir); |
5928
|
291 |
5947
|
292 ## Get the name of the directories produced by tar |
5801
|
293 [dirlist, err, msg] = readdir(tmpdir); |
|
294 if (err) |
6042
|
295 error("Couldn't read directory produced by tar: %s", msg); |
5801
|
296 endif |
5987
|
297 |
|
298 if (length(dirlist) > 3) |
|
299 error("Bundles of packages are not allowed") |
|
300 endif |
5947
|
301 |
|
302 for k = 3:length(dirlist) # the two first entries of dirlist are "." and ".." |
6233
|
303 packdir = fullfile(tmpdir, dirlist{k}); |
5947
|
304 packdirs{end+1} = packdir; |
5801
|
305 |
5947
|
306 ## Make sure the package contains necessary files |
|
307 verify_directory(packdir); |
5928
|
308 |
5947
|
309 ## Read the DESCRIPTION file |
6233
|
310 filename = fullfile(packdir, "DESCRIPTION"); |
5947
|
311 desc = get_description(filename); |
5928
|
312 |
6233
|
313 ## Verify that package name corresponds with filename |
|
314 [dummy, nm] = fileparts(tgz); |
|
315 if ((length(nm) >= length(desc.name)) && |
|
316 ! strcmp(desc.name,nm(1:length(desc.name)))) |
|
317 error("Package name '%s' doesn't correspond to its filename '%s'", desc.name, nm); |
|
318 endif |
5971
|
319 |
5947
|
320 ## Set default installation directory |
6233
|
321 desc.dir = fullfile(prefix, [desc.name "-" desc.version]); |
5801
|
322 |
5947
|
323 ## Save desc |
|
324 descriptions{end+1} = desc; |
5801
|
325 |
5947
|
326 ## Are any of the new packages already installed? |
|
327 ## If so we'll remove the old version. |
|
328 for j = 1:length(packages) |
|
329 if (strcmp(packages{j}.name, desc.name)) |
|
330 packages_to_uninstall(end+1) = j; |
|
331 endif |
|
332 endfor |
|
333 endfor |
5801
|
334 endfor |
|
335 catch |
|
336 ## Something went wrong, delete tmpdirs |
|
337 for i = 1:length(tmpdirs) |
5928
|
338 rm_rf(tmpdirs{i}); |
5801
|
339 endfor |
|
340 error(lasterr()(8:end)); |
|
341 end_try_catch |
|
342 |
|
343 ## Check dependencies |
|
344 if (handle_deps) |
|
345 ok = true; |
|
346 error_text = ""; |
|
347 for i = 1:length(descriptions) |
|
348 desc = descriptions{i}; |
|
349 idx1 = complement(packages_to_uninstall, 1:length(installed_packages)); |
|
350 idx2 = complement(i, 1:length(descriptions)); |
|
351 pseudo_installed_packages = {installed_packages{idx1} descriptions{idx2}}; |
|
352 bad_deps = get_unsatisfied_deps(desc, pseudo_installed_packages); |
|
353 ## Are there any unsatisfied dependencies? |
|
354 if (!isempty(bad_deps)) |
|
355 ok = false; |
|
356 for i = 1:length(bad_deps) |
|
357 dep = bad_deps{i}; |
|
358 error_text = [error_text " " desc.name " needs " ... |
|
359 dep.package " " dep.operator " " ... |
|
360 dep.version "\n"]; |
|
361 endfor |
|
362 endif |
|
363 endfor |
|
364 |
|
365 ## Did we find any unsatisfied dependencies? |
|
366 if (!ok) |
|
367 error("The following dependencies where unsatisfied:\n %s", error_text); |
|
368 endif |
|
369 endif |
|
370 |
|
371 ## Prepare each package for installation |
|
372 try |
|
373 for i = 1:length(descriptions) |
|
374 desc = descriptions{i}; |
|
375 pdir = packdirs{i}; |
|
376 prepare_installation (desc, pdir); |
|
377 configure_make (desc, pdir); |
|
378 endfor |
|
379 catch |
|
380 ## Something went wrong, delete tmpdirs |
|
381 for i = 1:length(tmpdirs) |
5928
|
382 rm_rf(tmpdirs{i}); |
5801
|
383 endfor |
|
384 error(lasterr()(8:end)); |
|
385 end_try_catch |
|
386 |
|
387 ## Uninstall the packages that will be replaced |
|
388 try |
|
389 for i = packages_to_uninstall |
6189
|
390 uninstall({installed_packages{i}.name}, false, local_list, |
6245
|
391 global_list); |
5801
|
392 endfor |
|
393 catch |
|
394 ## Something went wrong, delete tmpdirs |
|
395 for i = 1:length(tmpdirs) |
5928
|
396 rm_rf(tmpdirs{i}); |
5801
|
397 endfor |
|
398 error(lasterr()(8:end)); |
|
399 end_try_catch |
|
400 |
|
401 ## Install each package |
|
402 try |
|
403 for i = 1:length(descriptions) |
|
404 desc = descriptions{i}; |
|
405 pdir = packdirs{i}; |
6245
|
406 copy_files(desc, pdir); |
|
407 create_pkgadddel(desc, pdir, "PKG_ADD"); |
|
408 create_pkgadddel(desc, pdir, "PKG_DEL"); |
5801
|
409 finish_installation (desc, pdir) |
|
410 endfor |
|
411 catch |
|
412 ## Something went wrong, delete tmpdirs |
|
413 for i = 1:length(tmpdirs) |
5928
|
414 rm_rf(tmpdirs{i}); |
5801
|
415 endfor |
5971
|
416 for i = 1:length(descriptions) |
|
417 rm_rf(descriptions{i}.dir); |
|
418 endfor |
5801
|
419 error(lasterr()(8:end)); |
|
420 end_try_catch |
|
421 |
5971
|
422 ## Check if the installed directory is empty. If it is remove it |
|
423 ## from the list |
|
424 for i = length(descriptions):-1:1 |
|
425 if (dirempty(descriptions{i}.dir,{"packinfo","doc"})) |
6245
|
426 rm_rf(descriptions{i}.dir); |
|
427 descriptions(i) = []; |
5971
|
428 endif |
|
429 endfor |
|
430 |
6258
|
431 ## If the package requested that it is autoloaded, or the installer |
|
432 ## requested that it is, then mark the package as autoloaded. |
|
433 for i = length(descriptions):-1:1 |
|
434 if (autoload > 0 || (autoload == 0 && isautoload(descriptions(i)))) |
|
435 fclose(fopen(fullfile(descriptions{i}.dir, "packinfo", |
|
436 ".autoload"),"wt")); |
|
437 endif |
|
438 endfor |
|
439 |
5971
|
440 ## Add the packages to the package list |
5801
|
441 try |
|
442 if (global_install) |
|
443 idx = complement(packages_to_uninstall, 1:length(global_packages)); |
|
444 global_packages = {global_packages{idx} descriptions{:}}; |
|
445 save(global_list, "global_packages"); |
|
446 else |
|
447 idx = complement(packages_to_uninstall, 1:length(local_packages)); |
|
448 local_packages = {local_packages{idx} descriptions{:}}; |
|
449 save(local_list, "local_packages"); |
|
450 endif |
|
451 catch |
|
452 ## Something went wrong, delete tmpdirs |
|
453 for i = 1:length(tmpdirs) |
5928
|
454 rm_rf(tmpdirs{i}); |
5801
|
455 endfor |
|
456 for i = 1:length(descriptions) |
5928
|
457 rm_rf(descriptions{i}.dir); |
5801
|
458 endfor |
|
459 if (global_install) |
6042
|
460 error("Couldn't append to %s: %s", global_list, lasterr()(8:end)); |
5801
|
461 else |
6042
|
462 error("Couldn't append to %s: %s", local_list, lasterr()(8:end)); |
5801
|
463 endif |
|
464 end_try_catch |
|
465 |
|
466 ## All is well, let's clean up |
|
467 for i = 1:length(tmpdirs) |
5928
|
468 [status, msg] = rm_rf(tmpdirs{i}); |
5801
|
469 if (status != 1) |
|
470 warning("Couldn't clean up after my self: %s\n", msg); |
|
471 endif |
|
472 endfor |
|
473 |
|
474 ## Add the newly installed packages to the path, so the user |
5971
|
475 ## can begin usings them. |
|
476 if (length(descriptions) > 0) |
|
477 dirs = cell(1, length(descriptions)); |
|
478 for i = 1:length(descriptions) |
5801
|
479 dirs{i} = descriptions{i}.dir; |
5971
|
480 endfor |
|
481 addpath(dirs{:}); |
|
482 endif |
5801
|
483 endfunction |
|
484 |
6189
|
485 function uninstall(pkgnames, handle_deps, local_list, global_list) |
5801
|
486 ## Get the list of installed packages |
6189
|
487 [local_packages, global_packages] = installed_packages(local_list, |
|
488 global_list); |
5801
|
489 if (issuperuser()) |
6189
|
490 installed_packages = {local_packages{:}, global_packages{:}}; |
5801
|
491 else |
|
492 installed_packages = local_packages; |
|
493 endif |
6042
|
494 |
5801
|
495 num_packages = length(installed_packages); |
|
496 delete_idx = []; |
|
497 for i = 1:num_packages |
|
498 cur_name = installed_packages{i}.name; |
|
499 if (any(strcmp(cur_name, pkgnames))) |
|
500 delete_idx(end+1) = i; |
|
501 endif |
|
502 endfor |
6042
|
503 |
5801
|
504 ## Are all the packages that should be uninstalled already installed? |
|
505 if (length(delete_idx) != length(pkgnames)) |
6233
|
506 if (issuperuser()) |
|
507 ## Try again for a locally installed package |
|
508 installed_packages = local_packages |
6189
|
509 |
6233
|
510 num_packages = length(installed_packages); |
|
511 delete_idx = []; |
|
512 for i = 1:num_packages |
|
513 cur_name = installed_packages{i}.name; |
|
514 if (any(strcmp(cur_name, pkgnames))) |
|
515 delete_idx(end+1) = i; |
|
516 endif |
|
517 endfor |
|
518 if (length(delete_idx) != length(pkgnames)) |
|
519 ## XXX: We should have a better error message |
|
520 error("Some of the packages you want to uninstall are not installed."); |
|
521 endif |
|
522 else |
|
523 ## XXX: We should have a better error message |
|
524 error("Some of the packages you want to uninstall are not installed."); |
|
525 endif |
5801
|
526 endif |
|
527 |
|
528 ## Compute the packages that will remain installed |
|
529 idx = complement(delete_idx, 1:num_packages); |
|
530 remaining_packages = {installed_packages{idx}}; |
|
531 |
|
532 ## Check dependencies |
|
533 if (handle_deps) |
|
534 error_text = ""; |
|
535 for i = 1:length(remaining_packages) |
|
536 desc = remaining_packages{i}; |
|
537 bad_deps = get_unsatisfied_deps(desc, remaining_packages); |
|
538 |
|
539 ## Will the uninstallation break any dependencies? |
|
540 if (!isempty(bad_deps)) |
|
541 for i = 1:length(bad_deps) |
|
542 dep = bad_deps{i}; |
|
543 error_text = [error_text " " desc.name " needs " ... |
|
544 dep.package " " dep.operator " " ... |
|
545 dep.version "\n"]; |
|
546 endfor |
|
547 endif |
|
548 endfor |
|
549 |
6042
|
550 if (! isempty(error_text)) |
5801
|
551 error("The following dependencies where unsatisfied:\n %s", error_text); |
|
552 endif |
|
553 endif |
|
554 |
|
555 ## Delete the directories containing the packages |
|
556 for i = delete_idx |
|
557 desc = installed_packages{i}; |
|
558 ## If an 'on_uninstall.m' exist, call it! |
6233
|
559 if (exist(fullfile(desc.dir, "packinfo", "on_uninstall.m"), "file")) |
5801
|
560 try |
|
561 wd = pwd(); |
6233
|
562 cd(fullfile(desc.dir, "packinfo")); |
5801
|
563 on_uninstall(desc); |
|
564 cd(wd); |
|
565 catch |
|
566 # XXX: Should this rather be an error? |
6042
|
567 warning("The 'on_uninstall' script retsurned the following error: %s", lasterr); |
5801
|
568 cd(wd); |
|
569 end_try_catch |
|
570 endif |
|
571 ## Do the actual deletion |
5928
|
572 rmpath(desc.dir); |
6233
|
573 if (exist (desc.dir, "dir")) |
|
574 [status, msg] = rm_rf(desc.dir); |
|
575 if (status != 1) |
|
576 error("Couldn't delete directory %s: %s", desc.dir, msg); |
|
577 endif |
|
578 else |
|
579 warning("Directory %s previously lost", desc.dir); |
|
580 endif |
5801
|
581 endfor |
|
582 |
|
583 ## Write a new ~/.octave_packages |
|
584 if (issuperuser()) |
|
585 if (length(remaining_packages) == 0) |
|
586 unlink(global_list); |
|
587 else |
|
588 global_packages = remaining_packages; |
|
589 save(global_list, "global_packages"); |
|
590 endif |
|
591 else |
|
592 if (length(remaining_packages) == 0) |
|
593 unlink(local_list); |
|
594 else |
|
595 local_packages = remaining_packages; |
|
596 save(local_list, "local_packages"); |
|
597 endif |
|
598 endif |
|
599 |
|
600 endfunction |
|
601 |
|
602 ########################################################## |
|
603 ## A U X I L A R Y F U N C T I O N S ## |
|
604 ########################################################## |
|
605 |
|
606 function prepare_installation(desc, packdir) |
|
607 ## Is there a pre_install to call? |
6364
|
608 if (exist(fullfile(packdir, "pre_install.m"), "file")) |
5801
|
609 wd = pwd(); |
|
610 try |
|
611 cd(packdir); |
|
612 pre_install(desc); |
|
613 cd(wd); |
|
614 catch |
|
615 cd(wd); |
6042
|
616 error("The pre-install function returned the following error: %s", lasterr); |
5801
|
617 end_try_catch |
|
618 endif |
|
619 |
5993
|
620 ## If the directory "inst" doesn't exist, we create it |
6364
|
621 inst_dir = fullfile(packdir, "inst"); |
|
622 if (!exist(inst_dir, "dir")) |
|
623 [status, msg] = mkdir(inst_dir); |
5801
|
624 if (status != 1) |
5928
|
625 rm_rf(desc.dir); |
6042
|
626 error("The 'inst' directory did not exist and could not be created: %s", msg); |
5801
|
627 endif |
|
628 endif |
|
629 endfunction |
|
630 |
|
631 function configure_make (desc, packdir) |
|
632 ## Perform ./configure, make, make install in "src" |
6246
|
633 if (exist(fullfile(packdir, "src"), "dir")) |
|
634 src = fullfile(packdir, "src"); |
5801
|
635 ## configure |
6233
|
636 if (exist(fullfile(src, "configure"), "file")) |
5928
|
637 [status, output] = system(["cd " src " ;./configure --prefix=" desc.dir]); |
5801
|
638 if (status != 0) |
5928
|
639 rm_rf(desc.dir); |
6042
|
640 error("The configure script returned the following error: %s", output); |
5801
|
641 endif |
|
642 endif |
|
643 |
|
644 ## make |
6233
|
645 if (exist(fullfile(src, "Makefile"), "file")) |
5928
|
646 [status, output] = system(["export INSTALLDIR=" desc.dir "; make -C " src]); |
5801
|
647 if (status != 0) |
5928
|
648 rm_rf(desc.dir); |
6042
|
649 error("'make' returned the following error: %s", output); |
5801
|
650 endif |
|
651 %# make install |
5928
|
652 %[status, output] = system(["export INSTALLDIR=" desc.dir "; make install -C " src]); |
5801
|
653 %if (status != 0) |
5928
|
654 % rm_rf(desc.dir); |
6042
|
655 % error("'make install' returned the following error: %s", output); |
5801
|
656 %endif |
|
657 endif |
|
658 |
|
659 ## Copy files to "inst" (this is instead of 'make install') |
6233
|
660 files = fullfile(src, "FILES"); |
|
661 instdir = fullfile(packdir, "inst"); |
5801
|
662 if (exist(files, "file")) |
6233
|
663 ## Get file names |
5801
|
664 [fid, msg] = fopen(files, "r"); |
|
665 if (fid < 0) |
6042
|
666 error("Couldn't open %s: %s", files, msg); |
5801
|
667 endif |
5928
|
668 filenames = char(fread(fid))'; |
5801
|
669 fclose(fid); |
6356
|
670 if (filenames(end) == "\n") |
|
671 filenames(end) = []; |
|
672 endif |
6233
|
673 ## Copy the files |
|
674 fn = split_by(filenames, "\n"); |
6356
|
675 delete_idx = []; |
6233
|
676 for i = 1:length(fn) |
6356
|
677 if (!all(isspace(fn{i}))) |
|
678 fn{i} = fullfile(src, fn{i}); |
|
679 else |
|
680 delete_idx(end+1) = i; |
|
681 endif |
6233
|
682 endfor |
6356
|
683 fn(delete_idx) = []; |
6233
|
684 filenames = sprintf("%s ", fn{:}); |
5801
|
685 else |
6233
|
686 m = dir(fullfile(src, "*.m")); |
|
687 oct = dir(fullfile(src, "*.oct")); |
6357
|
688 mex = dir(fullfile(src, "*.mex")); |
6233
|
689 filenames = ""; |
5801
|
690 if (length(m) > 0) |
6246
|
691 filenames = sprintf(fullfile(src, "%s "), m.name); |
5801
|
692 endif |
|
693 if (length(oct) > 0) |
6246
|
694 filenames = [filenames " " sprintf(fullfile(src, "%s "), oct.name)]; |
5928
|
695 endif |
6357
|
696 if (length(mex) > 0) |
|
697 filenames = [filenames " " sprintf(fullfile(src, "%s "), mex.name)]; |
|
698 endif |
5801
|
699 endif |
6246
|
700 filenames = split_by(filenames, " "); |
6233
|
701 |
|
702 if (!all(isspace(filenames))) |
|
703 mkdir(instdir); |
|
704 [status, output] = copyfile(filenames, instdir); |
|
705 if (status != 1) |
|
706 rm_rf(desc.dir); |
|
707 error("Couldn't copy files from 'src' to 'inst': %s", output); |
|
708 endif |
5801
|
709 endif |
|
710 endif |
|
711 endfunction |
|
712 |
5971
|
713 function pkg = extract_pkg (nm, pat) |
5955
|
714 fid = fopen (nm, "rt"); |
5971
|
715 pkg = ""; |
5955
|
716 if (fid >= 0) |
|
717 while (! feof(fid)) |
|
718 ln = fgetl (fid); |
|
719 if (ln > 0) |
6143
|
720 t = regexp(ln, pat, "tokens"); |
5955
|
721 if (!isempty(t)) |
5971
|
722 pkg = [pkg, "\n", t{1}{1}]; |
5955
|
723 endif |
|
724 endif |
|
725 endwhile |
5971
|
726 if (!isempty(pkg)) |
|
727 pkg = [pkg, "\n"]; |
5955
|
728 endif |
|
729 fclose (fid); |
|
730 endif |
|
731 endfunction |
|
732 |
5971
|
733 function create_pkgadddel (desc, packdir, nm) |
6233
|
734 pkg = fullfile(desc.dir, nm); |
5971
|
735 fid = fopen(pkg, "wt"); |
5976
|
736 |
5955
|
737 if (fid >= 0) |
5971
|
738 ## Search all dot-m files for PKG commands |
6233
|
739 lst = dir (fullfile(packdir, "inst", "*.m")); |
5955
|
740 for i=1:length(lst) |
6233
|
741 nam = fullfile(packdir, "inst", lst(i).name); |
5976
|
742 fwrite (fid, extract_pkg (nam, ['^[#%][#%]* *' nm ': *(.*)$'])); |
5955
|
743 endfor |
|
744 |
5971
|
745 ## Search all C++ source files for PKG commands |
6233
|
746 lst = dir (fullfile(packdir, "src", "*.cc")); |
5955
|
747 for i=1:length(lst) |
6233
|
748 nam = fullfile(packdir, "src", lst(i).name); |
5976
|
749 fwrite (fid, extract_pkg (nam, ['^//* *' nm ': *(.*)$'])); |
|
750 fwrite (fid, extract_pkg (nam, ['^/\** *' nm ': *(.*) *\*/$'])); |
5955
|
751 endfor |
|
752 |
5971
|
753 ## Add developer included PKG commands |
6245
|
754 packdirnm = fullfile(packdir, nm); |
6233
|
755 if (exist(packdirnm, "file")) |
|
756 fid2 = fopen(packdirnm,"rt"); |
5955
|
757 if (fid2 >= 0) |
|
758 while (! feof(fid2)) |
|
759 ln = fgets (fid2); |
|
760 if (ln > 0) |
|
761 fwrite(fid, ln); |
|
762 endif |
|
763 endwhile |
5971
|
764 fclose(fid2); |
5955
|
765 endif |
|
766 endif |
|
767 fclose(fid); |
5971
|
768 |
|
769 ## If the file is empty remove it |
|
770 t = dir (pkg); |
|
771 if (t.bytes <= 0) |
|
772 unlink (pkg); |
|
773 endif |
5955
|
774 endif |
|
775 endfunction |
|
776 |
5971
|
777 function copy_files (desc, packdir, bindir) |
6020
|
778 ## Create the installation directory |
|
779 if (! exist (desc.dir, "dir")) |
6233
|
780 [status, output] = mkdir (desc.dir); |
|
781 if (status != 1) |
|
782 error("Couldn't create installation directory %s : %s", |
|
783 desc.dir, output); |
|
784 endif |
6020
|
785 endif |
|
786 |
5801
|
787 ## Copy the files from "inst" to installdir |
6233
|
788 instdir = fullfile(packdir, "inst"); |
|
789 if (!dirempty(instdir)) |
|
790 [status, output] = copyfile(fullfile(instdir, "*"), desc.dir); |
|
791 if (status != 1) |
5971
|
792 rm_rf(desc.dir); |
6042
|
793 error("Couldn't copy files to the installation directory"); |
5971
|
794 endif |
5801
|
795 endif |
|
796 |
|
797 ## Create the "packinfo" directory |
6233
|
798 packinfo = fullfile(desc.dir, "packinfo"); |
5801
|
799 [status, msg] = mkdir (packinfo); |
|
800 if (status != 1) |
5928
|
801 rm_rf(desc.dir); |
6042
|
802 error("Couldn't create packinfo directory: %s", msg); |
5801
|
803 endif |
|
804 |
|
805 ## Copy DESCRIPTION |
6233
|
806 [status, output] = copyfile(fullfile(packdir, "DESCRIPTION"), packinfo); |
|
807 if (status != 1) |
5928
|
808 rm_rf(desc.dir); |
6042
|
809 error("Couldn't copy DESCRIPTION: %s", output); |
5801
|
810 endif |
|
811 |
5993
|
812 ## Copy COPYING |
6233
|
813 [status, output] = copyfile(fullfile(packdir, "COPYING"), packinfo); |
|
814 if (status != 1) |
5993
|
815 rm_rf(desc.dir); |
6042
|
816 error("Couldn't copy COPYING: %s", output); |
5993
|
817 endif |
|
818 |
6254
|
819 ## If the file ChangeLog exists, copy it |
|
820 fChangeLog = fullfile(packdir, "ChangeLog"); |
|
821 if (exist(fChangeLog, "file")) |
|
822 [status, output] = copyfile(fChangeLog, packinfo); |
|
823 if (status != 1) |
|
824 rm_rf(desc.dir); |
|
825 error("Couldn't copy ChangeLog file: %s", output); |
|
826 endif |
|
827 endif |
|
828 |
5801
|
829 ## Is there an INDEX file to copy or should we generate one? |
6233
|
830 fINDEX = fullfile(packdir, "INDEX"); |
|
831 if (exist(fINDEX, "file")) |
|
832 [status, output] = copyfile(fINDEX, packinfo); |
|
833 if (status != 1) |
5928
|
834 rm_rf(desc.dir); |
6042
|
835 error("Couldn't copy INDEX file: %s", output); |
5801
|
836 endif |
|
837 else |
|
838 try |
6254
|
839 write_INDEX(desc, fullfile(packdir, "inst"), fullfile(packinfo, "INDEX")); |
5801
|
840 catch |
5928
|
841 rm_rf(desc.dir); |
5801
|
842 error(lasterr); |
|
843 end_try_catch |
|
844 endif |
|
845 |
|
846 ## Is there an 'on_uninstall.m' to install? |
6233
|
847 fon_uninstall = fullfile(packdir, "on_uninstall.m"); |
|
848 if (exist(fon_uninstall, "file")) |
|
849 [status, output] = copyfile(fon_uninstall, packinfo); |
|
850 if (status != 1) |
5928
|
851 rm_rf(desc.dir); |
6042
|
852 error("Couldn't copy on_uninstall.m: %s", output); |
5801
|
853 endif |
|
854 endif |
5971
|
855 |
|
856 ## Is there a doc/ directory that needs to be installed |
6233
|
857 docdir = fullfile(packdir, "doc"); |
|
858 if (exist(docdir, "dir") && !dirempty(docdir)) |
|
859 [status, output] = copyfile(docdir, desc.dir); |
5971
|
860 endif |
|
861 |
|
862 ## Is there a bin/ directory that needs to be installed |
6233
|
863 bindir = fullfile(packdir, "bin"); |
|
864 if (exist(bindir, "dir") && !dirempty(bindir)) |
|
865 [status, output] = copyfile(bindir, desc.dir); |
5971
|
866 endif |
5801
|
867 endfunction |
|
868 |
|
869 function finish_installation (desc, packdir) |
|
870 ## Is there a post-install to call? |
|
871 if (exist([packdir "post_install.m"], "file")) |
|
872 wd = pwd(); |
|
873 try |
|
874 cd(packdir); |
|
875 post_install(desc); |
|
876 cd(wd); |
|
877 catch |
|
878 cd(wd); |
5928
|
879 rm_rf(desc.dir); |
6042
|
880 error("The post_install function returned the following error: %s", lasterr); |
5801
|
881 end_try_catch |
|
882 endif |
|
883 endfunction |
|
884 |
|
885 function out = issuperuser() |
|
886 out = strcmp(getenv("USER"), "root"); |
|
887 endfunction |
|
888 |
|
889 ## This function makes sure the package contains the |
|
890 ## essential files. |
|
891 function verify_directory(dir) |
|
892 needed_files = {"COPYING", "DESCRIPTION"}; |
|
893 for f = needed_files |
6233
|
894 if (!exist(fullfile(dir, f{1}), "file")) |
5801
|
895 error("Package is missing file: %s", f{1}); |
|
896 endif |
|
897 endfor |
|
898 endfunction |
|
899 |
|
900 ## This function parses the DESCRIPTION file |
|
901 function desc = get_description(filename) |
6042
|
902 [fid, msg] = fopen(filename, "r"); |
5801
|
903 if (fid == -1) |
6042
|
904 error("The DESCRIPTION file %s could not be read: %s", filename, msg); |
5801
|
905 endif |
|
906 |
|
907 desc = struct(); |
|
908 |
|
909 line = fgetl(fid); |
|
910 while (line != -1) |
|
911 ## Comments |
|
912 if (line(1) == "#") |
|
913 # Do nothing |
|
914 ## Continuation lines |
|
915 elseif (isspace(line(1))) |
|
916 if (exist("keyword", "var") && isfield(desc, keyword)) |
|
917 desc.(keyword) = [desc.(keyword) " " rstrip(line)]; |
|
918 endif |
|
919 ## Keyword/value pair |
|
920 else |
|
921 colon = find(line == ":"); |
|
922 if (length(colon) == 0) |
|
923 disp("Skipping line."); |
|
924 else |
|
925 colon = colon(1); |
|
926 keyword = tolower(strip(line(1:colon-1))); |
|
927 value = strip(line(colon+1:end)); |
|
928 if (length(value) == 0) |
|
929 fclose(fid); |
6042
|
930 error("The keyword %s have empty value", desc.keywords{end}); |
5801
|
931 endif |
|
932 desc.(keyword) = value; |
|
933 endif |
|
934 endif |
|
935 line = fgetl(fid); |
|
936 endwhile |
|
937 fclose(fid); |
|
938 |
|
939 ## Make sure all is okay |
|
940 needed_fields = {"name", "version", "date", "title", ... |
|
941 "author", "maintainer", "description"}; |
|
942 for f = needed_fields |
|
943 if (!isfield(desc, f{1})) |
6042
|
944 error("Description is missing needed field %s", f{1}); |
5801
|
945 endif |
|
946 endfor |
|
947 desc.version = fix_version(desc.version); |
|
948 if (isfield(desc, "depends")) |
|
949 desc.depends = fix_depends(desc.depends); |
|
950 else |
|
951 desc.depends = ""; |
|
952 endif |
|
953 desc.name = tolower(desc.name); |
|
954 endfunction |
|
955 |
|
956 ## Makes sure the version string v is a valid x.y.z version string |
|
957 ## Examples: "0.1" => "0.1.0", "monkey" => error(...) |
|
958 function out = fix_version(v) |
|
959 dots = find(v == "."); |
|
960 if (length(dots) == 1) |
|
961 major = str2num(v(1:dots-1)); |
|
962 minor = str2num(v(dots+1:end)); |
|
963 if (length(major) != 0 && length(minor) != 0) |
|
964 out = sprintf("%d.%d.0", major, minor); |
|
965 return |
|
966 endif |
|
967 elseif (length(dots) == 2) |
|
968 major = str2num(v(1:dots(1)-1)); |
|
969 minor = str2num(v(dots(1)+1:dots(2)-1)); |
|
970 rev = str2num(v(dots(2)+1:end)); |
|
971 if (length(major) != 0 && length(minor) != 0 && length(rev) != 0) |
|
972 out = sprintf("%d.%d.%d", major, minor, rev); |
|
973 return |
|
974 endif |
|
975 endif |
6042
|
976 error("Bad version string: %s", v); |
5801
|
977 endfunction |
|
978 |
|
979 ## Makes sure the depends field is of the right format. |
|
980 ## This function returns a cell of structures with the following fields: |
|
981 ## package, version, operator |
|
982 function deps_cell = fix_depends(depends) |
|
983 deps = split_by(tolower(depends), ","); |
|
984 deps_cell = cell(1, length(deps)); |
|
985 |
|
986 ## For each dependency |
|
987 for i = 1:length(deps) |
|
988 dep = deps{i}; |
|
989 lpar = find(dep == "("); |
|
990 rpar = find(dep == ")"); |
|
991 ## Does the dependency specify a version |
|
992 ## Example: package(>= version) |
|
993 if (length(lpar) == 1 && length(rpar) == 1) |
|
994 package = tolower(strip(dep(1:lpar-1))); |
|
995 sub = dep( lpar(1)+1:rpar(1)-1 ); |
|
996 parts = split_by(sub, " "); |
|
997 idx = []; |
|
998 for r = 1:size(parts,1) |
|
999 if (length(parts{r}) > 0) |
|
1000 idx(end+1) = r; |
|
1001 endif |
|
1002 endfor |
|
1003 |
|
1004 if (length(idx) != 2) |
|
1005 error(["There's something wrong with the DESCRIPTION file. " ... |
|
1006 "The dependency %s has the wrong syntax.\n"], dep); |
|
1007 endif |
|
1008 operator = parts{idx(1)}; |
6336
|
1009 if (!any(strcmp(operator, {">", ">=", "<=", "<", "=="}))) |
6042
|
1010 error("Unsupported operator: %s", operator); |
5801
|
1011 endif |
|
1012 version = fix_version(parts{idx(2)}); |
|
1013 |
|
1014 ## If no version is specified for the dependency |
|
1015 ## we say that the version should be greater than |
|
1016 ## or equal to 0.0.0 |
|
1017 else |
|
1018 package = tolower(strip(dep)); |
|
1019 operator = ">="; |
|
1020 version = "0.0.0"; |
|
1021 endif |
|
1022 deps_cell{i} = struct("package", package, "operator", operator, "version", version); |
|
1023 endfor |
|
1024 endfunction |
|
1025 |
|
1026 ## Strips the text of spaces from the right |
|
1027 ## Example: " hello world " => " hello world" (XXX: is this the same as deblank?) |
|
1028 function text = rstrip(text) |
|
1029 chars = find(!isspace(text)); |
|
1030 if (length(chars) > 0) |
|
1031 text = text(chars(1):end); # XXX: shouldn't it be text = text(1:chars(end)); |
|
1032 else |
|
1033 text = ""; |
|
1034 endif |
|
1035 endfunction |
|
1036 |
|
1037 ## Strips the text of spaces from the left and the right |
|
1038 ## Example: " hello world " => "hello world" |
|
1039 function text = strip(text) |
|
1040 chars = find(!isspace(text)); |
|
1041 if (length(chars) > 0) |
|
1042 text = text(chars(1):chars(end)); |
|
1043 else |
|
1044 text = ""; |
|
1045 endif |
|
1046 endfunction |
|
1047 |
|
1048 ## Splits the text into a cell array of strings by sep |
|
1049 ## Example: "A, B" => {"A", "B"} (with sep = ",") |
|
1050 function out = split_by(text, sep) |
|
1051 text_matrix = split(text, sep); |
|
1052 num_words = size(text_matrix,1); |
|
1053 out = cell(num_words, 1); |
|
1054 for i = 1:num_words |
|
1055 out{i} = strip(text_matrix(i, :)); |
|
1056 endfor |
|
1057 endfunction |
|
1058 |
|
1059 ## Creates an INDEX file for a package that doesn't provide one. |
|
1060 ## 'desc' describes the package. |
|
1061 ## 'dir' is the 'inst' direcotyr in temporary directory. |
|
1062 ## 'INDEX' is the name (including path) of resulting INDEX file. |
|
1063 function write_INDEX(desc, dir, INDEX) |
|
1064 ## Get names of functions in dir |
|
1065 [files, err, msg] = readdir(dir); |
|
1066 if (err) |
6042
|
1067 error("Couldn't read directory %s: %s", dir, msg); |
5801
|
1068 endif |
|
1069 |
|
1070 functions = {}; |
|
1071 for i = 1:length(files) |
|
1072 file = files{i}; |
|
1073 lf = length(file); |
|
1074 if (lf > 2 && strcmp( file(end-1:end), ".m" )) |
|
1075 functions{end+1} = file(1:end-2); |
|
1076 elseif (lf > 4 && strcmp( file(end-3:end), ".oct" )) |
|
1077 functions{end+1} = file(1:end-4); |
|
1078 endif |
|
1079 endfor |
|
1080 |
|
1081 ## Does desc have a categories field? |
|
1082 if (!isfield(desc, "categories")) |
6042
|
1083 error("The DESCRIPTION file must have a Categories field, when no INDEX file is given."); |
5801
|
1084 endif |
|
1085 categories = split_by(desc.categories, ","); |
|
1086 if (length(categories) < 1) |
6042
|
1087 error("The Category field is empty."); |
5801
|
1088 endif |
|
1089 |
|
1090 ## Write INDEX |
|
1091 fid = fopen(INDEX, "w"); |
|
1092 if (fid == -1) |
6042
|
1093 error("Couldn't open %s for writing.", INDEX); |
5801
|
1094 endif |
|
1095 fprintf(fid, "%s >> %s\n", desc.name, desc.title); |
|
1096 fprintf(fid, "%s\n", categories{1}); |
|
1097 fprintf(fid, " %s\n", functions{:}); |
|
1098 fclose(fid); |
|
1099 endfunction |
|
1100 |
|
1101 function bad_deps = get_unsatisfied_deps(desc, installed_packages) |
|
1102 bad_deps = {}; |
|
1103 |
|
1104 ## For each dependency |
|
1105 for i = 1:length(desc.depends) |
|
1106 dep = desc.depends{i}; |
|
1107 |
|
1108 ## Is the current dependency Octave? |
|
1109 if (strcmp(dep.package, "octave")) |
|
1110 if (!compare_versions(OCTAVE_VERSION, dep.version, dep.operator)) |
|
1111 bad_deps{end+1} = dep; |
|
1112 endif |
|
1113 ## Is the current dependency not Octave? |
|
1114 else |
|
1115 ok = false; |
|
1116 for i = 1:length(installed_packages) |
|
1117 cur_name = installed_packages{i}.name; |
|
1118 cur_version = installed_packages{i}.version; |
|
1119 if (strcmp(dep.package, cur_name) && compare_versions(cur_version, dep.version, dep.operator)) |
|
1120 ok = true; |
|
1121 break; |
|
1122 endif |
|
1123 endfor |
|
1124 if (!ok) |
|
1125 bad_deps{end+1} = dep; |
|
1126 endif |
|
1127 endif |
|
1128 endfor |
|
1129 endfunction |
|
1130 |
6189
|
1131 function [out1, out2] = installed_packages(local_list, global_list) |
5801
|
1132 ## Get the list of installed packages |
|
1133 try |
|
1134 local_packages = load(local_list).local_packages; |
|
1135 catch |
|
1136 local_packages = {}; |
|
1137 end_try_catch |
|
1138 try |
6189
|
1139 if (strcmp(local_list, global_list)) |
|
1140 global_packages = {}; |
|
1141 else |
|
1142 global_packages = load(global_list).global_packages; |
|
1143 endif |
5801
|
1144 catch |
|
1145 global_packages = {}; |
|
1146 end_try_catch |
|
1147 installed_packages = {local_packages{:} global_packages{:}}; |
6258
|
1148 |
|
1149 ## Eliminate duplicates in the installed package list. |
|
1150 ## Locally installed packages take precedence |
|
1151 dup = []; |
|
1152 for i=1:length(installed_packages) |
|
1153 if (find(dup,i)) |
|
1154 continue; |
|
1155 endif |
|
1156 for j=(i+1):length(installed_packages) |
|
1157 if (find(dup,j)) |
|
1158 continue; |
|
1159 endif |
|
1160 if (strcmp(installed_packages{i}.name,installed_packages{j}.name)) |
|
1161 dup = [dup, j]; |
|
1162 endif |
|
1163 endfor |
|
1164 endfor |
|
1165 if (! isempty(dup)) |
|
1166 installed_packages(dup) = []; |
|
1167 endif |
|
1168 |
5801
|
1169 ## Should we return something? |
|
1170 if (nargout == 2) |
|
1171 out1 = local_packages; |
|
1172 out2 = global_packages; |
|
1173 return; |
|
1174 elseif (nargout == 1) |
|
1175 out1 = installed_packages; |
|
1176 return; |
|
1177 endif |
|
1178 |
|
1179 ## We shouldn't return something, so we'll print something |
|
1180 num_packages = length(installed_packages); |
|
1181 if (num_packages == 0) |
|
1182 printf("No packages installed.\n"); |
|
1183 return; |
|
1184 endif |
|
1185 |
5987
|
1186 ## Compute the maximal lengths of name, version, and dir |
5801
|
1187 h1 = "Package Name"; |
|
1188 h2 = "Version"; |
|
1189 h3 = "Installation directory"; |
5987
|
1190 max_name_length = length(h1); |
|
1191 max_version_length = length(h2); |
|
1192 names = cell(num_packages,1); |
|
1193 for i = 1:num_packages |
|
1194 max_name_length = max(max_name_length, |
|
1195 length(installed_packages{i}.name)); |
|
1196 max_version_length = max(max_version_length, |
|
1197 length(installed_packages{i}.version)); |
|
1198 names{i} = installed_packages{i}.name; |
|
1199 endfor |
|
1200 h1 = postpad (h1, max_name_length,' '); |
|
1201 h2 = postpad (h2, max_version_length, ' ');; |
|
1202 |
|
1203 ## Print a header |
5801
|
1204 header = sprintf("%s | %s | %s\n", h1, h2, h3); |
|
1205 printf(header); |
|
1206 tmp = sprintf(repmat("-", 1, length(header)-1)); |
|
1207 tmp(length(h1)+2) = "+"; tmp(length(h1)+length(h2)+5) = "+"; |
|
1208 printf("%s\n", tmp); |
|
1209 |
|
1210 ## Print the packages |
|
1211 format = sprintf("%%%ds | %%%ds | %%s\n", max_name_length, max_version_length); |
5987
|
1212 [dummy, idx] = sort(names); |
5801
|
1213 for i = 1:num_packages |
5987
|
1214 cur_name = installed_packages{idx(i)}.name; |
|
1215 cur_version = installed_packages{idx(i)}.version; |
|
1216 cur_dir = installed_packages{idx(i)}.dir; |
5801
|
1217 printf(format, cur_name, cur_version, cur_dir); |
|
1218 endfor |
|
1219 endfunction |
|
1220 |
6189
|
1221 function load_packages(files, handle_deps, local_list, global_list) |
|
1222 installed_packages = installed_packages(local_list, global_list); |
5801
|
1223 num_packages = length(installed_packages); |
|
1224 |
6037
|
1225 ## Read package names and installdirs into a more convenient format |
|
1226 pnames = pdirs = cell(1, num_packages); |
|
1227 for i = 1:num_packages |
|
1228 pnames{i} = installed_packages{i}.name; |
|
1229 pdirs{i} = installed_packages{i}.dir; |
|
1230 pdeps{i} = installed_packages{i}.depends; |
|
1231 endfor |
|
1232 |
|
1233 ## load all |
5801
|
1234 if (length(files) == 1 && strcmp(files{1}, "all")) |
6037
|
1235 dirs = pdirs; |
6258
|
1236 ## load auto |
|
1237 elseif (length(files) == 1 && strcmp(files{1}, "auto")) |
|
1238 dirs = {}; |
|
1239 for i = 1:length(installed_packages) |
|
1240 if (exist(fullfile(pdirs{i}, "packinfo", ".autoload"), "file")) |
|
1241 dirs{end+1} = pdirs{i}; |
|
1242 endif |
|
1243 endfor |
6037
|
1244 ## load package_name1 ... |
5801
|
1245 else |
6037
|
1246 dirs = {}; |
|
1247 for i = 1:length(files) |
|
1248 idx = strcmp(pnames, files{i}); |
|
1249 if (!any(idx)) |
6042
|
1250 error("Package %s is not installed", files{i}); |
6037
|
1251 endif |
|
1252 dirs{end+1} = pdirs{idx}; |
|
1253 if (handle_deps) |
|
1254 pdep = pdeps{idx}; |
|
1255 for j = 1:length(pdep) |
|
1256 depname = pdep{j}.package; |
|
1257 if (strcmp(depname, "octave")) continue; endif |
|
1258 idx = strcmp(pnames, depname); |
|
1259 if (!any(idx)) |
|
1260 error("Package %s could not be loaded since it depends on %s", ... |
|
1261 files{i}, depname); |
|
1262 endif |
|
1263 dirs{end+1} = pdirs{idx}; |
|
1264 endfor |
5971
|
1265 endif |
5801
|
1266 endfor |
6037
|
1267 dirs = unique(dirs); |
5801
|
1268 endif |
6037
|
1269 |
|
1270 ## Load the packages |
6025
|
1271 if (length(dirs) > 0) |
|
1272 addpath(dirs{:}); |
|
1273 endif |
5971
|
1274 |
|
1275 ## Add local binaries, if any, to the EXEC_PATH |
|
1276 for i = 1:length(dirs) |
6233
|
1277 if (exist (fullfile(dirs{i}, "bin"), "dir")) |
|
1278 EXEC_PATH ([fullfile(dirs{i}, "bin") ":" EXEC_PATH()]); |
5971
|
1279 endif |
|
1280 endfor |
5801
|
1281 endfunction |
5928
|
1282 |
6203
|
1283 function unload_packages(files, handle_deps, local_list, global_list) |
|
1284 installed_packages = installed_packages(local_list, global_list); |
|
1285 num_packages = length(installed_packages); |
|
1286 |
|
1287 ## Read package names and installdirs into a more convenient format |
|
1288 pnames = pdirs = cell(1, num_packages); |
|
1289 for i = 1:num_packages |
|
1290 pnames{i} = installed_packages{i}.name; |
|
1291 pdirs{i} = installed_packages{i}.dir; |
|
1292 pdeps{i} = installed_packages{i}.depends; |
|
1293 endfor |
|
1294 |
|
1295 ## Get the current octave path |
|
1296 p = split_by(path(), pathsep()); |
|
1297 |
|
1298 ## unload all |
|
1299 if (length(files) == 1 && strcmp(files{1}, "all")) |
|
1300 dirs = pdirs; |
|
1301 ## unload package_name1 ... |
|
1302 else |
|
1303 dirs = {}; |
|
1304 for i = 1:length(files) |
|
1305 idx = strcmp(pnames, files{i}); |
|
1306 if (!any(idx)) |
|
1307 error("Package %s is not installed", files{i}); |
|
1308 endif |
|
1309 dirs{end+1} = pdirs{idx}; |
|
1310 endfor |
|
1311 endif |
|
1312 |
|
1313 ## Unload the packages |
|
1314 for i = 1:length(dirs) |
|
1315 d = dirs{i}; |
|
1316 idx = strcmp(p, d); |
|
1317 if (any(idx)) |
|
1318 rmpath(d); |
|
1319 # XXX: We should also check if we need to remove items from EXEC_PATH |
|
1320 endif |
|
1321 endfor |
|
1322 endfunction |
|
1323 |
5928
|
1324 function [status_out, msg_out] = rm_rf (dir) |
|
1325 crr = confirm_recursive_rmdir (); |
|
1326 unwind_protect |
|
1327 confirm_recursive_rmdir (false); |
|
1328 [status, msg] = rmdir (dir, "s"); |
|
1329 unwind_protect_cleanup |
|
1330 confirm_recursive_rmdir (crr); |
|
1331 end_unwind_protect |
|
1332 if (nargout > 0) |
|
1333 status_out = status; |
|
1334 endif |
|
1335 if (nargout > 1) |
|
1336 msg_out = msg; |
|
1337 endif |
|
1338 endfunction |
5971
|
1339 |
|
1340 function emp = dirempty (nm, ign) |
|
1341 if (nargin < 2) |
|
1342 ign = {".",".."}; |
|
1343 else |
|
1344 ign = [{".",".."},ign]; |
|
1345 endif |
|
1346 l = dir (nm); |
|
1347 for i=1:length(l) |
|
1348 found = false; |
|
1349 for j=1:length(ign) |
|
1350 if (strcmp(l(i).name,ign{j})) |
|
1351 found = true; |
|
1352 break; |
|
1353 endif |
|
1354 endfor |
|
1355 if (!found) |
|
1356 emp = false; |
|
1357 return |
|
1358 endif |
|
1359 endfor |
|
1360 emp = true; |
|
1361 return; |
|
1362 endfunction |