]> Creatis software - CreaPhase.git/blob - octave_packages/m/pkg/private/get_forge_pkg.m
update packages
[CreaPhase.git] / octave_packages / m / pkg / private / get_forge_pkg.m
1 ## Copyright (C) 2010-2012 VZLU Prague, a.s.
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {[@var{ver}, @var{url}] =} get_forge_pkg (@var{name})
21 ## Try to discover the current version of an OctaveForge package from the web,
22 ## using a working internet connection and the urlread function.
23 ## If two output arguments are requested, also return an address from which
24 ## to download the file.
25 ## @end deftypefn
26
27 function [ver, url] = get_forge_pkg (name)
28   if (nargin != 1)
29     print_usage ();
30   endif
31   ## Verify that name is valid.
32   if (! (ischar (name) && rows (name) == 1 && ndims (name) == 2))
33     error ("get_forge_pkg: package NAME must be a string");
34   elseif (! all (isalnum (name) | name == "-" | name == "." | name == "_"))
35     error ("get_forge_pkg: invalid package name: %s", name);
36   endif
37
38   name = tolower (name);
39
40   ## Try to download package's index page.
41   [html, succ] = urlread (sprintf ("http://octave.sourceforge.net/%s/index.html", name));
42   if (succ)
43     ## Remove blanks for simpler matching.
44     html(isspace(html)) = [];
45     ## Good. Let's grep for the version.
46     pat = "<tdclass=""package_table"">PackageVersion:</td><td>([\\d.]*)</td>";
47     t = regexp (html, pat, "tokens");
48     if (isempty (t) || isempty(t{1}))
49       error ("get_forge_pkg: could not read version number from package's page");
50     else
51       ver = t{1}{1};
52       if (nargout > 1)
53         # Build download string.
54         urlbase = "http://downloads.sourceforge.net/octave/%s-%s.tar.gz?download";
55         url = sprintf (urlbase, name, ver);
56         ## Verify that the string exists on the page.
57         if (isempty (strfind (html, url)))
58           warning ("get_forge_pkg: download URL not verified");
59         endif
60       endif
61     endif
62   else
63     ## Try get the list of all packages.
64     [html, succ] = urlread ("http://octave.sourceforge.net/packages.php");
65     if (succ)
66       t = regexp (html, "<div class=""package"" id=""(\\w+)"">", "tokens");
67       t = horzcat (t{:});
68       if (any (strcmp (t, name)))
69         error ("get_forge_pkg: package NAME exists, but index page not available");
70       else
71         ## Try a simplistic method to determine close names.
72         dist = cellfun (@(n) length (setdiff (name, n)), t);
73         [~, i] = min (dist);
74         error ("get_forge_pkg: package not found: ""%s"". Maybe you meant ""%s?""", name, t{i});
75       endif
76     else
77       error ("get_forge_pkg: could not read URL, please verify internet connection");
78     endif
79   endif
80
81 endfunction