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