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