Mercurial > octave
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 |
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 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
6082 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
6082 | 18 |
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 | 22 ## @deftypefnx {Function File} {@var{files} =} unpack (@var{file}, @var{dir}, @var{filetype}) |
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 | 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 | 31 ## @end deftypefn |
32 | |
33 ## Author: Bill Denney <denney@seas.upenn.edu> | |
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 | 36 |
37 if (nargin < 1 || nargin > 3) | |
38 print_usage (); | |
39 endif | |
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 | 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 | 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 | 56 files = {}; |
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 | 59 files = {files{:} tmpfiles{:}}; |
60 endfor | |
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 | 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 | 115 endif |
116 | |
117 ## Instructions on what to do for any extension. | |
118 ## | |
119 ## The field names are the file extension without periods. | |
120 ## The first cell is what is executed to unpack an archive verbosely. | |
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 | 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 | 126 persistent commandlist; |
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 | 130 @__parse_gzip__, true}; |
6082 | 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 | 134 @__parse_bzip2__, true}; |
6082 | 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 | 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 | 141 @__parse_tar__, false}; |
6082 | 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 | 145 @__parse_tar__, false}; |
6082 | 146 commandlist.tarbz = commandlist.tarbz2; |
147 commandlist.tbz2 = commandlist.tarbz2; | |
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 | 151 @__parse_zip__, false}; |
6082 | 152 endif |
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 | 161 origdir = pwd (); |
162 | |
163 if (isfield (commandlist, nodotext)) | |
164 [commandv, commandq, parser, move] = deal (commandlist.(nodotext){:}); | |
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 | 167 needmove = move && ! strcmp (cstartdir, cenddir); |
168 if (nargout > 0 || needmove) | |
169 command = commandv; | |
170 else | |
171 command = commandq; | |
172 endif | |
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 | 175 files = file; |
176 return; | |
177 endif | |
178 | |
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 | 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 | 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 | 185 endif |
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 | 188 endif |
189 | |
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 | 193 unwind_protect_cleanup |
194 cd (origdir); | |
195 end_unwind_protect | |
196 | |
197 if (status) | |
198 error ("unpack: unarchiving program exited with status: %d\n%s", | |
10549 | 199 status, output); |
6082 | 200 endif |
201 | |
6084 | 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 | 207 endif |
16724
b7667fcb9fbc
Substitute ostrsplit() for strsplit().
Ben Abbott <bpabbott@mac.com>
parents:
16403
diff
changeset
|
208 files = parser (ostrsplit (output, "\n"))'; |
6082 | 209 |
210 ## Move files if necessary | |
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 | 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 | 215 endif |
216 | |
217 ## Fix the names for the files since they were moved. | |
218 for i = 1:numel (files) | |
10549 | 219 files{i} = strrep (files{i}, cstartdir, cenddir); |
6082 | 220 endfor |
221 endif | |
222 | |
223 ## Return output if requested. | |
224 if (nargout > 0) | |
225 filelist = files; | |
226 endif | |
227 endif | |
228 | |
229 endfunction | |
230 | |
231 function files = __parse_zip__ (output) | |
232 ## Parse the output from zip and unzip. | |
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 | 238 endfunction |
239 | |
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 | 242 endfunction |
243 | |
244 function files = __parse_gzip__ (output) | |
245 ## Parse the output from gzip and gunzip returning the files | |
246 ## commpressed (or decompressed). | |
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 | 249 endfunction |
250 | |
251 function files = __parse_bzip2__ (output) | |
252 ## Parse the output from bzip2 and bunzip2 returning the files | |
253 ## commpressed (or decompressed). | |
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 |