changeset 8362:03b414516dd8

clean up bzip2, gzip and __xzip__
author Thorsten Meyer <thorsten.meyier@gmx.de>
date Sat, 29 Nov 2008 23:03:16 +0100
parents cf620941af1a
children be07fdf798cf
files scripts/ChangeLog scripts/miscellaneous/__xzip__.m scripts/miscellaneous/bzip2.m scripts/miscellaneous/gzip.m
diffstat 4 files changed, 84 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Sat Nov 29 17:57:50 2008 +0100
+++ b/scripts/ChangeLog	Sat Nov 29 23:03:16 2008 +0100
@@ -6,6 +6,15 @@
 
 	* specfun/nchoosek.m: Set max_recursion_depth and use a subfunction.
 
+2008-11-29  Thorsten Meyer  <thorsten.meyier@gmx.de>
+
+        * 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 <bpabbott@mac.com>
 
 	* plot/legend.m: Correct ording of legend labels.
--- a/scripts/miscellaneous/__xzip__.m	Sat Nov 29 17:57:50 2008 +0100
+++ b/scripts/miscellaneous/__xzip__.m	Sat Nov 29 23:03:16 2008 +0100
@@ -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);
--- a/scripts/miscellaneous/bzip2.m	Sat Nov 29 17:57:50 2008 +0100
+++ b/scripts/miscellaneous/bzip2.m	Sat Nov 29 23:03:16 2008 +0100
@@ -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
--- a/scripts/miscellaneous/gzip.m	Sat Nov 29 17:57:50 2008 +0100
+++ b/scripts/miscellaneous/gzip.m	Sat Nov 29 23:03:16 2008 +0100
@@ -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 <Invalid call to gzip.  Correct usage is> gzip("1", "2", "3");
 %!error <Invalid call to gzip.  Correct usage is> gzip();
 %!error <output directory does not exist> gzip("1", tmpnam);
-%!error <expecting all arguments to be character strings> gzip(1);
+%!error <expecting FILES to be a character array> 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