annotate scripts/miscellaneous/unpack.m @ 11523:fd0a3ac60b0e

update copyright notices
author John W. Eaton <jwe@octave.org>
date Fri, 14 Jan 2011 05:47:45 -0500
parents 1740012184f9
children c792872f8942
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11523
fd0a3ac60b0e update copyright notices
John W. Eaton <jwe@octave.org>
parents: 11472
diff changeset
1 ## Copyright (C) 2006-2011 Bill Denney
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
2 ##
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
3 ## This file is part of Octave.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
4 ##
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6546
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6546
diff changeset
8 ## your option) any later version.
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
9 ##
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
13 ## General Public License for more details.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
14 ##
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6546
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6546
diff changeset
17 ## <http://www.gnu.org/licenses/>.
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
18
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
19 ## -*- texinfo -*-
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
20 ## @deftypefn {Function File} {@var{files} =} unpack (@var{file})
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
21 ## @deftypefnx {Function File} {@var{files} =} unpack (@var{file}, @var{dir})
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
22 ## @deftypefnx {Function File} {@var{files} =} unpack (@var{file}, @var{dir}, @var{filetype})
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
23 ## Unpack the archive @var{file} based on its extension to the directory
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
24 ## @var{dir}. If @var{file} is a cellstr, then all files will be
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
25 ## handled individually. If @var{dir} is not specified, it defaults to
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
26 ## the current directory. It returns a list of @var{files}
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
27 ## unpacked. If a directory is in the file list, then the
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
28 ## @var{filetype} to unpack must also be specified.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
29 ##
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
30 ## The @var{files} includes the entire path to the output files.
8297
dc62132651db doc fixes
John W. Eaton <jwe@octave.org>
parents: 7540
diff changeset
31 ## @seealso{bunzip2, tar, untar, gzip, gunzip, zip, unzip}
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
32 ## @end deftypefn
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
33
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
34 ## Author: Bill Denney <denney@seas.upenn.edu>
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
35
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
36 function filelist = unpack (file, dir = ".", filetype = "")
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
37
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
38 if (nargin < 1 || nargin > 3)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
39 print_usage ();
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
40 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
41
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
42 if (ischar (file))
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
43 if (isdir (file))
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
44 if (isempty (filetype))
11472
1740012184f9 Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents: 11469
diff changeset
45 error ("unpack: FILETYPE must be given for a directory");
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
46 elseif (! any (strcmpi (filetype, "gunzip")))
11472
1740012184f9 Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents: 11469
diff changeset
47 error ("unpack: FILETYPE must be gunzip for a directory");
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
48 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
49 else
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
50 [pathstr, name, ext] = fileparts (file);
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
51
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
52 ## Check to see if it's .tar.gz, .tar.Z, etc.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
53 if (any (strcmpi ({".gz" ".Z" ".bz2" ".bz"}, ext)))
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
54 [tmppathstr, tmpname, tmpext] = fileparts (name);
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
55 if (strcmpi (tmpext, ".tar"))
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
56 name = tmpname;
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
57 ext = cstrcat (tmpext, ext);
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
58 endif
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
59 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
60
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
61 ## If the file is a URL, download it and then work with that
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
62 ## file.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
63 if (! isempty (strfind (file, "://")))
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
64 ## FIXME -- the above is not a perfect test for a URL
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
65 urlfile = file;
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
66 ## FIXME -- should we name the file that we download with the
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
67 ## same file name as the URL requests?
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
68 tmpfile = cstrcat (tmpnam (), ext);
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
69 [file, success, msg] = urlwrite (urlfile, tmpfile);
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
70 if (! success)
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
71 error ("unpack: could not get \"%s\": %s", urlfile, msg);
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
72 endif
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
73 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
74
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
75 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
76
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
77 ## canonicalize_file_name returns empty if the file isn't found, so
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
78 ## use that to check for existence.
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
79 cfile = canonicalize_file_name (file);
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
80
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
81 if (isempty (cfile))
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
82 error ("unpack: file \"%s\" not found.", file);
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
83 else
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
84 file = cfile;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
85 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
86
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
87 elseif (iscellstr (file))
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
88 files = {};
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
89 for i = 1:numel (file)
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
90 tmpfiles = unpack (file{i}, dir);
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
91 files = {files{:} tmpfiles{:}};
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
92 endfor
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
93
8352
33337f1aca75 fix bug in cell string handling of unpack function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8297
diff changeset
94 ## Return output if requested.
33337f1aca75 fix bug in cell string handling of unpack function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8297
diff changeset
95 if (nargout > 0)
33337f1aca75 fix bug in cell string handling of unpack function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8297
diff changeset
96 filelist = files;
33337f1aca75 fix bug in cell string handling of unpack function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8297
diff changeset
97 endif
33337f1aca75 fix bug in cell string handling of unpack function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8297
diff changeset
98
33337f1aca75 fix bug in cell string handling of unpack function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8297
diff changeset
99 return
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
100 else
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
101 error ("unpack: invalid input file class, %s", class(file));
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
102 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
103
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
104 ## Instructions on what to do for any extension.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
105 ##
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
106 ## The field names are the file extension without periods.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
107 ## The first cell is what is executed to unpack an archive verbosely.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
108 ## The second cell is what is executed to unpack an archive quietly.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
109 ## The third cell is the function to execute on output to get the
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
110 ## files list.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
111 ## The fourth cell indicates if the files may need to be manually moved
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
112 ## (i.e. tar and unzip decompress into the current directory while
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
113 ## bzip2 and gzip decompress the file at its location).
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
114 persistent commandlist;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
115 if (isempty (commandlist))
6546
44a4f68fbd76 [project @ 2007-04-19 17:06:22 by jwe]
jwe
parents: 6112
diff changeset
116 commandlist.gz = {"gzip -d -v -r \"%s\"", ...
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
117 "gzip -d -r \"%s\"", ...
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
118 @__parse_gzip__, true};
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
119 commandlist.z = commandlist.gz;
6546
44a4f68fbd76 [project @ 2007-04-19 17:06:22 by jwe]
jwe
parents: 6112
diff changeset
120 commandlist.bz2 = {"bzip2 -d -v \"%s\"", ...
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
121 "bzip2 -d \"%s\"", ...
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
122 @__parse_bzip2__, true};
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
123 commandlist.bz = commandlist.bz2;
9170
81a755db4db4 pass xvf to tar instead of -x -v -f
Peter O'Gorman <pogma@thewrittenword.com>
parents: 9051
diff changeset
124 commandlist.tar = {"tar xvf \"%s\"", ...
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
125 "tar xf \"%s\"", ...
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
126 @__parse_tar__, false};
9170
81a755db4db4 pass xvf to tar instead of -x -v -f
Peter O'Gorman <pogma@thewrittenword.com>
parents: 9051
diff changeset
127 commandlist.targz = {"gzip -d -c \"%s\" | tar xvf -", ...
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
128 "gzip -d -c \"%s\" | tar xf -", ...
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
129 @__parse_tar__, false};
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
130 commandlist.tgz = commandlist.targz;
9170
81a755db4db4 pass xvf to tar instead of -x -v -f
Peter O'Gorman <pogma@thewrittenword.com>
parents: 9051
diff changeset
131 commandlist.tarbz2 = {"bzip2 -d -c \"%s\" | tar xvf -", ...
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
132 "bzip2 -d -c \"%s\" | tar xf -", ...
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
133 @__parse_tar__, false};
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
134 commandlist.tarbz = commandlist.tarbz2;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
135 commandlist.tbz2 = commandlist.tarbz2;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
136 commandlist.tbz = commandlist.tarbz2;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
137 commandlist.zip = {"unzip \"%s\"", ...
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
138 "unzip -q \"%s\"", ...
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
139 @__parse_zip__, false};
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
140 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
141
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
142 nodotext = ext(! ismember (ext, "."));
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
143
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
144 origdir = pwd ();
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
145
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
146 if (isfield (commandlist, nodotext))
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
147 [commandv, commandq, parser, move] = deal (commandlist.(nodotext){:});
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
148 cstartdir = canonicalize_file_name (origdir);
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
149 cenddir = canonicalize_file_name (dir);
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
150 needmove = move && ! strcmp (cstartdir, cenddir);
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
151 if (nargout > 0 || needmove)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
152 command = commandv;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
153 else
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
154 command = commandq;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
155 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
156 else
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
157 warning ("unpack:filetype", "unrecognised file type, %s", ext);
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
158 files = file;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
159 return;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
160 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
161
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
162 ## Create the directory if necessary.
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
163 s = stat (dir);
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
164 if (isempty (s))
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
165 [status, msg] = mkdir (dir);
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
166 if (! status)
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
167 error ("unpack: mkdir failed to create %s: %s", dir, msg);
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
168 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
169 elseif (! S_ISDIR (s.mode))
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
170 error ("unpack: %s: not a directory", dir);
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
171 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
172
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
173 unwind_protect
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
174 cd (dir);
7540
3422f39573b1 strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents: 7525
diff changeset
175 [status, output] = system (sprintf (cstrcat (command, " 2>&1"), file));
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
176 unwind_protect_cleanup
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
177 cd (origdir);
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
178 end_unwind_protect
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
179
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
180 if (status)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
181 error ("unpack: unarchiving program exited with status: %d\n%s",
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
182 status, output);
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
183 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
184
6084
38114c34b44a [project @ 2006-10-25 03:52:48 by jwe]
jwe
parents: 6083
diff changeset
185 if (nargout > 0 || needmove)
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
186 ## Trim the last cr if needed.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
187 ## FIXME -- will this need to change to a check for "\r\n" for windows?
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
188 if (output(length (output)) == "\n")
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
189 output(length (output)) = [];
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
190 endif
8877
2c8b2399247b implement strsplit; deprecate split
Jaroslav Hajek <highegg@gmail.com>
parents: 8352
diff changeset
191 files = parser (strsplit (output, "\n"))';
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
192
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
193 ## Move files if necessary
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
194 if (needmove)
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
195 [st, msg, msgid] = movefile (files, dir);
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
196 if (! st)
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
197 error ("unpack: unable to move files to \"%s\": %s",
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
198 dir, msg);
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
199 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
200
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
201 ## Fix the names for the files since they were moved.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
202 for i = 1:numel (files)
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
203 files{i} = strrep (files{i}, cstartdir, cenddir);
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
204 endfor
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
205 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
206
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
207 ## Return output if requested.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
208 if (nargout > 0)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
209 filelist = files;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
210 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
211 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
212
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
213 endfunction
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
214
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
215 function files = __parse_zip__ (output)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
216 ## Parse the output from zip and unzip.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
217
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
218 for i = 1:length (output)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
219 files{i} = output{i}(14:length(output{i}));
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
220 endfor
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
221 endfunction
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
222
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
223 function output = __parse_tar__ (output)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
224 ## This is a noop, but it makes things simpler for other cases.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
225 endfunction
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
226
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
227 function files = __parse_gzip__ (output)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
228 ## Parse the output from gzip and gunzip returning the files
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
229 ## commpressed (or decompressed).
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
230
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
231 files = {};
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
232 ## The middle ": " should indicate a good place to start looking for
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
233 ## the filename.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
234 for i = 1:length (output)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
235 colons = strfind (output{i}, ":");
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
236 if (isempty (colons))
6083
0295db941ae7 [project @ 2006-10-25 03:47:48 by jwe]
jwe
parents: 6082
diff changeset
237 warning ("unpack:parsing",
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
238 "Unable to parse line (gzip missing colon):\n%s", output{i});
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
239 else
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
240 midcolon = colons(ceil (length (colons)/2));
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
241 thisstr = output{i}(midcolon+2:length(output{i}));
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
242 idx = index (thisstr, "with") + 5;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
243 if (isempty (idx))
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
244 warning ("unpack:parsing",
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
245 "Unable to parse line (gzip missing with):\n%s", output{i});
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
246 else
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
247 files{i} = thisstr(idx:length (thisstr));
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
248 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
249 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
250 endfor
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
251 endfunction
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
252
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
253 function files = __parse_bzip2__ (output)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
254 ## Parse the output from bzip2 and bunzip2 returning the files
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
255 ## commpressed (or decompressed).
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
256
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
257 files = {};
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
258 for i = 1:length (output)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
259 ## the -5 is to remove the ".bz2:"
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
260 endoffilename = rindex (output{i}, ": ") - 5;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
261 if (isempty (endoffilename))
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
262 warning ("unpack:parsing", "Unable to parse line:\n%s", output{i});
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
263 else
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
264 files{i} = output{i}(3:endoffilename);
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
265 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
266 endfor
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
267 endfunction