Mercurial > octave
annotate scripts/miscellaneous/unpack.m @ 22511:1e81abd0314a
tempname: respect the value of $TMPDIR if set, be consistent with tempdir
* file-io.cc (Ftempname): Default to creating files in $TMPDIR when called
without a directory argument.
* unpack.m: Update BIST test to unpack files in tempdir, which may differ from
P_tmpdir.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Fri, 16 Sep 2016 15:36:26 -0700 |
parents | 119f408f2dd1 |
children | 3a2b891d0b33 e9a0469dedd9 |
rev | line source |
---|---|
22323
bac0d6f07a3e
maint: Update copyright notices for 2016.
John W. Eaton <jwe@octave.org>
parents:
22270
diff
changeset
|
1 ## Copyright (C) 2006-2016 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 -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20755
diff
changeset
|
20 ## @deftypefn {} {@var{files} =} unpack (@var{file}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20755
diff
changeset
|
21 ## @deftypefnx {} {@var{files} =} unpack (@var{file}, @var{dir}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20755
diff
changeset
|
22 ## @deftypefnx {} {@var{files} =} unpack (@var{file}, @var{dir}, @var{filetype}) |
6082 | 23 ## Unpack the archive @var{file} based on its extension to the directory |
19234 | 24 ## @var{dir}. |
25 ## | |
26 ## If @var{file} is a list of strings, then each file is unpacked | |
20164
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19951
diff
changeset
|
27 ## individually. Shell wildcards in the filename such as @samp{*} or |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19951
diff
changeset
|
28 ## @samp{?} are accepted and expanded. |
19234 | 29 ## |
30 ## If @var{dir} is not specified or is empty (@code{[]}), it defaults to the | |
31 ## current directory. If a directory is in the file list, then @var{filetype} | |
32 ## must also be specified. | |
33 ## | |
34 ## The specific archive filetype is inferred from the extension of the file. | |
35 ## The @var{filetype} may also be specified directly using a string which | |
36 ## corresponds to a known extension. | |
37 ## | |
38 ## Valid filetype extensions: | |
39 ## | |
40 ## @table @code | |
41 ## @item bz | |
42 ## @itemx bz2 | |
43 ## bzip archive | |
44 ## | |
45 ## @item gz | |
46 ## gzip archive | |
47 ## | |
48 ## @item tar | |
49 ## tar archive | |
50 ## | |
51 ## @item tarbz | |
19235
cac9d4c49522
unpack.m: Fix Texinfo error in docstring.
Rik <rik@octave.org>
parents:
19234
diff
changeset
|
52 ## @itemx tarbz2 |
19234 | 53 ## @itemx tbz |
54 ## @itemx tbz2 | |
55 ## tar + bzip archive | |
56 ## | |
57 ## @item targz | |
58 ## @itemx tgz | |
59 ## tar + gzip archive | |
60 ## | |
61 ## @item z | |
62 ## compress archive | |
63 ## | |
64 ## @item zip | |
65 ## zip archive | |
66 ## @end table | |
6082 | 67 ## |
12642
f96b9b9f141b
doc: Periodic grammarcheck and spellcheck of documentation.
Rik <octave@nomad.inbox5.com>
parents:
12500
diff
changeset
|
68 ## The optional return value is a list of @var{files} unpacked. |
19243 | 69 ## @seealso{bunzip2, gunzip, unzip, untar, bzip2, gzip, zip, tar} |
6082 | 70 ## @end deftypefn |
71 | |
72 ## Author: Bill Denney <denney@seas.upenn.edu> | |
73 | |
19234 | 74 function filelist = unpack (file, dir = ".", filetype = "") |
6082 | 75 |
76 if (nargin < 1 || nargin > 3) | |
77 print_usage (); | |
78 endif | |
79 | |
19234 | 80 if (! ischar (file) && ! iscellstr (file)) |
81 error ("unpack: FILE must be a string or cell array of strings"); | |
12499
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
82 endif |
6082 | 83 |
19234 | 84 ## Convert char arrays to cell strings to simplify further processing |
85 if (ischar (file)) | |
86 file = cellstr (file); | |
87 endif | |
88 if (numel (file) == 1) | |
20748
bb219f1d92db
Fix indexing error in cset 69aeef6736df.
Rik <rik@octave.org>
parents:
20746
diff
changeset
|
89 if (isempty (strfind (file{1}, "://"))) |
20746
69aeef6736df
unpack.m: Restore ability for input to be a URL (bug #46504).
Rik <rik@octave.org>
parents:
20713
diff
changeset
|
90 ## FIXME: The above code is not a perfect test for a URL |
69aeef6736df
unpack.m: Restore ability for input to be a URL (bug #46504).
Rik <rik@octave.org>
parents:
20713
diff
changeset
|
91 gfile = glob (file); |
69aeef6736df
unpack.m: Restore ability for input to be a URL (bug #46504).
Rik <rik@octave.org>
parents:
20713
diff
changeset
|
92 if (isempty (gfile)) |
69aeef6736df
unpack.m: Restore ability for input to be a URL (bug #46504).
Rik <rik@octave.org>
parents:
20713
diff
changeset
|
93 error ('unpack: file "%s" not found', file{1}); |
69aeef6736df
unpack.m: Restore ability for input to be a URL (bug #46504).
Rik <rik@octave.org>
parents:
20713
diff
changeset
|
94 else |
69aeef6736df
unpack.m: Restore ability for input to be a URL (bug #46504).
Rik <rik@octave.org>
parents:
20713
diff
changeset
|
95 file = gfile; |
69aeef6736df
unpack.m: Restore ability for input to be a URL (bug #46504).
Rik <rik@octave.org>
parents:
20713
diff
changeset
|
96 endif |
19234 | 97 endif |
12499
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
98 endif |
6082 | 99 |
12499
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
100 ## Recursively unpack cellstr arrays one file at a time |
19234 | 101 if (numel (file) > 1) |
6082 | 102 files = {}; |
103 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
|
104 tmpfiles = unpack (file{i}, dir); |
6082 | 105 files = {files{:} tmpfiles{:}}; |
106 endfor | |
107 | |
8352
33337f1aca75
fix bug in cell string handling of unpack function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8297
diff
changeset
|
108 ## Return output if requested. |
33337f1aca75
fix bug in cell string handling of unpack function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8297
diff
changeset
|
109 if (nargout > 0) |
33337f1aca75
fix bug in cell string handling of unpack function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8297
diff
changeset
|
110 filelist = files; |
33337f1aca75
fix bug in cell string handling of unpack function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8297
diff
changeset
|
111 endif |
19234 | 112 return; |
8352
33337f1aca75
fix bug in cell string handling of unpack function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8297
diff
changeset
|
113 |
19233
0ded8964c13a
Added wildcard * support to archive-related routines (bug #43211).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents:
18857
diff
changeset
|
114 else |
19234 | 115 file = file{1}; |
12499
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
116 endif |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
117 |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
118 if (isdir (file)) |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
119 if (isempty (filetype)) |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
120 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
|
121 elseif (! any (strcmpi (filetype, "gunzip"))) |
19234 | 122 error ('unpack: FILETYPE must be "gunzip" for a directory'); |
12499
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
123 endif |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
124 ext = ".gz"; |
6082 | 125 else |
12499
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
126 [pathstr, name, ext] = fileparts (file); |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
127 |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
128 ## 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
|
129 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
|
130 [~, tmpname, tmpext] = fileparts (name); |
12499
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
131 if (strcmpi (tmpext, ".tar")) |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
132 name = tmpname; |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16724
diff
changeset
|
133 ext = [tmpext ext]; |
12499
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
134 endif |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
135 endif |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
136 |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
137 ## 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
|
138 if (! isempty (strfind (file, "://"))) |
19234 | 139 ## FIXME: The above code 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
|
140 urlfile = file; |
20755
46b15c018fa5
unpack.m: Fix handling of gzip archives expanded into /tmp (bug #40274).
Rik <rik@octave.org>
parents:
20748
diff
changeset
|
141 tmpfile = fullfile (tempdir (), [name ext]); |
12499
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
142 [file, success, msg] = urlwrite (urlfile, tmpfile); |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
143 if (! success) |
19234 | 144 error ('unpack: could not fetch "%s": %s', urlfile, msg); |
12499
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
145 endif |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
146 endif |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
147 |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
148 endif |
a3019189ac51
Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents:
11588
diff
changeset
|
149 |
19579
0cc52d752f99
unpack.m: replace canonicalize_file_name by make_absolute_filename (bug #43979)
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19278
diff
changeset
|
150 file = make_absolute_filename (file); |
6082 | 151 |
20291
2fc43288a6c4
unpack.m: Stop hang when unpacking on to existing filename (bug #45331).
Rik <rik@octave.org>
parents:
20164
diff
changeset
|
152 if (isempty (dir)) |
2fc43288a6c4
unpack.m: Stop hang when unpacking on to existing filename (bug #45331).
Rik <rik@octave.org>
parents:
20164
diff
changeset
|
153 dir = "."; |
2fc43288a6c4
unpack.m: Stop hang when unpacking on to existing filename (bug #45331).
Rik <rik@octave.org>
parents:
20164
diff
changeset
|
154 else |
2fc43288a6c4
unpack.m: Stop hang when unpacking on to existing filename (bug #45331).
Rik <rik@octave.org>
parents:
20164
diff
changeset
|
155 dir = tilde_expand (dir); |
2fc43288a6c4
unpack.m: Stop hang when unpacking on to existing filename (bug #45331).
Rik <rik@octave.org>
parents:
20164
diff
changeset
|
156 endif |
2fc43288a6c4
unpack.m: Stop hang when unpacking on to existing filename (bug #45331).
Rik <rik@octave.org>
parents:
20164
diff
changeset
|
157 |
6082 | 158 ## Instructions on what to do for any extension. |
159 ## | |
160 ## The field names are the file extension without periods. | |
161 ## The first cell is what is executed to unpack an archive verbosely. | |
162 ## 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
|
163 ## The third cell is the function to execute on output to get the files list. |
6082 | 164 ## 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
|
165 ## (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
|
166 ## bzip2 and gzip decompress the file at its location). |
6082 | 167 persistent commandlist; |
168 if (isempty (commandlist)) | |
20291
2fc43288a6c4
unpack.m: Stop hang when unpacking on to existing filename (bug #45331).
Rik <rik@octave.org>
parents:
20164
diff
changeset
|
169 commandlist.gz = {'gzip -d -v -f -r "%s"', ... |
2fc43288a6c4
unpack.m: Stop hang when unpacking on to existing filename (bug #45331).
Rik <rik@octave.org>
parents:
20164
diff
changeset
|
170 'gzip -d -f -r "%s"', ... |
10549 | 171 @__parse_gzip__, true}; |
6082 | 172 commandlist.z = commandlist.gz; |
20291
2fc43288a6c4
unpack.m: Stop hang when unpacking on to existing filename (bug #45331).
Rik <rik@octave.org>
parents:
20164
diff
changeset
|
173 commandlist.bz2 = {'bzip2 -d -v -f "%s"', ... |
2fc43288a6c4
unpack.m: Stop hang when unpacking on to existing filename (bug #45331).
Rik <rik@octave.org>
parents:
20164
diff
changeset
|
174 'bzip2 -d -f "%s"', ... |
10549 | 175 @__parse_bzip2__, true}; |
6082 | 176 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
|
177 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
|
178 'tar xf "%s"', ... |
10549 | 179 @__parse_tar__, false}; |
17000
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
180 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
|
181 'gzip -d -c "%s" | tar xf -', ... |
10549 | 182 @__parse_tar__, false}; |
6082 | 183 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
|
184 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
|
185 'bzip2 -d -c "%s" | tar xf -', ... |
10549 | 186 @__parse_tar__, false}; |
6082 | 187 commandlist.tarbz = commandlist.tarbz2; |
188 commandlist.tbz2 = commandlist.tarbz2; | |
189 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
|
190 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
|
191 'unzip -nq "%s"', ... |
10549 | 192 @__parse_zip__, false}; |
6082 | 193 endif |
194 | |
17000
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
195 ## Unzip doesn't actually care about the extension |
19234 | 196 if (strcmpi (filetype, "unzip")) |
17000
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
197 nodotext = "zip"; |
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
198 else |
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
199 nodotext = ext(ext != '.'); |
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
200 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
201 |
19951
8c20fb6caa16
Fix XFAIL with tar and unpack tests (bug #44007, bug #43979).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19927
diff
changeset
|
202 if (ispc && strcmp (nodotext, "tar")) |
8c20fb6caa16
Fix XFAIL with tar and unpack tests (bug #44007, bug #43979).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19927
diff
changeset
|
203 ## Change file pathname into a mingw style acceptable for tar |
8c20fb6caa16
Fix XFAIL with tar and unpack tests (bug #44007, bug #43979).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19927
diff
changeset
|
204 file = __w2mpth__ (file); |
8c20fb6caa16
Fix XFAIL with tar and unpack tests (bug #44007, bug #43979).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19927
diff
changeset
|
205 endif |
8c20fb6caa16
Fix XFAIL with tar and unpack tests (bug #44007, bug #43979).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19927
diff
changeset
|
206 |
19234 | 207 if (isfield (commandlist, tolower (nodotext))) |
208 [commandv, commandq, parsefcn, move] = deal (commandlist.(nodotext){:}); | |
209 origdir = pwd (); | |
210 if (move) | |
19597
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19579
diff
changeset
|
211 startdir = fileparts (file); |
19234 | 212 else |
213 startdir = origdir; | |
214 endif | |
19951
8c20fb6caa16
Fix XFAIL with tar and unpack tests (bug #44007, bug #43979).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19927
diff
changeset
|
215 cstartdir = make_absolute_filename (startdir); |
8c20fb6caa16
Fix XFAIL with tar and unpack tests (bug #44007, bug #43979).
Philip Nienhuis <prnienhuis@users.sf.net>
parents:
19927
diff
changeset
|
216 cenddir = make_absolute_filename (dir); |
20291
2fc43288a6c4
unpack.m: Stop hang when unpacking on to existing filename (bug #45331).
Rik <rik@octave.org>
parents:
20164
diff
changeset
|
217 if (cenddir(end) == filesep) |
2fc43288a6c4
unpack.m: Stop hang when unpacking on to existing filename (bug #45331).
Rik <rik@octave.org>
parents:
20164
diff
changeset
|
218 cenddir(end) = []; |
2fc43288a6c4
unpack.m: Stop hang when unpacking on to existing filename (bug #45331).
Rik <rik@octave.org>
parents:
20164
diff
changeset
|
219 endif |
6082 | 220 needmove = move && ! strcmp (cstartdir, cenddir); |
221 if (nargout > 0 || needmove) | |
222 command = commandv; | |
223 else | |
224 command = commandq; | |
225 endif | |
226 else | |
19234 | 227 warning ("unpack: unrecognized FILETYPE <%s>", ext); |
6082 | 228 files = file; |
229 return; | |
230 endif | |
231 | |
232 ## 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
|
233 s = stat (dir); |
6082 | 234 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
|
235 [status, msg] = mkdir (dir); |
6082 | 236 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
|
237 error ("unpack: mkdir failed to create %s: %s", dir, msg); |
6082 | 238 endif |
239 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
|
240 error ("unpack: %s: not a directory", dir); |
6082 | 241 endif |
242 | |
243 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
|
244 cd (dir); |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
16724
diff
changeset
|
245 [status, output] = system (sprintf ([command " 2>&1"], file)); |
6082 | 246 unwind_protect_cleanup |
247 cd (origdir); | |
248 end_unwind_protect | |
249 | |
250 if (status) | |
251 error ("unpack: unarchiving program exited with status: %d\n%s", | |
10549 | 252 status, output); |
6082 | 253 endif |
254 | |
6084 | 255 if (nargout > 0 || needmove) |
19927
0cc365dada52
Allow tar, unpack, and zip to run on Windows with CR/NL EOL.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
256 ## Trim the last CR or NL if needed. |
0cc365dada52
Allow tar, unpack, and zip to run on Windows with CR/NL EOL.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
257 files = parsefcn (ostrsplit (output, "\r\n", true))'; |
6082 | 258 |
19234 | 259 ## Move files if necessary. |
6082 | 260 if (needmove) |
19234 | 261 [st, msg] = movefile (files, cenddir); |
6082 | 262 if (! st) |
17000
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
263 error ('unpack: unable to move files to "%s": %s', dir, msg); |
6082 | 264 endif |
265 | |
19234 | 266 ## Fix the names of the files since they were moved. |
267 files = strrep (files, cstartdir, cenddir); | |
6082 | 268 endif |
269 | |
270 ## Return output if requested. | |
271 if (nargout > 0) | |
272 filelist = files; | |
273 endif | |
274 endif | |
275 | |
276 endfunction | |
277 | |
278 function files = __parse_zip__ (output) | |
279 ## Parse the output from zip and unzip. | |
280 | |
19234 | 281 ## 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
|
282 files = char (output(2:end)); |
19234 | 283 ## Trim constant width prefix and return cell array. |
21385
89fa0694aa2e
Fix check-missing-semicolon QA target (bug #47277).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
284 files = cellstr (files(:,14:end)); |
6082 | 285 endfunction |
286 | |
287 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
|
288 ## This is a no-op, but it makes things simpler for other cases. |
6082 | 289 endfunction |
290 | |
291 function files = __parse_gzip__ (output) | |
292 ## Parse the output from gzip and gunzip returning the files | |
21758
ffad2baa90f7
maint: Use newlines to make code more readable.
Rik <rik@octave.org>
parents:
21571
diff
changeset
|
293 ## compressed (or decompressed). |
6082 | 294 |
21571
feac06371be1
unpack.m: Use more specific regexp to prevent accidental hits.
Rik <rik@octave.org>
parents:
21385
diff
changeset
|
295 files = regexprep (output, '^.+ -- replaced with (.*)$', '$1'); |
6082 | 296 endfunction |
297 | |
298 function files = __parse_bzip2__ (output) | |
299 ## Parse the output from bzip2 and bunzip2 returning the files | |
21758
ffad2baa90f7
maint: Use newlines to make code more readable.
Rik <rik@octave.org>
parents:
21571
diff
changeset
|
300 ## compressed (or decompressed). |
6082 | 301 |
20713
2469d78a1d8b
Consistently use 'filename' rather than 'file name' throughout code base.
Rik <rik@octave.org>
parents:
20291
diff
changeset
|
302 ## Strip leading blanks and .bz2 extension from filename |
17000
45165d6c4738
unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
303 files = regexprep (output, '^\s+(.*)\.bz2: .*', '$1'); |
19234 | 304 endfunction |
305 | |
306 | |
22270
2c7acd39e9b7
Conditionally disable tests that rely on optional features
Mike Miller <mtmiller@octave.org>
parents:
21758
diff
changeset
|
307 %!testif HAVE_ZLIB |
19259
f8552bd35180
unpack.m: Rewrite BIST test to not produce warning on stderr.
Rik <rik@octave.org>
parents:
19243
diff
changeset
|
308 %! ## Create temporary directory and file for packing and unpacking |
f8552bd35180
unpack.m: Rewrite BIST test to not produce warning on stderr.
Rik <rik@octave.org>
parents:
19243
diff
changeset
|
309 %! dirname = tempname (); |
f8552bd35180
unpack.m: Rewrite BIST test to not produce warning on stderr.
Rik <rik@octave.org>
parents:
19243
diff
changeset
|
310 %! assert (mkdir (dirname)); |
f8552bd35180
unpack.m: Rewrite BIST test to not produce warning on stderr.
Rik <rik@octave.org>
parents:
19243
diff
changeset
|
311 %! filename = tempname (); |
f8552bd35180
unpack.m: Rewrite BIST test to not produce warning on stderr.
Rik <rik@octave.org>
parents:
19243
diff
changeset
|
312 %! fid = fopen (filename, "wt"); |
f8552bd35180
unpack.m: Rewrite BIST test to not produce warning on stderr.
Rik <rik@octave.org>
parents:
19243
diff
changeset
|
313 %! assert (fid >= 0); |
19234 | 314 %! fprintf (fid, "Hello World\n"); |
315 %! fprintf (fid, "123 456 789\n"); | |
316 %! fclose (fid); | |
317 %! unwind_protect | |
19259
f8552bd35180
unpack.m: Rewrite BIST test to not produce warning on stderr.
Rik <rik@octave.org>
parents:
19243
diff
changeset
|
318 %! copyfile (filename, [filename ".orig"]); |
f8552bd35180
unpack.m: Rewrite BIST test to not produce warning on stderr.
Rik <rik@octave.org>
parents:
19243
diff
changeset
|
319 %! gzip (filename, dirname); |
f8552bd35180
unpack.m: Rewrite BIST test to not produce warning on stderr.
Rik <rik@octave.org>
parents:
19243
diff
changeset
|
320 %! [~, f] = fileparts (filename); |
22511
1e81abd0314a
tempname: respect the value of $TMPDIR if set, be consistent with tempdir
Mike Miller <mtmiller@octave.org>
parents:
22365
diff
changeset
|
321 %! filelist = unpack (fullfile (dirname, [f ".gz"]), tempdir); |
19597
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19579
diff
changeset
|
322 %! assert (filelist{1}, filename); |
19259
f8552bd35180
unpack.m: Rewrite BIST test to not produce warning on stderr.
Rik <rik@octave.org>
parents:
19243
diff
changeset
|
323 %! fid = fopen ([filename ".orig"], "rb"); |
f8552bd35180
unpack.m: Rewrite BIST test to not produce warning on stderr.
Rik <rik@octave.org>
parents:
19243
diff
changeset
|
324 %! assert (fid >= 0); |
f8552bd35180
unpack.m: Rewrite BIST test to not produce warning on stderr.
Rik <rik@octave.org>
parents:
19243
diff
changeset
|
325 %! orig_data = fread (fid); |
f8552bd35180
unpack.m: Rewrite BIST test to not produce warning on stderr.
Rik <rik@octave.org>
parents:
19243
diff
changeset
|
326 %! fclose (fid); |
f8552bd35180
unpack.m: Rewrite BIST test to not produce warning on stderr.
Rik <rik@octave.org>
parents:
19243
diff
changeset
|
327 %! fid = fopen (filename, "rb"); |
f8552bd35180
unpack.m: Rewrite BIST test to not produce warning on stderr.
Rik <rik@octave.org>
parents:
19243
diff
changeset
|
328 %! assert (fid >= 0); |
f8552bd35180
unpack.m: Rewrite BIST test to not produce warning on stderr.
Rik <rik@octave.org>
parents:
19243
diff
changeset
|
329 %! new_data = fread (fid); |
f8552bd35180
unpack.m: Rewrite BIST test to not produce warning on stderr.
Rik <rik@octave.org>
parents:
19243
diff
changeset
|
330 %! fclose (fid); |
f8552bd35180
unpack.m: Rewrite BIST test to not produce warning on stderr.
Rik <rik@octave.org>
parents:
19243
diff
changeset
|
331 %! if (orig_data != new_data) |
19234 | 332 %! error ("unpack: Unpacked file does not equal original"); |
333 %! endif | |
334 %! unwind_protect_cleanup | |
19259
f8552bd35180
unpack.m: Rewrite BIST test to not produce warning on stderr.
Rik <rik@octave.org>
parents:
19243
diff
changeset
|
335 %! unlink (filename); |
f8552bd35180
unpack.m: Rewrite BIST test to not produce warning on stderr.
Rik <rik@octave.org>
parents:
19243
diff
changeset
|
336 %! unlink ([filename ".orig"]); |
22365
119f408f2dd1
Remove temporary directories and files used by BIST.
Carnë Draug <carandraug@octave.org>
parents:
22323
diff
changeset
|
337 %! confirm_recursive_rmdir (false, "local"); |
119f408f2dd1
Remove temporary directories and files used by BIST.
Carnë Draug <carandraug@octave.org>
parents:
22323
diff
changeset
|
338 %! rmdir (dirname, "s"); |
19234 | 339 %! end_unwind_protect |
340 | |
341 ## Test input validation | |
342 %!error unpack () | |
343 %!error unpack (1,2,3,4) | |
344 %!error <FILE must be a string or cell array of strings> unpack (1) | |
345 %!error <file "_%NOT_A_FILENAME%_" not found> unpack ("_%NOT_A_FILENAME%_") | |
346 %!error <file "_%NOT_A_FILENAME%_" not found> unpack ({"_%NOT_A_FILENAME%_"}) | |
347 %!error <file "_%NOT_A_FILENAME%_" not found> unpack ({"_%NOT_A_FILENAME%_", "2nd_filename"}) | |
348 %!error <FILETYPE must be given for a directory> | |
349 %! if (isunix || ismac) | |
350 %! unpack ("/"); | |
351 %! else | |
352 %! unpack ('C:\'); | |
353 %! endif | |
354 %!error <FILETYPE must be "gunzip" for a directory> | |
355 %! if (isunix || ismac) | |
356 %! unpack ("/", [], "foobar"); | |
357 %! else | |
358 %! unpack ('C:\', [], "foobar"); | |
359 %! endif | |
360 |