annotate scripts/miscellaneous/unpack.m @ 19233:0ded8964c13a

Added wildcard * support to archive-related routines (bug #43211). * unpack.m: added wildcard expansion via glob() for input filename.
author Massimiliano Fasi <massimiliano.fasi@gmail.com>
date Thu, 18 Sep 2014 11:05:52 +0200
parents 7bbe3658c5ef
children 6ebc2ca01c76
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17744
d63878346099 maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents: 17000
diff changeset
1 ## Copyright (C) 2006-2013 Bill Denney
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
2 ##
6082
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
12642
f96b9b9f141b doc: Periodic grammarcheck and spellcheck of documentation.
Rik <octave@nomad.inbox5.com>
parents: 12500
diff changeset
24 ## @var{dir}. If @var{file} is a list of strings, then each file is
12500
8f2056646dba Improve docstrings for archive functions (gzip, bzip2, etc.)
Rik <octave@nomad.inbox5.com>
parents: 12499
diff changeset
25 ## unpacked individually. If @var{dir} is not specified, it defaults to
8f2056646dba Improve docstrings for archive functions (gzip, bzip2, etc.)
Rik <octave@nomad.inbox5.com>
parents: 12499
diff changeset
26 ## the current directory. If a directory is in the file list, then the
8f2056646dba Improve docstrings for archive functions (gzip, bzip2, etc.)
Rik <octave@nomad.inbox5.com>
parents: 12499
diff changeset
27 ## @var{filetype} must also be specified.
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
28 ##
12642
f96b9b9f141b doc: Periodic grammarcheck and spellcheck of documentation.
Rik <octave@nomad.inbox5.com>
parents: 12500
diff changeset
29 ## The optional return value is a list of @var{files} unpacked.
12500
8f2056646dba Improve docstrings for archive functions (gzip, bzip2, etc.)
Rik <octave@nomad.inbox5.com>
parents: 12499
diff changeset
30 ## @seealso{bzip2, gzip, zip, tar}
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
31 ## @end deftypefn
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
32
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
33 ## Author: Bill Denney <denney@seas.upenn.edu>
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
34
19233
0ded8964c13a Added wildcard * support to archive-related routines (bug #43211).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents: 18857
diff changeset
35 function filelist = unpack (filename, dir = ".", filetype = "")
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
36
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
37 if (nargin < 1 || nargin > 3)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
38 print_usage ();
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
39 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
40
19233
0ded8964c13a Added wildcard * support to archive-related routines (bug #43211).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents: 18857
diff changeset
41 if (! ischar (filename) && ! iscellstr (filename))
0ded8964c13a Added wildcard * support to archive-related routines (bug #43211).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents: 18857
diff changeset
42 error ("unpack: invalid input file class, %s", class (filename));
12499
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
43 endif
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
44
12499
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
45 ## character arrays of more than one string must be treated as cell strings
19233
0ded8964c13a Added wildcard * support to archive-related routines (bug #43211).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents: 18857
diff changeset
46 if (ischar (filename) && ! isvector (filename))
0ded8964c13a Added wildcard * support to archive-related routines (bug #43211).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents: 18857
diff changeset
47 file = cellstr (filename);
0ded8964c13a Added wildcard * support to archive-related routines (bug #43211).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents: 18857
diff changeset
48 else
0ded8964c13a Added wildcard * support to archive-related routines (bug #43211).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents: 18857
diff changeset
49 file = glob (filename);
12499
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
50 endif
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
51
12499
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
52 ## Recursively unpack cellstr arrays one file at a time
19233
0ded8964c13a Added wildcard * support to archive-related routines (bug #43211).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents: 18857
diff changeset
53 n_elem = numel (file);
0ded8964c13a Added wildcard * support to archive-related routines (bug #43211).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents: 18857
diff changeset
54 if (n_elem > 1)
0ded8964c13a Added wildcard * support to archive-related routines (bug #43211).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents: 18857
diff changeset
55
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
56 files = {};
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
57 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
58 tmpfiles = unpack (file{i}, dir);
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
59 files = {files{:} tmpfiles{:}};
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
60 endfor
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
61
8352
33337f1aca75 fix bug in cell string handling of unpack function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8297
diff changeset
62 ## Return output if requested.
33337f1aca75 fix bug in cell string handling of unpack function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8297
diff changeset
63 if (nargout > 0)
33337f1aca75 fix bug in cell string handling of unpack function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8297
diff changeset
64 filelist = files;
33337f1aca75 fix bug in cell string handling of unpack function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8297
diff changeset
65 endif
33337f1aca75 fix bug in cell string handling of unpack function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8297
diff changeset
66
12499
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
67 return;
19233
0ded8964c13a Added wildcard * support to archive-related routines (bug #43211).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents: 18857
diff changeset
68
0ded8964c13a Added wildcard * support to archive-related routines (bug #43211).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents: 18857
diff changeset
69 else
0ded8964c13a Added wildcard * support to archive-related routines (bug #43211).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents: 18857
diff changeset
70 file = filename;
12499
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
71 endif
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
72
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
73 if (isdir (file))
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
74 if (isempty (filetype))
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
75 error ("unpack: FILETYPE must be given for a directory");
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
76 elseif (! any (strcmpi (filetype, "gunzip")))
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
77 error ("unpack: FILETYPE must be gunzip for a directory");
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
78 endif
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
79 ext = ".gz";
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
80 else
12499
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
81 [pathstr, name, ext] = fileparts (file);
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
82
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
83 ## Check to see if it's .tar.gz, .tar.Z, etc.
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
84 if (any (strcmpi ({".gz" ".Z" ".bz2" ".bz"}, ext)))
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
85 [~, tmpname, tmpext] = fileparts (name);
12499
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
86 if (strcmpi (tmpext, ".tar"))
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
87 name = tmpname;
16994
333243133364 Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents: 16724
diff changeset
88 ext = [tmpext ext];
12499
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
89 endif
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
90 endif
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
91
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
92 ## If the file is a URL, download it and then work with that file.
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
93 if (! isempty (strfind (file, "://")))
18857
7bbe3658c5ef maint: Use "FIXME:" coding convention in m-files.
Rik <rik@octave.org>
parents: 17744
diff changeset
94 ## FIXME: The above is not a perfect test for a URL
12499
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
95 urlfile = file;
18857
7bbe3658c5ef maint: Use "FIXME:" coding convention in m-files.
Rik <rik@octave.org>
parents: 17744
diff changeset
96 ## FIXME: Should we name the file that we download with the
7bbe3658c5ef maint: Use "FIXME:" coding convention in m-files.
Rik <rik@octave.org>
parents: 17744
diff changeset
97 ## same file name as the URL requests?
16994
333243133364 Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents: 16724
diff changeset
98 tmpfile = [tmpnam() ext];
12499
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
99 [file, success, msg] = urlwrite (urlfile, tmpfile);
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
100 if (! success)
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
101 error ('unpack: could not get "%s": %s', urlfile, msg);
12499
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
102 endif
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
103 endif
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
104
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
105 endif
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
106
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
107 ## canonicalize_file_name returns empty if the file isn't found, so
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
108 ## use that to check for existence.
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
109 cfile = canonicalize_file_name (file);
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
110
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
111 if (isempty (cfile))
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
112 error ('unpack: file "%s" not found', file);
12499
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
113 else
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
114 file = cfile;
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
115 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
116
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
117 ## Instructions on what to do for any extension.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
118 ##
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
119 ## The field names are the file extension without periods.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
120 ## 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
121 ## The second cell is what is executed to unpack an archive quietly.
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
122 ## The third cell is the function to execute on output to get the files list.
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
123 ## The fourth cell indicates if the files may need to be manually moved
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
124 ## (i.e., tar and unzip decompress into the current directory while
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
125 ## bzip2 and gzip decompress the file at its location).
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
126 persistent commandlist;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
127 if (isempty (commandlist))
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
128 commandlist.gz = {'gzip -d -v -r "%s"', ...
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
129 'gzip -d -r "%s"', ...
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
130 @__parse_gzip__, true};
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
131 commandlist.z = commandlist.gz;
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
132 commandlist.bz2 = {'bzip2 -d -v "%s"', ...
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
133 'bzip2 -d "%s"', ...
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
134 @__parse_bzip2__, true};
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
135 commandlist.bz = commandlist.bz2;
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
136 commandlist.tar = {'tar xvf "%s"', ...
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
137 'tar xf "%s"', ...
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
138 @__parse_tar__, false};
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
139 commandlist.targz = {'gzip -d -c "%s" | tar xvf -', ...
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
140 'gzip -d -c "%s" | tar xf -', ...
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
141 @__parse_tar__, false};
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
142 commandlist.tgz = commandlist.targz;
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
143 commandlist.tarbz2 = {'bzip2 -d -c "%s" | tar xvf -', ...
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
144 'bzip2 -d -c "%s" | tar xf -', ...
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
145 @__parse_tar__, false};
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
146 commandlist.tarbz = commandlist.tarbz2;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
147 commandlist.tbz2 = commandlist.tarbz2;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
148 commandlist.tbz = commandlist.tarbz2;
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
149 commandlist.zip = {'unzip -n "%s"', ...
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
150 'unzip -nq "%s"', ...
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
151 @__parse_zip__, false};
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
152 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
153
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
154 ## Unzip doesn't actually care about the extension
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
155 if (strcmp (filetype, "unzip"))
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
156 nodotext = "zip";
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
157 else
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
158 nodotext = ext(ext != '.');
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
159 endif
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
160
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
161 origdir = pwd ();
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
162
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
163 if (isfield (commandlist, nodotext))
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
164 [commandv, commandq, parser, move] = deal (commandlist.(nodotext){:});
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
165 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
166 cenddir = canonicalize_file_name (dir);
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
167 needmove = move && ! strcmp (cstartdir, cenddir);
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
168 if (nargout > 0 || needmove)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
169 command = commandv;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
170 else
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
171 command = commandq;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
172 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
173 else
12499
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
174 warning ("unpack:filetype", "unrecognized file type, %s", ext);
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
175 files = file;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
176 return;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
177 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
178
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
179 ## 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
180 s = stat (dir);
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
181 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
182 [status, msg] = mkdir (dir);
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
183 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
184 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
185 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
186 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
187 error ("unpack: %s: not a directory", dir);
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
188 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
189
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
190 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
191 cd (dir);
16994
333243133364 Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents: 16724
diff changeset
192 [status, output] = system (sprintf ([command " 2>&1"], file));
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
193 unwind_protect_cleanup
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
194 cd (origdir);
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
195 end_unwind_protect
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
196
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
197 if (status)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
198 error ("unpack: unarchiving program exited with status: %d\n%s",
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
199 status, output);
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
200 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
201
6084
38114c34b44a [project @ 2006-10-25 03:52:48 by jwe]
jwe
parents: 6083
diff changeset
202 if (nargout > 0 || needmove)
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
203 ## Trim the last CR if needed.
18857
7bbe3658c5ef maint: Use "FIXME:" coding convention in m-files.
Rik <rik@octave.org>
parents: 17744
diff changeset
204 ## FIXME: Will this need to change to a check for "\r\n" for windows?
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
205 if (output(end) == "\n")
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
206 output(end) = [];
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
207 endif
16724
b7667fcb9fbc Substitute ostrsplit() for strsplit().
Ben Abbott <bpabbott@mac.com>
parents: 16403
diff changeset
208 files = parser (ostrsplit (output, "\n"))';
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
209
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
210 ## Move files if necessary
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
211 if (needmove)
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
212 [st, msg, ~] = movefile (files, dir);
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
213 if (! st)
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
214 error ('unpack: unable to move files to "%s": %s', dir, msg);
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
215 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
216
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
217 ## Fix the names for the files since they were moved.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
218 for i = 1:numel (files)
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
219 files{i} = strrep (files{i}, cstartdir, cenddir);
6082
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 endif
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 ## Return output if requested.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
224 if (nargout > 0)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
225 filelist = files;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
226 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
227 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
228
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
229 endfunction
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 function files = __parse_zip__ (output)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
232 ## Parse the output from zip and unzip.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
233
12499
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
234 ## Skip first line which is Archive header
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
235 files = char (output(2:end));
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
236 ## Trim constant width prefix and return cell array
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
237 files = cellstr (files(:,14:end))
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
238 endfunction
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
239
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
240 function output = __parse_tar__ (output)
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
241 ## This is a no-op, but it makes things simpler for other cases.
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
242 endfunction
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
243
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
244 function files = __parse_gzip__ (output)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
245 ## Parse the output from gzip and gunzip returning the files
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
246 ## commpressed (or decompressed).
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
247
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
248 files = regexprep (output, '^.+ with (.*)$', '$1');
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
249 endfunction
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
250
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
251 function files = __parse_bzip2__ (output)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
252 ## Parse the output from bzip2 and bunzip2 returning the files
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
253 ## commpressed (or decompressed).
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
254
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
255 ## Strip leading blanks and .bz2 extension from file name
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
256 files = regexprep (output, '^\s+(.*)\.bz2: .*', '$1');
19233
0ded8964c13a Added wildcard * support to archive-related routines (bug #43211).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents: 18857
diff changeset
257 endfunction