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