view scripts/pkg/private/installed_packages.m @ 28912:0de38a6ef693

maint: Use Octave convention of space after function name in scripts dir. * cplxpair.m, gradient.m, integral.m, integral3.m, interp2.m, interpft.m, num2str.m, pol2cart.m, quad2d.m, quadgk.m, quadl.m, repelem.m, sph2cart.m, convhull.m, delaunay.m, delaunayn.m, movegui.m, print_usage.m, __strip_html_tags__.m, colormap.m, gray2ind.m, imformats.m, importdata.m, javachk.m, condest.m, isbanded.m, krylov.m, lscov.m, rref.m, inputParser.m, publish.m, symvar.m, validateattributes.m, ode15i.m, ode15s.m, ode23.m, ode23s.m, ode45.m, fminbnd.m, fminsearch.m, glpk.m, qp.m, get_forge_pkg.m, installed_packages.m, annotation.m, axis.m, camorbit.m, campos.m, camva.m, camzoom.m, daspect.m, legend.m, pbaspect.m, __gnuplot_legend__.m, light.m, patch.m, plotyy.m, __pie__.m, reducepatch.m, ribbon.m, streamline.m, trisurf.m, figure.m, ndgrid.m, __gnuplot_draw_axes__.m, __opengl_print__.m, padecoef.m, polygcd.m, ppval.m, spline.m, union.m, bicg.m, bicgstab.m, cgs.m, eigs.m, gmres.m, pcg.m, pcr.m, __alltohandles__.m, __sprand__.m, qmr.m, tfqmr.m, betaincinv.m, cosint.m, gammainc.m, discrete_cdf.m, discrete_inv.m, discrete_rnd.m, base2dec.m, strtok.m, compare_plot_demos.m, html_compare_plot_demos.m, speed.m, test.m, weboptions.m, webread.m, webwrite.m: Use Octave convention of space after function name and before '(' in scripts directory.
author Rik <rik@octave.org>
date Tue, 13 Oct 2020 18:17:29 -0700
parents bd51beb6205e
children 7854d5752dd2
line wrap: on
line source

########################################################################
##
## Copyright (C) 2005-2020 The Octave Project Developers
##
## See the file COPYRIGHT.md in the top-level directory of this
## distribution or <https://octave.org/copyright/>.
##
## This file is part of Octave.
##
## Octave is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, see
## <https://www.gnu.org/licenses/>.
##
########################################################################

## -*- texinfo -*-
## @deftypefn {} {[@var{out1}, @var{out2}] =} installed_packages (@var{local_list}, @var{global_list}, @var{pkgname})
## Undocumented internal function.
## @end deftypefn

function [out1, out2] = installed_packages (local_list, global_list, pkgname = {})

  ## Get the list of installed packages.
  try
    local_packages = load (local_list).local_packages;
  catch
    local_packages = {};
  end_try_catch
  try
    global_packages = load (global_list).global_packages;
    global_packages = expand_rel_paths (global_packages);
    if (ispc)
      ## On Windows ensure 8.3 style paths are turned into LFN paths
      global_packages = standardize_paths (global_packages);
    endif
  catch
    global_packages = {};
  end_try_catch
  installed_pkgs_lst = {local_packages{:}, global_packages{:}};

  ## Eliminate duplicates in the installed package list.
  ## Locally installed packages take precedence.
  installed_names = cellfun (@(x) x.name, installed_pkgs_lst,
                             "uniformoutput", false);
  [~, idx] = unique (installed_names, "first");
  installed_names = installed_names(idx);
  installed_pkgs_lst = installed_pkgs_lst(idx);

  ## Check whether info on a particular package was requested
  if (! isempty (pkgname))
    idx = [];
    for i = 1 : numel (pkgname)
      idx = [idx, find(strcmp (pkgname{i}, installed_names))];
    endfor
    if (isempty (idx))
      installed_names = {};
      installed_pkgs_lst = {};
    else
      installed_names = installed_names(idx);
      installed_pkgs_lst = installed_pkgs_lst(idx);
    endif
  endif

  ## Now check if the package is loaded.
  ## FIXME: Couldn't dir_in_loadpath() be used here?
  tmppath = path ();
  for i = 1:numel (installed_pkgs_lst)
    if (strfind (tmppath, installed_pkgs_lst{i}.dir))
      installed_pkgs_lst{i}.loaded = true;
    else
      installed_pkgs_lst{i}.loaded = false;
    endif
  endfor
  for i = 1:numel (local_packages)
    if (strfind (tmppath, local_packages{i}.dir))
      local_packages{i}.loaded = true;
    else
      local_packages{i}.loaded = false;
    endif
  endfor
  for i = 1:numel (global_packages)
    if (strfind (tmppath, global_packages{i}.dir))
      global_packages{i}.loaded = true;
    else
      global_packages{i}.loaded = false;
    endif
  endfor

  ## Should we return something?
  if (nargout == 1)
    out1 = installed_pkgs_lst;
  elseif (nargout > 1)
    out1 = local_packages;
    out2 = global_packages;
  else
    ## Don't return anything, instead we'll print something.
    num_packages = numel (installed_pkgs_lst);
    if (num_packages == 0)
      if (isempty (pkgname))
        printf ("no packages installed.\n");
      else
        printf ("package %s is not installed.\n", pkgname{1});
      endif
      return;
    endif

    ## Compute the maximal lengths of name, version, and dir.
    h1 = "Package Name";
    h2 = "Version";
    h3 = "Installation directory";
    max_name_length = max ([length(h1), cellfun(@length, installed_names)]);
    version_lengths = cellfun (@(x) length (x.version), installed_pkgs_lst);
    max_version_length = max ([length(h2), version_lengths]);
    ncols = terminal_size ()(2);
    max_dir_length = ncols - max_name_length - max_version_length - 7;
    if (max_dir_length < 20)
      max_dir_length = Inf;
    endif

    h1 = postpad (h1, max_name_length + 1, " ");
    h2 = postpad (h2, max_version_length, " ");;

    ## Print a header.
    header = sprintf ("%s | %s | %s\n", h1, h2, h3);
    printf (header);
    tmp = sprintf (repmat ("-", 1, length (header) - 1));
    tmp(length (h1)+2) = "+";
    tmp(length (h1)+length (h2)+5) = "+";
    printf ("%s\n", tmp);

    ## Print the packages.
    format = sprintf ("%%%ds %%1s| %%%ds | %%s\n",
                      max_name_length, max_version_length);
    for i = 1:num_packages
      cur_name = installed_pkgs_lst{i}.name;
      cur_version = installed_pkgs_lst{i}.version;
      cur_dir = installed_pkgs_lst{i}.dir;
      if (length (cur_dir) > max_dir_length)
        first_char = length (cur_dir) - max_dir_length + 4;
        first_filesep = strfind (cur_dir(first_char:end), filesep ());
        if (! isempty (first_filesep))
          cur_dir = ["..." cur_dir((first_char + first_filesep(1) - 1):end)];
        else
          cur_dir = ["..." cur_dir(first_char:end)];
        endif
      endif
      if (installed_pkgs_lst{i}.loaded)
        cur_loaded = "*";
      else
        cur_loaded = " ";
      endif
      printf (format, cur_name, cur_loaded, cur_version, cur_dir);
    endfor
  endif

endfunction