1######################################################################## 2## 3## Copyright (C) 2005-2021 The Octave Project Developers 4## 5## See the file COPYRIGHT.md in the top-level directory of this 6## distribution or <https://octave.org/copyright/>. 7## 8## This file is part of Octave. 9## 10## Octave is free software: you can redistribute it and/or modify it 11## under the terms of the GNU General Public License as published by 12## the Free Software Foundation, either version 3 of the License, or 13## (at your option) any later version. 14## 15## Octave is distributed in the hope that it will be useful, but 16## WITHOUT ANY WARRANTY; without even the implied warranty of 17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18## GNU General Public License for more details. 19## 20## You should have received a copy of the GNU General Public License 21## along with Octave; see the file COPYING. If not, see 22## <https://www.gnu.org/licenses/>. 23## 24######################################################################## 25 26## -*- texinfo -*- 27## @deftypefn {} {[@var{ver}, @var{url}] =} get_forge_pkg (@var{name}) 28## Try to discover the current version of an Octave Forge package from the web, 29## using a working internet connection and the urlread function. 30## If two output arguments are requested, also return an address from which 31## to download the file. 32## @end deftypefn 33 34function [ver, url] = get_forge_pkg (name) 35 36 ## Verify that name is valid. 37 if (! (ischar (name) && rows (name) == 1 && ndims (name) == 2)) 38 error ("get_forge_pkg: package NAME must be a string"); 39 elseif (! all (isalnum (name) | name == "-" | name == "." | name == "_")) 40 error ("get_forge_pkg: invalid package NAME: %s", name); 41 endif 42 43 name = tolower (name); 44 45 ## Try to download package's index page. 46 [html, succ] = urlread (sprintf ("https://packages.octave.org/%s/index.html", ... 47 name)); 48 if (succ) 49 ## Remove blanks for simpler matching. 50 html(isspace (html)) = []; 51 ## Good. Let's grep for the version. 52 pat = "<tdclass=""package_table"">PackageVersion:</td><td>([\\d.]*)</td>"; 53 t = regexp (html, pat, "tokens"); 54 if (isempty (t) || isempty (t{1})) 55 error ("get_forge_pkg: could not read version number from package's page"); 56 else 57 ver = t{1}{1}; 58 if (nargout > 1) 59 ## Build download string. 60 pkg_file = sprintf ("%s-%s.tar.gz", name, ver); 61 url = ["https://packages.octave.org/download/" pkg_file]; 62 ## Verify that the package string exists on the page. 63 if (isempty (strfind (html, pkg_file))) 64 warning ("get_forge_pkg: download URL not verified"); 65 endif 66 endif 67 endif 68 else 69 ## Try get the list of all packages. 70 [html, succ] = urlread ("https://packages.octave.org/list_packages.php"); 71 if (! succ) 72 error ("get_forge_pkg: could not read URL, please verify internet connection"); 73 endif 74 t = strsplit (html); 75 if (any (strcmp (t, name))) 76 error ("get_forge_pkg: package NAME exists, but index page not available"); 77 endif 78 ## Try a simplistic method to determine similar names. 79 function d = fdist (x) 80 len1 = length (name); 81 len2 = length (x); 82 if (len1 <= len2) 83 d = sum (abs (tolower (name(1:len1)) - tolower (x(1:len1)))) ... 84 + (len2 - len1)*23; 85 else 86 d = sum (abs (tolower (name(1:len2)) - tolower (x(1:len2)))) ... 87 + (len1 - len2)*23; 88 endif 89 endfunction 90 dist = cellfun ("fdist", t); 91 [~, i] = min (dist); 92 error ("get_forge_pkg: package not found: ""%s"". Did you mean ""%s""?", ... 93 name, t{i}); 94 endif 95 96endfunction 97