# HG changeset patch # User Thorsten Meyer # Date 1227996196 -3600 # Node ID 03b414516dd8e72ed1a326a65e1346572696c187 # Parent cf620941af1a8b727ba014cd87f667e97dbfaa69 clean up bzip2, gzip and __xzip__ diff --git a/scripts/ChangeLog b/scripts/ChangeLog --- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -6,6 +6,15 @@ * specfun/nchoosek.m: Set max_recursion_depth and use a subfunction. +2008-11-29 Thorsten Meyer + + * miscellaneous/gzip.m: Remove @seealso reference to __xzip__, + improve tests + miscellaneous/bzip2.m: Remove @seealso reference to __xzip__, + fix handling of output argument, add test + miscellaneous/__xzip__.m: Improve error messages, fix cleanup + of temporary directories, remove tab characters + 2008-11-24 Ben Abbott * plot/legend.m: Correct ording of legend labels. diff --git a/scripts/miscellaneous/__xzip__.m b/scripts/miscellaneous/__xzip__.m --- a/scripts/miscellaneous/__xzip__.m +++ b/scripts/miscellaneous/__xzip__.m @@ -44,6 +44,8 @@ if (ischar (files)) files = cellstr (files); + else + error ("__xzip__: expecting FILES to be a character array"); endif if (nargin == 4) @@ -53,59 +55,54 @@ cwd = pwd(); unwind_protect - if (iscellstr (files)) - files = glob (files); + files = glob (files); + + ## Ignore any file with the compress extension + files (cellfun (@(x) length(x) > length(extension) + && strcmp (x((end - length(extension) + 1):end), extension), + files)) = []; + + copyfile (files, outdir); - ## Ignore any file with the compress extension - files (cellfun (@(x) length(x) > length(extension) - && strcmp (x((end - length(extension) + 1):end), extension), - files)) = []; - - copyfile (files, outdir); + [d, f] = myfileparts(files); + + cd (outdir); - [d, f] = myfileparts(files); - - cd (outdir); + cmd = sprintf (commandtemplate, sprintf (" %s", f{:})); - cmd = sprintf (commandtemplate, sprintf (" %s", f{:})); - - [status, output] = system (cmd); - if (status == 0) + [status, output] = system (cmd); + if (status == 0) - if (nargin == 5) - compressed_files = cellfun( - @(x) fullfile (outdir, sprintf ("%s.%s", x, extension)), - f, "UniformOutput", false); - else - movefile (cellfun(@(x) sprintf ("%s.%s", x, extension), f, - "UniformOutput", false), cwd); - ## FIXME this does not work when you try to compress directories - - compressed_files = cellfun(@(x) sprintf ("%s.%s", x, extension), - files, "UniformOutput", false); - endif + if (nargin == 5) + compressed_files = cellfun( + @(x) fullfile (outdir, sprintf ("%s.%s", x, extension)), + f, "UniformOutput", false); + else + movefile (cellfun(@(x) sprintf ("%s.%s", x, extension), f, + "UniformOutput", false), cwd); + ## FIXME this does not work when you try to compress directories - if (nargout > 0) - entries = compressed_files; - endif - else - error (sprintf("%s command failed with exit status = %d", - commandname, status)); - endif - + compressed_files = cellfun(@(x) sprintf ("%s.%s", x, extension), + files, "UniformOutput", false); + endif + + if (nargout > 0) + entries = compressed_files; + endif else - error ("__xzip__: expecting all arguments to be character strings"); + error (sprintf("%s command failed with exit status = %d", + commandname, status)); endif unwind_protect_cleanup cd(cwd); - if (nargin == 1) - crr = confirm_recursive_rmdir (); - unwind_protect - confirm_recursive_rmdir (false); - rmdir (outdir, "s"); - unwind_protect_cleanup - confirm_recursive_rmdir (crr); - end_unwind_protect + if (nargin == 4) + crr = confirm_recursive_rmdir (); + unwind_protect + confirm_recursive_rmdir (false); + rmdir (outdir, "s"); + unwind_protect_cleanup + confirm_recursive_rmdir (crr); + end_unwind_protect endif end_unwind_protect else @@ -117,7 +114,7 @@ function [d, f] = myfileparts (files) [d, f, ext] = cellfun (@(x) fileparts (x), files, "UniformOutput", false); f = cellfun (@(x, y) sprintf ("%s%s", x, y), f, ext, - "UniformOutput", false); + "UniformOutput", false); idx = cellfun (@(x) isdir (x), files); d(idx) = ""; f(idx) = files(idx); diff --git a/scripts/miscellaneous/bzip2.m b/scripts/miscellaneous/bzip2.m --- a/scripts/miscellaneous/bzip2.m +++ b/scripts/miscellaneous/bzip2.m @@ -25,15 +25,37 @@ ## is created. The original files are not touched. Existing compressed files ## are silently overwritten.If @var{outdir} is defined the compressed versions ## of the files are placed in this directory. -## @seealso{bunzip2, gzip, zip, tar, __xzip__} +## @seealso{bunzip2, gzip, zip, tar} ## @end deftypefn function entries = bzip2 (varargin) if (nargin == 1 || nargin == 2) - __xzip__ ("bzip2", "bz2", "bzip2 %s", varargin{:}); + if nargout == 0 + __xzip__ ("bzip2", "bz2", "bzip2 %s", varargin{:}); + else + entries = __xzip__ ("bzip2", "bz2", "bzip2 %s", varargin{:}); + endif else print_usage (); endif endfunction + +%!xtest +%! # test for correct cleanup of temporary files +%! unwind_protect +%! filename = tmpnam; +%! dummy = 1; +%! save(filename, "dummy"); +%! n_tmpfiles_before = length(find(strncmp("oct-", cellstr(ls(P_tmpdir)), 4))); +%! entry = bzip2(filename); +%! n_tmpfiles_after = length(find(strncmp("oct-", cellstr(ls(P_tmpdir)), 4))); +%! if (n_tmpfiles_before != n_tmpfiles_after) +%! error("bzip2 has not cleaned up temporary files correctly!"); +%! endif +%! unwind_protect_cleanup +%! delete(filename); +%! [path, basename, extension] = fileparts(filename); +%! delete([basename, extension, ".bz2"]); +%! end_unwind_protect diff --git a/scripts/miscellaneous/gzip.m b/scripts/miscellaneous/gzip.m --- a/scripts/miscellaneous/gzip.m +++ b/scripts/miscellaneous/gzip.m @@ -24,7 +24,7 @@ ## is created. The original files are not touched. Existing compressed ## files are silently overwritten. If @var{outdir} is defined the compressed ## versions of the files are placed in this directory. -## @seealso{gunzip, bzip2, zip, tar, __xzip__} +## @seealso{gunzip, bzip2, zip, tar} ## @end deftypefn function entries = gzip (varargin) @@ -42,8 +42,9 @@ %!error gzip("1", "2", "3"); %!error gzip(); %!error gzip("1", tmpnam); -%!error gzip(1); +%!error gzip(1); %!xtest +%! # test gzip together with gunzip %! unwind_protect %! filename = tmpnam; %! dummy = 1; @@ -58,8 +59,13 @@ %! if ! exist(entry, "file") %! error("gzipped file cannot be found!"); %! endif +%! gunzip(entry); +%! if (system(sprintf("diff %s %s/%s%s", filename, dirname, +%! basename, extension))) +%! error("unzipped file not equal to original file!"); +%! end %! unwind_protect_cleanup %! delete(filename); -%! delete(entry{:}); +%! delete([dirname, "/", basename, extension]); %! rmdir(dirname); %! end_unwind_protect