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