1 ## -*- mode: octave; coding: utf-8 -*-
2 0; # Don't make this a function file
3 function res = tcomp (fn)
5 %% imports components and rearranges them.
11 data = dlmread(fn, 3, 0);
17 cnty = repmat(x(:,1)(:), 10, 1);
20 ## Here and below, we test if the indentation aligns with a previous
21 ## fixindented line. This is important so as to make it easier for the
22 ## user to override some indentation somewhere, and also because it
23 ## reflects the fact that the indentation decision is taken with a minimum
24 ## amount of work (i.e. in the present case, without having to walk back
25 ## until the `function' line).
26 bir = x(:,11:20)(:); # fixindent
32 yrs = repmat(2000:2009, 39, 1)(:);
34 res = [yrs, cnty, pop, bir, dth, imig, dmig, gq];
38 ## Copyright (C) 2005, 2006, 2007, 2008, 2009 Søren Hauberg
40 ## This file is part of Octave.
42 ## Octave is free software; you can redistribute it and/or modify it
43 ## under the terms of the GNU General Public License as published by
44 ## the Free Software Foundation; either version 3 of the License, or (at
45 ## your option) any later version.
47 ## Octave is distributed in the hope that it will be useful, but
48 ## WITHOUT ANY WARRANTY; without even the implied warranty of
49 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
50 ## General Public License for more details.
52 ## You should have received a copy of the GNU General Public License
53 ## along with Octave; see the file COPYING. If not, see
54 ## <http://www.gnu.org/licenses/>.
57 ## @deftypefn {Command} pkg @var{command} @var{pkg_name}
58 ## @deftypefnx {Command} pkg @var{command} @var{option} @var{pkg_name}
59 ## This command interacts with the package manager. Different actions will
60 ## be taken depending on the value of @var{command}.
64 ## Install named packages. For example,
66 ## pkg install image-1.0.0.tar.gz
69 ## installs the package found in the file @file{image-1.0.0.tar.gz}.
71 ## The @var{option} variable can contain options that affect the manner
72 ## in which a package is installed. These options can be one or more of
76 ## The package manager will disable the dependency checking. That way it
77 ## is possible to install a package even if it depends on another package
78 ## that's not installed on the system. @strong{Use this option with care.}
81 ## The package manager will not automatically load the installed package
82 ## when starting Octave, even if the package requests that it is.
85 ## The package manager will automatically load the installed package when
86 ## starting Octave, even if the package requests that it isn't.
89 ## A local installation is forced, even if the user has system privileges.
92 ## A global installation is forced, even if the user doesn't normally have
96 ## The package manager will print the output of all of the commands that are
101 ## Uninstall named packages. For example,
103 ## pkg uninstall image
106 ## removes the @code{image} package from the system. If another installed
107 ## package depends on the @code{image} package an error will be issued.
108 ## The package can be uninstalled anyway by using the @code{-nodeps} option.
110 ## Add named packages to the path. After loading a package it is
111 ## possible to use the functions provided by the package. For example,
116 ## adds the @code{image} package to the path. It is possible to load all
117 ## installed packages at once with the command
122 ## Removes named packages from the path. After unloading a package it is
123 ## no longer possible to use the functions provided by the package.
124 ## This command behaves like the @code{load} command.
126 ## Show a list of the currently installed packages. By requesting one or two
127 ## output argument it is possible to get a list of the currently installed
128 ## packages. For example,
130 ## installed_packages = pkg list;
133 ## returns a cell array containing a structure for each installed package.
136 ## [@var{user_packages}, @var{system_packages}] = pkg list
139 ## splits the list of installed packages into those who are installed by
140 ## the current user, and those installed by the system administrator.
142 ## Show a short description of the named installed packages, with the option
143 ## '-verbose' also list functions provided by the package, e.g.:
145 ## pkg describe -verbose all
148 ## will describe all installed packages and the functions they provide.
149 ## If one output is requested a cell of structure containing the
150 ## description and list of functions of each package is returned as
151 ## output rather than printed on screen:
153 ## desc = pkg ("describe", "secs1d", "image")
156 ## If any of the requested packages is not installed, pkg returns an
157 ## error, unless a second output is requested:
159 ## [ desc, flag] = pkg ("describe", "secs1d", "image")
162 ## @var{flag} will take one of the values "Not installed", "Loaded" or
163 ## "Not loaded" for each of the named packages.
165 ## Set the installation prefix directory. For example,
167 ## pkg prefix ~/my_octave_packages
170 ## sets the installation prefix to @file{~/my_octave_packages}.
171 ## Packages will be installed in this directory.
173 ## It is possible to get the current installation prefix by requesting an
174 ## output argument. For example,
179 ## The location in which to install the architecture dependent files can be
180 ## independent specified with an addition argument. For example
183 ## pkg prefix ~/my_octave_packages ~/my_arch_dep_pkgs
186 ## Set the file in which to look for information on the locally
187 ## installed packages. Locally installed packages are those that are
188 ## typically available only to the current user. For example
190 ## pkg local_list ~/.octave_packages
192 ## It is possible to get the current value of local_list with the following
197 ## Set the file in which to look for, for information on the globally
198 ## installed packages. Globally installed packages are those that are
199 ## typically available to all users. For example
201 ## pkg global_list /usr/share/octave/octave_packages
203 ## It is possible to get the current value of global_list with the following
208 ## Rebuilds the package database from the installed directories. This can
209 ## be used in cases where for some reason the package database is corrupted.
210 ## It can also take the @code{-auto} and @code{-noauto} options to allow the
211 ## autoloading state of a package to be changed. For example
214 ## pkg rebuild -noauto image
217 ## will remove the autoloading status of the image package.
219 ## Builds a binary form of a package or packages. The binary file produced
220 ## will itself be an Octave package that can be installed normally with
221 ## @code{pkg}. The form of the command to build a binary package is
224 ## pkg build builddir image-1.0.0.tar.gz @dots{}
228 ## where @code{builddir} is the name of a directory where the temporary
229 ## installation will be produced and the binary packages will be found.
230 ## The options @code{-verbose} and @code{-nodeps} are respected, while
231 ## the other options are ignored.
235 function [local_packages, global_packages] = pkg (varargin)
236 ## Installation prefix (FIXME: what should these be on windows?)
237 persistent user_prefix = false;
238 persistent prefix = -1;
239 persistent archprefix = -1;
240 persistent local_list = tilde_expand (fullfile ("~", ".octave_packages"));
241 persistent global_list = fullfile (OCTAVE_HOME (), "share", "octave",
245 global_install = issuperuser ();
249 prefix = fullfile (OCTAVE_HOME (), "share", "octave", "packages");
250 archprefix = fullfile (octave_config_info ("libexecdir"),
251 "octave", "packages");
253 prefix = fullfile ("~", "octave");
256 prefix = tilde_expand (prefix);
257 archprefix = tilde_expand (archprefix);
260 available_actions = {"list", "install", "uninstall", "load", ...
261 "unload", "prefix", "local_list", ...
262 "global_list", "rebuild", "build","describe"};
264 if (length (varargin) == 0 || ! iscellstr (varargin))
272 for i = 1:length (varargin)
283 global_install = false;
285 prefix = tilde_expand (fullfile ("~", "octave"));
289 global_install = true;
291 prefix = fullfile (OCTAVE_HOME (), "share", "octave", "packages");
292 archprefix = fullfile (octave_config_info ("libexecdir"),
293 "octave", "packages");
295 case available_actions
296 if (strcmp (action, "none"))
297 action = varargin{i};
299 error ("more than one action specified");
302 files{end+1} = varargin{i};
310 installed_packages (local_list, global_list);
311 elseif (nargout == 1)
312 local_packages = installed_packages (local_list, global_list);
313 elseif (nargout == 2)
314 [local_packages, global_packages] = installed_packages (local_list,
317 error ("too many output arguments requested");
321 if (length (files) == 0)
322 error ("you must specify at least one filename when calling 'pkg install'");
324 install (files, deps, auto, prefix, archprefix, verbose, local_list,
325 global_list, global_install);
328 if (length (files) == 0)
329 error ("you must specify at least one package when calling 'pkg uninstall'");
331 uninstall (files, deps, verbose, local_list,
332 global_list, global_install);
335 if (length (files) == 0)
336 error ("you must specify at least one package, 'all' or 'auto' when calling 'pkg load'");
338 load_packages (files, deps, local_list, global_list);
341 if (length (files) == 0)
342 error ("you must specify at least one package or 'all' when calling 'pkg unload'");
344 unload_packages (files, deps, local_list, global_list);
347 if (length (files) == 0 && nargout == 0)
348 printf ("Installation prefix: %s\n", prefix);
349 printf ("Architecture dependent prefix: %s\n", archprefix);
350 elseif (length (files) == 0 && nargout >= 1)
351 local_packages = prefix;
352 global_packages = archprefix;
353 elseif (length (files) >= 1 && nargout <= 2 && ischar (files{1}))
355 prefix = absolute_pathname (prefix);
356 local_packages = prefix;
358 if (length (files) >= 2 && ischar (files{2}))
359 archprefix = files{2};
361 archprefix = absolute_pathname (archprefix);
364 warning ("creating the directory %s\n", archprefix);
365 archprefix = absolute_pathname (archprefix);
367 global_packages = archprefix;
370 error ("you must specify a prefix directory, or request an output argument");
374 if (length (files) == 0 && nargout == 0)
376 elseif (length (files) == 0 && nargout == 1)
377 local_packages = local_list;
378 elseif (length (files) == 1 && nargout == 0 && ischar (files{1}))
380 local_list = absolute_pathname (files{1});
382 ## Force file to be created
383 fclose (fopen (files{1}, "wt"));
384 local_list = absolute_pathname (files{1});
387 error ("you must specify a local_list file, or request an output argument");
391 if (length (files) == 0 && nargout == 0)
393 elseif (length (files) == 0 && nargout == 1)
394 local_packages = global_list;
395 elseif (length (files) == 1 && nargout == 0 && ischar (files{1}))
397 global_list = absolute_pathname (files{1});
399 ## Force file to be created
400 fclose (fopen (files{1}, "wt"));
401 global_list = absolute_pathname (files{1});
404 error ("you must specify a global_list file, or request an output argument");
409 global_packages = rebuild (prefix, archprefix, global_list, files,
411 global_packages = save_order (global_packages);
412 save (global_list, "global_packages");
414 local_packages = global_packages;
417 local_packages = rebuild (prefix, archprefix, local_list, files, auto,
419 local_packages = save_order (local_packages);
420 save (local_list, "local_packages");
422 clear ("local_packages");
427 if (length (files) < 2)
428 error ("you must specify at least the build directory and one filename\nwhen calling 'pkg build'");
430 build (files, deps, auto, verbose);
433 if (length (files) == 0)
434 error ("you must specify at least one package or 'all' when calling 'pkg describe'");
436 ## FIXME: the name of the output variables is inconsistent
437 ## with their content
440 describe (files, verbose, local_list, global_list);
442 pkg_desc_list = describe (files, verbose, local_list, ...
444 local_packages = pkg_desc_list;
446 [pkg_desc_list, flag] = describe (files, verbose, local_list, ...
448 local_packages = pkg_desc_list;
449 global_packages = flag;
451 error ("you can request at most two outputs when calling 'pkg describe'");
455 error ("you must specify a valid action for 'pkg'. See 'help pkg' for details");
459 function descriptions = rebuild (prefix, archprefix, list, files, auto, verbose)
461 [dirlist, err, msg] = readdir (prefix);
463 error ("couldn't read directory %s: %s", prefix, msg);
465 ## the two first entries of dirlist are "." and ".."
468 old_descriptions = installed_packages (list, list);
472 dirlist = glob (cellfun(@(x) cstrcat(x, '-*'), files, 'UniformOutput', 0));
473 unwind_protect_cleanup
478 for k = 1:length (dirlist)
479 descfile = fullfile (prefix, dirlist{k}, "packinfo", "DESCRIPTION");
481 printf ("recreating package description from %s\n", dirlist{k});
483 if (exist (descfile, "file"))
484 desc = get_description (descfile);
485 desc.dir = fullfile (prefix, dirlist{k});
486 desc.archprefix = fullfile (archprefix, cstrcat (desc.name, "-",
489 if (exist (fullfile (desc.dir, "packinfo", ".autoload"), "file"))
490 unlink (fullfile (desc.dir, "packinfo", ".autoload"));
496 fclose (fopen (fullfile (desc.dir, "packinfo", ".autoload"), "wt"));
499 if (exist (fullfile (desc.dir, "packinfo", ".autoload"), "file"))
505 descriptions{end + 1} = desc;
507 warning ("directory %s is not a valid package", dirlist{k});
511 if (! isempty (files))
512 ## We are rebuilding for a particular package(s) so we should take
513 ## care to keep the other untouched packages in the descriptions
514 descriptions = {descriptions{:}, old_descriptions{:}};
517 for i = 1:length (descriptions)
521 for j = (i+1):length (descriptions)
525 if (strcmp (descriptions{i}.name, descriptions{j}.name))
531 descriptions (dup) = [];
536 function build (files, handle_deps, autoload, verbose)
537 if (length (files) < 1)
538 error ("insufficient number of files");
541 if (! exist (builddir, "dir"))
542 warning ("creating build directory %s", builddir);
543 [status, msg] = mkdir (builddir);
545 error ("could not create installation directory: %s", msg);
548 builddir = absolute_pathname (builddir);
549 installdir = fullfile (builddir, "install");
550 if (! exist (installdir, "dir"))
551 [status, msg] = mkdir (installdir);
553 error ("could not create installation directory: %s", msg);
557 buildlist = fullfile (builddir, "octave_packages");
558 install (files, handle_deps, autoload, installdir, installdir, verbose,
559 buildlist, "", false);
561 repackage (builddir, buildlist);
562 unwind_protect_cleanup
563 unload_packages ({"all"}, handle_deps, buildlist, "");
564 if (exist (installdir, "dir"))
567 if (exist (buildlist, "file"))
573 function install (files, handle_deps, autoload, prefix, archprefix, verbose,
574 local_list, global_list, global_install)
576 ## Check that the directory in prefix exist. If it doesn't: create it!
577 if (! exist (prefix, "dir"))
578 warning ("creating installation directory %s", prefix);
579 [status, msg] = mkdir (prefix);
581 error ("could not create installation directory: %s", msg);
585 ## Get the list of installed packages.
586 [local_packages, global_packages] = installed_packages (local_list,
589 installed_pkgs_lst = {local_packages{:}, global_packages{:}};
592 packages = global_packages;
594 packages = local_packages;
597 ## Uncompress the packages and read the DESCRIPTION files.
598 tmpdirs = packdirs = descriptions = {};
600 ## Warn about non existent files.
601 for i = 1:length (files)
602 if (isempty (glob(files{i})))
603 warning ("file %s does not exist", files{i});
607 ## Unpack the package files and read the DESCRIPTION files.
608 files = glob (files);
609 packages_to_uninstall = [];
610 for i = 1:length (files)
613 if (exist (tgz, "file"))
614 ## Create a temporary directory.
616 tmpdirs{end+1} = tmpdir;
618 printf ("mkdir (%s)\n", tmpdir);
620 [status, msg] = mkdir (tmpdir);
622 error ("couldn't create temporary directory: %s", msg);
625 ## Uncompress the package.
627 printf ("untar (%s, %s)\n", tgz, tmpdir);
631 ## Get the name of the directories produced by tar.
632 [dirlist, err, msg] = readdir (tmpdir);
634 error ("couldn't read directory produced by tar: %s", msg);
637 if (length (dirlist) > 3)
638 error ("bundles of packages are not allowed")
642 ## The filename pointed to an uncompressed package to begin with.
643 if (exist (tgz, "dir"))
644 dirlist = {".", "..", tgz};
647 if (exist (tgz, "file") || exist (tgz, "dir"))
648 ## The two first entries of dirlist are "." and "..".
649 if (exist (tgz, "file"))
650 packdir = fullfile (tmpdir, dirlist{3});
652 packdir = fullfile (pwd(), dirlist{3});
654 packdirs{end+1} = packdir;
656 ## Make sure the package contains necessary files.
657 verify_directory (packdir);
659 ## Read the DESCRIPTION file.
660 filename = fullfile (packdir, "DESCRIPTION");
661 desc = get_description (filename);
663 ## Verify that package name corresponds with filename.
664 [dummy, nm] = fileparts (tgz);
665 if ((length (nm) >= length (desc.name))
666 && ! strcmp (desc.name, nm(1:length(desc.name))))
667 error ("package name '%s' doesn't correspond to its filename '%s'",
671 ## Set default installation directory.
672 desc.dir = fullfile (prefix, cstrcat (desc.name, "-", desc.version));
674 ## Set default architecture dependent installation directory.
675 desc.archprefix = fullfile (archprefix, cstrcat (desc.name, "-",
679 descriptions{end+1} = desc;
681 ## Are any of the new packages already installed?
682 ## If so we'll remove the old version.
683 for j = 1:length (packages)
684 if (strcmp (packages{j}.name, desc.name))
685 packages_to_uninstall(end+1) = j;
691 ## Something went wrong, delete tmpdirs.
692 for i = 1:length (tmpdirs)
695 rethrow (lasterror ());
698 ## Check dependencies.
702 for i = 1:length (descriptions)
703 desc = descriptions{i};
704 idx2 = complement (i, 1:length(descriptions));
706 ## Global installation is not allowed to have dependencies on locally
707 ## installed packages.
708 idx1 = complement (packages_to_uninstall,
709 1:length(global_packages));
710 pseudo_installed_packages = {global_packages{idx1}, ...
713 idx1 = complement (packages_to_uninstall,
714 1:length(local_packages));
715 pseudo_installed_packages = {local_packages{idx1}, ...
716 global_packages{:}, ...
719 bad_deps = get_unsatisfied_deps (desc, pseudo_installed_packages);
720 ## Are there any unsatisfied dependencies?
721 if (! isempty (bad_deps))
723 for i = 1:length (bad_deps)
725 error_text = cstrcat (error_text, " ", desc.name, " needs ",
726 dep.package, " ", dep.operator, " ",
732 ## Did we find any unsatisfied dependencies?
734 error ("the following dependencies where unsatisfied:\n %s", error_text);
738 ## Prepare each package for installation.
740 for i = 1:length (descriptions)
741 desc = descriptions{i};
743 prepare_installation (desc, pdir);
744 configure_make (desc, pdir, verbose);
747 ## Something went wrong, delete tmpdirs.
748 for i = 1:length (tmpdirs)
751 rethrow (lasterror ());
754 ## Uninstall the packages that will be replaced.
756 for i = packages_to_uninstall
758 uninstall ({global_packages{i}.name}, false, verbose, local_list,
759 global_list, global_install);
761 uninstall ({local_packages{i}.name}, false, verbose, local_list,
762 global_list, global_install);
766 ## Something went wrong, delete tmpdirs.
767 for i = 1:length (tmpdirs)
770 rethrow (lasterror ());
773 ## Install each package.
775 for i = 1:length (descriptions)
776 desc = descriptions{i};
778 copy_files (desc, pdir, global_install);
779 create_pkgadddel (desc, pdir, "PKG_ADD", global_install);
780 create_pkgadddel (desc, pdir, "PKG_DEL", global_install);
781 finish_installation (desc, pdir, global_install);
782 generate_lookfor_cache (desc);
785 ## Something went wrong, delete tmpdirs.
786 for i = 1:length (tmpdirs)
789 for i = 1:length (descriptions)
790 rm_rf (descriptions{i}.dir);
791 rm_rf (getarchdir (descriptions{i}));
793 rethrow (lasterror ());
796 ## Check if the installed directory is empty. If it is remove it
798 for i = length (descriptions):-1:1
799 if (dirempty (descriptions{i}.dir, {"packinfo", "doc"}) &&
800 dirempty (getarchdir (descriptions{i})))
801 warning ("package %s is empty\n", descriptions{i}.name);
802 rm_rf (descriptions{i}.dir);
803 rm_rf (getarchdir (descriptions{i}));
804 descriptions(i) = [];
808 ## If the package requested that it is autoloaded, or the installer
809 ## requested that it is, then mark the package as autoloaded.
810 for i = length (descriptions):-1:1
811 if (autoload > 0 || (autoload == 0 && isautoload (descriptions(i))))
812 fclose (fopen (fullfile (descriptions{i}.dir, "packinfo",
813 ".autoload"), "wt"));
814 descriptions{i}.autoload = 1;
818 ## Add the packages to the package list.
821 idx = complement (packages_to_uninstall, 1:length(global_packages));
822 global_packages = save_order ({global_packages{idx}, descriptions{:}});
823 save (global_list, "global_packages");
824 installed_pkgs_lst = {local_packages{:}, global_packages{:}};
826 idx = complement (packages_to_uninstall, 1:length(local_packages));
827 local_packages = save_order ({local_packages{idx}, descriptions{:}});
828 save (local_list, "local_packages");
829 installed_pkgs_lst = {local_packages{:}, global_packages{:}};
832 ## Something went wrong, delete tmpdirs.
833 for i = 1:length (tmpdirs)
836 for i = 1:length (descriptions)
837 rm_rf (descriptions{i}.dir);
840 printf ("error: couldn't append to %s\n", global_list);
842 printf ("error: couldn't append to %s\n", local_list);
844 rethrow (lasterror ());
847 ## All is well, let's clean up.
848 for i = 1:length (tmpdirs)
849 [status, msg] = rm_rf (tmpdirs{i});
851 warning ("couldn't clean up after my self: %s\n", msg);
855 ## Add the newly installed packages to the path, so the user
856 ## can begin using them. Only load them if they are marked autoload.
857 if (length (descriptions) > 0)
859 for i = 1:length (descriptions)
860 if (isautoload (descriptions(i)))
861 nm = descriptions{i}.name;
862 for j = 1:length (installed_pkgs_lst)
863 if (strcmp (nm, installed_pkgs_lst{j}.name))
870 load_packages_and_dependencies (idx, handle_deps, installed_pkgs_lst,
875 function uninstall (pkgnames, handle_deps, verbose, local_list,
876 global_list, global_install)
877 ## Get the list of installed packages.
878 [local_packages, global_packages] = installed_packages(local_list,
881 installed_pkgs_lst = {local_packages{:}, global_packages{:}};
883 installed_pkgs_lst = local_packages;
886 num_packages = length (installed_pkgs_lst);
888 for i = 1:num_packages
889 cur_name = installed_pkgs_lst{i}.name;
890 if (any (strcmp (cur_name, pkgnames)))
891 delete_idx(end+1) = i;
895 ## Are all the packages that should be uninstalled already installed?
896 if (length (delete_idx) != length (pkgnames))
898 ## Try again for a locally installed package.
899 installed_pkgs_lst = local_packages;
901 num_packages = length (installed_pkgs_lst);
903 for i = 1:num_packages
904 cur_name = installed_pkgs_lst{i}.name;
905 if (any (strcmp (cur_name, pkgnames)))
906 delete_idx(end+1) = i;
909 if (length (delete_idx) != length (pkgnames))
910 ## FIXME: We should have a better error message.
911 warning ("some of the packages you want to uninstall are not installed");
914 ## FIXME: We should have a better error message.
915 warning ("some of the packages you want to uninstall are not installed");
919 ## Compute the packages that will remain installed.
920 idx = complement (delete_idx, 1:num_packages);
921 remaining_packages = {installed_pkgs_lst{idx}};
923 ## Check dependencies.
926 for i = 1:length (remaining_packages)
927 desc = remaining_packages{i};
928 bad_deps = get_unsatisfied_deps (desc, remaining_packages);
930 ## Will the uninstallation break any dependencies?
931 if (! isempty (bad_deps))
932 for i = 1:length (bad_deps)
934 error_text = cstrcat (error_text, " ", desc.name, " needs ",
935 dep.package, " ", dep.operator, " ",
941 if (! isempty (error_text))
942 error ("the following dependencies where unsatisfied:\n %s", error_text);
946 ## Delete the directories containing the packages.
948 desc = installed_pkgs_lst{i};
949 ## If an 'on_uninstall.m' exist, call it!
950 if (exist (fullfile (desc.dir, "packinfo", "on_uninstall.m"), "file"))
952 cd (fullfile (desc.dir, "packinfo"));
956 ## Do the actual deletion.
959 if (exist (getarchdir (desc)))
960 rmpath (getarchdir (desc));
963 if (exist (desc.dir, "dir"))
964 [status, msg] = rm_rf (desc.dir);
966 error ("couldn't delete directory %s: %s", desc.dir, msg);
968 [status, msg] = rm_rf (getarchdir (desc));
970 error ("couldn't delete directory %s: %s", getarchdir (desc), msg);
972 if (dirempty (desc.archprefix))
973 rm_rf (desc.archprefix);
976 warning ("directory %s previously lost", desc.dir);
980 ## Write a new ~/.octave_packages.
982 if (length (remaining_packages) == 0)
983 unlink (global_list);
985 global_packages = save_order (remaining_packages);
986 save (global_list, "global_packages");
989 if (length (remaining_packages) == 0)
992 local_packages = save_order (remaining_packages);
993 save (local_list, "local_packages");
999 function [pkg_desc_list, flag] = describe (pkgnames, verbose,
1000 local_list, global_list)
1002 ## Get the list of installed packages.
1003 installed_pkgs_lst = installed_packages(local_list, global_list);
1004 num_packages = length (installed_pkgs_lst);
1007 describe_all = false;
1008 if (any (strcmp ("all", pkgnames)))
1009 describe_all = true;
1010 flag(1:num_packages) = {"Not Loaded"};
1011 num_pkgnames = num_packages;
1013 num_pkgnames = length (pkgnames);
1014 flag(1:num_pkgnames) = {"Not installed"};
1017 for i = 1:num_packages
1018 curr_name = installed_pkgs_lst{i}.name;
1022 name_pos = find(strcmp (curr_name, pkgnames));
1025 if (! isempty (name_pos))
1026 if (installed_pkgs_lst{i}.loaded)
1027 flag{name_pos} = "Loaded";
1029 flag{name_pos} = "Not loaded";
1032 pkg_desc_list{name_pos}.name = installed_pkgs_lst{i}.name;
1033 pkg_desc_list{name_pos}.version = installed_pkgs_lst{i}.version;
1034 pkg_desc_list{name_pos}.description = installed_pkgs_lst{i}.description;
1035 pkg_desc_list{name_pos}.provides = parse_pkg_idx (installed_pkgs_lst{i}.dir);
1040 non_inst = find (strcmp (flag, "Not installed"));
1041 if (! isempty (non_inst))
1043 non_inst_str = sprintf (" %s ", pkgnames{non_inst});
1044 error ("some packages are not installed: %s", non_inst_str);
1046 pkg_desc_list{non_inst} = struct ("name", {}, "description",
1047 {}, "provides", {});
1052 for i = 1:num_pkgnames
1053 print_package_description (pkg_desc_list{i}.name,
1054 pkg_desc_list{i}.version,
1055 pkg_desc_list{i}.provides,
1056 pkg_desc_list{i}.description,
1063 ## AUXILIARY FUNCTIONS
1065 ## Read an INDEX file.
1066 function [pkg_idx_struct] = parse_pkg_idx (packdir)
1068 index_file = fullfile (packdir, "packinfo", "INDEX");
1070 if (! exist (index_file, "file"))
1071 error ("could not find any INDEX file in directory %s, try 'pkg rebuild all' to generate missing INDEX files", packdir);
1075 [fid, msg] = fopen (index_file, "r");
1077 error ("the INDEX file %s could not be read: %s",
1082 pkg_idx_struct{1}.category = "Uncategorized";
1083 pkg_idx_struct{1}.functions = {};
1086 while (isempty (strfind (line, ">>")) && ! feof (fid))
1090 while (! feof (fid) || line != -1)
1091 if (! any (! isspace (line)) || line(1) == "#" || any (line == "="))
1092 ## Comments, blank lines or comments about unimplemented
1093 ## functions: do nothing
1094 ## FIXME: probably comments and pointers to external functions
1095 ## could be treated better when printing to screen?
1096 elseif (! isempty (strfind (line, ">>")))
1097 ## Skip package name and description as they are in DESCRIPTION
1099 elseif (! isspace (line(1)))
1101 if (! isempty (pkg_idx_struct{cat_num}.functions))
1102 pkg_idx_struct{++cat_num}.functions = {};
1104 pkg_idx_struct{cat_num}.category = deblank (line);
1107 while (any (! isspace (line)))
1108 [fun_name, line] = strtok (line);
1109 pkg_idx_struct{cat_num}.functions{end+1} = deblank (fun_name);
1117 function print_package_description (pkg_name, pkg_ver, pkg_idx_struct,
1118 pkg_desc, status, verbose)
1120 printf ("---\nPackage name:\n\t%s\n", pkg_name);
1121 printf ("Version:\n\t%s\n", pkg_ver);
1122 printf ("Short description:\n\t%s\n", pkg_desc);
1123 printf ("Status:\n\t%s\n", status);
1125 printf ("---\nProvides:\n");
1126 for i = 1:length(pkg_idx_struct)
1127 if (! isempty (pkg_idx_struct{i}.functions))
1128 printf ("%s\n", pkg_idx_struct{i}.category);
1129 for j = 1:length(pkg_idx_struct{i}.functions)
1130 printf ("\t%s\n", pkg_idx_struct{i}.functions{j});
1139 function pth = absolute_pathname (pth)
1140 [status, msg, msgid] = fileattrib (pth);
1142 error ("could not find the file or path %s", pth);
1148 function repackage (builddir, buildlist)
1149 packages = installed_packages (buildlist, buildlist);
1152 for i = 1 : length(packages)
1157 mkdir (fullfile (pack.name, "inst"));
1158 copyfile (fullfile (pack.dir, "*"), fullfile (pack.name, "inst"));
1159 movefile (fullfile (pack.name, "inst","packinfo", "*"), pack.name);
1160 if (exist (fullfile (pack.name, "inst","packinfo", ".autoload"), "file"))
1161 unlink (fullfile (pack.name, "inst","packinfo", ".autoload"));
1163 rmdir (fullfile (pack.name, "inst", "packinfo"));
1164 if (exist (fullfile (pack.name, "inst", "doc"), "dir"))
1165 movefile (fullfile (pack.name, "inst", "doc"), pack.name);
1167 if (exist (fullfile (pack.name, "inst", "bin"), "dir"))
1168 movefile (fullfile (pack.name, "inst", "bin"), pack.name);
1170 archdir = fullfile (pack.archprefix, cstrcat (pack.name, "-",
1171 pack.version), getarch ());
1172 if (exist (archdir, "dir"))
1173 if (exist (fullfile (pack.name, "inst", "PKG_ADD"), "file"))
1174 unlink (fullfile (pack.name, "inst", "PKG_ADD"));
1176 if (exist (fullfile (pack.name, "inst", "PKG_DEL"), "file"))
1177 unlink (fullfile (pack.name, "inst", "PKG_DEL"));
1179 if (exist (fullfile (archdir, "PKG_ADD"), "file"))
1180 movefile (fullfile (archdir, "PKG_ADD"),
1181 fullfile (pack.name, "PKG_ADD"));
1183 if (exist (fullfile (archdir, "PKG_DEL"), "file"))
1184 movefile (fullfile (archdir, "PKG_DEL"),
1185 fullfile (pack.name, "PKG_DEL"));
1188 if (exist (fullfile (pack.name, "inst", "PKG_ADD"), "file"))
1189 movefile (fullfile (pack.name, "inst", "PKG_ADD"),
1190 fullfile (pack.name, "PKG_ADD"));
1192 if (exist (fullfile (pack.name, "inst", "PKG_DEL"), "file"))
1193 movefile (fullfile (pack.name, "inst", "PKG_DEL"),
1194 fullfile (pack.name, "PKG_DEL"));
1197 tfile = cstrcat (pack.name, "-", pack.version, ".tar");
1198 tar (tfile, pack.name);
1203 warning ("failed to compress %s", tfile);
1205 unwind_protect_cleanup
1206 if (exist (pack.name, "dir"))
1214 function auto = isautoload (desc)
1216 if (isfield (desc{1}, "autoload"))
1217 a = desc{1}.autoload;
1218 if ((isnumeric (a) && a > 0)
1219 || (ischar (a) && (strcmpi (a, "true")
1220 || strcmpi (a, "on")
1221 || strcmpi (a, "yes")
1222 || strcmpi (a, "1"))))
1228 function prepare_installation (desc, packdir)
1229 ## Is there a pre_install to call?
1230 if (exist (fullfile (packdir, "pre_install.m"), "file"))
1238 rethrow (lasterror ());
1242 ## If the directory "inst" doesn't exist, we create it.
1243 inst_dir = fullfile (packdir, "inst");
1244 if (! exist (inst_dir, "dir"))
1245 [status, msg] = mkdir (inst_dir);
1248 error ("the 'inst' directory did not exist and could not be created: %s",
1254 function configure_make (desc, packdir, verbose)
1255 ## Perform ./configure, make, make install in "src".
1256 if (exist (fullfile (packdir, "src"), "dir"))
1257 src = fullfile (packdir, "src");
1259 if (exist (fullfile (src, "configure"), "file"))
1261 if (isempty (getenv ("CC")))
1262 flags = cstrcat (flags, " CC=\"", octave_config_info ("CC"), "\"");
1264 if (isempty (getenv ("CXX")))
1265 flags = cstrcat (flags, " CXX=\"", octave_config_info ("CXX"), "\"");
1267 if (isempty (getenv ("AR")))
1268 flags = cstrcat (flags, " AR=\"", octave_config_info ("AR"), "\"");
1270 if (isempty (getenv ("RANLIB")))
1271 flags = cstrcat (flags, " RANLIB=\"", octave_config_info ("RANLIB"), "\"");
1273 [status, output] = shell (strcat ("cd '", src, "'; ./configure --prefix=\"",
1274 desc.dir, "\"", flags));
1277 error ("the configure script returned the following error: %s", output);
1279 printf("%s", output);
1285 if (exist (fullfile (src, "Makefile"), "file"))
1286 [status, output] = shell (cstrcat ("export INSTALLDIR=\"", desc.dir,
1287 "\"; make -C '", src, "'"));
1290 error ("'make' returned the following error: %s", output);
1292 printf("%s", output);
1296 ## Copy files to "inst" and "inst/arch" (this is instead of 'make
1298 files = fullfile (src, "FILES");
1299 instdir = fullfile (packdir, "inst");
1300 archdir = fullfile (packdir, "inst", getarch ());
1303 if (exist (files, "file"))
1304 [fid, msg] = fopen (files, "r");
1306 error ("couldn't open %s: %s", files, msg);
1308 filenames = char (fread (fid))';
1310 if (filenames(end) == "\n")
1311 filenames(end) = [];
1313 filenames = split_by (filenames, "\n");
1315 for i = 1:length (filenames)
1316 if (! all (isspace (filenames{i})))
1317 filenames{i} = fullfile (src, filenames{i});
1319 delete_idx(end+1) = i;
1322 filenames(delete_idx) = [];
1324 m = dir (fullfile (src, "*.m"));
1325 oct = dir (fullfile (src, "*.oct"));
1326 mex = dir (fullfile (src, "*.mex"));
1328 filenames = cellfun (@(x) fullfile (src, x),
1329 {m.name, oct.name, mex.name},
1330 "UniformOutput", false);
1333 ## Split into architecture dependent and independent files.
1334 if (isempty (filenames))
1337 idx = cellfun (@is_architecture_dependent, filenames);
1339 archdependent = filenames (idx);
1340 archindependent = filenames (!idx);
1343 if (! all (isspace ([filenames{:}])))
1344 if (! exist (instdir, "dir")) # fixindent
1347 if (! all (isspace ([archindependent{:}])))
1349 printf ("copyfile");
1350 printf (" %s", archindependent{:});
1351 printf ("%s\n", instdir);
1353 [status, output] = copyfile (archindependent, instdir);
1356 error ("Couldn't copy files from 'src' to 'inst': %s", output);
1359 if (! all (isspace ([archdependent{:}])))
1361 printf ("copyfile");
1362 printf (" %s", archdependent{:});
1363 printf (" %s\n", archdir);
1365 if (! exist (archdir, "dir"))
1368 [status, output] = copyfile (archdependent, archdir);
1371 error ("Couldn't copy files from 'src' to 'inst': %s", output);
1378 function pkg = extract_pkg (nm, pat)
1379 fid = fopen (nm, "rt");
1382 while (! feof (fid))
1385 t = regexp (ln, pat, "tokens");
1387 pkg = cstrcat (pkg, "\n", t{1}{1});
1391 if (! isempty (pkg))
1392 pkg = cstrcat (pkg, "\n");
1398 function create_pkgadddel (desc, packdir, nm, global_install)
1399 instpkg = fullfile (desc.dir, nm);
1400 instfid = fopen (instpkg, "wt");
1401 ## If it is exists, most of the PKG_* file should go into the
1402 ## architecture dependent directory so that the autoload/mfilename
1403 ## commands work as expected. The only part that doesn't is the
1404 ## part in the main directory.
1405 archdir = fullfile (getarchprefix (desc), cstrcat (desc.name, "-",
1406 desc.version), getarch ());
1407 if (exist (getarchdir (desc, global_install), "dir"))
1408 archpkg = fullfile (getarchdir (desc, global_install), nm);
1409 archfid = fopen (archpkg, "at");
1415 if (archfid >= 0 && instfid >= 0)
1416 ## Search all dot-m files for PKG commands.
1417 lst = dir (fullfile (packdir, "inst", "*.m"));
1418 for i = 1:length (lst)
1419 nam = fullfile (packdir, "inst", lst(i).name);
1420 fwrite (instfid, extract_pkg (nam, ['^[#%][#%]* *' nm ': *(.*)$']));
1423 ## Search all C++ source files for PKG commands.
1424 lst = dir (fullfile (packdir, "src", "*.cc"));
1425 for i = 1:length (lst)
1426 nam = fullfile (packdir, "src", lst(i).name);
1427 fwrite (archfid, extract_pkg (nam, ['^//* *' nm ': *(.*)$']));
1428 fwrite (archfid, extract_pkg (nam, ['^/\** *' nm ': *(.*) *\*/$']));
1431 ## Add developer included PKG commands.
1432 packdirnm = fullfile (packdir, nm);
1433 if (exist (packdirnm, "file"))
1434 fid = fopen (packdirnm, "rt");
1436 while (! feof (fid))
1439 fwrite (archfid, ln);
1446 ## If the files is empty remove it.
1453 if (instfid != archfid)
1463 function copy_files (desc, packdir, global_install)
1464 ## Create the installation directory.
1465 if (! exist (desc.dir, "dir"))
1466 [status, output] = mkdir (desc.dir);
1468 error ("couldn't create installation directory %s : %s",
1473 octfiledir = getarchdir (desc);
1475 ## Copy the files from "inst" to installdir.
1476 instdir = fullfile (packdir, "inst");
1477 if (! dirempty (instdir))
1478 [status, output] = copyfile (fullfile (instdir, "*"), desc.dir);
1481 error ("couldn't copy files to the installation directory");
1483 if (exist (fullfile (desc.dir, getarch ()), "dir") &&
1484 ! strcmp (fullfile (desc.dir, getarch ()), octfiledir))
1485 if (! exist (octfiledir, "dir"))
1486 ## Can be required to create upto three levels of dirs.
1487 octm1 = fileparts (octfiledir);
1488 if (! exist (octm1, "dir"))
1489 octm2 = fileparts (octm1);
1490 if (! exist (octm2, "dir"))
1491 octm3 = fileparts (octm2);
1492 if (! exist (octm3, "dir"))
1493 [status, output] = mkdir (octm3);
1496 error ("couldn't create installation directory %s : %s",
1500 [status, output] = mkdir (octm2);
1503 error ("couldn't create installation directory %s : %s",
1507 [status, output] = mkdir (octm1);
1510 error ("couldn't create installation directory %s : %s",
1514 [status, output] = mkdir (octfiledir);
1517 error ("couldn't create installation directory %s : %s",
1518 octfiledir, output);
1521 [status, output] = movefile (fullfile (desc.dir, getarch (), "*"),
1523 rm_rf (fullfile (desc.dir, getarch ()));
1528 error ("couldn't copy files to the installation directory");
1534 ## Create the "packinfo" directory.
1535 packinfo = fullfile (desc.dir, "packinfo");
1536 [status, msg] = mkdir (packinfo);
1540 error ("couldn't create packinfo directory: %s", msg);
1543 ## Copy DESCRIPTION.
1544 [status, output] = copyfile (fullfile (packdir, "DESCRIPTION"), packinfo);
1548 error ("couldn't copy DESCRIPTION: %s", output);
1552 [status, output] = copyfile (fullfile (packdir, "COPYING"), packinfo);
1556 error ("couldn't copy COPYING: %s", output);
1559 ## If the file ChangeLog exists, copy it.
1560 changelog_file = fullfile (packdir, "ChangeLog");
1561 if (exist (changelog_file, "file"))
1562 [status, output] = copyfile (changelog_file, packinfo);
1566 error ("couldn't copy ChangeLog file: %s", output);
1570 ## Is there an INDEX file to copy or should we generate one?
1571 index_file = fullfile (packdir, "INDEX");
1572 if (exist(index_file, "file"))
1573 [status, output] = copyfile (index_file, packinfo);
1577 error ("couldn't copy INDEX file: %s", output);
1581 write_index (desc, fullfile (packdir, "inst"),
1582 fullfile (packinfo, "INDEX"), global_install);
1586 rethrow (lasterror ());
1590 ## Is there an 'on_uninstall.m' to install?
1591 fon_uninstall = fullfile (packdir, "on_uninstall.m");
1592 if (exist (fon_uninstall, "file"))
1593 [status, output] = copyfile (fon_uninstall, packinfo);
1597 error ("couldn't copy on_uninstall.m: %s", output);
1601 ## Is there a doc/ directory that needs to be installed?
1602 docdir = fullfile (packdir, "doc");
1603 if (exist (docdir, "dir") && ! dirempty (docdir))
1604 [status, output] = copyfile (docdir, desc.dir);
1607 ## Is there a bin/ directory that needs to be installed?
1608 ## FIXME: Need to treat architecture dependent files in bin/
1609 bindir = fullfile (packdir, "bin");
1610 if (exist (bindir, "dir") && ! dirempty (bindir))
1611 [status, output] = copyfile (bindir, desc.dir);
1615 function finish_installation (desc, packdir, global_install)
1616 ## Is there a post-install to call?
1617 if (exist (fullfile (packdir, "post_install.m"), "file"))
1621 post_install (desc);
1626 rm_rf (getarchdir (desc), global_install);
1627 rethrow (lasterror ());
1632 function generate_lookfor_cache (desc)
1633 dirs = split_by (genpath (desc.dir), pathsep ());
1634 for i = 1 : length (dirs)
1635 gen_doc_cache (fullfile (dirs{i}, "doc-cache"), dirs{i});
1639 ## Make sure the package contains the essential files.
1640 function verify_directory (dir)
1641 needed_files = {"COPYING", "DESCRIPTION"};
1642 for f = needed_files
1643 if (! exist (fullfile (dir, f{1}), "file"))
1644 error ("package is missing file: %s", f{1});
1649 ## Parse the DESCRIPTION file.
1650 function desc = get_description (filename)
1651 [fid, msg] = fopen (filename, "r");
1653 error ("the DESCRIPTION file %s could not be read: %s", filename, msg);
1661 ## Comments, do nothing.
1662 elseif (isspace(line(1)))
1663 ## Continuation lines
1664 if (exist ("keyword", "var") && isfield (desc, keyword))
1665 desc.(keyword) = cstrcat (desc.(keyword), " ", rstrip(line));
1668 ## Keyword/value pair
1669 colon = find (line == ":");
1670 if (length (colon) == 0)
1671 disp ("skipping line");
1674 keyword = tolower (strip (line(1:colon-1)));
1675 value = strip (line (colon+1:end));
1676 if (length (value) == 0)
1678 error ("the keyword %s has an empty value", desc.keywords{end});
1680 desc.(keyword) = value;
1687 ## Make sure all is okay.
1688 needed_fields = {"name", "version", "date", "title", ...
1689 "author", "maintainer", "description"};
1690 for f = needed_fields
1691 if (! isfield (desc, f{1}))
1692 error ("description is missing needed field %s", f{1});
1695 desc.version = fix_version (desc.version);
1696 if (isfield (desc, "depends"))
1697 desc.depends = fix_depends (desc.depends);
1701 desc.name = tolower (desc.name);
1704 ## Make sure the version string v is a valid x.y.z version string
1705 ## Examples: "0.1" => "0.1.0", "monkey" => error(...).
1706 function out = fix_version (v)
1707 dots = find (v == ".");
1708 if (length (dots) == 1)
1709 major = str2num (v(1:dots-1));
1710 minor = str2num (v(dots+1:end));
1711 if (length (major) != 0 && length (minor) != 0)
1712 out = sprintf ("%d.%d.0", major, minor);
1715 elseif (length (dots) == 2)
1716 major = str2num (v(1:dots(1)-1));
1717 minor = str2num (v(dots(1)+1:dots(2)-1));
1718 rev = str2num (v(dots(2)+1:end));
1719 if (length (major) != 0 && length (minor) != 0 && length (rev) != 0)
1720 out = sprintf ("%d.%d.%d", major, minor, rev);
1724 error ("bad version string: %s", v);
1727 ## Make sure the depends field is of the right format.
1728 ## This function returns a cell of structures with the following fields:
1729 ## package, version, operator
1730 function deps_cell = fix_depends (depends)
1731 deps = split_by (tolower (depends), ",");
1732 deps_cell = cell (1, length (deps));
1734 ## For each dependency.
1735 for i = 1:length (deps)
1737 lpar = find (dep == "(");
1738 rpar = find (dep == ")");
1739 ## Does the dependency specify a version
1740 ## Example: package(>= version).
1741 if (length (lpar) == 1 && length (rpar) == 1)
1742 package = tolower (strip (dep(1:lpar-1)));
1743 sub = dep(lpar(1)+1:rpar(1)-1);
1744 parts = strsplit (sub, " ", true);
1745 if (length (parts) != 2)
1746 error ("incorrect syntax for dependency `%s' in the DESCRIPTION file\n",
1749 operator = parts{1};
1750 if (! any (strcmp (operator, {">", ">=", "<=", "<", "=="})))
1751 error ("unsupported operator: %s", operator);
1753 version = fix_version (parts{2});
1755 ## If no version is specified for the dependency
1756 ## we say that the version should be greater than
1757 ## or equal to "0.0.0".
1759 package = tolower (strip (dep));
1763 deps_cell{i} = struct ("package", package, "operator", operator,
1764 "version", version);
1768 ## Strip the text of spaces from the right
1769 ## Example: " hello world " => " hello world"
1770 ## FIXME -- is this the same as deblank?
1771 function text = rstrip (text)
1772 chars = find (! isspace (text));
1773 if (length (chars) > 0)
1774 ## FIXME: shouldn't it be text = text(1:chars(end));
1775 text = text (chars(1):end);
1781 ## Strip the text of spaces from the left and the right.
1782 ## Example: " hello world " => "hello world"
1783 function text = strip (text)
1784 chars = find (! isspace (text));
1785 if (length (chars) > 0)
1786 text = text(chars(1):chars(end));
1792 ## Split the text into a cell array of strings by sep.
1793 ## Example: "A, B" => {"A", "B"} (with sep = ",")
1794 function out = split_by (text, sep)
1795 out = strtrim (strsplit (text, sep));
1798 ## Create an INDEX file for a package that doesn't provide one.
1799 ## 'desc' describes the package.
1800 ## 'dir' is the 'inst' directory in temporary directory.
1801 ## 'index_file' is the name (including path) of resulting INDEX file.
1802 function write_index (desc, dir, index_file, global_install)
1803 ## Get names of functions in dir
1804 [files, err, msg] = readdir (dir);
1806 error ("couldn't read directory %s: %s", dir, msg);
1809 ## Check for architecture dependent files.
1810 tmpdir = getarchdir (desc);
1811 if (exist (tmpdir, "dir"))
1812 [files2, err, msg] = readdir (tmpdir);
1814 error ("couldn't read directory %s: %s", tmpdir, msg);
1816 files = [files; files2];
1820 for i = 1:length (files)
1823 if (lf > 2 && strcmp (file(end-1:end), ".m"))
1824 functions{end+1} = file(1:end-2);
1825 elseif (lf > 4 && strcmp (file(end-3:end), ".oct"))
1826 functions{end+1} = file(1:end-4);
1830 ## Does desc have a categories field?
1831 if (! isfield (desc, "categories"))
1832 error ("the DESCRIPTION file must have a Categories field, when no INDEX file is given");
1834 categories = split_by (desc.categories, ",");
1835 if (length (categories) < 1)
1836 error ("the Category field is empty");
1840 fid = fopen (index_file, "w");
1842 error ("couldn't open %s for writing.", index_file);
1844 fprintf (fid, "%s >> %s\n", desc.name, desc.title);
1845 fprintf (fid, "%s\n", categories{1});
1846 fprintf (fid, " %s\n", functions{:});
1850 function bad_deps = get_unsatisfied_deps (desc, installed_pkgs_lst)
1853 ## For each dependency.
1854 for i = 1:length (desc.depends)
1855 dep = desc.depends{i};
1857 ## Is the current dependency Octave?
1858 if (strcmp (dep.package, "octave"))
1859 if (! compare_versions (OCTAVE_VERSION, dep.version, dep.operator))
1860 bad_deps{end+1} = dep;
1862 ## Is the current dependency not Octave?
1865 for i = 1:length (installed_pkgs_lst)
1866 cur_name = installed_pkgs_lst{i}.name;
1867 cur_version = installed_pkgs_lst{i}.version;
1868 if (strcmp (dep.package, cur_name)
1869 && compare_versions (cur_version, dep.version, dep.operator))
1875 bad_deps{end+1} = dep;
1881 function [out1, out2] = installed_packages (local_list, global_list)
1882 ## Get the list of installed packages.
1884 local_packages = load (local_list).local_packages;
1886 local_packages = {};
1889 global_packages = load (global_list).global_packages;
1891 global_packages = {};
1893 installed_pkgs_lst = {local_packages{:}, global_packages{:}};
1895 ## Eliminate duplicates in the installed package list.
1896 ## Locally installed packages take precedence.
1898 for i = 1:length (installed_pkgs_lst)
1902 for j = (i+1):length (installed_pkgs_lst)
1906 if (strcmp (installed_pkgs_lst{i}.name, installed_pkgs_lst{j}.name))
1912 installed_pkgs_lst(dup) = [];
1915 ## Now check if the package is loaded.
1916 tmppath = strrep (path(), "\\", "/");
1917 for i = 1:length (installed_pkgs_lst)
1918 if (findstr (tmppath, strrep (installed_pkgs_lst{i}.dir, "\\", "/")))
1919 installed_pkgs_lst{i}.loaded = true;
1921 installed_pkgs_lst{i}.loaded = false;
1924 for i = 1:length (local_packages)
1925 if (findstr (tmppath, strrep (local_packages{i}.dir, "\\", "/")))
1926 local_packages{i}.loaded = true;
1928 local_packages{i}.loaded = false;
1931 for i = 1:length (global_packages)
1932 if (findstr (tmppath, strrep (global_packages{i}.dir, "\\", "/")))
1933 global_packages{i}.loaded = true;
1935 global_packages{i}.loaded = false;
1939 ## Should we return something?
1941 out1 = local_packages;
1942 out2 = global_packages;
1944 elseif (nargout == 1)
1945 out1 = installed_pkgs_lst;
1949 ## We shouldn't return something, so we'll print something.
1950 num_packages = length (installed_pkgs_lst);
1951 if (num_packages == 0)
1952 printf ("no packages installed.\n");
1956 ## Compute the maximal lengths of name, version, and dir.
1957 h1 = "Package Name";
1959 h3 = "Installation directory";
1960 max_name_length = length (h1);
1961 max_version_length = length (h2);
1962 names = cell (num_packages, 1);
1963 for i = 1:num_packages
1964 max_name_length = max (max_name_length,
1965 length (installed_pkgs_lst{i}.name));
1966 max_version_length = max (max_version_length,
1967 length (installed_pkgs_lst{i}.version));
1968 names{i} = installed_pkgs_lst{i}.name;
1970 max_dir_length = terminal_size()(2) - max_name_length - ...
1971 max_version_length - 7;
1972 if (max_dir_length < 20)
1973 max_dir_length = Inf;
1976 h1 = postpad (h1, max_name_length + 1, " ");
1977 h2 = postpad (h2, max_version_length, " ");;
1980 header = sprintf("%s | %s | %s\n", h1, h2, h3);
1982 tmp = sprintf (repmat ("-", 1, length(header)-1));
1983 tmp(length(h1)+2) = "+";
1984 tmp(length(h1)+length(h2)+5) = "+";
1985 printf ("%s\n", tmp);
1987 ## Print the packages.
1988 format = sprintf ("%%%ds %%1s| %%%ds | %%s\n", max_name_length,
1989 max_version_length);
1990 [dummy, idx] = sort (names);
1991 for i = 1:num_packages
1992 cur_name = installed_pkgs_lst{idx(i)}.name;
1993 cur_version = installed_pkgs_lst{idx(i)}.version;
1994 cur_dir = installed_pkgs_lst{idx(i)}.dir;
1995 if (length (cur_dir) > max_dir_length)
1996 first_char = length (cur_dir) - max_dir_length + 4;
1997 first_filesep = strfind (cur_dir(first_char:end), filesep());
1998 if (! isempty (first_filesep))
1999 cur_dir = cstrcat ("...",
2000 cur_dir((first_char + first_filesep(1) - 1):end));
2002 cur_dir = cstrcat ("...", cur_dir(first_char:end));
2005 if (installed_pkgs_lst{idx(i)}.loaded)
2010 printf (format, cur_name, cur_loaded, cur_version, cur_dir);
2014 function load_packages (files, handle_deps, local_list, global_list)
2015 installed_pkgs_lst = installed_packages (local_list, global_list);
2016 num_packages = length (installed_pkgs_lst);
2018 ## Read package names and installdirs into a more convenient format.
2019 pnames = pdirs = cell (1, num_packages);
2020 for i = 1:num_packages
2021 pnames{i} = installed_pkgs_lst{i}.name;
2022 pdirs{i} = installed_pkgs_lst{i}.dir;
2026 if (length (files) == 1 && strcmp (files{1}, "all"))
2027 idx = [1:length(installed_pkgs_lst)];
2029 elseif (length (files) == 1 && strcmp (files{1}, "auto"))
2031 for i = 1:length (installed_pkgs_lst)
2032 if (exist (fullfile (pdirs{i}, "packinfo", ".autoload"), "file"))
2036 ## Load package_name1 ...
2039 for i = 1:length (files)
2040 idx2 = find (strcmp (pnames, files{i}));
2042 error ("package %s is not installed", files{i});
2044 idx (end + 1) = idx2;
2048 ## Load the packages, but take care of the ordering of dependencies.
2049 load_packages_and_dependencies (idx, handle_deps, installed_pkgs_lst, true);
2052 function unload_packages (files, handle_deps, local_list, global_list)
2053 installed_pkgs_lst = installed_packages (local_list, global_list);
2054 num_packages = length (installed_pkgs_lst);
2056 ## Read package names and installdirs into a more convenient format.
2057 pnames = pdirs = cell (1, num_packages);
2058 for i = 1:num_packages
2059 pnames{i} = installed_pkgs_lst{i}.name;
2060 pdirs{i} = installed_pkgs_lst{i}.dir;
2061 pdeps{i} = installed_pkgs_lst{i}.depends;
2064 ## Get the current octave path.
2065 p = split_by (path(), pathsep ());
2067 if (length (files) == 1 && strcmp (files{1}, "all"))
2070 desc = installed_pkgs_lst;
2072 ## Unload package_name1 ...
2075 for i = 1:length (files)
2076 idx = strcmp (pnames, files{i});
2078 error ("package %s is not installed", files{i});
2080 dirs{end+1} = pdirs{idx};
2081 desc{end+1} = installed_pkgs_lst{idx};
2085 ## Check for architecture dependent directories.
2087 for i = 1:length (dirs)
2088 tmpdir = getarchdir (desc{i});
2089 if (exist (tmpdir, "dir"))
2090 archdirs{end+1} = dirs{i};
2091 archdirs{end+1} = tmpdir;
2093 archdirs{end+1} = dirs{i};
2097 ## Unload the packages.
2098 for i = 1:length (archdirs)
2100 idx = strcmp (p, d);
2103 ## FIXME: We should also check if we need to remove items from
2109 function [status_out, msg_out] = rm_rf (dir)
2111 crr = confirm_recursive_rmdir ();
2113 confirm_recursive_rmdir (false);
2114 [status, msg] = rmdir (dir, "s");
2115 unwind_protect_cleanup
2116 confirm_recursive_rmdir (crr);
2123 status_out = status;
2130 function emp = dirempty (nm, ign)
2131 if (exist (nm, "dir"))
2135 ign = [{".", ".."}, ign];
2138 for i = 1:length (l)
2140 for j = 1:length (ign)
2141 if (strcmp (l(i).name, ign{j}))
2157 function arch = getarch ()
2158 persistent _arch = cstrcat (octave_config_info("canonical_host_type"), ...
2159 "-", octave_config_info("api_version"));
2163 function archprefix = getarchprefix (desc, global_install)
2164 if ((nargin == 2 && global_install) || (nargin < 2 && issuperuser ()))
2165 archprefix = fullfile (octave_config_info ("libexecdir"), "octave",
2166 "packages", cstrcat(desc.name, "-", desc.version));
2168 archprefix = desc.dir;
2172 function archdir = getarchdir (desc)
2173 archdir = fullfile (desc.archprefix, getarch());
2176 function s = issuperuser ()
2177 if ((ispc () && ! isunix ()) || (geteuid() == 0))
2184 function [status, output] = shell (cmd)
2187 cmd = strrep (cmd, "\\", "/");
2188 if (ispc () && ! isunix ())
2189 if (isempty(have_sh))
2190 if (system("sh.exe -c \"exit\""))
2197 [status, output] = system (cstrcat ("sh.exe -c \"", cmd, "\""));
2199 error ("Can not find the command shell")
2202 [status, output] = system (cmd);
2206 function newdesc = save_order (desc)
2208 for i = 1 : length(desc)
2209 deps = desc{i}.depends;
2210 if (isempty (deps) || (length (deps) == 1 &&
2211 strcmp(deps{1}.package, "octave")))
2212 newdesc {end + 1} = desc{i};
2215 for k = 1 : length (deps)
2216 for j = 1 : length (desc)
2217 if (strcmp (desc{j}.name, deps{k}.package))
2218 tmpdesc{end+1} = desc{j};
2223 if (! isempty (tmpdesc))
2224 newdesc = {newdesc{:}, save_order(tmpdesc){:}, desc{i}};
2226 newdesc{end+1} = desc{i};
2230 ## Eliminate the duplicates.
2232 for i = 1 : length (newdesc)
2233 for j = (i + 1) : length (newdesc)
2234 if (strcmp (newdesc{i}.name, newdesc{j}.name))
2242 function load_packages_and_dependencies (idx, handle_deps, installed_pkgs_lst,
2244 idx = load_package_dirs (idx, [], handle_deps, installed_pkgs_lst);
2246 execpath = EXEC_PATH ();
2248 ndir = installed_pkgs_lst{i}.dir;
2250 if (exist (fullfile (dirs{end}, "bin"), "dir"))
2251 execpath = cstrcat (fullfile (dirs{end}, "bin"), ":", execpath);
2253 tmpdir = getarchdir (installed_pkgs_lst{i});
2254 if (exist (tmpdir, "dir"))
2255 dirs{end + 1} = tmpdir;
2256 if (exist (fullfile (dirs{end}, "bin"), "dir"))
2257 execpath = cstrcat (fullfile (dirs{end}, "bin"), ":", execpath);
2262 ## Load the packages.
2263 if (length (dirs) > 0)
2267 ## Add the binaries to exec_path.
2268 if (! strcmp (EXEC_PATH, execpath))
2269 EXEC_PATH (execpath);
2273 function idx = load_package_dirs (lidx, idx, handle_deps, installed_pkgs_lst)
2275 if (isfield (installed_pkgs_lst{i}, "loaded") &&
2276 installed_pkgs_lst{i}.loaded)
2280 deps = installed_pkgs_lst{i}.depends;
2281 if ((length (deps) > 1) || (length (deps) == 1 &&
2282 ! strcmp(deps{1}.package, "octave")))
2284 for k = 1 : length (deps)
2285 for j = 1 : length (installed_pkgs_lst)
2286 if (strcmp (installed_pkgs_lst{j}.name, deps{k}.package))
2287 tmplidx (end + 1) = j;
2292 idx = load_package_dirs (tmplidx, idx, handle_deps,
2293 installed_pkgs_lst);
2296 if (isempty (find(idx == i)))
2303 function dep = is_architecture_dependent (nm)
2304 persistent archdepsuffix = {".oct",".mex",".a",".lib",".so",".so.*",".dll","dylib"};
2307 for i = 1 : length (archdepsuffix)
2308 ext = archdepsuffix{i};
2309 if (ext(end) == "*")
2313 isglob = false; # I am a test
2315 ### I shall align to column 0
2317 pos = findstr (nm, ext);
2319 if (! isglob && (length(nm) - pos(end) != length(ext) - 1))
2328 %!assert(norm(logm([1 -1;0 1]) - [0 -1; 0 0]) < 1e-5);
2329 %!assert(norm(expm(logm([-1 2 ; 4 -1])) - [-1 2 ; 4 -1]) < 1e-5);
2330 %!assert(logm([1 -1 -1;0 1 -1; 0 0 1]), [0 -1 -1.5; 0 0 -1; 0 0 0], 1e-5);
2331 %!assert (logm (expm ([0 1i; -1i 0])), [0 1i; -1i 0], 10 * eps)
2333 %% Test input validation
2335 %!error logm (1, 2, 3);
2336 %!error <logm: A must be a square matrix> logm([1 0;0 1; 2 2]);
2338 %!assert (logm (10), log (10))
2339 %!assert (full (logm (eye (3))), logm (full (eye (3))))
2340 %!assert (full (logm (10*eye (3))), logm (full (10*eye (3))), 8*eps)