Mercurial > octave-nkf
annotate scripts/pkg/pkg.m @ 7632:d6e63a15cc75
Allow installation of already extracted packages
* * *
* * *
author | Thomas Weber <thomas.weber.mail@gmail.com> |
---|---|
date | Mon, 24 Mar 2008 18:14:18 -0400 |
parents | 9cbf1e2011a3 |
children | ca9bfe159144 |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 2005, 2006, 2007 S�ren Hauberg |
5801 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
6440 | 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 |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
5801 | 18 |
19 ## -*- texinfo -*- | |
6032 | 20 ## @deftypefn {Command} pkg @var{command} @var{pkg_name} |
21 ## @deftypefnx {Command} pkg @var{command} @var{option} @var{pkg_name} | |
22 ## This command interacts with the package manager. Different actions will | |
6378 | 23 ## be taken depending on the value of @var{command}. |
6032 | 24 ## |
25 ## @table @samp | |
26 ## @item install | |
6070 | 27 ## Install named packages. For example, |
28 ## @example | |
29 ## pkg install image-1.0.0.tar.gz | |
30 ## @end example | |
31 ## @noindent | |
32 ## installs the package found in the file @code{image-1.0.0.tar.gz}. | |
33 ## | |
6645 | 34 ## The @var{option} variable can contain options that affect the manner |
35 ## in which a package is installed. These options can be one or more of | |
36 ## | |
37 ## @table @code | |
38 ## @item -nodeps | |
39 ## The package manager will disable the dependency checking. That way it | |
40 ## is possible to install a package even if it depends on another package | |
41 ## that's not installed on the system. @strong{Use this option with care.} | |
42 ## | |
43 ## @item -noauto | |
44 ## The package manager will not automatically load the installed package | |
45 ## when starting Octave, even if the package requests that it is. | |
6258 | 46 ## |
6645 | 47 ## @item -auto |
48 ## The package manager will automatically load the installed package when | |
49 ## starting Octave, even if the package requests that it isn't. | |
50 ## | |
51 ## @item -local | |
52 ## A local installation is forced, even if the user has system privileges. | |
6258 | 53 ## |
6645 | 54 ## @item -global |
55 ## A global installation is forced, even if the user doesn't normally have | |
56 ## system privileges | |
6614 | 57 ## |
6645 | 58 ## @item -verbose |
59 ## The package manager will print the output of all of the commands that are | |
60 ## performed. | |
61 ## @end table | |
62 ## | |
6032 | 63 ## @item uninstall |
6070 | 64 ## Uninstall named packages. For example, |
65 ## @example | |
66 ## pkg uninstall image | |
67 ## @end example | |
68 ## @noindent | |
69 ## removes the @code{image} package from the system. If another installed | |
70 ## package depends on the @code{image} package an error will be issued. | |
71 ## The package can be uninstalled anyway by using the @code{-nodeps} option. | |
6032 | 72 ## @item load |
6070 | 73 ## Add named packages to the path. After loading a package it is |
74 ## possible to use the functions provided by the package. For example, | |
75 ## @example | |
76 ## pkg load image | |
77 ## @end example | |
78 ## @noindent | |
79 ## adds the @code{image} package to the path. It is possible to load all | |
80 ## installed packages at once with the command | |
81 ## @example | |
82 ## pkg load all | |
83 ## @end example | |
6203 | 84 ## @item unload |
85 ## Removes named packages from the path. After unloading a package it is | |
86 ## no longer possible to use the functions provided by the package. | |
87 ## This command behaves like the @code{load} command. | |
6032 | 88 ## @item list |
6070 | 89 ## Show a list of the currently installed packages. By requesting one or two |
90 ## output argument it is possible to get a list of the currently installed | |
91 ## packages. For example, | |
92 ## @example | |
93 ## installed_packages = pkg list; | |
94 ## @end example | |
95 ## @noindent | |
96 ## returns a cell array containing a structure for each installed package. | |
97 ## The command | |
98 ## @example | |
99 ## [@var{user_packages}, @var{system_packages}] = pkg list | |
100 ## @end example | |
101 ## @noindent | |
102 ## splits the list of installed packages into those who are installed by | |
103 ## the current user, and those installed by the system administrator. | |
7497 | 104 ## @item describe |
105 ## Show a short description of the named installed packages, with the option | |
106 ## '-verbose' also list functions provided by the package, e.g.: | |
107 ## @example | |
108 ## pkg describe -verbose all | |
109 ## @end example | |
110 ## @noindent | |
111 ## will describe all installed packages and the functions they provide. | |
112 ## If one output is requested a cell of structure containing the | |
113 ## description and list of functions of each package is returned as | |
114 ## output rather than printed on screen: | |
115 ## @example | |
116 ## desc = pkg ("describe", "secs1d", "image") | |
117 ## @end example | |
118 ## @noindent | |
119 ## If any of the requested packages is not installed, pkg returns an | |
120 ## error, unless a second output is requested: | |
121 ## @example | |
122 ## [ desc, flag] = pkg ("describe", "secs1d", "image") | |
123 ## @end example | |
124 ## @noindent | |
125 ## @var{flag} will take one of the values "Not installed", "Loaded" or | |
126 ## "Not loaded" for each of the named packages. | |
6034 | 127 ## @item prefix |
6070 | 128 ## Set the installation prefix directory. For example, |
129 ## @example | |
130 ## pkg prefix ~/my_octave_packages | |
131 ## @end example | |
132 ## @noindent | |
133 ## sets the installation prefix to @code{~/my_octave_packages}. | |
134 ## Packages will be installed in this directory. | |
6034 | 135 ## |
6070 | 136 ## It is possible to get the current installation prefix by requesting an |
137 ## output argument. For example, | |
138 ## @example | |
139 ## p = pkg prefix | |
140 ## @end example | |
6925 | 141 ## |
142 ## The location in which to install the architecture dependent files can be | |
143 ## independent specified with an addition argument. For example | |
144 ## | |
145 ## @example | |
146 ## pkg prefix ~/my_octave_packages ~/my_octave_packages_for_my_pc | |
147 ## @end example | |
6189 | 148 ## @item local_list |
149 ## Set the file in which to look for information on the locally | |
150 ## installed packages. Locally installed packages are those that are | |
151 ## typically available only to the current user. For example | |
152 ## @example | |
153 ## pkg local_list ~/.octave_packages | |
154 ## @end example | |
155 ## It is possible to get the current value of local_list with the following | |
156 ## @example | |
157 ## pkg local_list | |
158 ## @end example | |
159 ## @item global_list | |
160 ## Set the file in which to look for, for information on the globally | |
161 ## installed packages. Globally installed packages are those that are | |
162 ## typically available to all users. For example | |
163 ## @example | |
164 ## pkg global_list /usr/share/octave/octave_packages | |
165 ## @end example | |
166 ## It is possible to get the current value of global_list with the following | |
167 ## @example | |
168 ## pkg global_list | |
169 ## @end example | |
6645 | 170 ## @item rebuild |
171 ## Rebuilds the package database from the installed directories. This can | |
172 ## be used in cases where for some reason the package database is corrupted. | |
173 ## It can also take the @code{-auto} and @code{-noauto} options to allow the | |
174 ## autolaoding state of a package to be changed. For example | |
175 ## | |
176 ## @example | |
177 ## pkg rebuild -noauto image | |
178 ## @end example | |
179 ## | |
180 ## will remove the autoloading status of the image package. | |
6675 | 181 ## @item build |
182 ## Builds a binary form of a package or packages. The binary file produced | |
183 ## will itself be an Octave package that can be installed normally with | |
184 ## @code{pkg}. The form of the command to build a binary package is | |
185 ## | |
186 ## @example | |
187 ## pkg build builddir image-1.0.0.tar.gz @dots{} | |
188 ## @end example | |
189 ## | |
190 ## @noindent | |
191 ## where @code{buiddir} is the name of a directory where the temporary | |
192 ## installation will be produced and the binary packages will be found. | |
193 ## The options @code{-verbose} and @code{-nodeps} are respected, while | |
194 ## the other options are ignored. | |
6032 | 195 ## @end table |
5801 | 196 ## @end deftypefn |
5947 | 197 |
198 ## PKG_ADD: mark_as_command pkg | |
199 | |
6496 | 200 function [local_packages, global_packages] = pkg (varargin) |
201 ## Installation prefix (XXX: what should these be on windows?) | |
6645 | 202 persistent user_prefix = false; |
6496 | 203 persistent prefix = -1; |
6925 | 204 persistent archprefix = -1; |
7498 | 205 persistent local_list = tilde_expand (fullfile ("~", ".octave_packages")); |
6496 | 206 persistent global_list = fullfile (OCTAVE_HOME (), "share", "octave", |
207 "octave_packages"); | |
208 mlock (); | |
6189 | 209 |
6925 | 210 global_install = issuperuser (); |
6683 | 211 |
6496 | 212 if (prefix == -1) |
6645 | 213 if (global_install) |
6496 | 214 prefix = fullfile (OCTAVE_HOME (), "share", "octave", "packages"); |
7498 | 215 archprefix = fullfile (octave_config_info ("libexecdir"), |
216 "octave", "packages"); | |
6496 | 217 else |
218 prefix = fullfile ("~", "octave"); | |
6925 | 219 archprefix = prefix; |
5801 | 220 endif |
6645 | 221 prefix = tilde_expand (prefix); |
6925 | 222 archprefix = tilde_expand (archprefix); |
6496 | 223 endif |
224 | |
7498 | 225 available_actions = {"list", "install", "uninstall", "load", ... |
226 "unload", "prefix", "local_list", ... | |
227 "global_list", "rebuild", "build","describe"}; | |
6496 | 228 ## Handle input |
229 if (length (varargin) == 0 || ! iscellstr (varargin)) | |
230 print_usage (); | |
231 endif | |
232 files = {}; | |
233 deps = true; | |
234 auto = 0; | |
235 action = "none"; | |
6614 | 236 verbose = false; |
6496 | 237 for i = 1:length (varargin) |
238 switch (varargin{i}) | |
239 case "-nodeps" | |
240 deps = false; | |
241 case "-noauto" | |
242 auto = -1; | |
243 case "-auto" | |
244 auto = 1; | |
6614 | 245 case "-verbose" |
246 verbose = true; | |
6645 | 247 case "-local" |
248 global_install = false; | |
249 if (! user_prefix) | |
6729 | 250 prefix = tilde_expand (fullfile ("~", "octave")); |
6645 | 251 endif |
252 case "-global" | |
253 global_install = true; | |
254 if (! user_prefix) | |
255 prefix = fullfile (OCTAVE_HOME (), "share", "octave", "packages"); | |
256 endif | |
7497 | 257 case available_actions |
6925 | 258 if (strcmp (action, "none")) |
6687 | 259 action = varargin{i}; |
260 else | |
261 error ("more than one action specified"); | |
262 endif | |
6496 | 263 otherwise |
264 files{end+1} = varargin{i}; | |
5801 | 265 endswitch |
6496 | 266 endfor |
267 | |
268 ## Take action | |
269 switch (action) | |
270 case "list" | |
271 if (nargout == 0) | |
272 installed_packages (local_list, global_list); | |
273 elseif (nargout == 1) | |
274 local_packages = installed_packages (local_list, global_list); | |
275 elseif (nargout == 2) | |
276 [local_packages, global_packages] = installed_packages (local_list, | |
277 global_list); | |
278 else | |
279 error ("too many output arguments requested"); | |
280 endif | |
281 | |
282 case "install" | |
283 if (length (files) == 0) | |
284 error ("you must specify at least one filename when calling 'pkg install'"); | |
285 endif | |
6925 | 286 install (files, deps, auto, prefix, archprefix, verbose, local_list, |
6645 | 287 global_list, global_install); |
6496 | 288 |
289 case "uninstall" | |
290 if (length (files) == 0) | |
291 error ("you must specify at least one package when calling 'pkg uninstall'"); | |
292 endif | |
6645 | 293 uninstall (files, deps, verbose, local_list, |
294 global_list, global_install); | |
6496 | 295 |
296 case "load" | |
297 if (length (files) == 0) | |
298 error ("you must specify at least one package, 'all' or 'auto' when calling 'pkg load'"); | |
299 endif | |
300 load_packages (files, deps, local_list, global_list); | |
301 | |
302 case "unload" | |
303 if (length (files) == 0) | |
304 error ("you must specify at least one package or 'all' when calling 'pkg unload'"); | |
305 endif | |
306 unload_packages (files, deps, local_list, global_list); | |
307 | |
308 case "prefix" | |
309 if (length (files) == 0 && nargout == 0) | |
6925 | 310 printf ("Installation prefix: %s\n", prefix); |
311 printf ("Architecture dependent prefix: %s\n", archprefix); | |
312 elseif (length (files) == 0 && nargout >= 1) | |
6496 | 313 local_packages = prefix; |
6925 | 314 global_packages = archprefix; |
315 elseif (length (files) >= 1 && nargout <= 2 && ischar (files{1})) | |
6496 | 316 prefix = files{1}; |
6675 | 317 prefix = absolute_pathname (prefix); |
318 local_packages = prefix; | |
6645 | 319 user_prefix = true; |
6925 | 320 if (length (files) >= 2 && ischar (files{2})) |
321 archprefix = files{2}; | |
322 try | |
323 archprefix = absolute_pathname (archprefix); | |
324 catch | |
325 mkdir (archprefix); | |
326 warning ("creating the directory %s\n", archprefix); | |
327 archprefix = absolute_pathname (archprefix); | |
328 end_try_catch | |
329 global_packages = archprefix; | |
330 endif | |
6496 | 331 else |
332 error ("you must specify a prefix directory, or request an output argument"); | |
333 endif | |
334 | |
335 case "local_list" | |
336 if (length (files) == 0 && nargout == 0) | |
337 disp (local_list); | |
338 elseif (length (files) == 0 && nargout == 1) | |
339 local_packages = local_list; | |
340 elseif (length (files) == 1 && nargout == 0 && ischar (files{1})) | |
6729 | 341 try |
342 local_list = absolute_pathname (files{1}); | |
343 catch | |
344 ## Force file to be created | |
7498 | 345 fclose (fopen (files{1}, "wt")); |
6729 | 346 local_list = absolute_pathname (files{1}); |
347 end_try_catch | |
6496 | 348 else |
349 error ("you must specify a local_list file, or request an output argument"); | |
350 endif | |
351 | |
352 case "global_list" | |
353 if (length (files) == 0 && nargout == 0) | |
354 disp(global_list); | |
355 elseif (length (files) == 0 && nargout == 1) | |
356 local_packages = global_list; | |
357 elseif (length (files) == 1 && nargout == 0 && ischar (files{1})) | |
6729 | 358 try |
359 global_list = absolute_pathname (files{1}); | |
360 catch | |
361 ## Force file to be created | |
7498 | 362 fclose (fopen (files{1}, "wt")); |
6729 | 363 global_list = absolute_pathname (files{1}); |
364 end_try_catch | |
6496 | 365 else |
366 error ("you must specify a global_list file, or request an output argument"); | |
367 endif | |
6645 | 368 |
369 case "rebuild" | |
370 if (global_install) | |
6925 | 371 global_packages = rebuild (prefix, archprefix, global_list, files, |
372 auto, verbose); | |
6695 | 373 global_packages = save_order (global_packages); |
6645 | 374 save (global_list, "global_packages"); |
6695 | 375 if (nargout > 0) |
376 local_packages = global_packages; | |
377 endif | |
6645 | 378 else |
6925 | 379 local_packages = rebuild (prefix, archprefix, local_list, files, auto, |
380 verbose); | |
6695 | 381 local_packages = save_order (local_packages); |
6645 | 382 save (local_list, "local_packages"); |
6695 | 383 if (nargout == 0) |
384 clear ("local_packages"); | |
385 endif | |
6645 | 386 endif |
387 | |
6675 | 388 case "build" |
389 if (length (files) < 2) | |
390 error ("you must specify at least the build directory and one filename\nwhen calling 'pkg build'"); | |
391 endif | |
392 build (files, deps, auto, verbose); | |
393 | |
7497 | 394 case "describe" |
395 if (length (files) == 0) | |
396 error ("you must specify at least one package or 'all' when calling 'pkg describe'"); | |
397 endif | |
398 ## XXX FIXME: the name of the output variables is inconsistent | |
399 ## with their content | |
400 switch (nargout) | |
7498 | 401 case 0 |
402 describe (files, verbose, local_list, global_list); | |
403 case 1 | |
404 pkg_desc_list = describe (files, verbose, local_list, ... | |
405 global_list); | |
406 local_packages = pkg_desc_list; | |
407 case 2 | |
408 [pkg_desc_list, flag] = describe (files, verbose, local_list, ... | |
409 global_list); | |
410 local_packages = pkg_desc_list; | |
411 global_packages = flag; | |
412 otherwise | |
413 error ("you can request at most two outputs when calling 'pkg describe'"); | |
414 endswitch | |
7497 | 415 |
6496 | 416 otherwise |
417 error ("you must specify a valid action for 'pkg'. See 'help pkg' for details"); | |
418 endswitch | |
419 endfunction | |
420 | |
6925 | 421 function descriptions = rebuild (prefix, archprefix, list, files, auto, verbose) |
6645 | 422 if (isempty (files)) |
423 [dirlist, err, msg] = readdir (prefix); | |
424 if (err) | |
425 error ("couldn't read directory %s: %s", prefix, msg); | |
426 endif | |
427 ## the two first entries of dirlist are "." and ".." | |
428 dirlist([1,2]) = []; | |
429 else | |
430 old_descriptions = installed_packages (list, list); | |
431 wd = pwd (); | |
6663 | 432 unwind_protect |
433 cd (prefix); | |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7498
diff
changeset
|
434 dirlist = glob (cellfun(@(x) cstrcat(x, '-*'), files, 'UniformOutput', 0)); |
6663 | 435 unwind_protect_cleanup |
436 cd (wd); | |
437 end_unwind_protect | |
6645 | 438 endif |
439 descriptions = {}; | |
440 for k = 1:length (dirlist) | |
441 descfile = fullfile (prefix, dirlist{k}, "packinfo", "DESCRIPTION"); | |
442 if (verbose) | |
443 printf ("recreating package description from %s\n", dirlist{k}); | |
444 endif | |
445 if (exist (descfile, "file")) | |
446 desc = get_description (descfile); | |
447 desc.dir = fullfile (prefix, dirlist{k}); | |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7498
diff
changeset
|
448 desc.archprefix = fullfile (archprefix, cstrcat (desc.name, "-", |
6925 | 449 desc.version)); |
6645 | 450 if (auto != 0) |
451 if (exist (fullfile (desc.dir, "packinfo", ".autoload"), "file")) | |
452 unlink (fullfile (desc.dir, "packinfo", ".autoload")); | |
453 endif | |
454 if (auto < 0) | |
455 desc.autoload = 0; | |
456 elseif (auto > 0) | |
457 desc.autoload = 1; | |
458 fclose (fopen (fullfile (desc.dir, "packinfo", ".autoload"), "wt")); | |
459 endif | |
460 else | |
461 if (exist (fullfile (desc.dir, "packinfo", ".autoload"), "file")) | |
462 desc.autoload = 1; | |
463 else | |
464 desc.autoload = 0; | |
465 endif | |
466 endif | |
467 descriptions{end + 1} = desc; | |
468 elseif (verbose) | |
469 warning ("directory %s is not a valid package", dirlist{k}); | |
470 endif | |
471 endfor | |
472 | |
6663 | 473 if (! isempty (files)) |
6645 | 474 ## We are rebuilding for a particular package(s) so we should take |
475 ## care to keep the other untouched packages in the descriptions | |
6663 | 476 descriptions = {descriptions{:}, old_descriptions{:}}; |
6645 | 477 |
478 dup = []; | |
479 for i = 1:length (descriptions) | |
480 if (find (dup, i)) | |
481 continue; | |
482 endif | |
483 for j = (i+1):length (descriptions) | |
484 if (find (dup, j)) | |
485 continue; | |
486 endif | |
487 if (strcmp (descriptions{i}.name, descriptions{j}.name)) | |
488 dup = [dup, j]; | |
489 endif | |
490 endfor | |
491 endfor | |
492 if (! isempty (dup)) | |
493 descriptions (dup) = []; | |
494 endif | |
495 endif | |
496 endfunction | |
497 | |
6675 | 498 function build (files, handle_deps, autoload, verbose) |
499 if (length (files) < 1) | |
500 error ("insufficient number of files"); | |
501 endif | |
502 builddir = files{1}; | |
503 if (! exist (builddir, "dir")) | |
504 warning ("creating build directory %s", builddir); | |
505 [status, msg] = mkdir (builddir); | |
506 if (status != 1) | |
507 error ("could not create installation directory: %s", msg); | |
508 endif | |
6496 | 509 endif |
6675 | 510 builddir = absolute_pathname (builddir); |
511 installdir = fullfile (builddir, "install"); | |
512 if (! exist (installdir, "dir")) | |
513 [status, msg] = mkdir (installdir); | |
514 if (status != 1) | |
515 error ("could not create installation directory: %s", msg); | |
516 endif | |
517 endif | |
518 files(1) = []; | |
519 buildlist = fullfile (builddir, "octave_packages"); | |
6925 | 520 install (files, handle_deps, autoload, installdir, installdir, verbose, |
6675 | 521 buildlist, "", false); |
522 unwind_protect | |
523 repackage (builddir, buildlist); | |
524 unwind_protect_cleanup | |
525 unload_packages ({"all"}, handle_deps, buildlist, ""); | |
526 if (exist (installdir, "dir")) | |
527 rm_rf (installdir); | |
528 endif | |
529 if (exist (buildlist, "file")) | |
530 unlink (buildlist); | |
531 endif | |
532 end_unwind_protect | |
5801 | 533 endfunction |
534 | |
6925 | 535 function install (files, handle_deps, autoload, prefix, archprefix, verbose, |
536 local_list, global_list, global_install) | |
6496 | 537 |
538 # Check that the directory in prefix exist. If it doesn't: create it! | |
539 if (! exist (prefix, "dir")) | |
540 warning ("creating installation directory %s", prefix); | |
541 [status, msg] = mkdir (prefix); | |
542 if (status != 1) | |
543 error ("could not create installation directory: %s", msg); | |
544 endif | |
545 endif | |
546 | |
547 ## Get the list of installed packages | |
548 [local_packages, global_packages] = installed_packages (local_list, | |
549 global_list); | |
550 | |
6820 | 551 installed_pkgs_lst = {local_packages{:}, global_packages{:}}; |
6496 | 552 |
553 if (global_install) | |
554 packages = global_packages; | |
555 else | |
556 packages = local_packages; | |
557 endif | |
558 | |
559 ## Uncompress the packages and read the DESCRIPTION files | |
560 tmpdirs = packdirs = descriptions = {}; | |
561 try | |
6683 | 562 ## Warn about non existent files |
563 for i = 1:length (files) | |
564 if (isempty (glob(files{i}))) | |
565 warning ("file %s does not exist", files{i}); | |
566 endif | |
567 endfor | |
568 | |
6496 | 569 ## Unpack the package files and read the DESCRIPTION files |
570 files = glob (files); | |
571 packages_to_uninstall = []; | |
572 for i = 1:length (files) | |
573 tgz = files{i}; | |
574 | |
6645 | 575 if (exist (tgz, "file")) |
576 ## Create a temporary directory | |
577 tmpdir = tmpnam (); | |
578 tmpdirs{end+1} = tmpdir; | |
579 if (verbose) | |
580 printf ("mkdir (%s)\n", tmpdir); | |
581 endif | |
582 [status, msg] = mkdir (tmpdir); | |
583 if (status != 1) | |
584 error ("couldn't create temporary directory: %s", msg); | |
585 endif | |
6496 | 586 |
6645 | 587 ## Uncompress the package |
588 if (verbose) | |
589 printf ("untar (%s, %s)\n", tgz, tmpdir); | |
590 endif | |
591 untar (tgz, tmpdir); | |
6496 | 592 |
6645 | 593 ## Get the name of the directories produced by tar |
594 [dirlist, err, msg] = readdir (tmpdir); | |
595 if (err) | |
596 error ("couldn't read directory produced by tar: %s", msg); | |
597 endif | |
6496 | 598 |
6645 | 599 if (length (dirlist) > 3) |
600 error ("bundles of packages are not allowed") | |
6496 | 601 endif |
7632
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
602 endif |
6496 | 603 |
7632
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
604 ## The filename pointed to an uncompressed package to begin with |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
605 if (exist (tgz, "dir")) |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
606 dirlist = {".", "..", tgz}; |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
607 endif |
6645 | 608 |
7632
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
609 if (exist (tgz, "file") || exist (tgz, "dir")) |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
610 ## the two first entries of dirlist are "." and ".." |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
611 if (exist (tgz, "file")) |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
612 packdir = fullfile (tmpdir, dirlist{3}); |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
613 else |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
614 packdir = fullfile (pwd(), dirlist{3}); |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
615 endif |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
616 packdirs{end+1} = packdir; |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
617 |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
618 ## Make sure the package contains necessary files |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
619 verify_directory (packdir); |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
620 |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
621 ## Read the DESCRIPTION file |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
622 filename = fullfile (packdir, "DESCRIPTION"); |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
623 desc = get_description (filename); |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
624 |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
625 ## Verify that package name corresponds with filename |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
626 [dummy, nm] = fileparts (tgz); |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
627 if ((length (nm) >= length (desc.name)) |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
628 && ! strcmp (desc.name, nm(1:length(desc.name)))) |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
629 error ("package name '%s' doesn't correspond to its filename '%s'", |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
630 desc.name, nm); |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
631 endif |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
632 |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
633 ## Set default installation directory |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
634 desc.dir = fullfile (prefix, cstrcat (desc.name, "-", desc.version)); |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
635 |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
636 ## Set default architectire dependent installation directory |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
637 desc.archprefix = fullfile (archprefix, cstrcat (desc.name, "-", |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
638 desc.version)); |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
639 |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
640 ## Save desc |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
641 descriptions{end+1} = desc; |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
642 |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
643 ## Are any of the new packages already installed? |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
644 ## If so we'll remove the old version. |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
645 for j = 1:length (packages) |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
646 if (strcmp (packages{j}.name, desc.name)) |
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
647 packages_to_uninstall(end+1) = j; |
6496 | 648 endif |
7632
d6e63a15cc75
Allow installation of already extracted packages
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7548
diff
changeset
|
649 endfor |
6645 | 650 endif |
6496 | 651 endfor |
652 catch | |
653 ## Something went wrong, delete tmpdirs | |
654 for i = 1:length (tmpdirs) | |
655 rm_rf (tmpdirs{i}); | |
656 endfor | |
6655 | 657 rethrow (lasterror ()); |
6496 | 658 end_try_catch |
659 | |
660 ## Check dependencies | |
661 if (handle_deps) | |
662 ok = true; | |
663 error_text = ""; | |
664 for i = 1:length (descriptions) | |
665 desc = descriptions{i}; | |
666 idx2 = complement (i, 1:length(descriptions)); | |
6695 | 667 if (global_install) |
668 ## Global installation is not allowed to have dependencies on locally | |
669 ## installed packages | |
670 idx1 = complement (packages_to_uninstall, | |
671 1:length(global_packages)); | |
672 pseudo_installed_packages = {global_packages{idx1}, ... | |
673 descriptions{idx2}}; | |
674 else | |
675 idx1 = complement (packages_to_uninstall, | |
676 1:length(local_packages)); | |
677 pseudo_installed_packages = {local_packages{idx1}, ... | |
678 global_packages{:}, ... | |
679 descriptions{idx2}}; | |
680 endif | |
6496 | 681 bad_deps = get_unsatisfied_deps (desc, pseudo_installed_packages); |
682 ## Are there any unsatisfied dependencies? | |
683 if (! isempty (bad_deps)) | |
684 ok = false; | |
685 for i = 1:length (bad_deps) | |
686 dep = bad_deps{i}; | |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7498
diff
changeset
|
687 error_text = cstrcat (error_text, " ", desc.name, " needs ", |
6496 | 688 dep.package, " ", dep.operator, " ", |
689 dep.version, "\n"); | |
690 endfor | |
691 endif | |
692 endfor | |
693 | |
694 ## Did we find any unsatisfied dependencies? | |
695 if (! ok) | |
696 error ("the following dependencies where unsatisfied:\n %s", error_text); | |
697 endif | |
698 endif | |
699 | |
700 ## Prepare each package for installation | |
701 try | |
702 for i = 1:length (descriptions) | |
703 desc = descriptions{i}; | |
704 pdir = packdirs{i}; | |
705 prepare_installation (desc, pdir); | |
6614 | 706 configure_make (desc, pdir, verbose); |
6496 | 707 endfor |
708 catch | |
709 ## Something went wrong, delete tmpdirs | |
710 for i = 1:length (tmpdirs) | |
711 rm_rf (tmpdirs{i}); | |
712 endfor | |
6655 | 713 rethrow (lasterror ()); |
6496 | 714 end_try_catch |
715 | |
716 ## Uninstall the packages that will be replaced | |
717 try | |
718 for i = packages_to_uninstall | |
6695 | 719 if (global_install) |
720 uninstall ({global_packages{i}.name}, false, verbose, local_list, | |
721 global_list, global_install); | |
722 else | |
723 uninstall ({local_packages{i}.name}, false, verbose, local_list, | |
724 global_list, global_install); | |
725 endif | |
6496 | 726 endfor |
727 catch | |
728 ## Something went wrong, delete tmpdirs | |
729 for i = 1:length (tmpdirs) | |
730 rm_rf (tmpdirs{i}); | |
731 endfor | |
6655 | 732 rethrow (lasterror ()); |
6496 | 733 end_try_catch |
734 | |
735 ## Install each package | |
736 try | |
737 for i = 1:length (descriptions) | |
738 desc = descriptions{i}; | |
739 pdir = packdirs{i}; | |
6925 | 740 copy_files (desc, pdir, global_install); |
741 create_pkgadddel (desc, pdir, "PKG_ADD", global_install); | |
742 create_pkgadddel (desc, pdir, "PKG_DEL", global_install); | |
743 finish_installation (desc, pdir, global_install) | |
6496 | 744 endfor |
745 catch | |
746 ## Something went wrong, delete tmpdirs | |
747 for i = 1:length (tmpdirs) | |
748 rm_rf (tmpdirs{i}); | |
749 endfor | |
750 for i = 1:length (descriptions) | |
751 rm_rf (descriptions{i}.dir); | |
6925 | 752 rm_rf (getarchdir (descriptions{i})); |
6496 | 753 endfor |
6655 | 754 rethrow (lasterror ()); |
6496 | 755 end_try_catch |
756 | |
757 ## Check if the installed directory is empty. If it is remove it | |
758 ## from the list | |
759 for i = length (descriptions):-1:1 | |
6925 | 760 if (dirempty (descriptions{i}.dir, {"packinfo", "doc"}) && |
761 dirempty (getarchdir (descriptions{i}))) | |
6655 | 762 warning ("package %s is empty\n", descriptions{i}.name); |
6496 | 763 rm_rf (descriptions{i}.dir); |
6925 | 764 rm_rf (getarchdir (descriptions{i})); |
6496 | 765 descriptions(i) = []; |
766 endif | |
767 endfor | |
768 | |
769 ## If the package requested that it is autoloaded, or the installer | |
770 ## requested that it is, then mark the package as autoloaded. | |
771 for i = length (descriptions):-1:1 | |
772 if (autoload > 0 || (autoload == 0 && isautoload (descriptions(i)))) | |
773 fclose (fopen (fullfile (descriptions{i}.dir, "packinfo", | |
774 ".autoload"), "wt")); | |
6695 | 775 descriptions{i}.autoload = 1; |
6496 | 776 endif |
777 endfor | |
778 | |
779 ## Add the packages to the package list | |
780 try | |
781 if (global_install) | |
782 idx = complement (packages_to_uninstall, 1:length(global_packages)); | |
6695 | 783 global_packages = save_order ({global_packages{idx}, descriptions{:}}); |
6496 | 784 save (global_list, "global_packages"); |
6820 | 785 installed_pkgs_lst = {local_packages{:}, global_packages{:}}; |
6496 | 786 else |
787 idx = complement (packages_to_uninstall, 1:length(local_packages)); | |
6695 | 788 local_packages = save_order ({local_packages{idx}, descriptions{:}}); |
6496 | 789 save (local_list, "local_packages"); |
6820 | 790 installed_pkgs_lst = {local_packages{:}, global_packages{:}}; |
6496 | 791 endif |
792 catch | |
793 ## Something went wrong, delete tmpdirs | |
794 for i = 1:length (tmpdirs) | |
795 rm_rf (tmpdirs{i}); | |
796 endfor | |
797 for i = 1:length (descriptions) | |
798 rm_rf (descriptions{i}.dir); | |
799 endfor | |
800 if (global_install) | |
6695 | 801 printf ("error: couldn't append to %s\n", global_list); |
6496 | 802 else |
6695 | 803 printf ("error: couldn't append to %s\n", local_list); |
6496 | 804 endif |
6695 | 805 rethrow (lasterror ()); |
6496 | 806 end_try_catch |
807 | |
808 ## All is well, let's clean up | |
809 for i = 1:length (tmpdirs) | |
810 [status, msg] = rm_rf (tmpdirs{i}); | |
811 if (status != 1) | |
812 warning ("couldn't clean up after my self: %s\n", msg); | |
813 endif | |
814 endfor | |
815 | |
816 ## Add the newly installed packages to the path, so the user | |
7192 | 817 ## can begin using them. Only load them if they are marked autoload |
6496 | 818 if (length (descriptions) > 0) |
6695 | 819 idx = []; |
6496 | 820 for i = 1:length (descriptions) |
6695 | 821 if (isautoload (descriptions(i))) |
7192 | 822 nm = descriptions{i}.name; |
823 for j = 1:length (installed_pkgs_lst) | |
824 if (strcmp (nm, installed_pkgs_lst{j}.name)) | |
825 idx (end + 1) = j; | |
826 break; | |
827 endif | |
828 endfor | |
6614 | 829 endif |
6496 | 830 endfor |
6925 | 831 load_packages_and_dependencies (idx, handle_deps, installed_pkgs_lst, |
832 global_install); | |
6496 | 833 endif |
834 endfunction | |
835 | |
6645 | 836 function uninstall (pkgnames, handle_deps, verbose, local_list, |
837 global_list, global_install) | |
6496 | 838 ## Get the list of installed packages |
839 [local_packages, global_packages] = installed_packages(local_list, | |
840 global_list); | |
6645 | 841 if (global_install) |
6820 | 842 installed_pkgs_lst = {local_packages{:}, global_packages{:}}; |
6496 | 843 else |
6820 | 844 installed_pkgs_lst = local_packages; |
6496 | 845 endif |
846 | |
6820 | 847 num_packages = length (installed_pkgs_lst); |
6496 | 848 delete_idx = []; |
849 for i = 1:num_packages | |
6820 | 850 cur_name = installed_pkgs_lst{i}.name; |
6496 | 851 if (any (strcmp (cur_name, pkgnames))) |
852 delete_idx(end+1) = i; | |
853 endif | |
854 endfor | |
855 | |
856 ## Are all the packages that should be uninstalled already installed? | |
857 if (length (delete_idx) != length (pkgnames)) | |
6645 | 858 if (global_install) |
6496 | 859 ## Try again for a locally installed package |
6820 | 860 installed_pkgs_lst = local_packages; |
6496 | 861 |
6820 | 862 num_packages = length (installed_pkgs_lst); |
6496 | 863 delete_idx = []; |
864 for i = 1:num_packages | |
6820 | 865 cur_name = installed_pkgs_lst{i}.name; |
6496 | 866 if (any (strcmp (cur_name, pkgnames))) |
867 delete_idx(end+1) = i; | |
868 endif | |
869 endfor | |
870 if (length (delete_idx) != length (pkgnames)) | |
871 ## XXX: We should have a better error message | |
6655 | 872 warning ("some of the packages you want to uninstall are not installed"); |
6496 | 873 endif |
874 else | |
875 ## XXX: We should have a better error message | |
6655 | 876 warning ("some of the packages you want to uninstall are not installed."); |
6496 | 877 endif |
878 endif | |
879 | |
880 ## Compute the packages that will remain installed | |
881 idx = complement (delete_idx, 1:num_packages); | |
6820 | 882 remaining_packages = {installed_pkgs_lst{idx}}; |
6496 | 883 |
884 ## Check dependencies | |
885 if (handle_deps) | |
886 error_text = ""; | |
887 for i = 1:length (remaining_packages) | |
888 desc = remaining_packages{i}; | |
889 bad_deps = get_unsatisfied_deps (desc, remaining_packages); | |
890 | |
891 ## Will the uninstallation break any dependencies? | |
892 if (! isempty (bad_deps)) | |
893 for i = 1:length (bad_deps) | |
894 dep = bad_deps{i}; | |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7498
diff
changeset
|
895 error_text = cstrcat (error_text, " ", desc.name, " needs ", |
6496 | 896 dep.package, " ", dep.operator, " ", |
897 dep.version, "\n"); | |
898 endfor | |
899 endif | |
900 endfor | |
901 | |
902 if (! isempty (error_text)) | |
903 error ("the following dependencies where unsatisfied:\n %s", error_text); | |
904 endif | |
905 endif | |
906 | |
907 ## Delete the directories containing the packages | |
908 for i = delete_idx | |
6820 | 909 desc = installed_pkgs_lst{i}; |
6496 | 910 ## If an 'on_uninstall.m' exist, call it! |
911 if (exist (fullfile (desc.dir, "packinfo", "on_uninstall.m"), "file")) | |
6614 | 912 wd = pwd (); |
7498 | 913 cd (fullfile (desc.dir, "packinfo")); |
6614 | 914 on_uninstall (desc); |
915 cd (wd); | |
6496 | 916 endif |
917 ## Do the actual deletion | |
6675 | 918 if (desc.loaded) |
919 rmpath (desc.dir); | |
6925 | 920 if (exist (getarchdir (desc))) |
921 rmpath (getarchdir (desc)); | |
6675 | 922 endif |
6614 | 923 endif |
6496 | 924 if (exist (desc.dir, "dir")) |
925 [status, msg] = rm_rf (desc.dir); | |
926 if (status != 1) | |
927 error ("couldn't delete directory %s: %s", desc.dir, msg); | |
928 endif | |
6925 | 929 [status, msg] = rm_rf (getarchdir (desc)); |
930 if (status != 1) | |
931 error ("couldn't delete directory %s: %s", getarchdir (desc), msg); | |
932 endif | |
933 if (dirempty (desc.archprefix)) | |
934 rm_rf (desc.archprefix); | |
935 endif | |
6496 | 936 else |
937 warning ("directory %s previously lost", desc.dir); | |
938 endif | |
939 endfor | |
940 | |
941 ## Write a new ~/.octave_packages | |
6645 | 942 if (global_install) |
6496 | 943 if (length (remaining_packages) == 0) |
944 unlink (global_list); | |
945 else | |
6695 | 946 global_packages = save_order (remaining_packages); |
6496 | 947 save (global_list, "global_packages"); |
948 endif | |
949 else | |
950 if (length (remaining_packages) == 0) | |
951 unlink (local_list); | |
952 else | |
6695 | 953 local_packages = save_order (remaining_packages); |
6496 | 954 save (local_list, "local_packages"); |
955 endif | |
956 endif | |
957 | |
958 endfunction | |
959 | |
7497 | 960 function [pkg_desc_list, flag] = describe (pkgnames, verbose, |
961 local_list, global_list) | |
962 | |
963 ## Get the list of installed packages | |
964 installed_pkgs_lst = installed_packages(local_list, global_list); | |
7498 | 965 num_packages = length (installed_pkgs_lst); |
7497 | 966 |
967 | |
7498 | 968 describe_all = false; |
969 if (any (strcmp ("all", pkgnames))) | |
970 describe_all = true; | |
7497 | 971 flag{1:num_packages} = "Not Loaded"; |
7498 | 972 num_pkgnames = num_packages; |
7497 | 973 else |
7498 | 974 num_pkgnames = length (pkgnames); |
7497 | 975 flag{1:num_pkgnames} = "Not installed"; |
7498 | 976 endif |
7497 | 977 |
978 for i = 1:num_packages | |
979 curr_name= installed_pkgs_lst{i}.name; | |
980 if (describe_all) | |
981 name_pos = i; | |
982 else | |
983 name_pos = find(strcmp (curr_name, pkgnames)); | |
7498 | 984 endif |
985 | |
986 if (! isempty (name_pos)) | |
7497 | 987 if (installed_pkgs_lst{i}.loaded) |
988 flag{name_pos} = "Loaded"; | |
989 else | |
990 flag{name_pos} = "Not loaded"; | |
991 endif | |
992 | |
7498 | 993 pkg_desc_list{name_pos}.name = installed_pkgs_lst{i}.name; |
994 pkg_desc_list{name_pos}.description = installed_pkgs_lst{i}.description; | |
995 pkg_desc_list{name_pos}.provides = parse_pkg_idx (installed_pkgs_lst{i}.dir); | |
7497 | 996 |
997 endif | |
998 endfor | |
999 | |
7498 | 1000 non_inst = find (strcmp (flag, "Not installed")); |
1001 if (! isempty (non_inst) && nargout < 2) | |
1002 non_inst_str = sprintf (" %s ", pkgnames{non_inst}); | |
1003 error ("some packages are not installed: %s", non_inst_str); | |
7497 | 1004 endif |
1005 | |
1006 if (nargout == 0) | |
7498 | 1007 for i = 1:num_pkgnames |
7497 | 1008 print_package_description (pkg_desc_list{i}.name, |
1009 pkg_desc_list{i}.provides, | |
1010 pkg_desc_list{i}.description, | |
1011 flag{i}, verbose); | |
1012 endfor | |
1013 endif | |
1014 | |
1015 endfunction | |
1016 | |
6496 | 1017 ########################################################## |
1018 ## A U X I L I A R Y F U N C T I O N S ## | |
1019 ########################################################## | |
1020 | |
7497 | 1021 ## This function reads an INDEX file |
7498 | 1022 function [pkg_idx_struct] = parse_pkg_idx (packdir) |
7497 | 1023 |
7498 | 1024 index_file = fullfile (packdir, "packinfo", "INDEX"); |
1025 | |
1026 if (! exist (index_file, "file")) | |
1027 error ("could not find any INDEX file in directory %s, try 'pkg rebuild all' to generate missing INDEX files", packdir); | |
7497 | 1028 endif |
1029 | |
1030 | |
7498 | 1031 [fid, msg] = fopen (index_file, "r"); |
7497 | 1032 if (fid == -1) |
7498 | 1033 error ("the INDEX file %s could not be read: %s", |
1034 index_file, msg); | |
7497 | 1035 endif |
7498 | 1036 |
7497 | 1037 cat_num = 1; |
1038 pkg_idx_struct{1}.category = "Uncategorized"; | |
1039 pkg_idx_struct{1}.functions = {}; | |
1040 | |
7498 | 1041 line = fgetl (fid); |
1042 while (isempty (strfind (line, ">>")) && ! feof (fid)) | |
1043 line = fgetl (fid); | |
7497 | 1044 endwhile |
1045 | |
7498 | 1046 while (! feof (fid) || line != -1) |
1047 if (! any (! isspace (line)) || line(1) == "#" || any (line == "=")) | |
7497 | 1048 ## Comments, blank lines or comments about unimplemented |
1049 ## functions: do nothing | |
1050 ## XXX: probably comments and pointers to external functions | |
1051 ## could be treated better when printing to screen? | |
7498 | 1052 elseif (! isempty (strfind (line, ">>"))) |
7497 | 1053 ## Skip package name and description as they are in |
1054 ## DESCRIPTION already | |
7498 | 1055 elseif (! isspace (line(1))) |
7497 | 1056 ## Category |
7498 | 1057 if (! isempty (pkg_idx_struct{cat_num}.functions)) |
1058 pkg_idx_struct{++cat_num}.functions = {}; | |
7497 | 1059 endif |
7498 | 1060 pkg_idx_struct{cat_num}.category = deblank (line); |
7497 | 1061 else |
1062 ## Function names | |
7498 | 1063 while (any (! isspace (line))) |
1064 [fun_name, line] = strtok (line); | |
1065 pkg_idx_struct{cat_num}.functions{end+1} = deblank (fun_name); | |
7497 | 1066 endwhile |
1067 endif | |
7498 | 1068 line = fgetl (fid); |
7497 | 1069 endwhile |
1070 fclose (fid); | |
1071 endfunction | |
1072 | |
1073 function print_package_description (pkg_name, pkg_idx_struct, | |
1074 pkg_desc, status, verbose) | |
1075 | |
7498 | 1076 printf ("---\nPackage name:\n\t%s\n", pkg_name); |
1077 printf ("Short description:\n\t%s\n", pkg_desc); | |
1078 printf ("Status:\n\t%s\n", status); | |
7497 | 1079 if (verbose) |
7498 | 1080 printf ("---\nProvides:\n"); |
1081 for i = 1:length(pkg_idx_struct) | |
1082 if (! isempty (pkg_idx_struct{i}.functions)) | |
1083 printf ("%s\n", pkg_idx_struct{i}.category); | |
1084 for j = 1:length(pkg_idx_struct{i}.functions) | |
1085 printf ("\t%s\n", pkg_idx_struct{i}.functions{j}); | |
7497 | 1086 endfor |
1087 endif | |
1088 endfor | |
1089 endif | |
1090 | |
1091 endfunction | |
1092 | |
1093 | |
6675 | 1094 function pth = absolute_pathname (pth) |
7498 | 1095 [status, msg, msgid] = fileattrib (pth); |
6729 | 1096 if (status != 1) |
1097 error ("could not find the file or path %s", pth); | |
1098 else | |
1099 pth = msg.Name; | |
1100 endif | |
6675 | 1101 endfunction |
1102 | |
1103 function repackage (builddir, buildlist) | |
1104 packages = installed_packages (buildlist, buildlist); | |
1105 | |
1106 wd = pwd(); | |
1107 for i = 1 : length(packages) | |
1108 pack = packages{i}; | |
1109 unwind_protect | |
1110 cd (builddir); | |
1111 mkdir (pack.name); | |
1112 mkdir (fullfile (pack.name, "inst")); | |
1113 copyfile (fullfile (pack.dir, "*"), fullfile (pack.name, "inst")); | |
1114 movefile (fullfile (pack.name, "inst","packinfo", "*"), pack.name); | |
1115 if (exist (fullfile (pack.name, "inst","packinfo", ".autoload"), "file")) | |
1116 unlink (fullfile (pack.name, "inst","packinfo", ".autoload")); | |
1117 endif | |
1118 rmdir (fullfile (pack.name, "inst", "packinfo")); | |
1119 if (exist (fullfile (pack.name, "inst", "doc"), "dir")) | |
1120 movefile (fullfile (pack.name, "inst", "doc"), pack.name); | |
1121 endif | |
1122 if (exist (fullfile (pack.name, "inst", "bin"), "dir")) | |
1123 movefile (fullfile (pack.name, "inst", "bin"), pack.name); | |
1124 endif | |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7498
diff
changeset
|
1125 archdir = fullfile (pack.archprefix, cstrcat (pack.name, "-", |
6925 | 1126 pack.version), getarch ()); |
6675 | 1127 if (exist (archdir, "dir")) |
1128 if (exist (fullfile (pack.name, "inst", "PKG_ADD"), "file")) | |
1129 unlink (fullfile (pack.name, "inst", "PKG_ADD")); | |
1130 endif | |
1131 if (exist (fullfile (pack.name, "inst", "PKG_DEL"), "file")) | |
1132 unlink (fullfile (pack.name, "inst", "PKG_DEL")); | |
1133 endif | |
1134 if (exist (fullfile (archdir, "PKG_ADD"), "file")) | |
1135 movefile (fullfile (archdir, "PKG_ADD"), | |
1136 fullfile (pack.name, "PKG_ADD")); | |
1137 endif | |
1138 if (exist (fullfile (archdir, "PKG_DEL"), "file")) | |
1139 movefile (fullfile (archdir, "PKG_DEL"), | |
1140 fullfile (pack.name, "PKG_DEL")); | |
1141 endif | |
1142 else | |
1143 if (exist (fullfile (pack.name, "inst", "PKG_ADD"), "file")) | |
1144 movefile (fullfile (pack.name, "inst", "PKG_ADD"), | |
1145 fullfile (pack.name, "PKG_ADD")); | |
1146 endif | |
1147 if (exist (fullfile (pack.name, "inst", "PKG_DEL"), "file")) | |
1148 movefile (fullfile (pack.name, "inst", "PKG_DEL"), | |
1149 fullfile (pack.name, "PKG_DEL")); | |
1150 endif | |
1151 endif | |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7498
diff
changeset
|
1152 tfile = cstrcat (pack.name, "-", pack.version, ".tar"); |
6675 | 1153 tar (tfile, pack.name); |
6925 | 1154 try |
1155 gzip (tfile); | |
1156 unlink (tfile); | |
1157 catch | |
1158 warning ("failed to compress %s", tfile); | |
1159 end_try_catch | |
6675 | 1160 unwind_protect_cleanup |
1161 if (exist (pack.name, "dir")) | |
1162 rm_rf (pack.name); | |
1163 endif | |
1164 cd (wd); | |
1165 end_unwind_protect | |
1166 endfor | |
1167 endfunction | |
1168 | |
1169 function auto = isautoload (desc) | |
6695 | 1170 auto = false; |
1171 if (isfield (desc{1}, "autoload")) | |
1172 a = desc{1}.autoload; | |
1173 if ((isnumeric (a) && a > 0) | |
1174 || (ischar (a) && (strcmpi (a, "true") | |
6675 | 1175 || strcmpi (a, "on") |
1176 || strcmpi (a, "yes") | |
1177 || strcmpi (a, "1")))) | |
6695 | 1178 auto = true; |
1179 endif | |
6675 | 1180 endif |
1181 endfunction | |
1182 | |
6496 | 1183 function prepare_installation (desc, packdir) |
1184 ## Is there a pre_install to call? | |
1185 if (exist (fullfile (packdir, "pre_install.m"), "file")) | |
1186 wd = pwd (); | |
1187 try | |
1188 cd (packdir); | |
1189 pre_install (desc); | |
1190 cd (wd); | |
1191 catch | |
1192 cd (wd); | |
6695 | 1193 rethrow (lasterror ()); |
6496 | 1194 end_try_catch |
1195 endif | |
1196 | |
1197 ## If the directory "inst" doesn't exist, we create it | |
1198 inst_dir = fullfile (packdir, "inst"); | |
1199 if (! exist (inst_dir, "dir")) | |
1200 [status, msg] = mkdir (inst_dir); | |
1201 if (status != 1) | |
1202 rm_rf (desc.dir); | |
1203 error ("the 'inst' directory did not exist and could not be created: %s", | |
1204 msg); | |
6258 | 1205 endif |
1206 endif | |
1207 endfunction | |
1208 | |
6614 | 1209 function configure_make (desc, packdir, verbose) |
6496 | 1210 ## Perform ./configure, make, make install in "src" |
1211 if (exist (fullfile (packdir, "src"), "dir")) | |
1212 src = fullfile (packdir, "src"); | |
1213 ## configure | |
1214 if (exist (fullfile (src, "configure"), "file")) | |
7111 | 1215 flags = ""; |
1216 if (isempty (getenv ("CC"))) | |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7498
diff
changeset
|
1217 flags = cstrcat (flags, " CC=\"", octave_config_info ("CC"), "\""); |
7111 | 1218 endif |
1219 if (isempty (getenv ("CXX"))) | |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7498
diff
changeset
|
1220 flags = cstrcat (flags, " CXX=\"", octave_config_info ("CXX"), "\""); |
7111 | 1221 endif |
1222 if (isempty (getenv ("AR"))) | |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7498
diff
changeset
|
1223 flags = cstrcat (flags, " AR=\"", octave_config_info ("AR"), "\""); |
7111 | 1224 endif |
1225 if (isempty (getenv ("RANLIB"))) | |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7498
diff
changeset
|
1226 flags = cstrcat (flags, " RANLIB=\"", octave_config_info ("RANLIB"), "\""); |
7111 | 1227 endif |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7498
diff
changeset
|
1228 [status, output] = shell (cstrcat ("cd ", src, "; ./configure --prefix=\"", |
7111 | 1229 desc.dir, "\"", flags)); |
6496 | 1230 if (status != 0) |
1231 rm_rf (desc.dir); | |
1232 error ("the configure script returned the following error: %s", output); | |
6675 | 1233 elseif (verbose) |
1234 printf("%s", output); | |
6496 | 1235 endif |
6675 | 1236 |
5801 | 1237 endif |
1238 | |
6496 | 1239 ## make |
1240 if (exist (fullfile (src, "Makefile"), "file")) | |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7498
diff
changeset
|
1241 [status, output] = shell (cstrcat ("export INSTALLDIR=\"", desc.dir, |
6645 | 1242 "\"; make -C ", src)); |
6496 | 1243 if (status != 0) |
1244 rm_rf (desc.dir); | |
1245 error ("'make' returned the following error: %s", output); | |
6675 | 1246 elseif (verbose) |
1247 printf("%s", output); | |
6496 | 1248 endif |
5801 | 1249 endif |
1250 | |
6614 | 1251 ## Copy files to "inst" and "inst/arch" (this is instead of 'make install') |
6496 | 1252 files = fullfile (src, "FILES"); |
1253 instdir = fullfile (packdir, "inst"); | |
6614 | 1254 archdir = fullfile (packdir, "inst", getarch ()); |
6950 | 1255 |
6655 | 1256 ## Get file names |
6496 | 1257 if (exist (files, "file")) |
1258 [fid, msg] = fopen (files, "r"); | |
1259 if (fid < 0) | |
1260 error ("couldn't open %s: %s", files, msg); | |
5971 | 1261 endif |
6496 | 1262 filenames = char (fread (fid))'; |
1263 fclose (fid); | |
1264 if (filenames(end) == "\n") | |
1265 filenames(end) = []; | |
6258 | 1266 endif |
6655 | 1267 filenames = split_by (filenames, "\n"); |
6496 | 1268 delete_idx = []; |
6655 | 1269 for i = 1:length (filenames) |
1270 if (! all (isspace (filenames{i}))) | |
1271 filenames{i} = fullfile (src, filenames{i}); | |
6496 | 1272 else |
1273 delete_idx(end+1) = i; | |
1274 endif | |
5971 | 1275 endfor |
6655 | 1276 filenames(delete_idx) = []; |
6496 | 1277 else |
1278 m = dir (fullfile (src, "*.m")); | |
1279 oct = dir (fullfile (src, "*.oct")); | |
1280 mex = dir (fullfile (src, "*.mex")); | |
6614 | 1281 archdependent = ""; |
1282 archindependent = ""; | |
6496 | 1283 filenames = ""; |
1284 if (length (m) > 0) | |
1285 filenames = sprintf (fullfile (src, "%s "), m.name); | |
1286 endif | |
1287 if (length (oct) > 0) | |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7498
diff
changeset
|
1288 filenames = cstrcat (filenames, " ", |
7498 | 1289 sprintf (fullfile (src, "%s "), oct.name)); |
6496 | 1290 endif |
1291 if (length (mex) > 0) | |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7498
diff
changeset
|
1292 filenames = cstrcat (filenames, " ", |
7498 | 1293 sprintf (fullfile (src, "%s "), mex.name)); |
6496 | 1294 endif |
6614 | 1295 filenames = split_by (filenames, " "); |
5971 | 1296 endif |
5801 | 1297 |
6950 | 1298 ## Split into architecture dependent and independent files |
7024 | 1299 if (isempty (filenames)) |
1300 idx = []; | |
1301 else | |
1302 idx = cellfun (@(x) is_architecture_dependent (x), filenames); | |
1303 endif | |
6950 | 1304 archdependent = filenames (idx); |
1305 archindependent = filenames (!idx); | |
1306 | |
6655 | 1307 ## Copy the files |
7548
9cbf1e2011a3
pkg.m: adapt to changes in isspace for cell arrays of strings.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7540
diff
changeset
|
1308 if (! all (isspace ([filenames{:}]))) |
6655 | 1309 if (! exist (instdir, "dir")) |
1310 mkdir (instdir); | |
1311 endif | |
7548
9cbf1e2011a3
pkg.m: adapt to changes in isspace for cell arrays of strings.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7540
diff
changeset
|
1312 if (! all (isspace ([archindependent{:}]))) |
6634 | 1313 if (verbose) |
1314 printf ("copyfile"); | |
1315 printf (" %s", archindependent{:}); | |
1316 printf ("%s\n", instdir); | |
1317 endif | |
6614 | 1318 [status, output] = copyfile (archindependent, instdir); |
1319 if (status != 1) | |
6496 | 1320 rm_rf (desc.dir); |
1321 error ("Couldn't copy files from 'src' to 'inst': %s", output); | |
6614 | 1322 endif |
1323 endif | |
7548
9cbf1e2011a3
pkg.m: adapt to changes in isspace for cell arrays of strings.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7540
diff
changeset
|
1324 if (! all (isspace ([archdependent{:}]))) |
6634 | 1325 if (verbose) |
1326 printf ("copyfile"); | |
1327 printf (" %s", archdependent{:}); | |
1328 printf (" %s\n", archdir); | |
1329 endif | |
6655 | 1330 if (! exist (archdir, "dir")) |
1331 mkdir (archdir); | |
1332 endif | |
6614 | 1333 [status, output] = copyfile (archdependent, archdir); |
1334 if (status != 1) | |
1335 rm_rf (desc.dir); | |
1336 error ("Couldn't copy files from 'src' to 'inst': %s", output); | |
1337 endif | |
1338 endif | |
5801 | 1339 endif |
6496 | 1340 endif |
5801 | 1341 endfunction |
1342 | |
5971 | 1343 function pkg = extract_pkg (nm, pat) |
5955 | 1344 fid = fopen (nm, "rt"); |
5971 | 1345 pkg = ""; |
5955 | 1346 if (fid >= 0) |
6496 | 1347 while (! feof (fid)) |
5955 | 1348 ln = fgetl (fid); |
1349 if (ln > 0) | |
6496 | 1350 t = regexp (ln, pat, "tokens"); |
1351 if (! isempty (t)) | |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7498
diff
changeset
|
1352 pkg = cstrcat (pkg, "\n", t{1}{1}); |
5955 | 1353 endif |
1354 endif | |
1355 endwhile | |
6496 | 1356 if (! isempty (pkg)) |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7498
diff
changeset
|
1357 pkg = cstrcat (pkg, "\n"); |
5955 | 1358 endif |
1359 fclose (fid); | |
1360 endif | |
1361 endfunction | |
1362 | |
6925 | 1363 function create_pkgadddel (desc, packdir, nm, global_install) |
6675 | 1364 instpkg = fullfile (desc.dir, nm); |
1365 instfid = fopen (instpkg, "wt"); | |
1366 ## If it is exists, most of the PKG_* file should go into the | |
1367 ## architecture dependent directory so that the autoload/mfilename | |
1368 ## commands work as expected. The only part that doesn't is the | |
1369 ## part in the main directory. | |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7498
diff
changeset
|
1370 archdir = fullfile (getarchprefix (desc), cstrcat (desc.name, "-", |
6925 | 1371 desc.version), getarch ()); |
1372 if (exist (getarchdir (desc, global_install), "dir")) | |
1373 archpkg = fullfile (getarchdir (desc, global_install), nm); | |
1374 archfid = fopen (archpkg, "at"); | |
6675 | 1375 else |
1376 archpkg = instpkg; | |
1377 archfid = instfid; | |
1378 endif | |
6634 | 1379 |
6675 | 1380 if (archfid >= 0 && instfid >= 0) |
5971 | 1381 ## Search all dot-m files for PKG commands |
7498 | 1382 lst = dir (fullfile (packdir, "inst", "*.m")); |
6496 | 1383 for i = 1:length (lst) |
7498 | 1384 nam = fullfile (packdir, "inst", lst(i).name); |
6675 | 1385 fwrite (instfid, extract_pkg (nam, ['^[#%][#%]* *' nm ': *(.*)$'])); |
5955 | 1386 endfor |
1387 | |
5971 | 1388 ## Search all C++ source files for PKG commands |
7498 | 1389 lst = dir (fullfile (packdir, "src", "*.cc")); |
6496 | 1390 for i = 1:length (lst) |
7498 | 1391 nam = fullfile (packdir, "src", lst(i).name); |
6675 | 1392 fwrite (archfid, extract_pkg (nam, ['^//* *' nm ': *(.*)$'])); |
1393 fwrite (archfid, extract_pkg (nam, ['^/\** *' nm ': *(.*) *\*/$'])); | |
5955 | 1394 endfor |
1395 | |
5971 | 1396 ## Add developer included PKG commands |
6496 | 1397 packdirnm = fullfile (packdir, nm); |
1398 if (exist (packdirnm, "file")) | |
6675 | 1399 fid = fopen (packdirnm, "rt"); |
1400 if (fid >= 0) | |
1401 while (! feof (fid)) | |
1402 ln = fgets (fid); | |
5955 | 1403 if (ln > 0) |
6675 | 1404 fwrite (archfid, ln); |
5955 | 1405 endif |
1406 endwhile | |
6675 | 1407 fclose (fid); |
5955 | 1408 endif |
1409 endif | |
5971 | 1410 |
6675 | 1411 ## If the files is empty remove it |
1412 fclose (instfid); | |
1413 t = dir (instpkg); | |
5971 | 1414 if (t.bytes <= 0) |
6675 | 1415 unlink (instpkg); |
1416 endif | |
1417 | |
1418 if (instfid != archfid) | |
1419 fclose (archfid); | |
1420 t = dir (archpkg); | |
1421 if (t.bytes <= 0) | |
1422 unlink (archpkg); | |
1423 endif | |
5971 | 1424 endif |
5955 | 1425 endif |
1426 endfunction | |
1427 | |
6925 | 1428 function copy_files (desc, packdir, global_install) |
6496 | 1429 ## Create the installation directory |
1430 if (! exist (desc.dir, "dir")) | |
1431 [status, output] = mkdir (desc.dir); | |
1432 if (status != 1) | |
1433 error ("couldn't create installation directory %s : %s", | |
1434 desc.dir, output); | |
6020 | 1435 endif |
6496 | 1436 endif |
6020 | 1437 |
6925 | 1438 octfiledir = getarchdir (desc); |
1439 | |
6496 | 1440 ## Copy the files from "inst" to installdir |
1441 instdir = fullfile (packdir, "inst"); | |
1442 if (! dirempty (instdir)) | |
1443 [status, output] = copyfile (fullfile (instdir, "*"), desc.dir); | |
1444 if (status != 1) | |
1445 rm_rf (desc.dir); | |
1446 error ("couldn't copy files to the installation directory"); | |
5801 | 1447 endif |
6925 | 1448 if (exist (fullfile (desc.dir, getarch ()), "dir") && |
1449 ! strcmp (fullfile (desc.dir, getarch ()), octfiledir)) | |
1450 if (! exist (octfiledir, "dir")) | |
1451 ## Can be required to create upto three levels of dirs | |
1452 octm1 = fileparts (octfiledir); | |
1453 if (! exist (octm1, "dir")) | |
1454 octm2 = fileparts (octm1); | |
1455 if (! exist (octm2, "dir")) | |
1456 octm3 = fileparts (octm2); | |
1457 if (! exist (octm3, "dir")) | |
1458 [status, output] = mkdir (octm3); | |
1459 if (status != 1) | |
1460 rm_rf (desc.dir); | |
1461 error ("couldn't create installation directory %s : %s", | |
1462 octm3, output); | |
1463 endif | |
1464 endif | |
1465 [status, output] = mkdir (octm2); | |
1466 if (status != 1) | |
1467 rm_rf (desc.dir); | |
1468 error ("couldn't create installation directory %s : %s", | |
1469 octm2, output); | |
1470 endif | |
1471 endif | |
1472 [status, output] = mkdir (octm1); | |
1473 if (status != 1) | |
1474 rm_rf (desc.dir); | |
1475 error ("couldn't create installation directory %s : %s", | |
1476 octm1, output); | |
1477 endif | |
1478 endif | |
1479 [status, output] = mkdir (octfiledir); | |
1480 if (status != 1) | |
1481 rm_rf (desc.dir); | |
1482 error ("couldn't create installation directory %s : %s", | |
1483 octfiledir, output); | |
1484 endif | |
1485 endif | |
1486 [status, output] = movefile (fullfile (desc.dir, getarch (), "*"), | |
1487 octfiledir); | |
1488 rm_rf (fullfile (desc.dir, getarch ())); | |
1489 | |
1490 if (status != 1) | |
1491 rm_rf (desc.dir); | |
1492 rm_rf (octfiledir); | |
1493 error ("couldn't copy files to the installation directory"); | |
1494 endif | |
1495 endif | |
1496 | |
6496 | 1497 endif |
5801 | 1498 |
6496 | 1499 ## Create the "packinfo" directory |
1500 packinfo = fullfile (desc.dir, "packinfo"); | |
1501 [status, msg] = mkdir (packinfo); | |
1502 if (status != 1) | |
1503 rm_rf (desc.dir); | |
6925 | 1504 rm_rf (octfiledir); |
6496 | 1505 error ("couldn't create packinfo directory: %s", msg); |
1506 endif | |
5801 | 1507 |
6496 | 1508 ## Copy DESCRIPTION |
1509 [status, output] = copyfile (fullfile (packdir, "DESCRIPTION"), packinfo); | |
1510 if (status != 1) | |
6925 | 1511 rm_rf (desc.dir); |
1512 rm_rf (octfiledir); | |
1513 error ("couldn't copy DESCRIPTION: %s", output); | |
6496 | 1514 endif |
5801 | 1515 |
6496 | 1516 ## Copy COPYING |
1517 [status, output] = copyfile (fullfile (packdir, "COPYING"), packinfo); | |
1518 if (status != 1) | |
6925 | 1519 rm_rf (desc.dir); |
1520 rm_rf (octfiledir); | |
1521 error ("couldn't copy COPYING: %s", output); | |
6496 | 1522 endif |
5993 | 1523 |
6496 | 1524 ## If the file ChangeLog exists, copy it |
7498 | 1525 changelog_file = fullfile (packdir, "ChangeLog"); |
1526 if (exist (changelog_file, "file")) | |
1527 [status, output] = copyfile (changelog_file, packinfo); | |
6496 | 1528 if (status != 1) |
1529 rm_rf (desc.dir); | |
6925 | 1530 rm_rf (octfiledir); |
6496 | 1531 error ("couldn't copy ChangeLog file: %s", output); |
6254 | 1532 endif |
6496 | 1533 endif |
6254 | 1534 |
6496 | 1535 ## Is there an INDEX file to copy or should we generate one? |
7498 | 1536 index_file = fullfile (packdir, "INDEX"); |
1537 if (exist(index_file, "file")) | |
1538 [status, output] = copyfile (index_file, packinfo); | |
6496 | 1539 if (status != 1) |
1540 rm_rf (desc.dir); | |
6925 | 1541 rm_rf (octfiledir); |
6496 | 1542 error ("couldn't copy INDEX file: %s", output); |
5801 | 1543 endif |
6496 | 1544 else |
1545 try | |
7498 | 1546 write_index (desc, fullfile (packdir, "inst"), |
6925 | 1547 fullfile (packinfo, "INDEX"), global_install); |
6496 | 1548 catch |
1549 rm_rf (desc.dir); | |
6925 | 1550 rm_rf (octfiledir); |
6695 | 1551 rethrow (lasterror ()); |
6496 | 1552 end_try_catch |
1553 endif | |
5971 | 1554 |
6496 | 1555 ## Is there an 'on_uninstall.m' to install? |
7498 | 1556 fon_uninstall = fullfile (packdir, "on_uninstall.m"); |
6496 | 1557 if (exist (fon_uninstall, "file")) |
1558 [status, output] = copyfile (fon_uninstall, packinfo); | |
1559 if (status != 1) | |
1560 rm_rf (desc.dir); | |
6925 | 1561 rm_rf (octfiledir); |
6496 | 1562 error ("couldn't copy on_uninstall.m: %s", output); |
5971 | 1563 endif |
6496 | 1564 endif |
5971 | 1565 |
6496 | 1566 ## Is there a doc/ directory that needs to be installed |
1567 docdir = fullfile (packdir, "doc"); | |
1568 if (exist (docdir, "dir") && ! dirempty (docdir)) | |
6925 | 1569 [status, output] = copyfile (docdir, desc.dir); |
6496 | 1570 endif |
1571 | |
1572 ## Is there a bin/ directory that needs to be installed | |
6950 | 1573 ## FIXME: Need to treat architecture dependent files in bin/ |
6496 | 1574 bindir = fullfile (packdir, "bin"); |
1575 if (exist (bindir, "dir") && ! dirempty (bindir)) | |
6925 | 1576 [status, output] = copyfile (bindir, desc.dir); |
6496 | 1577 endif |
5801 | 1578 endfunction |
1579 | |
6925 | 1580 function finish_installation (desc, packdir, global_install) |
6496 | 1581 ## Is there a post-install to call? |
1582 if (exist (fullfile (packdir, "post_install.m"), "file")) | |
1583 wd = pwd (); | |
1584 try | |
1585 cd (packdir); | |
1586 post_install (desc); | |
1587 cd (wd); | |
1588 catch | |
1589 cd (wd); | |
1590 rm_rf (desc.dir); | |
6925 | 1591 rm_rf (getarchdir (desc), global_install); |
6695 | 1592 rethrow (lasterror ()); |
6496 | 1593 end_try_catch |
1594 endif | |
5801 | 1595 endfunction |
1596 | |
7498 | 1597 ## Make sure the package contains the essential files. |
6496 | 1598 function verify_directory (dir) |
1599 needed_files = {"COPYING", "DESCRIPTION"}; | |
1600 for f = needed_files | |
1601 if (! exist (fullfile (dir, f{1}), "file")) | |
1602 error ("package is missing file: %s", f{1}); | |
1603 endif | |
1604 endfor | |
5801 | 1605 endfunction |
1606 | |
7498 | 1607 ## Parse the DESCRIPTION file |
6496 | 1608 function desc = get_description (filename) |
1609 [fid, msg] = fopen (filename, "r"); | |
1610 if (fid == -1) | |
1611 error ("the DESCRIPTION file %s could not be read: %s", filename, msg); | |
1612 endif | |
1613 | |
1614 desc = struct (); | |
5801 | 1615 |
6496 | 1616 line = fgetl (fid); |
1617 while (line != -1) | |
1618 if (line(1) == "#") | |
1619 ## Comments, do nothing | |
1620 elseif (isspace(line(1))) | |
1621 ## Continuation lines | |
1622 if (exist ("keyword", "var") && isfield (desc, keyword)) | |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7498
diff
changeset
|
1623 desc.(keyword) = cstrcat (desc.(keyword), " ", rstrip(line)); |
6496 | 1624 endif |
5801 | 1625 else |
6496 | 1626 ## Keyword/value pair |
1627 colon = find (line == ":"); | |
1628 if (length (colon) == 0) | |
1629 disp ("skipping line"); | |
1630 else | |
1631 colon = colon(1); | |
1632 keyword = tolower (strip (line(1:colon-1))); | |
7498 | 1633 value = strip (line (colon+1:end)); |
6496 | 1634 if (length (value) == 0) |
1635 fclose (fid); | |
1636 error ("the keyword %s has an empty value", desc.keywords{end}); | |
1637 endif | |
1638 desc.(keyword) = value; | |
1639 endif | |
5801 | 1640 endif |
6496 | 1641 line = fgetl (fid); |
1642 endwhile | |
1643 fclose (fid); | |
1644 | |
1645 ## Make sure all is okay | |
1646 needed_fields = {"name", "version", "date", "title", ... | |
1647 "author", "maintainer", "description"}; | |
1648 for f = needed_fields | |
1649 if (! isfield (desc, f{1})) | |
1650 error ("description is missing needed field %s", f{1}); | |
1651 endif | |
1652 endfor | |
1653 desc.version = fix_version (desc.version); | |
1654 if (isfield (desc, "depends")) | |
1655 desc.depends = fix_depends (desc.depends); | |
1656 else | |
1657 desc.depends = ""; | |
1658 endif | |
1659 desc.name = tolower (desc.name); | |
5801 | 1660 endfunction |
1661 | |
7498 | 1662 ## Make sure the version string v is a valid x.y.z version string |
5801 | 1663 ## Examples: "0.1" => "0.1.0", "monkey" => error(...) |
6496 | 1664 function out = fix_version (v) |
1665 dots = find (v == "."); | |
1666 if (length (dots) == 1) | |
1667 major = str2num (v(1:dots-1)); | |
1668 minor = str2num (v(dots+1:end)); | |
1669 if (length (major) != 0 && length (minor) != 0) | |
1670 out = sprintf ("%d.%d.0", major, minor); | |
1671 return; | |
5801 | 1672 endif |
6496 | 1673 elseif (length (dots) == 2) |
1674 major = str2num (v(1:dots(1)-1)); | |
1675 minor = str2num (v(dots(1)+1:dots(2)-1)); | |
7498 | 1676 rev = str2num (v(dots(2)+1:end)); |
6496 | 1677 if (length (major) != 0 && length (minor) != 0 && length (rev) != 0) |
1678 out = sprintf ("%d.%d.%d", major, minor, rev); | |
1679 return; | |
1680 endif | |
1681 endif | |
1682 error ("bad version string: %s", v); | |
5801 | 1683 endfunction |
1684 | |
7498 | 1685 ## Make sure the depends field is of the right format. |
5801 | 1686 ## This function returns a cell of structures with the following fields: |
1687 ## package, version, operator | |
6496 | 1688 function deps_cell = fix_depends (depends) |
1689 deps = split_by (tolower (depends), ","); | |
1690 deps_cell = cell (1, length (deps)); | |
1691 | |
1692 ## For each dependency | |
1693 for i = 1:length (deps) | |
1694 dep = deps{i}; | |
1695 lpar = find (dep == "("); | |
1696 rpar = find (dep == ")"); | |
1697 ## Does the dependency specify a version | |
1698 ## Example: package(>= version) | |
1699 if (length (lpar) == 1 && length (rpar) == 1) | |
1700 package = tolower (strip (dep(1:lpar-1))); | |
1701 sub = dep(lpar(1)+1:rpar(1)-1); | |
1702 parts = split_by (sub, " "); | |
1703 idx = []; | |
1704 for r = 1:size (parts, 1) | |
1705 if (length (parts{r}) > 0) | |
1706 idx(end+1) = r; | |
1707 endif | |
1708 endfor | |
1709 | |
1710 if (length (idx) != 2) | |
1711 error ("incorrect syntax for dependency `%s' in the DESCRIPTION file\n", | |
1712 dep); | |
1713 endif | |
1714 operator = parts{idx(1)}; | |
1715 if (! any (strcmp (operator, {">", ">=", "<=", "<", "=="}))) | |
1716 error ("unsupported operator: %s", operator); | |
1717 endif | |
1718 version = fix_version (parts{idx(2)}); | |
1719 | |
1720 ## If no version is specified for the dependency | |
1721 ## we say that the version should be greater than | |
1722 ## or equal to 0.0.0 | |
1723 else | |
1724 package = tolower (strip (dep)); | |
1725 operator = ">="; | |
1726 version = "0.0.0"; | |
1727 endif | |
1728 deps_cell{i} = struct ("package", package, "operator", operator, | |
1729 "version", version); | |
1730 endfor | |
5801 | 1731 endfunction |
1732 | |
7498 | 1733 ## Strip the text of spaces from the right |
5801 | 1734 ## Example: " hello world " => " hello world" (XXX: is this the same as deblank?) |
6496 | 1735 function text = rstrip (text) |
1736 chars = find (! isspace (text)); | |
1737 if (length (chars) > 0) | |
1738 ## XXX: shouldn't it be text = text(1:chars(end)); | |
1739 text = text (chars(1):end); | |
1740 else | |
1741 text = ""; | |
1742 endif | |
5801 | 1743 endfunction |
1744 | |
7498 | 1745 ## Strip the text of spaces from the left and the right |
5801 | 1746 ## Example: " hello world " => "hello world" |
6496 | 1747 function text = strip (text) |
1748 chars = find (! isspace (text)); | |
1749 if (length (chars) > 0) | |
1750 text = text(chars(1):chars(end)); | |
1751 else | |
1752 text = ""; | |
1753 endif | |
5801 | 1754 endfunction |
1755 | |
7498 | 1756 ## Split the text into a cell array of strings by sep |
5801 | 1757 ## Example: "A, B" => {"A", "B"} (with sep = ",") |
6496 | 1758 function out = split_by (text, sep) |
1759 text_matrix = split (text, sep); | |
1760 num_words = size (text_matrix, 1); | |
1761 out = cell (num_words, 1); | |
1762 for i = 1:num_words | |
1763 out{i} = strip (text_matrix(i, :)); | |
1764 endfor | |
5801 | 1765 endfunction |
1766 | |
7498 | 1767 ## Create an INDEX file for a package that doesn't provide one. |
5801 | 1768 ## 'desc' describes the package. |
7498 | 1769 ## 'dir' is the 'inst' directory in temporary directory. |
1770 ## 'index_file' is the name (including path) of resulting INDEX file. | |
1771 function write_index (desc, dir, index_file, global_install) | |
6496 | 1772 ## Get names of functions in dir |
1773 [files, err, msg] = readdir (dir); | |
1774 if (err) | |
1775 error ("couldn't read directory %s: %s", dir, msg); | |
1776 endif | |
1777 | |
6634 | 1778 ## Check for architecture dependent files |
6925 | 1779 tmpdir = getarchdir (desc); |
6634 | 1780 if (exist (tmpdir, "dir")) |
1781 [files2, err, msg] = readdir (tmpdir); | |
1782 if (err) | |
1783 error ("couldn't read directory %s: %s", tmpdir, msg); | |
1784 endif | |
1785 files = [files; files2]; | |
1786 endif | |
1787 | |
6496 | 1788 functions = {}; |
1789 for i = 1:length (files) | |
1790 file = files{i}; | |
1791 lf = length (file); | |
1792 if (lf > 2 && strcmp (file(end-1:end), ".m")) | |
1793 functions{end+1} = file(1:end-2); | |
1794 elseif (lf > 4 && strcmp (file(end-3:end), ".oct")) | |
1795 functions{end+1} = file(1:end-4); | |
5801 | 1796 endif |
6496 | 1797 endfor |
1798 | |
1799 ## Does desc have a categories field? | |
1800 if (! isfield (desc, "categories")) | |
1801 error ("the DESCRIPTION file must have a Categories field, when no INDEX file is given"); | |
1802 endif | |
1803 categories = split_by (desc.categories, ","); | |
1804 if (length (categories) < 1) | |
1805 error ("the Category field is empty"); | |
1806 endif | |
1807 | |
1808 ## Write INDEX | |
7498 | 1809 fid = fopen (index_file, "w"); |
6496 | 1810 if (fid == -1) |
7498 | 1811 error ("couldn't open %s for writing.", index_file); |
6496 | 1812 endif |
1813 fprintf (fid, "%s >> %s\n", desc.name, desc.title); | |
1814 fprintf (fid, "%s\n", categories{1}); | |
1815 fprintf (fid, " %s\n", functions{:}); | |
1816 fclose (fid); | |
5801 | 1817 endfunction |
1818 | |
6820 | 1819 function bad_deps = get_unsatisfied_deps (desc, installed_pkgs_lst) |
6496 | 1820 bad_deps = {}; |
5801 | 1821 |
6496 | 1822 ## For each dependency |
1823 for i = 1:length (desc.depends) | |
1824 dep = desc.depends{i}; | |
5801 | 1825 |
6496 | 1826 ## Is the current dependency Octave? |
1827 if (strcmp (dep.package, "octave")) | |
1828 if (! compare_versions (OCTAVE_VERSION, dep.version, dep.operator)) | |
1829 bad_deps{end+1} = dep; | |
6258 | 1830 endif |
6496 | 1831 ## Is the current dependency not Octave? |
1832 else | |
1833 ok = false; | |
6820 | 1834 for i = 1:length (installed_pkgs_lst) |
1835 cur_name = installed_pkgs_lst{i}.name; | |
1836 cur_version = installed_pkgs_lst{i}.version; | |
6496 | 1837 if (strcmp (dep.package, cur_name) |
1838 && compare_versions (cur_version, dep.version, dep.operator)) | |
1839 ok = true; | |
1840 break; | |
6258 | 1841 endif |
1842 endfor | |
6496 | 1843 if (! ok) |
1844 bad_deps{end+1} = dep; | |
1845 endif | |
5801 | 1846 endif |
6496 | 1847 endfor |
1848 endfunction | |
1849 | |
1850 function [out1, out2] = installed_packages (local_list, global_list) | |
1851 ## Get the list of installed packages | |
1852 try | |
1853 local_packages = load (local_list).local_packages; | |
1854 catch | |
1855 local_packages = {}; | |
1856 end_try_catch | |
1857 try | |
6675 | 1858 global_packages = load (global_list).global_packages; |
6496 | 1859 catch |
1860 global_packages = {}; | |
1861 end_try_catch | |
6820 | 1862 installed_pkgs_lst = {local_packages{:}, global_packages{:}}; |
6496 | 1863 |
1864 ## Eliminate duplicates in the installed package list. | |
1865 ## Locally installed packages take precedence | |
1866 dup = []; | |
6820 | 1867 for i = 1:length (installed_pkgs_lst) |
6496 | 1868 if (find (dup, i)) |
1869 continue; | |
5801 | 1870 endif |
6820 | 1871 for j = (i+1):length (installed_pkgs_lst) |
6496 | 1872 if (find (dup, j)) |
1873 continue; | |
1874 endif | |
6820 | 1875 if (strcmp (installed_pkgs_lst{i}.name, installed_pkgs_lst{j}.name)) |
6496 | 1876 dup = [dup, j]; |
1877 endif | |
5987 | 1878 endfor |
6496 | 1879 endfor |
1880 if (! isempty(dup)) | |
6820 | 1881 installed_pkgs_lst(dup) = []; |
6496 | 1882 endif |
1883 | |
6616 | 1884 ## Now check if the package is loaded |
6645 | 1885 tmppath = strrep (path(), "\\", "/"); |
6820 | 1886 for i = 1:length (installed_pkgs_lst) |
1887 if (findstr (tmppath, strrep (installed_pkgs_lst{i}.dir, "\\", "/"))) | |
1888 installed_pkgs_lst{i}.loaded = true; | |
6616 | 1889 else |
6820 | 1890 installed_pkgs_lst{i}.loaded = false; |
6616 | 1891 endif |
1892 endfor | |
6675 | 1893 for i = 1:length (local_packages) |
6776 | 1894 if (findstr (tmppath, strrep (local_packages{i}.dir, "\\", "/"))) |
6675 | 1895 local_packages{i}.loaded = true; |
1896 else | |
1897 local_packages{i}.loaded = false; | |
1898 endif | |
1899 endfor | |
1900 for i = 1:length (global_packages) | |
6776 | 1901 if (findstr (tmppath, strrep (global_packages{i}.dir, "\\", "/"))) |
6675 | 1902 global_packages{i}.loaded = true; |
1903 else | |
1904 global_packages{i}.loaded = false; | |
1905 endif | |
1906 endfor | |
6616 | 1907 |
6496 | 1908 ## Should we return something? |
1909 if (nargout == 2) | |
1910 out1 = local_packages; | |
1911 out2 = global_packages; | |
1912 return; | |
1913 elseif (nargout == 1) | |
6820 | 1914 out1 = installed_pkgs_lst; |
6496 | 1915 return; |
1916 endif | |
1917 | |
1918 ## We shouldn't return something, so we'll print something | |
6820 | 1919 num_packages = length (installed_pkgs_lst); |
6496 | 1920 if (num_packages == 0) |
1921 printf ("no packages installed.\n"); | |
1922 return; | |
1923 endif | |
1924 | |
1925 ## Compute the maximal lengths of name, version, and dir | |
1926 h1 = "Package Name"; | |
1927 h2 = "Version"; | |
1928 h3 = "Installation directory"; | |
1929 max_name_length = length (h1); | |
1930 max_version_length = length (h2); | |
1931 names = cell (num_packages, 1); | |
1932 for i = 1:num_packages | |
1933 max_name_length = max (max_name_length, | |
6820 | 1934 length (installed_pkgs_lst{i}.name)); |
6496 | 1935 max_version_length = max (max_version_length, |
6820 | 1936 length (installed_pkgs_lst{i}.version)); |
1937 names{i} = installed_pkgs_lst{i}.name; | |
6496 | 1938 endfor |
6698 | 1939 max_dir_length = terminal_size()(2) - max_name_length - ... |
1940 max_version_length - 7; | |
1941 if (max_dir_length < 20) | |
1942 max_dir_length = Inf; | |
1943 endif | |
1944 | |
6616 | 1945 h1 = postpad (h1, max_name_length + 1, " "); |
6496 | 1946 h2 = postpad (h2, max_version_length, " ");; |
1947 | |
1948 ## Print a header | |
1949 header = sprintf("%s | %s | %s\n", h1, h2, h3); | |
1950 printf (header); | |
1951 tmp = sprintf (repmat ("-", 1, length(header)-1)); | |
1952 tmp(length(h1)+2) = "+"; | |
1953 tmp(length(h1)+length(h2)+5) = "+"; | |
1954 printf ("%s\n", tmp); | |
1955 | |
1956 ## Print the packages | |
6616 | 1957 format = sprintf ("%%%ds %%1s| %%%ds | %%s\n", max_name_length, |
6496 | 1958 max_version_length); |
1959 [dummy, idx] = sort (names); | |
1960 for i = 1:num_packages | |
6820 | 1961 cur_name = installed_pkgs_lst{idx(i)}.name; |
1962 cur_version = installed_pkgs_lst{idx(i)}.version; | |
1963 cur_dir = installed_pkgs_lst{idx(i)}.dir; | |
6698 | 1964 if (length (cur_dir) > max_dir_length) |
1965 first_char = length (cur_dir) - max_dir_length + 4; | |
1966 first_filesep = strfind (cur_dir(first_char:end), filesep()); | |
1967 if (! isempty (first_filesep)) | |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7498
diff
changeset
|
1968 cur_dir = cstrcat ("...", |
6698 | 1969 cur_dir((first_char + first_filesep(1) - 1):end)); |
1970 else | |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7498
diff
changeset
|
1971 cur_dir = cstrcat ("...", cur_dir(first_char:end)); |
6698 | 1972 endif |
1973 endif | |
6820 | 1974 if (installed_pkgs_lst{idx(i)}.loaded) |
6616 | 1975 cur_loaded = "*"; |
1976 else | |
1977 cur_loaded = " "; | |
1978 endif | |
1979 printf (format, cur_name, cur_loaded, cur_version, cur_dir); | |
6496 | 1980 endfor |
5801 | 1981 endfunction |
1982 | |
6496 | 1983 function load_packages (files, handle_deps, local_list, global_list) |
6820 | 1984 installed_pkgs_lst = installed_packages (local_list, global_list); |
1985 num_packages = length (installed_pkgs_lst); | |
6496 | 1986 |
1987 ## Read package names and installdirs into a more convenient format | |
1988 pnames = pdirs = cell (1, num_packages); | |
1989 for i = 1:num_packages | |
6820 | 1990 pnames{i} = installed_pkgs_lst{i}.name; |
1991 pdirs{i} = installed_pkgs_lst{i}.dir; | |
6496 | 1992 endfor |
1993 | |
1994 ## load all | |
1995 if (length (files) == 1 && strcmp (files{1}, "all")) | |
6820 | 1996 idx = [1:length(installed_pkgs_lst)]; |
6496 | 1997 ## load auto |
6695 | 1998 elseif (length (files) == 1 && strcmp (files{1}, "auto")) |
1999 idx = []; | |
6820 | 2000 for i = 1:length (installed_pkgs_lst) |
6496 | 2001 if (exist (fullfile (pdirs{i}, "packinfo", ".autoload"), "file")) |
6695 | 2002 idx (end + 1) = i; |
6496 | 2003 endif |
6037 | 2004 endfor |
6496 | 2005 ## load package_name1 ... |
2006 else | |
6695 | 2007 idx = []; |
6496 | 2008 for i = 1:length (files) |
6695 | 2009 idx2 = find (strcmp (pnames, files{i})); |
2010 if (! any (idx2)) | |
6496 | 2011 error ("package %s is not installed", files{i}); |
2012 endif | |
6695 | 2013 idx (end + 1) = idx2; |
6496 | 2014 endfor |
2015 endif | |
6037 | 2016 |
6695 | 2017 ## Load the packages, but take care of the ordering of dependencies |
6925 | 2018 load_packages_and_dependencies (idx, handle_deps, installed_pkgs_lst, true); |
5801 | 2019 endfunction |
5928 | 2020 |
6496 | 2021 function unload_packages (files, handle_deps, local_list, global_list) |
6820 | 2022 installed_pkgs_lst = installed_packages (local_list, global_list); |
2023 num_packages = length (installed_pkgs_lst); | |
6496 | 2024 |
2025 ## Read package names and installdirs into a more convenient format | |
2026 pnames = pdirs = cell (1, num_packages); | |
2027 for i = 1:num_packages | |
6820 | 2028 pnames{i} = installed_pkgs_lst{i}.name; |
2029 pdirs{i} = installed_pkgs_lst{i}.dir; | |
2030 pdeps{i} = installed_pkgs_lst{i}.depends; | |
6496 | 2031 endfor |
2032 | |
2033 ## Get the current octave path | |
2034 p = split_by (path(), pathsep ()); | |
6203 | 2035 |
6496 | 2036 ## unload all |
2037 if (length (files) == 1 && strcmp (files{1}, "all")) | |
2038 dirs = pdirs; | |
6925 | 2039 desc = installed_pkgs_lst; |
6496 | 2040 ## unload package_name1 ... |
2041 else | |
2042 dirs = {}; | |
6925 | 2043 desc = {}; |
6496 | 2044 for i = 1:length (files) |
7208 | 2045 idx = strcmp (pnames, files{i}); |
6496 | 2046 if (! any (idx)) |
2047 error ("package %s is not installed", files{i}); | |
2048 endif | |
7208 | 2049 dirs{end+1} = pdirs{idx}; |
2050 desc{end+1} = installed_pkgs_lst{idx}; | |
6496 | 2051 endfor |
2052 endif | |
2053 | |
6614 | 2054 ## Check for architecture dependent directories |
2055 archdirs = {}; | |
2056 for i = 1:length (dirs) | |
7208 | 2057 tmpdir = getarchdir (desc{i}); |
6614 | 2058 if (exist (tmpdir, "dir")) |
7208 | 2059 archdirs{end+1} = dirs{i}; |
2060 archdirs{end+1} = tmpdir; | |
6925 | 2061 else |
7208 | 2062 archdirs{end+1} = dirs{i}; |
6614 | 2063 endif |
2064 endfor | |
2065 | |
6496 | 2066 ## Unload the packages |
6925 | 2067 for i = 1:length (archdirs) |
2068 d = archdirs{i}; | |
6496 | 2069 idx = strcmp (p, d); |
2070 if (any (idx)) | |
2071 rmpath (d); | |
2072 ## XXX: We should also check if we need to remove items from EXEC_PATH | |
6203 | 2073 endif |
6496 | 2074 endfor |
6203 | 2075 endfunction |
2076 | |
5928 | 2077 function [status_out, msg_out] = rm_rf (dir) |
6925 | 2078 if (exist (dir)) |
2079 crr = confirm_recursive_rmdir (); | |
2080 unwind_protect | |
2081 confirm_recursive_rmdir (false); | |
2082 [status, msg] = rmdir (dir, "s"); | |
2083 unwind_protect_cleanup | |
2084 confirm_recursive_rmdir (crr); | |
2085 end_unwind_protect | |
2086 else | |
2087 status = 1; | |
2088 msg = ""; | |
2089 endif | |
5928 | 2090 if (nargout > 0) |
2091 status_out = status; | |
2092 endif | |
2093 if (nargout > 1) | |
2094 msg_out = msg; | |
2095 endif | |
2096 endfunction | |
5971 | 2097 |
2098 function emp = dirempty (nm, ign) | |
6925 | 2099 if (exist (nm, "dir")) |
2100 if (nargin < 2) | |
2101 ign = {".", ".."}; | |
2102 else | |
2103 ign = [{".", ".."}, ign]; | |
2104 endif | |
2105 l = dir (nm); | |
2106 for i = 1:length (l) | |
2107 found = false; | |
2108 for j = 1:length (ign) | |
2109 if (strcmp (l(i).name, ign{j})) | |
2110 found = true; | |
2111 break; | |
2112 endif | |
2113 endfor | |
2114 if (! found) | |
2115 emp = false; | |
2116 return | |
5971 | 2117 endif |
2118 endfor | |
6925 | 2119 emp = true; |
2120 else | |
2121 emp = true; | |
2122 endif | |
5971 | 2123 endfunction |
6614 | 2124 |
2125 function arch = getarch () | |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7498
diff
changeset
|
2126 persistent _arch = cstrcat (octave_config_info("canonical_host_type"), ... |
6675 | 2127 "-", octave_config_info("api_version")); |
6614 | 2128 arch = _arch; |
2129 endfunction | |
6645 | 2130 |
6925 | 2131 function archprefix = getarchprefix (desc, global_install) |
2132 if ((nargin == 2 && global_install) || (nargin < 2 && issuperuser ())) | |
2133 archprefix = fullfile (octave_config_info ("libexecdir"), "octave", | |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7498
diff
changeset
|
2134 "packages", cstrcat(desc.name, "-", desc.version)); |
6925 | 2135 else |
2136 archprefix = desc.dir; | |
2137 endif | |
2138 endfunction | |
2139 | |
2140 function archdir = getarchdir (desc) | |
2141 archdir = fullfile (desc.archprefix, getarch()); | |
2142 endfunction | |
2143 | |
2144 function s = issuperuser () | |
2145 if ((ispc () && ! isunix ()) || (geteuid() == 0)) | |
2146 s = true; | |
2147 else | |
2148 s = false; | |
2149 endif | |
2150 endfunction | |
2151 | |
6645 | 2152 function [status, output] = shell (cmd) |
2153 persistent have_sh; | |
2154 | |
2155 cmd = strrep (cmd, "\\", "/"); | |
2156 if (ispc () && ! isunix ()) | |
2157 if (isempty(have_sh)) | |
2158 if (system("sh.exe -c \"exit\"")) | |
2159 have_sh = false; | |
2160 else | |
2161 have_sh = true; | |
2162 endif | |
2163 endif | |
2164 if (have_sh) | |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7498
diff
changeset
|
2165 [status, output] = system (cstrcat ("sh.exe -c \"", cmd, "\"")); |
6645 | 2166 else |
2167 error ("Can not find the command shell") | |
2168 endif | |
2169 else | |
2170 [status, output] = system (cmd); | |
2171 endif | |
2172 endfunction | |
6695 | 2173 |
2174 function newdesc = save_order (desc) | |
2175 newdesc = {}; | |
2176 for i = 1 : length(desc) | |
2177 deps = desc{i}.depends; | |
2178 if (isempty (deps) || (length (deps) == 1 && | |
2179 strcmp(deps{1}.package, "octave"))) | |
2180 newdesc {end + 1} = desc{i}; | |
2181 else | |
2182 tmpdesc = {}; | |
2183 for k = 1 : length (deps) | |
2184 for j = 1 : length (desc) | |
2185 if (strcmp (desc{j}.name, deps{k}.package)) | |
7208 | 2186 tmpdesc{end+1} = desc{j}; |
6695 | 2187 break; |
2188 endif | |
2189 endfor | |
2190 endfor | |
2191 if (! isempty (tmpdesc)) | |
2192 newdesc = {newdesc{:}, save_order(tmpdesc){:}, desc{i}}; | |
2193 else | |
7208 | 2194 newdesc{end+1} = desc{i}; |
6695 | 2195 endif |
2196 endif | |
2197 endfor | |
2198 ## Eliminate the duplicates | |
2199 idx = []; | |
2200 for i = 1 : length (newdesc) | |
2201 for j = (i + 1) : length (newdesc) | |
2202 if (strcmp (newdesc{i}.name, newdesc{j}.name)) | |
2203 idx (end + 1) = j; | |
2204 endif | |
2205 endfor | |
2206 endfor | |
2207 newdesc(idx) = []; | |
2208 endfunction | |
2209 | |
6925 | 2210 function load_packages_and_dependencies (idx, handle_deps, installed_pkgs_lst, |
2211 global_install) | |
6820 | 2212 idx = load_package_dirs (idx, [], handle_deps, installed_pkgs_lst); |
6695 | 2213 dirs = {}; |
2214 execpath = EXEC_PATH (); | |
2215 for i = idx; | |
6820 | 2216 ndir = installed_pkgs_lst{i}.dir; |
7208 | 2217 dirs{end+1} = ndir; |
6695 | 2218 if (exist (fullfile (dirs{end}, "bin"), "dir")) |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7498
diff
changeset
|
2219 execpath = cstrcat (fullfile (dirs{end}, "bin"), ":", execpath); |
6695 | 2220 endif |
7208 | 2221 tmpdir = getarchdir (installed_pkgs_lst{i}); |
6695 | 2222 if (exist (tmpdir, "dir")) |
2223 dirs{end + 1} = tmpdir; | |
6950 | 2224 if (exist (fullfile (dirs{end}, "bin"), "dir")) |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7498
diff
changeset
|
2225 execpath = cstrcat (fullfile (dirs{end}, "bin"), ":", execpath); |
6950 | 2226 endif |
6695 | 2227 endif |
2228 endfor | |
2229 | |
2230 ## Load the packages | |
2231 if (length (dirs) > 0) | |
2232 addpath (dirs{:}); | |
2233 endif | |
2234 | |
2235 ## Add the binaries to exec_path | |
2236 if (! strcmp (EXEC_PATH, execpath)) | |
2237 EXEC_PATH (execpath); | |
2238 endif | |
2239 endfunction | |
2240 | |
6820 | 2241 function idx = load_package_dirs (lidx, idx, handle_deps, installed_pkgs_lst) |
6695 | 2242 for i = lidx |
6820 | 2243 if (isfield (installed_pkgs_lst{i}, "loaded") && |
2244 installed_pkgs_lst{i}.loaded) | |
6695 | 2245 continue; |
2246 else | |
2247 if (handle_deps) | |
6820 | 2248 deps = installed_pkgs_lst{i}.depends; |
6695 | 2249 if ((length (deps) > 1) || (length (deps) == 1 && |
2250 ! strcmp(deps{1}.package, "octave"))) | |
2251 tmplidx = []; | |
2252 for k = 1 : length (deps) | |
6820 | 2253 for j = 1 : length (installed_pkgs_lst) |
2254 if (strcmp (installed_pkgs_lst{j}.name, deps{k}.package)) | |
6695 | 2255 tmplidx (end + 1) = j; |
2256 break; | |
2257 endif | |
2258 endfor | |
2259 endfor | |
2260 idx = load_package_dirs (tmplidx, idx, handle_deps, | |
6820 | 2261 installed_pkgs_lst); |
6695 | 2262 endif |
2263 endif | |
2264 if (isempty (find(idx == i))) | |
2265 idx (end + 1) = i; | |
2266 endif | |
2267 endif | |
2268 endfor | |
2269 endfunction | |
6950 | 2270 |
2271 function dep = is_architecture_dependent (nm) | |
7329 | 2272 persistent archdepsuffix = {".oct",".mex",".a",".lib",".so",".so.*",".dll","dylib"}; |
6950 | 2273 |
2274 dep = false; | |
2275 for i = 1 : length (archdepsuffix) | |
7208 | 2276 ext = archdepsuffix{i}; |
6950 | 2277 if (ext(end) == "*") |
2278 isglob = true; | |
2279 ext(end) = []; | |
2280 else | |
2281 isglob = false; | |
2282 endif | |
2283 pos = findstr (nm, ext); | |
2284 if (pos) | |
7208 | 2285 if (! isglob && (length(nm) - pos(end) != length(ext) - 1)) |
6950 | 2286 continue; |
2287 endif | |
2288 dep = true; | |
2289 break; | |
2290 endif | |
2291 endfor | |
2292 endfunction |