Mercurial > hg > octave-lyh
changeset 5808:a18d85bdff31
[project @ 2006-05-11 03:11:03 by jwe]
author | jwe |
---|---|
date | Thu, 11 May 2006 03:11:08 +0000 |
parents | 29c4fb42b210 |
children | c794ed00d473 |
files | scripts/ChangeLog scripts/miscellaneous/tar.m scripts/miscellaneous/untar.m scripts/miscellaneous/unzip.m |
diffstat | 4 files changed, 143 insertions(+), 167 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,8 @@ +2006-05-10 John W. Eaton <jwe@octave.org> + + * tar.m, untar.m, unzip.m: Adapt to Octave coding style. + * tar.m, untar.m: Only tar; don't compress or uncompress. + 2006-05-10 Soren Hauberg <hauberg@gmail.com> * tar.m, untar.m, unzip.m: New files.
--- a/scripts/miscellaneous/tar.m +++ b/scripts/miscellaneous/tar.m @@ -15,80 +15,58 @@ ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## -*- texinfo -*- -## @deftypefn {Function File} tar (@var{filename}, @var{files}) -## @deftypefnx{Function File} tar (@var{filename}, @var{files}, @var{root}) -## @deftypefnx{Function File} @var{entries} = tar (...) -## Packs the files listed in @var{files} into @var{filename} using -## the @code{tar} program. @var{files} must either be a string or a cell -## array of strings containing the files to be packed. The extension of -## @var{filename} determines if the tar-file is also to be compressed -## (if no extension is present @code{.tar} will be appended to @var{filename}) -## @table @asis -## @item @code{.tar} -## No compression. -## @item @code{.tar.gz} or @code{.tgz} -## The tar-file will be compressed using @code{gzip}. -## @item @code{tar.bz}, @code{tar.bz2}, @code{tbz}, or @code{tbz2} -## The tar-file will be compressed using @code{bzip2}. -## @end table +## @deftypefn {Function File} {@var{entries} =} tar (@var{tarfile}, @var{files}, @var{root}) +## Pack @var{files} @var{files} into the TAR archive @var{tarfile}. The +## list of files must be a string or a cell array of strings. +## ## The optional argument @var{root} changes the relative path of @var{files} ## from the current directory. ## -## If an output argument is requested the filename entries in the archive -## is returned. -## +## If an output argument is requested the entries in the archive are +## returned in a cell array. +## @seealso{untar, gzip, gunzip, zip, unzip} ## @end deftypefn -## @seealso{untar, gzip, gunzip, zip, unzip} -## Author: Søren Hauberg <hauberg at gmail dot com> +## Author: Søren Hauberg <hauberg@gmail.com> -function entries = tar(filename, files, root) - if (nargin < 2 || nargin > 3) - print_usage("tar"); - elseif (nargin == 2) - root = "."; +function entries = tar (tarfile, files, root) + + if (nargin == 2 || nargin == 3) + + if (nargin == 2) + root = "."; endif - - supported_extensions = {"tar", "tar.gz", "tgz", "tar.bz", "tar.bz2", "tbz", "tbz2"}; ## Test type of input - if (ischar(files)) - files = {files}; - endif - if (!ischar(filename) || !iscellstr(files) || !ischar(root)) - error("All arguments must be strings.\n"); - endif - - ## Get extension of filename - dots = find(filename == "."); - for dot = dots - curext = filename(dot+1:end); - if (any(strcmp(curext, supported_extensions))) - ext = curext; - break; - endif - endfor - - ## If no extension was found default to "tar" - if (!exist("ext", "var")) - filename = sprintf("%s.tar", filename); - ext = "tar"; + if (ischar (files)) + files = cellstr (files); endif - ## Determine which flags to use with tar - switch (ext) - case {"tar"} flag = ""; - case {"tar.gz", "tgz"} flag = "z"; - case {"tar.bz", "tar.bz2", "tbz", "tbz2"} flag = "j"; - endswitch + if (ischar (tarfile) && iscellstr (files) && ischar (root)) + + cmd = sprintf ("tar -c -v -f %s -C %s %s", tarfile, root, + sprintf (" %s", files{:})); + + [status, output] = system (cmd); - ## Call tar - [output, status] = system(["tar -" flag "cvf " filename " -C " root sprintf(" %s", files{:})]); - if (status != 0) - error("tar returned the following error: %s\n", output); + if (status == 0) + if (nargout > 0) + if (output(end) == "\n") + output(end) = []; + endif + entries = cellstr (split (output, "\n")); + entries = entries'; + endif + else + error ("tar: tar exited with status = %d", status); + endif + + else + error ("tar: expecting all arguments to be character strings"); endif - - if (nargout) - entries = split(output(1:end-1), "\n"); - endif + + else + print_usage("tar"); + endif + endfunction
--- a/scripts/miscellaneous/untar.m +++ b/scripts/miscellaneous/untar.m @@ -15,83 +15,76 @@ ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## -*- texinfo -*- -## @deftypefn {Function File} untar (@var{filename}) -## @deftypefnx {Function File} untar (@var{filename}, @var{outputdir}) -## Unpacks the archive @var{filename} using the tar program. -## The resulting files are placed in the directory @var{outputdir}, -## which defaults to the current directory. -## The used uncompressing algorithm depends on the extension of -## @var{filename}. If @var{filename} doesn't have an extension a list -## of supported extensions is tried. +## @deftypefn {Function File} untar (@var{tarfile}, @var{dir}) +## Unpack the TAR archive @var{tarfile} to the directory @var{dir}. +## If @var{dir} is not specified, it defaults to the current directory. +## @seealso{tar, gzip, gunzip, zip, unzip} ## @end deftypefn -## Author: Søren Hauberg <hauberg at gmail dot com> +## Author: Søren Hauberg <hauberg@gmail.com> +## Adapted-By: jwe -# XXX: how do we support returning the extracted filenames? -# If strip was sane, we could simply return strip(output), -# which is what is currently done. However this is not -# Matlab compatible, since Matlab returns a cellstr and -# this implementation returns a matrix. -function out = untar(filename, outputdir) - if (nargin == 0) - print_usage("untar"); - elseif (nargin == 1) - outputdir = "."; - endif - - ## If filename is a sq_string, convert it to a string - filename = sprintf("%s", filename); - - ## XXX: what about "" (i.e. no extension) ? - supported_extensions = {"tar", "tar.gz", "tgz", "tar.bz", "tar.bz2", "tbz", "tbz2"}; +function files = untar (tarfile, dir) - ## Make sure filename and outputdir are strings - if (!ischar(filename) || !ischar(outputdir)) - error("All arguments must be strings.\n"); - endif - - ## Get extension of filename - dots = find(filename == "."); - for dot = dots - curext = filename(dot+1:end); - if (any(strcmp(curext, supported_extensions))) - ext = curext; - break; - endif - endfor + if (nargin == 1 || nargin == 2) - ## If no extension was found, iterate over possible extensions - ## and try to append them to filename - if (!exist("ext", "var")) - for i = 1:length(supported_extensions) - curext = supported_extensions{i}; - if (exist([filename "." curext], "file")) - filename = [filename "." curext]; - ext = curext; - break; - endif - endfor + if (nargin == 1) + dir = "."; endif - ## If no usable extension was found give an error - if (!exist("ext", "var")) - error("No supported extension was found for %s\n", filename); + if (ischar (tarfile) && ischar (dir)) + + orig_dir = pwd (); + + tarfile = canonicalize_file_name (tarfile); + + s = stat (dir); + if (isempty (s)) + [status, msg] = mkdir (dir); + if (! status) + error ("untar: mkdir failed to create %s: %s", dir, msg); + endif + elseif (! S_ISDIR (s.mode)) + error ("untar: %s: not a directory", dir); + endif + + unwind_protect + chdir (dir); + [status, output] = system (sprintf ("tar -x -v -f %s", tarfile)); + unwind_protect_cleanup + chdir (orig_dir); + end_unwind_protect + + if (status == 0) + if (nargout > 0) + fs = filesep (); + if (dir(end) != fs) + dir = strcat (dir, fs); + endif + ## Sadly not reliable if a filename contains a newline + ## character! + if (output(end) == "\n") + output(end) = []; + endif + files = cellstr (split (output, "\n")); + if (! strcmp (dir, ".")) + nf = length (files); + for i = 1:nf + files{i} = strcat (dir, files{i}); + endfor + endif + files = files'; + endif + else + error ("tar: tar exited with status = %s", status); + endif + + else + error ("untar: expecting arguments to be character strings"); endif - - ## Determine which flags to use with tar - switch (ext) - case {"tar"} flag = ""; - case {"tar.gz", "tgz"} flag = "z"; - case {"tar.bz", "tar.bz2", "tbz", "tbz2"} flag = "j"; - endswitch - ## Call tar - [output, status] = system(["tar -" flag "xvf " filename " -C " outputdir]); - if (status != 0) - error("tar returned the following error: %s\n", output); - endif - - if (nargout > 0) - out = split(output, "\n"); - endif + else + print_usage ("untar"); + endif + endfunction
--- a/scripts/miscellaneous/unzip.m +++ b/scripts/miscellaneous/unzip.m @@ -15,45 +15,45 @@ ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## -*- texinfo -*- -## @deftypefn {Function File} unzip (@var{filename}) -## @deftypefnx {Function File} unzip (@var{filename}, @var{outputdir}) -## Unpacks the archive @var{filename} using the unzip program. -## The resulting files are placed in the directory @var{outputdir}, -## which defaults to the current directory. +## @deftypefn {Function File} unzip (@var{zipfile}, @var{dir}) +## Unpack the ZIP archive @var{zipfile} to the directory @var{dir}. +## If @var{dir} is not specified, it defaults to the current directory. +## @seealso{tar, untar, gzip, gunzip, zip} ## @end deftypefn -## Author: Søren Hauberg <hauberg at gmail dot com> +## Author: Søren Hauberg <hauberg@gmail.com> +## Adapted-By: jwe + +function files = unzip (zipfile, dir) -function files = unzip(filename, outputdir) - if (nargin == 0) - print_usage("unzip"); - elseif (nargin == 1) - outputdir = "."; - endif - - ## Make sure filename and outputdir are strings - if (!ischar(filename) || !ischar(outputdir)) - error("All arguments must be strings.\n"); - endif - - ## Should we append ".zip" to filename? - if (length(filename) <= 4 || !strcmp(filename(end-3:end), ".zip")) - filename = sprintf("%s.zip", filename); + if (nargin == 1 || nargin == 2) + + if (nargin == 1) + dir = "."; endif - ## Call unzip - [output, status] = system(["unzip -o " filename " -d " outputdir]); - if (status != 0) - error("unzip returned the following error: %s\n", output); + if (ischar (zipfile) && ischar (dir)) + + [status, output] = system (sprintf ("unzip %s -d %s", zipfile, dir)); + + if (status == 0) + if (nargout > 0) + ## Create list of extracted files. It blows that there seems + ## to be no way to get unzip to print a simple list of file + ## names. + files = strrep (output, " inflating: ", ""); + files = cellstr (split (files, "\n")); + files = files(2:end-1,:); + files = files'; + endif + else + error ("unzip: unzip exited with status = %d", status); + endif endif - - ## Create list of extracted files. This might depend on which version - ## unzip that it used, although I do not know this for sure. - if (nargout) - files = strrep(output, " inflating: ", ""); - files = split(files, "\n"); - # remove first and last line from the output - files = files(2:end-1, :); - endif + + else + print_usage ("unzip"); + endif + endfunction