annotate scripts/miscellaneous/unpack.m @ 6084:38114c34b44a

[project @ 2006-10-25 03:52:48 by jwe]
author jwe
date Wed, 25 Oct 2006 03:52:48 +0000
parents 0295db941ae7
children 05257ee9753d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
1 ## Copyright (C) 2006 Bill Denney
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
2 ##
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
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
7 ## the Free Software Foundation; either version 2, or (at your option)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
8 ## any later version.
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
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, write to the Free
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
18 ## 02110-1301, USA.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
19
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
20 ## -*- texinfo -*-
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
21 ## @deftypefn {Function File} {@var{files} =} unpack (@var{file}, @var{dir})
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
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
24 ## @var{dir}. If @var{file} is a cellstr, then all files will be
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
25 ## handled individually. If @var{dir} is not specified, it defaults to
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
26 ## the current directory. It returns a list of @var{files}
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
27 ## unpacked. If a directory is in the file list, then the
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
28 ## @var{filetype} to unpack must also be specified.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
29 ##
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
30 ## The @var{files} includes the entire path to the output files.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
31 ## @seealso{bzip2,bunzip2,tar,untar,gzip,gunzip,zip,unzip}
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
32 ## @end deftypefn
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
33
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
34 ## Author: Bill Denney <denney@seas.upenn.edu>
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
35
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
36 function filelist = unpack (file, directory, filetype)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
37
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
38 if (nargin < 1 || nargin > 3)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
39 print_usage ();
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
40 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
41
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
42 if (nargin < 2)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
43 directory = ".";
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
44 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
45 if (nargin < 3)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
46 filetype = "";
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
47 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
48
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
49 if (ischar (file))
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
50 if (isdir (file))
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
51 if (isempty (filetype))
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
52 error ("unpack: filetype must be given for a directory");
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
53 elseif (! any (strcmpi (filetype, "gunzip")))
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
54 error ("unpack: filetype must be gunzip for a directory");
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
55 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
56 else
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
57 [pathstr, name, ext] = fileparts (file);
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
58
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
59 ## Check to see if it's .tar.gz, .tar.Z, etc.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
60 if (any (strcmpi ({".gz" ".Z" ".bz2" ".bz"}, ext)))
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
61 [tmppathstr, tmpname, tmpext] = fileparts (name);
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
62 if (strcmpi (tmpext, ".tar"))
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
63 name = tmpname;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
64 ext = strcat (tmpext, ext);
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
65 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
66 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
67
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
68 ## If the file is a url, download it and then work with that
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
69 ## file.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
70 if (! isempty (strfind (file, "://")))
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
71 ## FIXME -- the above is not a perfect test for a url
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
72 urlfile = file;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
73 ## FIXME -- should we name the file that we download with the
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
74 ## same file name as the url requests?
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
75 tmpfile = strcat (tmpnam (), ext);
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
76 [file, success, msg] = urlwrite (urlfile, tmpfile);
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
77 if (! success)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
78 error ("unpack: could not get \"%s\": %s", urlfile, msg);
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
79 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
80 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
81
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
82 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
83
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
84 ## canonicalize_file_name returns empty if the file isn't found, so
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
85 ## use that to check for existence
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
86 cfile = canonicalize_file_name (file);
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
87
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
88 if (isempty (cfile))
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
89 error ("unpack: file \"%s\" not found.", file);
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
90 else
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
91 file = cfile;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
92 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
93
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
94 elseif (iscellstr (file))
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
95 files = {};
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
96 for i = 1:numel (file)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
97 tmpfiles = unpack (file{i}, directory);
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
98 files = {files{:} tmpfiles{:}};
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
99 endfor
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
100
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
101 else
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
102 error ("unpack: invalid input file class, %s", class(file));
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
103 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
104
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
105 ## Instructions on what to do for any extension.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
106 ##
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
107 ## The field names are the file extension without periods.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
108 ## 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
109 ## The second cell is what is executed to unpack an archive quietly.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
110 ## The third cell is the function to execute on output to get the
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
111 ## files list.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
112 ## The fourth cell indicates if the files may need to be manually moved
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
113 ## (i.e. tar and unzip decompress into the current directory while
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
114 ## bzip2 and gzip decompress the file at its location).
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
115 persistent commandlist;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
116 if (isempty (commandlist))
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
117 commandlist.gz = {"gunzip -v -r \"%s\"", ...
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
118 "gunzip -r \"%s\"", ...
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
119 @__parse_gzip__, true};
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
120 commandlist.z = commandlist.gz;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
121 commandlist.bz2 = {"bunzip2 -v \"%s\"", ...
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
122 "bunzip2 \"%s\"", ...
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
123 @__parse_bzip2__, true};
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
124 commandlist.bz = commandlist.bz2;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
125 commandlist.tar = {"tar -x -v -f \"%s\"", ...
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
126 "tar -x -f \"%s\"", ...
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
127 @__parse_tar__, false};
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
128 commandlist.targz = {"gunzip -c \"%s\" | tar -x -v", ...
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
129 "gunzip -c \"%s\" | tar -x", ...
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
130 @__parse_tar__, false};
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
131 commandlist.tgz = commandlist.targz;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
132 commandlist.tarbz2 = {"bunzip2 -c \"%s\" | tar -x -v", ...
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
133 "bunzip2 -c \"%s\" | tar -x", ...
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
134 @__parse_tar__, false};
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
135 commandlist.tarbz = commandlist.tarbz2;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
136 commandlist.tbz2 = commandlist.tarbz2;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
137 commandlist.tbz = commandlist.tarbz2;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
138 commandlist.zip = {"unzip \"%s\"", ...
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
139 "unzip -q \"%s\"", ...
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
140 @__parse_zip__, false};
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
141 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
142
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
143 nodotext = ext(! ismember (ext, "."));
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
144
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
145 origdir = pwd ();
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
146
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
147 if (isfield (commandlist, nodotext))
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
148 [commandv, commandq, parser, move] = deal (commandlist.(nodotext){:});
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
149 cstartdir = canonicalize_file_name (origdir);
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
150 cenddir = canonicalize_file_name (directory);
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
151 needmove = move && ! strcmp (cstartdir, cenddir);
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
152 if (nargout > 0 || needmove)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
153 command = commandv;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
154 else
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
155 command = commandq;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
156 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
157 else
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
158 warning ("unpack:filetype", "unrecognised file type, %s", ext);
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
159 files = file;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
160 return;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
161 endif
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 ## Create the directory if necessary.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
164 s = stat (directory);
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
165 if (isempty (s))
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
166 [status, msg] = mkdir (directory);
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
167 if (! status)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
168 error ("unpack: mkdir failed to create %s: %s", directory, msg);
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
169 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
170 elseif (! S_ISDIR (s.mode))
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
171 error ("unpack: %s: not a directory", directory);
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
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
174 unwind_protect
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
175 cd (directory);
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
176 [status, output] = system (sprintf (strcat (command " 2>&1"), file));
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
177 unwind_protect_cleanup
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
178 cd (origdir);
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
179 end_unwind_protect
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
180
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
181 if (status)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
182 error ("unpack: unarchiving program exited with status: %d\n%s",
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
183 status, output);
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
184 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
185
6084
38114c34b44a [project @ 2006-10-25 03:52:48 by jwe]
jwe
parents: 6083
diff changeset
186 if (nargout > 0 || needmove)
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
187 ## Trim the last cr if needed.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
188 ## FIXME -- will this need to change to a check for "\r\n" for windows?
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
189 if (output(length (output)) == "\n")
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
190 output(length (output)) = [];
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
191 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
192 files = parser (cellstr (split (output, "\n")))';
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
193
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
194 ## Move files if necessary
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
195 if (needmove)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
196 [st, msg, msgid] = movefile (files, directory);
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
197 if (! st)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
198 error ("unpack: unable to move files to \"%s\": %s",
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
199 directory, msg);
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
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
202 ## Fix the names for the files since they were moved.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
203 for i = 1:numel (files)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
204 files{i} = strrep (files{i}, cstartdir, cenddir);
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
205 endfor
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
206 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
207
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
208 ## Return output if requested.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
209 if (nargout > 0)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
210 filelist = files;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
211 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
212 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
213
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
214 endfunction
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
215
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
216 function files = __parse_zip__ (output)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
217 ## Parse the output from zip and unzip.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
218
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
219 for i = 1:length (output)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
220 files{i} = output{i}(14:length(output{i}));
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
221 endfor
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
222 endfunction
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
223
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
224 function output = __parse_tar__ (output)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
225 ## This is a noop, but it makes things simpler for other cases.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
226 endfunction
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
227
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
228 function files = __parse_gzip__ (output)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
229 ## Parse the output from gzip and gunzip returning the files
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
230 ## commpressed (or decompressed).
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
231
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
232 files = {};
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
233 ## The middle ": " should indicate a good place to start looking for
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
234 ## the filename.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
235 for i = 1:length (output)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
236 colons = strfind (output{i}, ":");
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
237 if (isempty (colons))
6083
0295db941ae7 [project @ 2006-10-25 03:47:48 by jwe]
jwe
parents: 6082
diff changeset
238 warning ("unpack:parsing",
0295db941ae7 [project @ 2006-10-25 03:47:48 by jwe]
jwe
parents: 6082
diff changeset
239 "Unable to parse line (gzip missing colon):\n%s", output{i});
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
240 else
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
241 midcolon = colons(ceil (length (colons)/2));
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
242 thisstr = output{i}(midcolon+2:length(output{i}));
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
243 idx = index (thisstr, "with") + 5;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
244 if (isempty (idx))
6083
0295db941ae7 [project @ 2006-10-25 03:47:48 by jwe]
jwe
parents: 6082
diff changeset
245 warning ("unpack:parsing",
0295db941ae7 [project @ 2006-10-25 03:47:48 by jwe]
jwe
parents: 6082
diff changeset
246 "Unable to parse line (gzip missing with):\n%s", output{i});
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
247 else
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
248 files{i} = thisstr(idx:length (thisstr));
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
249 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
250 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
251 endfor
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
252 endfunction
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
253
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
254 function files = __parse_bzip2__ (output)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
255 ## Parse the output from bzip2 and bunzip2 returning the files
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
256 ## commpressed (or decompressed).
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
257
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
258 files = {};
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
259 for i = 1:length (output)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
260 ## the -5 is to remove the ".bz2:"
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
261 endoffilename = rindex (output{i}, ": ") - 5;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
262 if (isempty (endoffilename))
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
263 warning ("unpack:parsing", "Unable to parse line:\n%s", output{i});
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
264 else
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
265 files{i} = output{i}(3:endoffilename);
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
266 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
267 endfor
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
268 endfunction