1 ## -*- mode: octave; coding: utf-8 -*-
2 0; # Don't make this a function file
3 function res = tcomp (fn)
18 %% imports components and rearranges them.
24 data = dlmread(fn, 3, 0);
31 y = enumeration (x); #Beware: "enumeration" can also be a function!
32 y = foo(enumeration (x),
33 2); #Beware: "enumeration" can also be a function!
45 cnty = repmat(x(:,1)(:), 10, 1);
50 ## Here and below, we test if the indentation aligns with a previous
51 ## fixindented line. This is important so as to make it easier for the
52 ## user to override some indentation somewhere, and also because it
53 ## reflects the fact that the indentation decision is taken with a minimum
54 ## amount of work (i.e. in the present case, without having to walk back
55 ## until the `function' line).
56 bir = x(:,11:20)(:); # fixindent
62 yrs = repmat(2000:2009, 39, 1)(:);
64 res = [yrs, cnty, pop, bir, dth, imig, dmig, gq];
68 ## Copyright (C) 2005, 2006, 2007, 2008, 2009 Søren Hauberg
70 ## This file is part of Octave.
72 ## Octave is free software; you can redistribute it and/or modify it
73 ## under the terms of the GNU General Public License as published by
74 ## the Free Software Foundation; either version 3 of the License, or (at
75 ## your option) any later version.
77 ## Octave is distributed in the hope that it will be useful, but
78 ## WITHOUT ANY WARRANTY; without even the implied warranty of
79 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
80 ## General Public License for more details.
82 ## You should have received a copy of the GNU General Public License
83 ## along with Octave; see the file COPYING. If not, see
84 ## <http://www.gnu.org/licenses/>.
87 ## @deftypefn {Command} pkg @var{command} @var{pkg_name}
88 ## @deftypefnx {Command} pkg @var{command} @var{option} @var{pkg_name}
89 ## This command interacts with the package manager. Different actions will
90 ## be taken depending on the value of @var{command}.
94 ## Install named packages. For example,
96 ## pkg install image-1.0.0.tar.gz
99 ## installs the package found in the file @file{image-1.0.0.tar.gz}.
101 ## The @var{option} variable can contain options that affect the manner
102 ## in which a package is installed. These options can be one or more of
106 ## The package manager will disable the dependency checking. That way it
107 ## is possible to install a package even if it depends on another package
108 ## that's not installed on the system. @strong{Use this option with care.}
111 ## The package manager will not automatically load the installed package
112 ## when starting Octave, even if the package requests that it is.
115 ## The package manager will automatically load the installed package when
116 ## starting Octave, even if the package requests that it isn't.
119 ## A local installation is forced, even if the user has system privileges.
122 ## A global installation is forced, even if the user doesn't normally have
126 ## The package manager will print the output of all of the commands that are
131 ## Uninstall named packages. For example,
133 ## pkg uninstall image
136 ## removes the @code{image} package from the system. If another installed
137 ## package depends on the @code{image} package an error will be issued.
138 ## The package can be uninstalled anyway by using the @code{-nodeps} option.
140 ## Add named packages to the path. After loading a package it is
141 ## possible to use the functions provided by the package. For example,
146 ## adds the @code{image} package to the path. It is possible to load all
147 ## installed packages at once with the command
152 ## Removes named packages from the path. After unloading a package it is
153 ## no longer possible to use the functions provided by the package.
154 ## This command behaves like the @code{load} command.
156 ## Show a list of the currently installed packages. By requesting one or two
157 ## output argument it is possible to get a list of the currently installed
158 ## packages. For example,
160 ## installed_packages = pkg list;
163 ## returns a cell array containing a structure for each installed package.
166 ## [@var{user_packages}, @var{system_packages}] = pkg list
169 ## splits the list of installed packages into those who are installed by
170 ## the current user, and those installed by the system administrator.
172 ## Show a short description of the named installed packages, with the option
173 ## '-verbose' also list functions provided by the package, e.g.:
175 ## pkg describe -verbose all
178 ## will describe all installed packages and the functions they provide.
179 ## If one output is requested a cell of structure containing the
180 ## description and list of functions of each package is returned as
181 ## output rather than printed on screen:
183 ## desc = pkg ("describe", "secs1d", "image")
186 ## If any of the requested packages is not installed, pkg returns an
187 ## error, unless a second output is requested:
189 ## [ desc, flag] = pkg ("describe", "secs1d", "image")
192 ## @var{flag} will take one of the values "Not installed", "Loaded" or
193 ## "Not loaded" for each of the named packages.
195 ## Set the installation prefix directory. For example,
197 ## pkg prefix ~/my_octave_packages
200 ## sets the installation prefix to @file{~/my_octave_packages}.
201 ## Packages will be installed in this directory.
203 ## It is possible to get the current installation prefix by requesting an
204 ## output argument. For example,
209 ## The location in which to install the architecture dependent files can be
210 ## independent specified with an addition argument. For example
213 ## pkg prefix ~/my_octave_packages ~/my_arch_dep_pkgs
216 ## Set the file in which to look for information on the locally
217 ## installed packages. Locally installed packages are those that are
218 ## typically available only to the current user. For example
220 ## pkg local_list ~/.octave_packages
222 ## It is possible to get the current value of local_list with the following
227 ## Set the file in which to look for, for information on the globally
228 ## installed packages. Globally installed packages are those that are
229 ## typically available to all users. For example
231 ## pkg global_list /usr/share/octave/octave_packages
233 ## It is possible to get the current value of global_list with the following
238 ## Rebuilds the package database from the installed directories. This can
239 ## be used in cases where for some reason the package database is corrupted.
240 ## It can also take the @code{-auto} and @code{-noauto} options to allow the
241 ## autoloading state of a package to be changed. For example
244 ## pkg rebuild -noauto image
247 ## will remove the autoloading status of the image package.
249 ## Builds a binary form of a package or packages. The binary file produced
250 ## will itself be an Octave package that can be installed normally with
251 ## @code{pkg}. The form of the command to build a binary package is
254 ## pkg build builddir image-1.0.0.tar.gz @dots{}
258 ## where @code{builddir} is the name of a directory where the temporary
259 ## installation will be produced and the binary packages will be found.
260 ## The options @code{-verbose} and @code{-nodeps} are respected, while
261 ## the other options are ignored.
265 function [local_packages, global_packages] = pkg (varargin)
266 ## Installation prefix (FIXME: what should these be on windows?)
267 persistent user_prefix = false;
268 persistent prefix = -1;
269 persistent archprefix = -1;
270 persistent local_list = tilde_expand (fullfile ("~", ".octave_packages"));
271 persistent global_list = fullfile (OCTAVE_HOME (), "share", "octave",
275 global_install = issuperuser ();
279 prefix = fullfile (OCTAVE_HOME (), "share", "octave", "packages");
280 archprefix = fullfile (octave_config_info ("libexecdir"),
281 "octave", "packages");
283 prefix = fullfile ("~", "octave");
286 prefix = tilde_expand (prefix);
287 archprefix = tilde_expand (archprefix);
290 available_actions = {"list", "install", "uninstall", "load", ...
291 "unload", "prefix", "local_list", ...
292 "global_list", "rebuild", "build","describe"};
294 if (length (varargin) == 0 || ! iscellstr (varargin))
302 for i = 1:length (varargin)
313 global_install = false;
315 prefix = tilde_expand (fullfile ("~", "octave"));
319 global_install = true;
321 prefix = fullfile (OCTAVE_HOME (), "share", "octave", "packages");
322 archprefix = fullfile (octave_config_info ("libexecdir"),
323 "octave", "packages");
325 case available_actions
326 if (strcmp (action, "none"))
327 action = varargin{i};
329 error ("more than one action specified");
332 files{end+1} = varargin{i};
340 installed_packages (local_list, global_list);
341 elseif (nargout == 1)
342 local_packages = installed_packages (local_list, global_list);
343 elseif (nargout == 2)
344 [local_packages, global_packages] = installed_packages (local_list,
347 error ("too many output arguments requested");
351 if (length (files) == 0)
352 error ("you must specify at least one filename when calling 'pkg install'");
354 install (files, deps, auto, prefix, archprefix, verbose, local_list,
355 global_list, global_install);
358 if (length (files) == 0)
359 error ("you must specify at least one package when calling 'pkg uninstall'");
361 uninstall (files, deps, verbose, local_list,
362 global_list, global_install);
365 if (length (files) == 0)
366 error ("you must specify at least one package, 'all' or 'auto' when calling 'pkg load'");
368 load_packages (files, deps, local_list, global_list);
371 if (length (files) == 0)
372 error ("you must specify at least one package or 'all' when calling 'pkg unload'");
374 unload_packages (files, deps, local_list, global_list);
377 if (length (files) == 0 && nargout == 0)
378 printf ("Installation prefix: %s\n", prefix);
379 printf ("Architecture dependent prefix: %s\n", archprefix);
380 elseif (length (files) == 0 && nargout >= 1)
381 local_packages = prefix;
382 global_packages = archprefix;
383 elseif (length (files) >= 1 && nargout <= 2 && ischar (files{1}))
385 prefix = absolute_pathname (prefix);
386 local_packages = prefix;
388 if (length (files) >= 2 && ischar (files{2}))
389 archprefix = files{2};
391 archprefix = absolute_pathname (archprefix);
394 warning ("creating the directory %s\n", archprefix);
395 archprefix = absolute_pathname (archprefix);
397 global_packages = archprefix;
400 error ("you must specify a prefix directory, or request an output argument");
404 if (length (files) == 0 && nargout == 0)
406 elseif (length (files) == 0 && nargout == 1)
407 local_packages = local_list;
408 elseif (length (files) == 1 && nargout == 0 && ischar (files{1}))
410 local_list = absolute_pathname (files{1});
412 ## Force file to be created
413 fclose (fopen (files{1}, "wt"));
414 local_list = absolute_pathname (files{1});
417 error ("you must specify a local_list file, or request an output argument");
421 if (length (files) == 0 && nargout == 0)
423 elseif (length (files) == 0 && nargout == 1)
424 local_packages = global_list;
425 elseif (length (files) == 1 && nargout == 0 && ischar (files{1}))
427 global_list = absolute_pathname (files{1});
429 ## Force file to be created
430 fclose (fopen (files{1}, "wt"));
431 global_list = absolute_pathname (files{1});
434 error ("you must specify a global_list file, or request an output argument");
439 global_packages = rebuild (prefix, archprefix, global_list, files,
441 global_packages = save_order (global_packages);
442 save (global_list, "global_packages");
444 local_packages = global_packages;
447 local_packages = rebuild (prefix, archprefix, local_list, files, auto,
449 local_packages = save_order (local_packages);
450 save (local_list, "local_packages");
452 clear ("local_packages");
457 if (length (files) < 2)
458 error ("you must specify at least the build directory and one filename\nwhen calling 'pkg build'");
460 build (files, deps, auto, verbose);
463 if (length (files) == 0)
464 error ("you must specify at least one package or 'all' when calling 'pkg describe'");
466 ## FIXME: the name of the output variables is inconsistent
467 ## with their content
470 describe (files, verbose, local_list, global_list);
472 pkg_desc_list = describe (files, verbose, local_list, ...
474 local_packages = pkg_desc_list;
476 [pkg_desc_list, flag] = describe (files, verbose, local_list, ...
478 local_packages = pkg_desc_list;
479 global_packages = flag;
481 error ("you can request at most two outputs when calling 'pkg describe'");
485 error ("you must specify a valid action for 'pkg'. See 'help pkg' for details");
489 function descriptions = rebuild (prefix, archprefix, list, files, auto, verbose)
491 [dirlist, err, msg] = readdir (prefix);
493 error ("couldn't read directory %s: %s", prefix, msg);
495 ## the two first entries of dirlist are "." and ".."
498 old_descriptions = installed_packages (list, list);
502 dirlist = glob (cellfun(@(x) cstrcat(x, '-*'), files, 'UniformOutput', 0));
503 unwind_protect_cleanup
508 for k = 1:length (dirlist)
509 descfile = fullfile (prefix, dirlist{k}, "packinfo", "DESCRIPTION");
511 printf ("recreating package description from %s\n", dirlist{k});
513 if (exist (descfile, "file"))
514 desc = get_description (descfile);
515 desc.dir = fullfile (prefix, dirlist{k});
516 desc.archprefix = fullfile (archprefix, cstrcat (desc.name, "-",
519 if (exist (fullfile (desc.dir, "packinfo", ".autoload"), "file"))
520 unlink (fullfile (desc.dir, "packinfo", ".autoload"));
526 fclose (fopen (fullfile (desc.dir, "packinfo", ".autoload"), "wt"));
529 if (exist (fullfile (desc.dir, "packinfo", ".autoload"), "file"))
535 descriptions{end + 1} = desc;
537 warning ("directory %s is not a valid package", dirlist{k});
541 if (! isempty (files))
542 ## We are rebuilding for a particular package(s) so we should take
543 ## care to keep the other untouched packages in the descriptions
544 descriptions = {descriptions{:}, old_descriptions{:}};
547 for i = 1:length (descriptions)
551 for j = (i+1):length (descriptions)
555 if (strcmp (descriptions{i}.name, descriptions{j}.name))
561 descriptions (dup) = [];
566 function build (files, handle_deps, autoload, verbose)
567 if (length (files) < 1)
568 error ("insufficient number of files");
571 if (! exist (builddir, "dir"))
572 warning ("creating build directory %s", builddir);
573 [status, msg] = mkdir (builddir);
575 error ("could not create installation directory: %s", msg);
578 builddir = absolute_pathname (builddir);
579 installdir = fullfile (builddir, "install");
580 if (! exist (installdir, "dir"))
581 [status, msg] = mkdir (installdir);
583 error ("could not create installation directory: %s", msg);
587 buildlist = fullfile (builddir, "octave_packages");
588 install (files, handle_deps, autoload, installdir, installdir, verbose,
589 buildlist, "", false);
591 repackage (builddir, buildlist);
592 unwind_protect_cleanup
593 unload_packages ({"all"}, handle_deps, buildlist, "");
594 if (exist (installdir, "dir"))
597 if (exist (buildlist, "file"))
603 function install (files, handle_deps, autoload, prefix, archprefix, verbose,
604 local_list, global_list, global_install)
606 ## Check that the directory in prefix exist. If it doesn't: create it!
607 if (! exist (prefix, "dir"))
608 warning ("creating installation directory %s", prefix);
609 [status, msg] = mkdir (prefix);
611 error ("could not create installation directory: %s", msg);
615 ## Get the list of installed packages.
616 [local_packages, global_packages] = installed_packages (local_list,
619 installed_pkgs_lst = {local_packages{:}, global_packages{:}};
622 packages = global_packages;
624 packages = local_packages;
627 ## Uncompress the packages and read the DESCRIPTION files.
628 tmpdirs = packdirs = descriptions = {};
630 ## Warn about non existent files.
631 for i = 1:length (files)
632 if (isempty (glob(files{i})))
633 warning ("file %s does not exist", files{i});
637 ## Unpack the package files and read the DESCRIPTION files.
638 files = glob (files);
639 packages_to_uninstall = [];
640 for i = 1:length (files)
643 if (exist (tgz, "file"))
644 ## Create a temporary directory.
646 tmpdirs{end+1} = tmpdir;
648 printf ("mkdir (%s)\n", tmpdir);
650 [status, msg] = mkdir (tmpdir);
652 error ("couldn't create temporary directory: %s", msg);
655 ## Uncompress the package.
657 printf ("untar (%s, %s)\n", tgz, tmpdir);
661 ## Get the name of the directories produced by tar.
662 [dirlist, err, msg] = readdir (tmpdir);
664 error ("couldn't read directory produced by tar: %s", msg);
667 if (length (dirlist) > 3)
668 error ("bundles of packages are not allowed")
672 ## The filename pointed to an uncompressed package to begin with.
673 if (exist (tgz, "dir"))
674 dirlist = {".", "..", tgz};
677 if (exist (tgz, "file") || exist (tgz, "dir"))
678 ## The two first entries of dirlist are "." and "..".
679 if (exist (tgz, "file"))
680 packdir = fullfile (tmpdir, dirlist{3});
682 packdir = fullfile (pwd(), dirlist{3});
684 packdirs{end+1} = packdir;
686 ## Make sure the package contains necessary files.
687 verify_directory (packdir);
689 ## Read the DESCRIPTION file.
690 filename = fullfile (packdir, "DESCRIPTION");
691 desc = get_description (filename);
693 ## Verify that package name corresponds with filename.
694 [dummy, nm] = fileparts (tgz);
695 if ((length (nm) >= length (desc.name))
696 && ! strcmp (desc.name, nm(1:length(desc.name))))
697 error ("package name '%s' doesn't correspond to its filename '%s'",
701 ## Set default installation directory.
702 desc.dir = fullfile (prefix, cstrcat (desc.name, "-", desc.version));
704 ## Set default architecture dependent installation directory.
705 desc.archprefix = fullfile (archprefix, cstrcat (desc.name, "-",
709 descriptions{end+1} = desc;
711 ## Are any of the new packages already installed?
712 ## If so we'll remove the old version.
713 for j = 1:length (packages)
714 if (strcmp (packages{j}.name, desc.name))
715 packages_to_uninstall(end+1) = j;
721 ## Something went wrong, delete tmpdirs.
722 for i = 1:length (tmpdirs)
725 rethrow (lasterror ());
728 ## Check dependencies.
732 for i = 1:length (descriptions)
733 desc = descriptions{i};
734 idx2 = complement (i, 1:length(descriptions));
736 ## Global installation is not allowed to have dependencies on locally
737 ## installed packages.
738 idx1 = complement (packages_to_uninstall,
739 1:length(global_packages));
740 pseudo_installed_packages = {global_packages{idx1}, ...
743 idx1 = complement (packages_to_uninstall,
744 1:length(local_packages));
745 pseudo_installed_packages = {local_packages{idx1}, ...
746 global_packages{:}, ...
749 bad_deps = get_unsatisfied_deps (desc, pseudo_installed_packages);
750 ## Are there any unsatisfied dependencies?
751 if (! isempty (bad_deps))
753 for i = 1:length (bad_deps)
755 error_text = cstrcat (error_text, " ", desc.name, " needs ",
756 dep.package, " ", dep.operator, " ",
762 ## Did we find any unsatisfied dependencies?
764 error ("the following dependencies where unsatisfied:\n %s", error_text);
768 ## Prepare each package for installation.
770 for i = 1:length (descriptions)
771 desc = descriptions{i};
773 prepare_installation (desc, pdir);
774 configure_make (desc, pdir, verbose);
777 ## Something went wrong, delete tmpdirs.
778 for i = 1:length (tmpdirs)
781 rethrow (lasterror ());
784 ## Uninstall the packages that will be replaced.
786 for i = packages_to_uninstall
788 uninstall ({global_packages{i}.name}, false, verbose, local_list,
789 global_list, global_install);
791 uninstall ({local_packages{i}.name}, false, verbose, local_list,
792 global_list, global_install);
796 ## Something went wrong, delete tmpdirs.
797 for i = 1:length (tmpdirs)
800 rethrow (lasterror ());
803 ## Install each package.
805 for i = 1:length (descriptions)
806 desc = descriptions{i};
808 copy_files (desc, pdir, global_install);
809 create_pkgadddel (desc, pdir, "PKG_ADD", global_install);
810 create_pkgadddel (desc, pdir, "PKG_DEL", global_install);
811 finish_installation (desc, pdir, global_install);
812 generate_lookfor_cache (desc);
815 ## Something went wrong, delete tmpdirs.
816 for i = 1:length (tmpdirs)
819 for i = 1:length (descriptions)
820 rm_rf (descriptions{i}.dir);
821 rm_rf (getarchdir (descriptions{i}));
823 rethrow (lasterror ());
826 ## Check if the installed directory is empty. If it is remove it
828 for i = length (descriptions):-1:1
829 if (dirempty (descriptions{i}.dir, {"packinfo", "doc"}) &&
830 dirempty (getarchdir (descriptions{i})))
831 warning ("package %s is empty\n", descriptions{i}.name);
832 rm_rf (descriptions{i}.dir);
833 rm_rf (getarchdir (descriptions{i}));
834 descriptions(i) = [];
838 ## If the package requested that it is autoloaded, or the installer
839 ## requested that it is, then mark the package as autoloaded.
840 for i = length (descriptions):-1:1
841 if (autoload > 0 || (autoload == 0 && isautoload (descriptions(i))))
842 fclose (fopen (fullfile (descriptions{i}.dir, "packinfo",
843 ".autoload"), "wt"));
844 descriptions{i}.autoload = 1;
848 ## Add the packages to the package list.
851 idx = complement (packages_to_uninstall, 1:length(global_packages));
852 global_packages = save_order ({global_packages{idx}, descriptions{:}});
853 save (global_list, "global_packages");
854 installed_pkgs_lst = {local_packages{:}, global_packages{:}};
856 idx = complement (packages_to_uninstall, 1:length(local_packages));
857 local_packages = save_order ({local_packages{idx}, descriptions{:}});
858 save (local_list, "local_packages");
859 installed_pkgs_lst = {local_packages{:}, global_packages{:}};
862 ## Something went wrong, delete tmpdirs.
863 for i = 1:length (tmpdirs)
866 for i = 1:length (descriptions)
867 rm_rf (descriptions{i}.dir);
870 printf ("error: couldn't append to %s\n", global_list);
872 printf ("error: couldn't append to %s\n", local_list);
874 rethrow (lasterror ());
877 ## All is well, let's clean up.
878 for i = 1:length (tmpdirs)
879 [status, msg] = rm_rf (tmpdirs{i});
881 warning ("couldn't clean up after my self: %s\n", msg);
885 ## Add the newly installed packages to the path, so the user
886 ## can begin using them. Only load them if they are marked autoload.
887 if (length (descriptions) > 0)
889 for i = 1:length (descriptions)
890 if (isautoload (descriptions(i)))
891 nm = descriptions{i}.name;
892 for j = 1:length (installed_pkgs_lst)
893 if (strcmp (nm, installed_pkgs_lst{j}.name))
900 load_packages_and_dependencies (idx, handle_deps, installed_pkgs_lst,
905 function uninstall (pkgnames, handle_deps, verbose, local_list,
906 global_list, global_install)
907 ## Get the list of installed packages.
908 [local_packages, global_packages] = installed_packages(local_list,
911 installed_pkgs_lst = {local_packages{:}, global_packages{:}};
913 installed_pkgs_lst = local_packages;
916 num_packages = length (installed_pkgs_lst);
918 for i = 1:num_packages
919 cur_name = installed_pkgs_lst{i}.name;
920 if (any (strcmp (cur_name, pkgnames)))
921 delete_idx(end+1) = i;
925 ## Are all the packages that should be uninstalled already installed?
926 if (length (delete_idx) != length (pkgnames))
928 ## Try again for a locally installed package.
929 installed_pkgs_lst = local_packages;
931 num_packages = length (installed_pkgs_lst);
933 for i = 1:num_packages
934 cur_name = installed_pkgs_lst{i}.name;
935 if (any (strcmp (cur_name, pkgnames)))
936 delete_idx(end+1) = i;
939 if (length (delete_idx) != length (pkgnames))
940 ## FIXME: We should have a better error message.
941 warning ("some of the packages you want to uninstall are not installed");
944 ## FIXME: We should have a better error message.
945 warning ("some of the packages you want to uninstall are not installed");
949 ## Compute the packages that will remain installed.
950 idx = complement (delete_idx, 1:num_packages);
951 remaining_packages = {installed_pkgs_lst{idx}};
953 ## Check dependencies.
956 for i = 1:length (remaining_packages)
957 desc = remaining_packages{i};
958 bad_deps = get_unsatisfied_deps (desc, remaining_packages);
960 ## Will the uninstallation break any dependencies?
961 if (! isempty (bad_deps))
962 for i = 1:length (bad_deps)
964 error_text = cstrcat (error_text, " ", desc.name, " needs ",
965 dep.package, " ", dep.operator, " ",
971 if (! isempty (error_text))
972 error ("the following dependencies where unsatisfied:\n %s", error_text);
976 ## Delete the directories containing the packages.
978 desc = installed_pkgs_lst{i};
979 ## If an 'on_uninstall.m' exist, call it!
980 if (exist (fullfile (desc.dir, "packinfo", "on_uninstall.m"), "file"))
982 cd (fullfile (desc.dir, "packinfo"));
986 ## Do the actual deletion.
989 if (exist (getarchdir (desc)))
990 rmpath (getarchdir (desc));
993 if (exist (desc.dir, "dir"))
994 [status, msg] = rm_rf (desc.dir);
996 error ("couldn't delete directory %s: %s", desc.dir, msg);
998 [status, msg] = rm_rf (getarchdir (desc));
1000 error ("couldn't delete directory %s: %s", getarchdir (desc), msg);
1002 if (dirempty (desc.archprefix))
1003 rm_rf (desc.archprefix);
1006 warning ("directory %s previously lost", desc.dir);
1010 ## Write a new ~/.octave_packages.
1012 if (length (remaining_packages) == 0)
1013 unlink (global_list);
1015 global_packages = save_order (remaining_packages);
1016 save (global_list, "global_packages");
1019 if (length (remaining_packages) == 0)
1020 unlink (local_list);
1022 local_packages = save_order (remaining_packages);
1023 save (local_list, "local_packages");
1029 function [pkg_desc_list, flag] = describe (pkgnames, verbose,
1030 local_list, global_list)
1032 ## Get the list of installed packages.
1033 installed_pkgs_lst = installed_packages(local_list, global_list);
1034 num_packages = length (installed_pkgs_lst);
1037 describe_all = false;
1038 if (any (strcmp ("all", pkgnames)))
1039 describe_all = true;
1040 flag(1:num_packages) = {"Not Loaded"};
1041 num_pkgnames = num_packages;
1043 num_pkgnames = length (pkgnames);
1044 flag(1:num_pkgnames) = {"Not installed"};
1047 for i = 1:num_packages
1048 curr_name = installed_pkgs_lst{i}.name;
1052 name_pos = find(strcmp (curr_name, pkgnames));
1055 if (! isempty (name_pos))
1056 if (installed_pkgs_lst{i}.loaded)
1057 flag{name_pos} = "Loaded";
1059 flag{name_pos} = "Not loaded";
1062 pkg_desc_list{name_pos}.name = installed_pkgs_lst{i}.name;
1063 pkg_desc_list{name_pos}.version = installed_pkgs_lst{i}.version;
1064 pkg_desc_list{name_pos}.description = installed_pkgs_lst{i}.description;
1065 pkg_desc_list{name_pos}.provides = parse_pkg_idx (installed_pkgs_lst{i}.dir);
1070 non_inst = find (strcmp (flag, "Not installed"));
1071 if (! isempty (non_inst))
1073 non_inst_str = sprintf (" %s ", pkgnames{non_inst});
1074 error ("some packages are not installed: %s", non_inst_str);
1076 pkg_desc_list{non_inst} = struct ("name", {}, "description",
1077 {}, "provides", {});
1082 for i = 1:num_pkgnames
1083 print_package_description (pkg_desc_list{i}.name,
1084 pkg_desc_list{i}.version,
1085 pkg_desc_list{i}.provides,
1086 pkg_desc_list{i}.description,
1093 ## AUXILIARY FUNCTIONS
1095 ## Read an INDEX file.
1096 function [pkg_idx_struct] = parse_pkg_idx (packdir)
1098 index_file = fullfile (packdir, "packinfo", "INDEX");
1100 if (! exist (index_file, "file"))
1101 error ("could not find any INDEX file in directory %s, try 'pkg rebuild all' to generate missing INDEX files", packdir);
1105 [fid, msg] = fopen (index_file, "r");
1107 error ("the INDEX file %s could not be read: %s",
1112 pkg_idx_struct{1}.category = "Uncategorized";
1113 pkg_idx_struct{1}.functions = {};
1116 while (isempty (strfind (line, ">>")) && ! feof (fid))
1120 while (! feof (fid) || line != -1)
1121 if (! any (! isspace (line)) || line(1) == "#" || any (line == "="))
1122 ## Comments, blank lines or comments about unimplemented
1123 ## functions: do nothing
1124 ## FIXME: probably comments and pointers to external functions
1125 ## could be treated better when printing to screen?
1126 elseif (! isempty (strfind (line, ">>")))
1127 ## Skip package name and description as they are in DESCRIPTION
1129 elseif (! isspace (line(1)))
1131 if (! isempty (pkg_idx_struct{cat_num}.functions))
1132 pkg_idx_struct{++cat_num}.functions = {};
1134 pkg_idx_struct{cat_num}.category = deblank (line);
1137 while (any (! isspace (line)))
1138 [fun_name, line] = strtok (line);
1139 pkg_idx_struct{cat_num}.functions{end+1} = deblank (fun_name);
1147 function print_package_description (pkg_name, pkg_ver, pkg_idx_struct,
1148 pkg_desc, status, verbose)
1150 printf ("---\nPackage name:\n\t%s\n", pkg_name);
1151 printf ("Version:\n\t%s\n", pkg_ver);
1152 printf ("Short description:\n\t%s\n", pkg_desc);
1153 printf ("Status:\n\t%s\n", status);
1155 printf ("---\nProvides:\n");
1156 for i = 1:length(pkg_idx_struct)
1157 if (! isempty (pkg_idx_struct{i}.functions))
1158 printf ("%s\n", pkg_idx_struct{i}.category);
1159 for j = 1:length(pkg_idx_struct{i}.functions)
1160 printf ("\t%s\n", pkg_idx_struct{i}.functions{j});
1169 function pth = absolute_pathname (pth)
1170 [status, msg, msgid] = fileattrib (pth);
1172 error ("could not find the file or path %s", pth);
1178 function repackage (builddir, buildlist)
1179 packages = installed_packages (buildlist, buildlist);
1182 for i = 1 : length(packages)
1187 mkdir (fullfile (pack.name, "inst"));
1188 copyfile (fullfile (pack.dir, "*"), fullfile (pack.name, "inst"));
1189 movefile (fullfile (pack.name, "inst","packinfo", "*"), pack.name);
1190 if (exist (fullfile (pack.name, "inst","packinfo", ".autoload"), "file"))
1191 unlink (fullfile (pack.name, "inst","packinfo", ".autoload"));
1193 rmdir (fullfile (pack.name, "inst", "packinfo"));
1194 if (exist (fullfile (pack.name, "inst", "doc"), "dir"))
1195 movefile (fullfile (pack.name, "inst", "doc"), pack.name);
1197 if (exist (fullfile (pack.name, "inst", "bin"), "dir"))
1198 movefile (fullfile (pack.name, "inst", "bin"), pack.name);
1200 archdir = fullfile (pack.archprefix, cstrcat (pack.name, "-",
1201 pack.version), getarch ());
1202 if (exist (archdir, "dir"))
1203 if (exist (fullfile (pack.name, "inst", "PKG_ADD"), "file"))
1204 unlink (fullfile (pack.name, "inst", "PKG_ADD"));
1206 if (exist (fullfile (pack.name, "inst", "PKG_DEL"), "file"))
1207 unlink (fullfile (pack.name, "inst", "PKG_DEL"));
1209 if (exist (fullfile (archdir, "PKG_ADD"), "file"))
1210 movefile (fullfile (archdir, "PKG_ADD"),
1211 fullfile (pack.name, "PKG_ADD"));
1213 if (exist (fullfile (archdir, "PKG_DEL"), "file"))
1214 movefile (fullfile (archdir, "PKG_DEL"),
1215 fullfile (pack.name, "PKG_DEL"));
1218 if (exist (fullfile (pack.name, "inst", "PKG_ADD"), "file"))
1219 movefile (fullfile (pack.name, "inst", "PKG_ADD"),
1220 fullfile (pack.name, "PKG_ADD"));
1222 if (exist (fullfile (pack.name, "inst", "PKG_DEL"), "file"))
1223 movefile (fullfile (pack.name, "inst", "PKG_DEL"),
1224 fullfile (pack.name, "PKG_DEL"));
1227 tfile = cstrcat (pack.name, "-", pack.version, ".tar");
1228 tar (tfile, pack.name);
1233 warning ("failed to compress %s", tfile);
1235 unwind_protect_cleanup
1236 if (exist (pack.name, "dir"))
1244 function auto = isautoload (desc)
1246 if (isfield (desc{1}, "autoload"))
1247 a = desc{1}.autoload;
1248 if ((isnumeric (a) && a > 0)
1249 || (ischar (a) && (strcmpi (a, "true")
1250 || strcmpi (a, "on")
1251 || strcmpi (a, "yes")
1252 || strcmpi (a, "1"))))
1258 function prepare_installation (desc, packdir)
1259 ## Is there a pre_install to call?
1260 if (exist (fullfile (packdir, "pre_install.m"), "file"))
1268 rethrow (lasterror ());
1272 ## If the directory "inst" doesn't exist, we create it.
1273 inst_dir = fullfile (packdir, "inst");
1274 if (! exist (inst_dir, "dir"))
1275 [status, msg] = mkdir (inst_dir);
1278 error ("the 'inst' directory did not exist and could not be created: %s",
1284 function configure_make (desc, packdir, verbose)
1285 ## Perform ./configure, make, make install in "src".
1286 if (exist (fullfile (packdir, "src"), "dir"))
1287 src = fullfile (packdir, "src");
1289 if (exist (fullfile (src, "configure"), "file"))
1291 if (isempty (getenv ("CC")))
1292 flags = cstrcat (flags, " CC=\"", octave_config_info ("CC"), "\"");
1294 if (isempty (getenv ("CXX")))
1295 flags = cstrcat (flags, " CXX=\"", octave_config_info ("CXX"), "\"");
1297 if (isempty (getenv ("AR")))
1298 flags = cstrcat (flags, " AR=\"", octave_config_info ("AR"), "\"");
1300 if (isempty (getenv ("RANLIB")))
1301 flags = cstrcat (flags, " RANLIB=\"", octave_config_info ("RANLIB"), "\"");
1303 [status, output] = shell (strcat ("cd '", src, "'; ./configure --prefix=\"",
1304 desc.dir, "\"", flags));
1307 error ("the configure script returned the following error: %s", output);
1309 printf("%s", output);
1315 if (exist (fullfile (src, "Makefile"), "file"))
1316 [status, output] = shell (cstrcat ("export INSTALLDIR=\"", desc.dir,
1317 "\"; make -C '", src, "'"));
1320 error ("'make' returned the following error: %s", output);
1322 printf("%s", output);
1326 ## Copy files to "inst" and "inst/arch" (this is instead of 'make
1328 files = fullfile (src, "FILES");
1329 instdir = fullfile (packdir, "inst");
1330 archdir = fullfile (packdir, "inst", getarch ());
1333 if (exist (files, "file"))
1334 [fid, msg] = fopen (files, "r");
1336 error ("couldn't open %s: %s", files, msg);
1338 filenames = char (fread (fid))';
1340 if (filenames(end) == "\n")
1341 filenames(end) = [];
1343 filenames = split_by (filenames, "\n");
1345 for i = 1:length (filenames)
1346 if (! all (isspace (filenames{i})))
1347 filenames{i} = fullfile (src, filenames{i});
1349 delete_idx(end+1) = i;
1352 filenames(delete_idx) = [];
1354 m = dir (fullfile (src, "*.m"));
1355 oct = dir (fullfile (src, "*.oct"));
1356 mex = dir (fullfile (src, "*.mex"));
1358 filenames = cellfun (@(x) fullfile (src, x),
1359 {m.name, oct.name, mex.name},
1360 "UniformOutput", false);
1363 ## Split into architecture dependent and independent files.
1364 if (isempty (filenames))
1367 idx = cellfun (@is_architecture_dependent, filenames);
1369 archdependent = filenames (idx);
1370 archindependent = filenames (!idx);
1373 if (! all (isspace ([filenames{:}])))
1374 if (! exist (instdir, "dir")) # fixindent
1377 if (! all (isspace ([archindependent{:}])))
1379 printf ("copyfile");
1380 printf (" %s", archindependent{:});
1381 printf ("%s\n", instdir);
1383 [status, output] = copyfile (archindependent, instdir);
1386 error ("Couldn't copy files from 'src' to 'inst': %s", output);
1389 if (! all (isspace ([archdependent{:}])))
1391 printf ("copyfile");
1392 printf (" %s", archdependent{:});
1393 printf (" %s\n", archdir);
1395 if (! exist (archdir, "dir"))
1398 [status, output] = copyfile (archdependent, archdir);
1401 error ("Couldn't copy files from 'src' to 'inst': %s", output);
1408 function pkg = extract_pkg (nm, pat)
1409 fid = fopen (nm, "rt");
1412 while (! feof (fid))
1415 t = regexp (ln, pat, "tokens");
1417 pkg = cstrcat (pkg, "\n", t{1}{1});
1421 if (! isempty (pkg))
1422 pkg = cstrcat (pkg, "\n");
1428 function create_pkgadddel (desc, packdir, nm, global_install)
1429 instpkg = fullfile (desc.dir, nm);
1430 instfid = fopen (instpkg, "wt");
1431 ## If it is exists, most of the PKG_* file should go into the
1432 ## architecture dependent directory so that the autoload/mfilename
1433 ## commands work as expected. The only part that doesn't is the
1434 ## part in the main directory.
1435 archdir = fullfile (getarchprefix (desc), cstrcat (desc.name, "-",
1436 desc.version), getarch ());
1437 if (exist (getarchdir (desc, global_install), "dir"))
1438 archpkg = fullfile (getarchdir (desc, global_install), nm);
1439 archfid = fopen (archpkg, "at");
1445 if (archfid >= 0 && instfid >= 0)
1446 ## Search all dot-m files for PKG commands.
1447 lst = dir (fullfile (packdir, "inst", "*.m"));
1448 for i = 1:length (lst)
1449 nam = fullfile (packdir, "inst", lst(i).name);
1450 fwrite (instfid, extract_pkg (nam, ['^[#%][#%]* *' nm ': *(.*)$']));
1453 ## Search all C++ source files for PKG commands.
1454 lst = dir (fullfile (packdir, "src", "*.cc"));
1455 for i = 1:length (lst)
1456 nam = fullfile (packdir, "src", lst(i).name);
1457 fwrite (archfid, extract_pkg (nam, ['^//* *' nm ': *(.*)$']));
1458 fwrite (archfid, extract_pkg (nam, ['^/\** *' nm ': *(.*) *\*/$']));
1461 ## Add developer included PKG commands.
1462 packdirnm = fullfile (packdir, nm);
1463 if (exist (packdirnm, "file"))
1464 fid = fopen (packdirnm, "rt");
1466 while (! feof (fid))
1469 fwrite (archfid, ln);
1476 ## If the files is empty remove it.
1483 if (instfid != archfid)
1493 function copy_files (desc, packdir, global_install)
1494 ## Create the installation directory.
1495 if (! exist (desc.dir, "dir"))
1496 [status, output] = mkdir (desc.dir);
1498 error ("couldn't create installation directory %s : %s",
1503 octfiledir = getarchdir (desc);
1505 ## Copy the files from "inst" to installdir.
1506 instdir = fullfile (packdir, "inst");
1507 if (! dirempty (instdir))
1508 [status, output] = copyfile (fullfile (instdir, "*"), desc.dir);
1511 error ("couldn't copy files to the installation directory");
1513 if (exist (fullfile (desc.dir, getarch ()), "dir") &&
1514 ! strcmp (fullfile (desc.dir, getarch ()), octfiledir))
1515 if (! exist (octfiledir, "dir"))
1516 ## Can be required to create upto three levels of dirs.
1517 octm1 = fileparts (octfiledir);
1518 if (! exist (octm1, "dir"))
1519 octm2 = fileparts (octm1);
1520 if (! exist (octm2, "dir"))
1521 octm3 = fileparts (octm2);
1522 if (! exist (octm3, "dir"))
1523 [status, output] = mkdir (octm3);
1526 error ("couldn't create installation directory %s : %s",
1530 [status, output] = mkdir (octm2);
1533 error ("couldn't create installation directory %s : %s",
1537 [status, output] = mkdir (octm1);
1540 error ("couldn't create installation directory %s : %s",
1544 [status, output] = mkdir (octfiledir);
1547 error ("couldn't create installation directory %s : %s",
1548 octfiledir, output);
1551 [status, output] = movefile (fullfile (desc.dir, getarch (), "*"),
1553 rm_rf (fullfile (desc.dir, getarch ()));
1558 error ("couldn't copy files to the installation directory");
1564 ## Create the "packinfo" directory.
1565 packinfo = fullfile (desc.dir, "packinfo");
1566 [status, msg] = mkdir (packinfo);
1570 error ("couldn't create packinfo directory: %s", msg);
1573 ## Copy DESCRIPTION.
1574 [status, output] = copyfile (fullfile (packdir, "DESCRIPTION"), packinfo);
1578 error ("couldn't copy DESCRIPTION: %s", output);
1582 [status, output] = copyfile (fullfile (packdir, "COPYING"), packinfo);
1586 error ("couldn't copy COPYING: %s", output);
1589 ## If the file ChangeLog exists, copy it.
1590 changelog_file = fullfile (packdir, "ChangeLog");
1591 if (exist (changelog_file, "file"))
1592 [status, output] = copyfile (changelog_file, packinfo);
1596 error ("couldn't copy ChangeLog file: %s", output);
1600 ## Is there an INDEX file to copy or should we generate one?
1601 index_file = fullfile (packdir, "INDEX");
1602 if (exist(index_file, "file"))
1603 [status, output] = copyfile (index_file, packinfo);
1607 error ("couldn't copy INDEX file: %s", output);
1611 write_index (desc, fullfile (packdir, "inst"),
1612 fullfile (packinfo, "INDEX"), global_install);
1616 rethrow (lasterror ());
1620 ## Is there an 'on_uninstall.m' to install?
1621 fon_uninstall = fullfile (packdir, "on_uninstall.m");
1622 if (exist (fon_uninstall, "file"))
1623 [status, output] = copyfile (fon_uninstall, packinfo);
1627 error ("couldn't copy on_uninstall.m: %s", output);
1631 ## Is there a doc/ directory that needs to be installed?
1632 docdir = fullfile (packdir, "doc");
1633 if (exist (docdir, "dir") && ! dirempty (docdir))
1634 [status, output] = copyfile (docdir, desc.dir);
1637 ## Is there a bin/ directory that needs to be installed?
1638 ## FIXME: Need to treat architecture dependent files in bin/
1639 bindir = fullfile (packdir, "bin");
1640 if (exist (bindir, "dir") && ! dirempty (bindir))
1641 [status, output] = copyfile (bindir, desc.dir);
1645 function finish_installation (desc, packdir, global_install)
1646 ## Is there a post-install to call?
1647 if (exist (fullfile (packdir, "post_install.m"), "file"))
1651 post_install (desc);
1656 rm_rf (getarchdir (desc), global_install);
1657 rethrow (lasterror ());
1662 function generate_lookfor_cache (desc)
1663 dirs = split_by (genpath (desc.dir), pathsep ());
1664 for i = 1 : length (dirs)
1665 gen_doc_cache (fullfile (dirs{i}, "doc-cache"), dirs{i});
1669 ## Make sure the package contains the essential files.
1670 function verify_directory (dir)
1671 needed_files = {"COPYING", "DESCRIPTION"};
1672 for f = needed_files
1673 if (! exist (fullfile (dir, f{1}), "file"))
1674 error ("package is missing file: %s", f{1});
1679 ## Parse the DESCRIPTION file.
1680 function desc = get_description (filename)
1681 [fid, msg] = fopen (filename, "r");
1683 error ("the DESCRIPTION file %s could not be read: %s", filename, msg);
1691 ## Comments, do nothing.
1692 elseif (isspace(line(1)))
1693 ## Continuation lines
1694 if (exist ("keyword", "var") && isfield (desc, keyword))
1695 desc.(keyword) = cstrcat (desc.(keyword), " ", rstrip(line));
1698 ## Keyword/value pair
1699 colon = find (line == ":");
1700 if (length (colon) == 0)
1701 disp ("skipping line");
1704 keyword = tolower (strip (line(1:colon-1)));
1705 value = strip (line (colon+1:end));
1706 if (length (value) == 0)
1708 error ("the keyword %s has an empty value", desc.keywords{end});
1710 desc.(keyword) = value;
1717 ## Make sure all is okay.
1718 needed_fields = {"name", "version", "date", "title", ...
1719 "author", "maintainer", "description"};
1720 for f = needed_fields
1721 if (! isfield (desc, f{1}))
1722 error ("description is missing needed field %s", f{1});
1725 desc.version = fix_version (desc.version);
1726 if (isfield (desc, "depends"))
1727 desc.depends = fix_depends (desc.depends);
1731 desc.name = tolower (desc.name);
1734 ## Make sure the version string v is a valid x.y.z version string
1735 ## Examples: "0.1" => "0.1.0", "monkey" => error(...).
1736 function out = fix_version (v)
1737 dots = find (v == ".");
1738 if (length (dots) == 1)
1739 major = str2num (v(1:dots-1));
1740 minor = str2num (v(dots+1:end));
1741 if (length (major) != 0 && length (minor) != 0)
1742 out = sprintf ("%d.%d.0", major, minor);
1745 elseif (length (dots) == 2)
1746 major = str2num (v(1:dots(1)-1));
1747 minor = str2num (v(dots(1)+1:dots(2)-1));
1748 rev = str2num (v(dots(2)+1:end));
1749 if (length (major) != 0 && length (minor) != 0 && length (rev) != 0)
1750 out = sprintf ("%d.%d.%d", major, minor, rev);
1754 error ("bad version string: %s", v);
1757 ## Make sure the depends field is of the right format.
1758 ## This function returns a cell of structures with the following fields:
1759 ## package, version, operator
1760 function deps_cell = fix_depends (depends)
1761 deps = split_by (tolower (depends), ",");
1762 deps_cell = cell (1, length (deps));
1764 ## For each dependency.
1765 for i = 1:length (deps)
1767 lpar = find (dep == "(");
1768 rpar = find (dep == ")");
1769 ## Does the dependency specify a version
1770 ## Example: package(>= version).
1771 if (length (lpar) == 1 && length (rpar) == 1)
1772 package = tolower (strip (dep(1:lpar-1)));
1773 sub = dep(lpar(1)+1:rpar(1)-1);
1774 parts = strsplit (sub, " ", true);
1775 if (length (parts) != 2)
1776 error ("incorrect syntax for dependency `%s' in the DESCRIPTION file\n",
1779 operator = parts{1};
1780 if (! any (strcmp (operator, {">", ">=", "<=", "<", "=="})))
1781 error ("unsupported operator: %s", operator);
1783 version = fix_version (parts{2});
1785 ## If no version is specified for the dependency
1786 ## we say that the version should be greater than
1787 ## or equal to "0.0.0".
1789 package = tolower (strip (dep));
1793 deps_cell{i} = struct ("package", package, "operator", operator,
1794 "version", version);
1798 ## Strip the text of spaces from the right
1799 ## Example: " hello world " => " hello world"
1800 ## FIXME -- is this the same as deblank?
1801 function text = rstrip (text)
1802 chars = find (! isspace (text));
1803 if (length (chars) > 0)
1804 ## FIXME: shouldn't it be text = text(1:chars(end));
1805 text = text (chars(1):end);
1811 ## Strip the text of spaces from the left and the right.
1812 ## Example: " hello world " => "hello world"
1813 function text = strip (text)
1814 chars = find (! isspace (text));
1815 if (length (chars) > 0)
1816 text = text(chars(1):chars(end));
1822 ## Split the text into a cell array of strings by sep.
1823 ## Example: "A, B" => {"A", "B"} (with sep = ",")
1824 function out = split_by (text, sep)
1825 out = strtrim (strsplit (text, sep));
1828 ## Create an INDEX file for a package that doesn't provide one.
1829 ## 'desc' describes the package.
1830 ## 'dir' is the 'inst' directory in temporary directory.
1831 ## 'index_file' is the name (including path) of resulting INDEX file.
1832 function write_index (desc, dir, index_file, global_install)
1833 ## Get names of functions in dir
1834 [files, err, msg] = readdir (dir);
1836 error ("couldn't read directory %s: %s", dir, msg);
1839 ## Check for architecture dependent files.
1840 tmpdir = getarchdir (desc);
1841 if (exist (tmpdir, "dir"))
1842 [files2, err, msg] = readdir (tmpdir);
1844 error ("couldn't read directory %s: %s", tmpdir, msg);
1846 files = [files; files2];
1850 for i = 1:length (files)
1853 if (lf > 2 && strcmp (file(end-1:end), ".m"))
1854 functions{end+1} = file(1:end-2);
1855 elseif (lf > 4 && strcmp (file(end-3:end), ".oct"))
1856 functions{end+1} = file(1:end-4);
1860 ## Does desc have a categories field?
1861 if (! isfield (desc, "categories"))
1862 error ("the DESCRIPTION file must have a Categories field, when no INDEX file is given");
1864 categories = split_by (desc.categories, ",");
1865 if (length (categories) < 1)
1866 error ("the Category field is empty");
1870 fid = fopen (index_file, "w");
1872 error ("couldn't open %s for writing.", index_file);
1874 fprintf (fid, "%s >> %s\n", desc.name, desc.title);
1875 fprintf (fid, "%s\n", categories{1});
1876 fprintf (fid, " %s\n", functions{:});
1880 function bad_deps = get_unsatisfied_deps (desc, installed_pkgs_lst)
1883 ## For each dependency.
1884 for i = 1:length (desc.depends)
1885 dep = desc.depends{i};
1887 ## Is the current dependency Octave?
1888 if (strcmp (dep.package, "octave"))
1889 if (! compare_versions (OCTAVE_VERSION, dep.version, dep.operator))
1890 bad_deps{end+1} = dep;
1892 ## Is the current dependency not Octave?
1895 for i = 1:length (installed_pkgs_lst)
1896 cur_name = installed_pkgs_lst{i}.name;
1897 cur_version = installed_pkgs_lst{i}.version;
1898 if (strcmp (dep.package, cur_name)
1899 && compare_versions (cur_version, dep.version, dep.operator))
1905 bad_deps{end+1} = dep;
1911 function [out1, out2] = installed_packages (local_list, global_list)
1912 ## Get the list of installed packages.
1914 local_packages = load (local_list).local_packages;
1916 local_packages = {};
1919 global_packages = load (global_list).global_packages;
1921 global_packages = {};
1923 installed_pkgs_lst = {local_packages{:}, global_packages{:}};
1925 ## Eliminate duplicates in the installed package list.
1926 ## Locally installed packages take precedence.
1928 for i = 1:length (installed_pkgs_lst)
1932 for j = (i+1):length (installed_pkgs_lst)
1936 if (strcmp (installed_pkgs_lst{i}.name, installed_pkgs_lst{j}.name))
1942 installed_pkgs_lst(dup) = [];
1945 ## Now check if the package is loaded.
1946 tmppath = strrep (path(), "\\", "/");
1947 for i = 1:length (installed_pkgs_lst)
1948 if (findstr (tmppath, strrep (installed_pkgs_lst{i}.dir, "\\", "/")))
1949 installed_pkgs_lst{i}.loaded = true;
1951 installed_pkgs_lst{i}.loaded = false;
1954 for i = 1:length (local_packages)
1955 if (findstr (tmppath, strrep (local_packages{i}.dir, "\\", "/")))
1956 local_packages{i}.loaded = true;
1958 local_packages{i}.loaded = false;
1961 for i = 1:length (global_packages)
1962 if (findstr (tmppath, strrep (global_packages{i}.dir, "\\", "/")))
1963 global_packages{i}.loaded = true;
1965 global_packages{i}.loaded = false;
1969 ## Should we return something?
1971 out1 = local_packages;
1972 out2 = global_packages;
1974 elseif (nargout == 1)
1975 out1 = installed_pkgs_lst;
1979 ## We shouldn't return something, so we'll print something.
1980 num_packages = length (installed_pkgs_lst);
1981 if (num_packages == 0)
1982 printf ("no packages installed.\n");
1986 ## Compute the maximal lengths of name, version, and dir.
1987 h1 = "Package Name";
1989 h3 = "Installation directory";
1990 max_name_length = length (h1);
1991 max_version_length = length (h2);
1992 names = cell (num_packages, 1);
1993 for i = 1:num_packages
1994 max_name_length = max (max_name_length,
1995 length (installed_pkgs_lst{i}.name));
1996 max_version_length = max (max_version_length,
1997 length (installed_pkgs_lst{i}.version));
1998 names{i} = installed_pkgs_lst{i}.name;
2000 max_dir_length = terminal_size()(2) - max_name_length - ...
2001 max_version_length - 7;
2002 if (max_dir_length < 20)
2003 max_dir_length = Inf;
2006 h1 = postpad (h1, max_name_length + 1, " ");
2007 h2 = postpad (h2, max_version_length, " ");
2010 header = sprintf("%s | %s | %s\n", h1, h2, h3);
2012 tmp = sprintf (repmat ("-", 1, length(header)-1));
2013 tmp(length(h1)+2) = "+";
2014 tmp(length(h1)+length(h2)+5) = "+";
2015 printf ("%s\n", tmp);
2017 ## Print the packages.
2018 format = sprintf ("%%%ds %%1s| %%%ds | %%s\n", max_name_length,
2019 max_version_length);
2020 [dummy, idx] = sort (names);
2021 for i = 1:num_packages
2022 cur_name = installed_pkgs_lst{idx(i)}.name;
2023 cur_version = installed_pkgs_lst{idx(i)}.version;
2024 cur_dir = installed_pkgs_lst{idx(i)}.dir;
2025 if (length (cur_dir) > max_dir_length)
2026 first_char = length (cur_dir) - max_dir_length + 4;
2027 first_filesep = strfind (cur_dir(first_char:end), filesep());
2028 if (! isempty (first_filesep))
2029 cur_dir = cstrcat ("...",
2030 cur_dir((first_char + first_filesep(1) - 1):end));
2032 cur_dir = cstrcat ("...", cur_dir(first_char:end));
2035 if (installed_pkgs_lst{idx(i)}.loaded)
2040 printf (format, cur_name, cur_loaded, cur_version, cur_dir);
2044 function load_packages (files, handle_deps, local_list, global_list)
2045 installed_pkgs_lst = installed_packages (local_list, global_list);
2046 num_packages = length (installed_pkgs_lst);
2048 ## Read package names and installdirs into a more convenient format.
2049 pnames = pdirs = cell (1, num_packages);
2050 for i = 1:num_packages
2051 pnames{i} = installed_pkgs_lst{i}.name;
2052 pdirs{i} = installed_pkgs_lst{i}.dir;
2056 if (length (files) == 1 && strcmp (files{1}, "all"))
2057 idx = [1:length(installed_pkgs_lst)];
2059 elseif (length (files) == 1 && strcmp (files{1}, "auto"))
2061 for i = 1:length (installed_pkgs_lst)
2062 if (exist (fullfile (pdirs{i}, "packinfo", ".autoload"), "file"))
2066 ## Load package_name1 ...
2069 for i = 1:length (files)
2070 idx2 = find (strcmp (pnames, files{i}));
2072 error ("package %s is not installed", files{i});
2074 idx (end + 1) = idx2;
2078 ## Load the packages, but take care of the ordering of dependencies.
2079 load_packages_and_dependencies (idx, handle_deps, installed_pkgs_lst, true);
2082 function unload_packages (files, handle_deps, local_list, global_list)
2083 installed_pkgs_lst = installed_packages (local_list, global_list);
2084 num_packages = length (installed_pkgs_lst);
2086 ## Read package names and installdirs into a more convenient format.
2087 pnames = pdirs = cell (1, num_packages);
2088 for i = 1:num_packages
2089 pnames{i} = installed_pkgs_lst{i}.name;
2090 pdirs{i} = installed_pkgs_lst{i}.dir;
2091 pdeps{i} = installed_pkgs_lst{i}.depends;
2094 ## Get the current octave path.
2095 p = split_by (path(), pathsep ());
2097 if (length (files) == 1 && strcmp (files{1}, "all"))
2100 desc = installed_pkgs_lst;
2102 ## Unload package_name1 ...
2105 for i = 1:length (files)
2106 idx = strcmp (pnames, files{i});
2108 error ("package %s is not installed", files{i});
2110 dirs{end+1} = pdirs{idx};
2111 desc{end+1} = installed_pkgs_lst{idx};
2115 ## Check for architecture dependent directories.
2117 for i = 1:length (dirs)
2118 tmpdir = getarchdir (desc{i});
2119 if (exist (tmpdir, "dir"))
2120 archdirs{end+1} = dirs{i};
2121 archdirs{end+1} = tmpdir;
2123 archdirs{end+1} = dirs{i};
2127 ## Unload the packages.
2128 for i = 1:length (archdirs)
2130 idx = strcmp (p, d);
2133 ## FIXME: We should also check if we need to remove items from
2139 function [status_out, msg_out] = rm_rf (dir)
2141 crr = confirm_recursive_rmdir ();
2143 confirm_recursive_rmdir (false);
2144 [status, msg] = rmdir (dir, "s");
2145 unwind_protect_cleanup
2146 confirm_recursive_rmdir (crr);
2153 status_out = status;
2160 function emp = dirempty (nm, ign)
2161 if (exist (nm, "dir"))
2165 ign = [{".", ".."}, ign];
2168 for i = 1:length (l)
2170 for j = 1:length (ign)
2171 if (strcmp (l(i).name, ign{j}))
2187 function arch = getarch ()
2188 persistent _arch = cstrcat (octave_config_info("canonical_host_type"), ...
2189 "-", octave_config_info("api_version"));
2193 function archprefix = getarchprefix (desc, global_install)
2194 if ((nargin == 2 && global_install) || (nargin < 2 && issuperuser ()))
2195 archprefix = fullfile (octave_config_info ("libexecdir"), "octave",
2196 "packages", cstrcat(desc.name, "-", desc.version));
2198 archprefix = desc.dir;
2202 function archdir = getarchdir (desc)
2203 archdir = fullfile (desc.archprefix, getarch());
2206 function s = issuperuser ()
2207 if ((ispc () && ! isunix ()) || (geteuid() == 0))
2214 function [status, output] = shell (cmd)
2217 cmd = strrep (cmd, "\\", "/");
2218 if (ispc () && ! isunix ())
2219 if (isempty(have_sh))
2220 if (system("sh.exe -c \"exit\""))
2227 [status, output] = system (cstrcat ("sh.exe -c \"", cmd, "\""));
2229 error ("Can not find the command shell")
2232 [status, output] = system (cmd);
2236 function newdesc = save_order (desc)
2238 for i = 1 : length(desc)
2239 deps = desc{i}.depends;
2240 if (isempty (deps) || (length (deps) == 1 &&
2241 strcmp(deps{1}.package, "octave")))
2242 newdesc {end + 1} = desc{i};
2245 for k = 1 : length (deps)
2246 for j = 1 : length (desc)
2247 if (strcmp (desc{j}.name, deps{k}.package))
2248 tmpdesc{end+1} = desc{j};
2253 if (! isempty (tmpdesc))
2254 newdesc = {newdesc{:}, save_order(tmpdesc){:}, desc{i}};
2256 newdesc{end+1} = desc{i};
2260 ## Eliminate the duplicates.
2262 for i = 1 : length (newdesc)
2263 for j = (i + 1) : length (newdesc)
2264 if (strcmp (newdesc{i}.name, newdesc{j}.name))
2272 function load_packages_and_dependencies (idx, handle_deps, installed_pkgs_lst,
2274 idx = load_package_dirs (idx, [], handle_deps, installed_pkgs_lst);
2276 execpath = EXEC_PATH ();
2278 ndir = installed_pkgs_lst{i}.dir;
2280 if (exist (fullfile (dirs{end}, "bin"), "dir"))
2281 execpath = cstrcat (fullfile (dirs{end}, "bin"), ":", execpath);
2283 tmpdir = getarchdir (installed_pkgs_lst{i});
2284 if (exist (tmpdir, "dir"))
2285 dirs{end + 1} = tmpdir;
2286 if (exist (fullfile (dirs{end}, "bin"), "dir"))
2287 execpath = cstrcat (fullfile (dirs{end}, "bin"), ":", execpath);
2292 ## Load the packages.
2293 if (length (dirs) > 0)
2297 ## Add the binaries to exec_path.
2298 if (! strcmp (EXEC_PATH, execpath))
2299 EXEC_PATH (execpath);
2303 function idx = load_package_dirs (lidx, idx, handle_deps, installed_pkgs_lst)
2305 if (isfield (installed_pkgs_lst{i}, "loaded") &&
2306 installed_pkgs_lst{i}.loaded)
2310 deps = installed_pkgs_lst{i}.depends;
2311 if ((length (deps) > 1) || (length (deps) == 1 &&
2312 ! strcmp(deps{1}.package, "octave")))
2314 for k = 1 : length (deps)
2315 for j = 1 : length (installed_pkgs_lst)
2316 if (strcmp (installed_pkgs_lst{j}.name, deps{k}.package))
2317 tmplidx (end + 1) = j;
2322 idx = load_package_dirs (tmplidx, idx, handle_deps,
2323 installed_pkgs_lst);
2326 if (isempty (find(idx == i)))
2333 function dep = is_architecture_dependent (nm)
2334 persistent archdepsuffix = {".oct",".mex",".a",".lib",".so",".so.*",".dll","dylib"};
2337 for i = 1 : length (archdepsuffix)
2338 ext = archdepsuffix{i};
2339 if (ext(end) == "*")
2343 isglob = false; # I am a test
2345 ### I shall align to column 0
2347 pos = findstr (nm, ext);
2349 if (! isglob && (length(nm) - pos(end) != length(ext) - 1))
2358 %!assert(norm(logm([1 -1;0 1]) - [0 -1; 0 0]) < 1e-5);
2359 %!assert(norm(expm(logm([-1 2 ; 4 -1])) - [-1 2 ; 4 -1]) < 1e-5);
2360 %!assert(logm([1 -1 -1;0 1 -1; 0 0 1]), [0 -1 -1.5; 0 0 -1; 0 0 0], 1e-5);
2361 %!assert (logm (expm ([0 1i; -1i 0])), [0 1i; -1i 0], 10 * eps)
2363 %% Test input validation
2365 %!error logm (1, 2, 3);
2366 %!error <logm: A must be a square matrix> logm([1 0;0 1; 2 2]);
2368 %!assert (logm (10), log (10))
2369 %!assert (full (logm (eye (3))), logm (full (eye (3))))
2370 %!assert (full (logm (10*eye (3))), logm (full (10*eye (3))), 8*eps)