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 |
|
21 ## be taking depending on the value of @var{command}. |
|
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; |
6189
|
130 persistent local_list = tilde_expand("~/.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()) |
6233
|
136 prefix = fullfile (OCTAVE_HOME (), "/share/octave/packages"); |
6034
|
137 else |
6233
|
138 prefix = "~/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}; |
6233
|
206 #if (!strcmp(prefix(end), "/")) prefix(end+1) = "/"; 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? |
|
608 if (exist([packdir "pre_install.m"], "file")) |
|
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 |
|
621 if (!exist([packdir "inst"], "dir")) |
5801
|
622 [status, msg] = mkdir([packdir "inst"]); |
|
623 if (status != 1) |
5928
|
624 rm_rf(desc.dir); |
6042
|
625 error("The 'inst' directory did not exist and could not be created: %s", msg); |
5801
|
626 endif |
|
627 endif |
|
628 endfunction |
|
629 |
|
630 function configure_make (desc, packdir) |
|
631 ## Perform ./configure, make, make install in "src" |
6246
|
632 if (exist(fullfile(packdir, "src"), "dir")) |
|
633 src = fullfile(packdir, "src"); |
5801
|
634 ## configure |
6233
|
635 if (exist(fullfile(src, "configure"), "file")) |
5928
|
636 [status, output] = system(["cd " src " ;./configure --prefix=" desc.dir]); |
5801
|
637 if (status != 0) |
5928
|
638 rm_rf(desc.dir); |
6042
|
639 error("The configure script returned the following error: %s", output); |
5801
|
640 endif |
|
641 endif |
|
642 |
|
643 ## make |
6233
|
644 if (exist(fullfile(src, "Makefile"), "file")) |
5928
|
645 [status, output] = system(["export INSTALLDIR=" desc.dir "; make -C " src]); |
5801
|
646 if (status != 0) |
5928
|
647 rm_rf(desc.dir); |
6042
|
648 error("'make' returned the following error: %s", output); |
5801
|
649 endif |
|
650 %# make install |
5928
|
651 %[status, output] = system(["export INSTALLDIR=" desc.dir "; make install -C " src]); |
5801
|
652 %if (status != 0) |
5928
|
653 % rm_rf(desc.dir); |
6042
|
654 % error("'make install' returned the following error: %s", output); |
5801
|
655 %endif |
|
656 endif |
|
657 |
|
658 ## Copy files to "inst" (this is instead of 'make install') |
6233
|
659 files = fullfile(src, "FILES"); |
|
660 instdir = fullfile(packdir, "inst"); |
5801
|
661 if (exist(files, "file")) |
6233
|
662 ## Get file names |
5801
|
663 [fid, msg] = fopen(files, "r"); |
|
664 if (fid < 0) |
6042
|
665 error("Couldn't open %s: %s", files, msg); |
5801
|
666 endif |
5928
|
667 filenames = char(fread(fid))'; |
5801
|
668 fclose(fid); |
6233
|
669 ## Copy the files |
|
670 fn = split_by(filenames, "\n"); |
|
671 for i = 1:length(fn) |
|
672 fn{i} = fullfile(src, fn{i}); |
|
673 endfor |
|
674 filenames = sprintf("%s ", fn{:}); |
5801
|
675 else |
6233
|
676 m = dir(fullfile(src, "*.m")); |
|
677 oct = dir(fullfile(src, "*.oct")); |
|
678 filenames = ""; |
5801
|
679 if (length(m) > 0) |
6246
|
680 filenames = sprintf(fullfile(src, "%s "), m.name); |
5801
|
681 endif |
|
682 if (length(oct) > 0) |
6246
|
683 filenames = [filenames " " sprintf(fullfile(src, "%s "), oct.name)]; |
5928
|
684 endif |
5801
|
685 endif |
6246
|
686 filenames = split_by(filenames, " "); |
6233
|
687 |
|
688 if (!all(isspace(filenames))) |
|
689 mkdir(instdir); |
|
690 [status, output] = copyfile(filenames, instdir); |
|
691 if (status != 1) |
|
692 rm_rf(desc.dir); |
|
693 error("Couldn't copy files from 'src' to 'inst': %s", output); |
|
694 endif |
5801
|
695 endif |
|
696 endif |
|
697 endfunction |
|
698 |
5971
|
699 function pkg = extract_pkg (nm, pat) |
5955
|
700 fid = fopen (nm, "rt"); |
5971
|
701 pkg = ""; |
5955
|
702 if (fid >= 0) |
|
703 while (! feof(fid)) |
|
704 ln = fgetl (fid); |
|
705 if (ln > 0) |
6143
|
706 t = regexp(ln, pat, "tokens"); |
5955
|
707 if (!isempty(t)) |
5971
|
708 pkg = [pkg, "\n", t{1}{1}]; |
5955
|
709 endif |
|
710 endif |
|
711 endwhile |
5971
|
712 if (!isempty(pkg)) |
|
713 pkg = [pkg, "\n"]; |
5955
|
714 endif |
|
715 fclose (fid); |
|
716 endif |
|
717 endfunction |
|
718 |
5971
|
719 function create_pkgadddel (desc, packdir, nm) |
6233
|
720 pkg = fullfile(desc.dir, nm); |
5971
|
721 fid = fopen(pkg, "wt"); |
5976
|
722 |
5955
|
723 if (fid >= 0) |
5971
|
724 ## Search all dot-m files for PKG commands |
6233
|
725 lst = dir (fullfile(packdir, "inst", "*.m")); |
5955
|
726 for i=1:length(lst) |
6233
|
727 nam = fullfile(packdir, "inst", lst(i).name); |
5976
|
728 fwrite (fid, extract_pkg (nam, ['^[#%][#%]* *' nm ': *(.*)$'])); |
5955
|
729 endfor |
|
730 |
5971
|
731 ## Search all C++ source files for PKG commands |
6233
|
732 lst = dir (fullfile(packdir, "src", "*.cc")); |
5955
|
733 for i=1:length(lst) |
6233
|
734 nam = fullfile(packdir, "src", lst(i).name); |
5976
|
735 fwrite (fid, extract_pkg (nam, ['^//* *' nm ': *(.*)$'])); |
|
736 fwrite (fid, extract_pkg (nam, ['^/\** *' nm ': *(.*) *\*/$'])); |
5955
|
737 endfor |
|
738 |
5971
|
739 ## Add developer included PKG commands |
6245
|
740 packdirnm = fullfile(packdir, nm); |
6233
|
741 if (exist(packdirnm, "file")) |
|
742 fid2 = fopen(packdirnm,"rt"); |
5955
|
743 if (fid2 >= 0) |
|
744 while (! feof(fid2)) |
|
745 ln = fgets (fid2); |
|
746 if (ln > 0) |
|
747 fwrite(fid, ln); |
|
748 endif |
|
749 endwhile |
5971
|
750 fclose(fid2); |
5955
|
751 endif |
|
752 endif |
|
753 fclose(fid); |
5971
|
754 |
|
755 ## If the file is empty remove it |
|
756 t = dir (pkg); |
|
757 if (t.bytes <= 0) |
|
758 unlink (pkg); |
|
759 endif |
5955
|
760 endif |
|
761 endfunction |
|
762 |
5971
|
763 function copy_files (desc, packdir, bindir) |
6020
|
764 ## Create the installation directory |
|
765 if (! exist (desc.dir, "dir")) |
6233
|
766 [status, output] = mkdir (desc.dir); |
|
767 if (status != 1) |
|
768 error("Couldn't create installation directory %s : %s", |
|
769 desc.dir, output); |
|
770 endif |
6020
|
771 endif |
|
772 |
5801
|
773 ## Copy the files from "inst" to installdir |
6233
|
774 instdir = fullfile(packdir, "inst"); |
|
775 if (!dirempty(instdir)) |
|
776 [status, output] = copyfile(fullfile(instdir, "*"), desc.dir); |
|
777 if (status != 1) |
5971
|
778 rm_rf(desc.dir); |
6042
|
779 error("Couldn't copy files to the installation directory"); |
5971
|
780 endif |
5801
|
781 endif |
|
782 |
|
783 ## Create the "packinfo" directory |
6233
|
784 packinfo = fullfile(desc.dir, "packinfo"); |
5801
|
785 [status, msg] = mkdir (packinfo); |
|
786 if (status != 1) |
5928
|
787 rm_rf(desc.dir); |
6042
|
788 error("Couldn't create packinfo directory: %s", msg); |
5801
|
789 endif |
|
790 |
|
791 ## Copy DESCRIPTION |
6233
|
792 [status, output] = copyfile(fullfile(packdir, "DESCRIPTION"), packinfo); |
|
793 if (status != 1) |
5928
|
794 rm_rf(desc.dir); |
6042
|
795 error("Couldn't copy DESCRIPTION: %s", output); |
5801
|
796 endif |
|
797 |
5993
|
798 ## Copy COPYING |
6233
|
799 [status, output] = copyfile(fullfile(packdir, "COPYING"), packinfo); |
|
800 if (status != 1) |
5993
|
801 rm_rf(desc.dir); |
6042
|
802 error("Couldn't copy COPYING: %s", output); |
5993
|
803 endif |
|
804 |
6254
|
805 ## If the file ChangeLog exists, copy it |
|
806 fChangeLog = fullfile(packdir, "ChangeLog"); |
|
807 if (exist(fChangeLog, "file")) |
|
808 [status, output] = copyfile(fChangeLog, packinfo); |
|
809 if (status != 1) |
|
810 rm_rf(desc.dir); |
|
811 error("Couldn't copy ChangeLog file: %s", output); |
|
812 endif |
|
813 endif |
|
814 |
5801
|
815 ## Is there an INDEX file to copy or should we generate one? |
6233
|
816 fINDEX = fullfile(packdir, "INDEX"); |
|
817 if (exist(fINDEX, "file")) |
|
818 [status, output] = copyfile(fINDEX, packinfo); |
|
819 if (status != 1) |
5928
|
820 rm_rf(desc.dir); |
6042
|
821 error("Couldn't copy INDEX file: %s", output); |
5801
|
822 endif |
|
823 else |
|
824 try |
6254
|
825 write_INDEX(desc, fullfile(packdir, "inst"), fullfile(packinfo, "INDEX")); |
5801
|
826 catch |
5928
|
827 rm_rf(desc.dir); |
5801
|
828 error(lasterr); |
|
829 end_try_catch |
|
830 endif |
|
831 |
|
832 ## Is there an 'on_uninstall.m' to install? |
6233
|
833 fon_uninstall = fullfile(packdir, "on_uninstall.m"); |
|
834 if (exist(fon_uninstall, "file")) |
|
835 [status, output] = copyfile(fon_uninstall, packinfo); |
|
836 if (status != 1) |
5928
|
837 rm_rf(desc.dir); |
6042
|
838 error("Couldn't copy on_uninstall.m: %s", output); |
5801
|
839 endif |
|
840 endif |
5971
|
841 |
|
842 ## Is there a doc/ directory that needs to be installed |
6233
|
843 docdir = fullfile(packdir, "doc"); |
|
844 if (exist(docdir, "dir") && !dirempty(docdir)) |
|
845 [status, output] = copyfile(docdir, desc.dir); |
5971
|
846 endif |
|
847 |
|
848 ## Is there a bin/ directory that needs to be installed |
6233
|
849 bindir = fullfile(packdir, "bin"); |
|
850 if (exist(bindir, "dir") && !dirempty(bindir)) |
|
851 [status, output] = copyfile(bindir, desc.dir); |
5971
|
852 endif |
5801
|
853 endfunction |
|
854 |
|
855 function finish_installation (desc, packdir) |
|
856 ## Is there a post-install to call? |
|
857 if (exist([packdir "post_install.m"], "file")) |
|
858 wd = pwd(); |
|
859 try |
|
860 cd(packdir); |
|
861 post_install(desc); |
|
862 cd(wd); |
|
863 catch |
|
864 cd(wd); |
5928
|
865 rm_rf(desc.dir); |
6042
|
866 error("The post_install function returned the following error: %s", lasterr); |
5801
|
867 end_try_catch |
|
868 endif |
|
869 endfunction |
|
870 |
|
871 function out = issuperuser() |
|
872 out = strcmp(getenv("USER"), "root"); |
|
873 endfunction |
|
874 |
|
875 ## This function makes sure the package contains the |
|
876 ## essential files. |
|
877 function verify_directory(dir) |
|
878 needed_files = {"COPYING", "DESCRIPTION"}; |
|
879 for f = needed_files |
6233
|
880 if (!exist(fullfile(dir, f{1}), "file")) |
5801
|
881 error("Package is missing file: %s", f{1}); |
|
882 endif |
|
883 endfor |
|
884 endfunction |
|
885 |
|
886 ## This function parses the DESCRIPTION file |
|
887 function desc = get_description(filename) |
6042
|
888 [fid, msg] = fopen(filename, "r"); |
5801
|
889 if (fid == -1) |
6042
|
890 error("The DESCRIPTION file %s could not be read: %s", filename, msg); |
5801
|
891 endif |
|
892 |
|
893 desc = struct(); |
|
894 |
|
895 line = fgetl(fid); |
|
896 while (line != -1) |
|
897 ## Comments |
|
898 if (line(1) == "#") |
|
899 # Do nothing |
|
900 ## Continuation lines |
|
901 elseif (isspace(line(1))) |
|
902 if (exist("keyword", "var") && isfield(desc, keyword)) |
|
903 desc.(keyword) = [desc.(keyword) " " rstrip(line)]; |
|
904 endif |
|
905 ## Keyword/value pair |
|
906 else |
|
907 colon = find(line == ":"); |
|
908 if (length(colon) == 0) |
|
909 disp("Skipping line."); |
|
910 else |
|
911 colon = colon(1); |
|
912 keyword = tolower(strip(line(1:colon-1))); |
|
913 value = strip(line(colon+1:end)); |
|
914 if (length(value) == 0) |
|
915 fclose(fid); |
6042
|
916 error("The keyword %s have empty value", desc.keywords{end}); |
5801
|
917 endif |
|
918 desc.(keyword) = value; |
|
919 endif |
|
920 endif |
|
921 line = fgetl(fid); |
|
922 endwhile |
|
923 fclose(fid); |
|
924 |
|
925 ## Make sure all is okay |
|
926 needed_fields = {"name", "version", "date", "title", ... |
|
927 "author", "maintainer", "description"}; |
|
928 for f = needed_fields |
|
929 if (!isfield(desc, f{1})) |
6042
|
930 error("Description is missing needed field %s", f{1}); |
5801
|
931 endif |
|
932 endfor |
|
933 desc.version = fix_version(desc.version); |
|
934 if (isfield(desc, "depends")) |
|
935 desc.depends = fix_depends(desc.depends); |
|
936 else |
|
937 desc.depends = ""; |
|
938 endif |
|
939 desc.name = tolower(desc.name); |
|
940 endfunction |
|
941 |
|
942 ## Makes sure the version string v is a valid x.y.z version string |
|
943 ## Examples: "0.1" => "0.1.0", "monkey" => error(...) |
|
944 function out = fix_version(v) |
|
945 dots = find(v == "."); |
|
946 if (length(dots) == 1) |
|
947 major = str2num(v(1:dots-1)); |
|
948 minor = str2num(v(dots+1:end)); |
|
949 if (length(major) != 0 && length(minor) != 0) |
|
950 out = sprintf("%d.%d.0", major, minor); |
|
951 return |
|
952 endif |
|
953 elseif (length(dots) == 2) |
|
954 major = str2num(v(1:dots(1)-1)); |
|
955 minor = str2num(v(dots(1)+1:dots(2)-1)); |
|
956 rev = str2num(v(dots(2)+1:end)); |
|
957 if (length(major) != 0 && length(minor) != 0 && length(rev) != 0) |
|
958 out = sprintf("%d.%d.%d", major, minor, rev); |
|
959 return |
|
960 endif |
|
961 endif |
6042
|
962 error("Bad version string: %s", v); |
5801
|
963 endfunction |
|
964 |
|
965 ## Makes sure the depends field is of the right format. |
|
966 ## This function returns a cell of structures with the following fields: |
|
967 ## package, version, operator |
|
968 function deps_cell = fix_depends(depends) |
|
969 deps = split_by(tolower(depends), ","); |
|
970 deps_cell = cell(1, length(deps)); |
|
971 |
|
972 ## For each dependency |
|
973 for i = 1:length(deps) |
|
974 dep = deps{i}; |
|
975 lpar = find(dep == "("); |
|
976 rpar = find(dep == ")"); |
|
977 ## Does the dependency specify a version |
|
978 ## Example: package(>= version) |
|
979 if (length(lpar) == 1 && length(rpar) == 1) |
|
980 package = tolower(strip(dep(1:lpar-1))); |
|
981 sub = dep( lpar(1)+1:rpar(1)-1 ); |
|
982 parts = split_by(sub, " "); |
|
983 idx = []; |
|
984 for r = 1:size(parts,1) |
|
985 if (length(parts{r}) > 0) |
|
986 idx(end+1) = r; |
|
987 endif |
|
988 endfor |
|
989 |
|
990 if (length(idx) != 2) |
|
991 error(["There's something wrong with the DESCRIPTION file. " ... |
|
992 "The dependency %s has the wrong syntax.\n"], dep); |
|
993 endif |
|
994 operator = parts{idx(1)}; |
|
995 if (!any(strcmp(operator, {">=", "<=", "=="}))) ## XXX: I belive we also support ">" and "<" |
6042
|
996 error("Unsupported operator: %s", operator); |
5801
|
997 endif |
|
998 version = fix_version(parts{idx(2)}); |
|
999 |
|
1000 ## If no version is specified for the dependency |
|
1001 ## we say that the version should be greater than |
|
1002 ## or equal to 0.0.0 |
|
1003 else |
|
1004 package = tolower(strip(dep)); |
|
1005 operator = ">="; |
|
1006 version = "0.0.0"; |
|
1007 endif |
|
1008 deps_cell{i} = struct("package", package, "operator", operator, "version", version); |
|
1009 endfor |
|
1010 endfunction |
|
1011 |
|
1012 ## Strips the text of spaces from the right |
|
1013 ## Example: " hello world " => " hello world" (XXX: is this the same as deblank?) |
|
1014 function text = rstrip(text) |
|
1015 chars = find(!isspace(text)); |
|
1016 if (length(chars) > 0) |
|
1017 text = text(chars(1):end); # XXX: shouldn't it be text = text(1:chars(end)); |
|
1018 else |
|
1019 text = ""; |
|
1020 endif |
|
1021 endfunction |
|
1022 |
|
1023 ## Strips the text of spaces from the left and the right |
|
1024 ## Example: " hello world " => "hello world" |
|
1025 function text = strip(text) |
|
1026 chars = find(!isspace(text)); |
|
1027 if (length(chars) > 0) |
|
1028 text = text(chars(1):chars(end)); |
|
1029 else |
|
1030 text = ""; |
|
1031 endif |
|
1032 endfunction |
|
1033 |
|
1034 ## Splits the text into a cell array of strings by sep |
|
1035 ## Example: "A, B" => {"A", "B"} (with sep = ",") |
|
1036 function out = split_by(text, sep) |
|
1037 text_matrix = split(text, sep); |
|
1038 num_words = size(text_matrix,1); |
|
1039 out = cell(num_words, 1); |
|
1040 for i = 1:num_words |
|
1041 out{i} = strip(text_matrix(i, :)); |
|
1042 endfor |
|
1043 endfunction |
|
1044 |
|
1045 ## Creates an INDEX file for a package that doesn't provide one. |
|
1046 ## 'desc' describes the package. |
|
1047 ## 'dir' is the 'inst' direcotyr in temporary directory. |
|
1048 ## 'INDEX' is the name (including path) of resulting INDEX file. |
|
1049 function write_INDEX(desc, dir, INDEX) |
|
1050 ## Get names of functions in dir |
|
1051 [files, err, msg] = readdir(dir); |
|
1052 if (err) |
6042
|
1053 error("Couldn't read directory %s: %s", dir, msg); |
5801
|
1054 endif |
|
1055 |
|
1056 functions = {}; |
|
1057 for i = 1:length(files) |
|
1058 file = files{i}; |
|
1059 lf = length(file); |
|
1060 if (lf > 2 && strcmp( file(end-1:end), ".m" )) |
|
1061 functions{end+1} = file(1:end-2); |
|
1062 elseif (lf > 4 && strcmp( file(end-3:end), ".oct" )) |
|
1063 functions{end+1} = file(1:end-4); |
|
1064 endif |
|
1065 endfor |
|
1066 |
|
1067 ## Does desc have a categories field? |
|
1068 if (!isfield(desc, "categories")) |
6042
|
1069 error("The DESCRIPTION file must have a Categories field, when no INDEX file is given."); |
5801
|
1070 endif |
|
1071 categories = split_by(desc.categories, ","); |
|
1072 if (length(categories) < 1) |
6042
|
1073 error("The Category field is empty."); |
5801
|
1074 endif |
|
1075 |
|
1076 ## Write INDEX |
|
1077 fid = fopen(INDEX, "w"); |
|
1078 if (fid == -1) |
6042
|
1079 error("Couldn't open %s for writing.", INDEX); |
5801
|
1080 endif |
|
1081 fprintf(fid, "%s >> %s\n", desc.name, desc.title); |
|
1082 fprintf(fid, "%s\n", categories{1}); |
|
1083 fprintf(fid, " %s\n", functions{:}); |
|
1084 fclose(fid); |
|
1085 endfunction |
|
1086 |
|
1087 function bad_deps = get_unsatisfied_deps(desc, installed_packages) |
|
1088 bad_deps = {}; |
|
1089 |
|
1090 ## For each dependency |
|
1091 for i = 1:length(desc.depends) |
|
1092 dep = desc.depends{i}; |
|
1093 |
|
1094 ## Is the current dependency Octave? |
|
1095 if (strcmp(dep.package, "octave")) |
|
1096 if (!compare_versions(OCTAVE_VERSION, dep.version, dep.operator)) |
|
1097 bad_deps{end+1} = dep; |
|
1098 endif |
|
1099 ## Is the current dependency not Octave? |
|
1100 else |
|
1101 ok = false; |
|
1102 for i = 1:length(installed_packages) |
|
1103 cur_name = installed_packages{i}.name; |
|
1104 cur_version = installed_packages{i}.version; |
|
1105 if (strcmp(dep.package, cur_name) && compare_versions(cur_version, dep.version, dep.operator)) |
|
1106 ok = true; |
|
1107 break; |
|
1108 endif |
|
1109 endfor |
|
1110 if (!ok) |
|
1111 bad_deps{end+1} = dep; |
|
1112 endif |
|
1113 endif |
|
1114 endfor |
|
1115 endfunction |
|
1116 |
6189
|
1117 function [out1, out2] = installed_packages(local_list, global_list) |
5801
|
1118 ## Get the list of installed packages |
|
1119 try |
|
1120 local_packages = load(local_list).local_packages; |
|
1121 catch |
|
1122 local_packages = {}; |
|
1123 end_try_catch |
|
1124 try |
6189
|
1125 if (strcmp(local_list, global_list)) |
|
1126 global_packages = {}; |
|
1127 else |
|
1128 global_packages = load(global_list).global_packages; |
|
1129 endif |
5801
|
1130 catch |
|
1131 global_packages = {}; |
|
1132 end_try_catch |
|
1133 installed_packages = {local_packages{:} global_packages{:}}; |
6258
|
1134 |
|
1135 ## Eliminate duplicates in the installed package list. |
|
1136 ## Locally installed packages take precedence |
|
1137 dup = []; |
|
1138 for i=1:length(installed_packages) |
|
1139 if (find(dup,i)) |
|
1140 continue; |
|
1141 endif |
|
1142 for j=(i+1):length(installed_packages) |
|
1143 if (find(dup,j)) |
|
1144 continue; |
|
1145 endif |
|
1146 if (strcmp(installed_packages{i}.name,installed_packages{j}.name)) |
|
1147 dup = [dup, j]; |
|
1148 endif |
|
1149 endfor |
|
1150 endfor |
|
1151 if (! isempty(dup)) |
|
1152 installed_packages(dup) = []; |
|
1153 endif |
|
1154 |
5801
|
1155 ## Should we return something? |
|
1156 if (nargout == 2) |
|
1157 out1 = local_packages; |
|
1158 out2 = global_packages; |
|
1159 return; |
|
1160 elseif (nargout == 1) |
|
1161 out1 = installed_packages; |
|
1162 return; |
|
1163 endif |
|
1164 |
|
1165 ## We shouldn't return something, so we'll print something |
|
1166 num_packages = length(installed_packages); |
|
1167 if (num_packages == 0) |
|
1168 printf("No packages installed.\n"); |
|
1169 return; |
|
1170 endif |
|
1171 |
5987
|
1172 ## Compute the maximal lengths of name, version, and dir |
5801
|
1173 h1 = "Package Name"; |
|
1174 h2 = "Version"; |
|
1175 h3 = "Installation directory"; |
5987
|
1176 max_name_length = length(h1); |
|
1177 max_version_length = length(h2); |
|
1178 names = cell(num_packages,1); |
|
1179 for i = 1:num_packages |
|
1180 max_name_length = max(max_name_length, |
|
1181 length(installed_packages{i}.name)); |
|
1182 max_version_length = max(max_version_length, |
|
1183 length(installed_packages{i}.version)); |
|
1184 names{i} = installed_packages{i}.name; |
|
1185 endfor |
|
1186 h1 = postpad (h1, max_name_length,' '); |
|
1187 h2 = postpad (h2, max_version_length, ' ');; |
|
1188 |
|
1189 ## Print a header |
5801
|
1190 header = sprintf("%s | %s | %s\n", h1, h2, h3); |
|
1191 printf(header); |
|
1192 tmp = sprintf(repmat("-", 1, length(header)-1)); |
|
1193 tmp(length(h1)+2) = "+"; tmp(length(h1)+length(h2)+5) = "+"; |
|
1194 printf("%s\n", tmp); |
|
1195 |
|
1196 ## Print the packages |
|
1197 format = sprintf("%%%ds | %%%ds | %%s\n", max_name_length, max_version_length); |
5987
|
1198 [dummy, idx] = sort(names); |
5801
|
1199 for i = 1:num_packages |
5987
|
1200 cur_name = installed_packages{idx(i)}.name; |
|
1201 cur_version = installed_packages{idx(i)}.version; |
|
1202 cur_dir = installed_packages{idx(i)}.dir; |
5801
|
1203 printf(format, cur_name, cur_version, cur_dir); |
|
1204 endfor |
|
1205 endfunction |
|
1206 |
6189
|
1207 function load_packages(files, handle_deps, local_list, global_list) |
|
1208 installed_packages = installed_packages(local_list, global_list); |
5801
|
1209 num_packages = length(installed_packages); |
|
1210 |
6037
|
1211 ## Read package names and installdirs into a more convenient format |
|
1212 pnames = pdirs = cell(1, num_packages); |
|
1213 for i = 1:num_packages |
|
1214 pnames{i} = installed_packages{i}.name; |
|
1215 pdirs{i} = installed_packages{i}.dir; |
|
1216 pdeps{i} = installed_packages{i}.depends; |
|
1217 endfor |
|
1218 |
|
1219 ## load all |
5801
|
1220 if (length(files) == 1 && strcmp(files{1}, "all")) |
6037
|
1221 dirs = pdirs; |
6258
|
1222 ## load auto |
|
1223 elseif (length(files) == 1 && strcmp(files{1}, "auto")) |
|
1224 dirs = {}; |
|
1225 for i = 1:length(installed_packages) |
|
1226 if (exist(fullfile(pdirs{i}, "packinfo", ".autoload"), "file")) |
|
1227 dirs{end+1} = pdirs{i}; |
|
1228 endif |
|
1229 endfor |
6037
|
1230 ## load package_name1 ... |
5801
|
1231 else |
6037
|
1232 dirs = {}; |
|
1233 for i = 1:length(files) |
|
1234 idx = strcmp(pnames, files{i}); |
|
1235 if (!any(idx)) |
6042
|
1236 error("Package %s is not installed", files{i}); |
6037
|
1237 endif |
|
1238 dirs{end+1} = pdirs{idx}; |
|
1239 if (handle_deps) |
|
1240 pdep = pdeps{idx}; |
|
1241 for j = 1:length(pdep) |
|
1242 depname = pdep{j}.package; |
|
1243 if (strcmp(depname, "octave")) continue; endif |
|
1244 idx = strcmp(pnames, depname); |
|
1245 if (!any(idx)) |
|
1246 error("Package %s could not be loaded since it depends on %s", ... |
|
1247 files{i}, depname); |
|
1248 endif |
|
1249 dirs{end+1} = pdirs{idx}; |
|
1250 endfor |
5971
|
1251 endif |
5801
|
1252 endfor |
6037
|
1253 dirs = unique(dirs); |
5801
|
1254 endif |
6037
|
1255 |
|
1256 ## Load the packages |
6025
|
1257 if (length(dirs) > 0) |
|
1258 addpath(dirs{:}); |
|
1259 endif |
5971
|
1260 |
|
1261 ## Add local binaries, if any, to the EXEC_PATH |
|
1262 for i = 1:length(dirs) |
6233
|
1263 if (exist (fullfile(dirs{i}, "bin"), "dir")) |
|
1264 EXEC_PATH ([fullfile(dirs{i}, "bin") ":" EXEC_PATH()]); |
5971
|
1265 endif |
|
1266 endfor |
5801
|
1267 endfunction |
5928
|
1268 |
6203
|
1269 function unload_packages(files, handle_deps, local_list, global_list) |
|
1270 installed_packages = installed_packages(local_list, global_list); |
|
1271 num_packages = length(installed_packages); |
|
1272 |
|
1273 ## Read package names and installdirs into a more convenient format |
|
1274 pnames = pdirs = cell(1, num_packages); |
|
1275 for i = 1:num_packages |
|
1276 pnames{i} = installed_packages{i}.name; |
|
1277 pdirs{i} = installed_packages{i}.dir; |
|
1278 pdeps{i} = installed_packages{i}.depends; |
|
1279 endfor |
|
1280 |
|
1281 ## Get the current octave path |
|
1282 p = split_by(path(), pathsep()); |
|
1283 |
|
1284 ## unload all |
|
1285 if (length(files) == 1 && strcmp(files{1}, "all")) |
|
1286 dirs = pdirs; |
|
1287 ## unload package_name1 ... |
|
1288 else |
|
1289 dirs = {}; |
|
1290 for i = 1:length(files) |
|
1291 idx = strcmp(pnames, files{i}); |
|
1292 if (!any(idx)) |
|
1293 error("Package %s is not installed", files{i}); |
|
1294 endif |
|
1295 dirs{end+1} = pdirs{idx}; |
|
1296 endfor |
|
1297 endif |
|
1298 |
|
1299 ## Unload the packages |
|
1300 for i = 1:length(dirs) |
|
1301 d = dirs{i}; |
|
1302 idx = strcmp(p, d); |
|
1303 if (any(idx)) |
|
1304 rmpath(d); |
|
1305 # XXX: We should also check if we need to remove items from EXEC_PATH |
|
1306 endif |
|
1307 endfor |
|
1308 endfunction |
|
1309 |
5928
|
1310 function [status_out, msg_out] = rm_rf (dir) |
|
1311 crr = confirm_recursive_rmdir (); |
|
1312 unwind_protect |
|
1313 confirm_recursive_rmdir (false); |
|
1314 [status, msg] = rmdir (dir, "s"); |
|
1315 unwind_protect_cleanup |
|
1316 confirm_recursive_rmdir (crr); |
|
1317 end_unwind_protect |
|
1318 if (nargout > 0) |
|
1319 status_out = status; |
|
1320 endif |
|
1321 if (nargout > 1) |
|
1322 msg_out = msg; |
|
1323 endif |
|
1324 endfunction |
5971
|
1325 |
|
1326 function emp = dirempty (nm, ign) |
|
1327 if (nargin < 2) |
|
1328 ign = {".",".."}; |
|
1329 else |
|
1330 ign = [{".",".."},ign]; |
|
1331 endif |
|
1332 l = dir (nm); |
|
1333 for i=1:length(l) |
|
1334 found = false; |
|
1335 for j=1:length(ign) |
|
1336 if (strcmp(l(i).name,ign{j})) |
|
1337 found = true; |
|
1338 break; |
|
1339 endif |
|
1340 endfor |
|
1341 if (!found) |
|
1342 emp = false; |
|
1343 return |
|
1344 endif |
|
1345 endfor |
|
1346 emp = true; |
|
1347 return; |
|
1348 endfunction |