changeset 8586:31ab3b83bc8a

savepath: Respect cmd-line and env paths.
author Ben Abbott <bpabbott@mac.com>
date Sat, 24 Jan 2009 14:53:44 -0500
parents e6497be3f3d6
children 35656d6ad061
files scripts/ChangeLog scripts/path/savepath.m src/ChangeLog src/load-path.cc src/load-path.h
diffstat 5 files changed, 107 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Sat Jan 24 12:09:25 2009 -0500
+++ b/scripts/ChangeLog	Sat Jan 24 14:53:44 2009 -0500
@@ -1,3 +1,7 @@
+2008-12-24 Ben Abbott <bpabbott@mac.com>
+
+	* path/savepath.m: Respect cmd-line and env paths.
+
 2009-01-24 Ben Abbott <bpabbott@mac.com>
 
 	* sparse/svds.m: svds.m: skip tests if ARPACK is missing.
--- a/scripts/path/savepath.m	Sat Jan 24 12:09:25 2009 -0500
+++ b/scripts/path/savepath.m	Sat Jan 24 14:53:44 2009 -0500
@@ -18,8 +18,9 @@
 
 ## -*- texinfo -*-
 ## @deftypefn {Function File} {} savepath (@var{file})
-## Save the current function search path to @var{file}.  If @var{file}
-## is omitted, @file{~/.octaverc} is used.  If successful,
+## Save the the portion of the current function search path, that is
+## not set during Octave's initialization process, to @var{file}.
+## If @var{file} is omitted, @file{~/.octaverc} is used.  If successful,
 ## @code{savepath} returns 0.
 ## @seealso{path, addpath, rmpath, genpath, pathdef, pathsep}
 ## @end deftypefn
@@ -112,11 +113,80 @@
       fprintf (fid, "%s\n", pre{i})
     endfor
 
+    ## Remove the portion of the path defined via the command line
+    ## and/or the environment.
+    workingpath = parsepath (path);
+    command_line_path = parsepath (commandlinepath ());
+    octave_path = parsepath (getenv ("OCTAVE_PATH"));
+    if (isempty (pathdef ()))
+      ## This occurs when running octave via run-octave. In this instance
+      ## the entire path is specified via the command line and pathdef()
+      ## is empty.
+      [tmp, n] = setdiff (workingpath, octave_path);
+      default_path = command_line_path;
+    else
+      [tmp, n] = setdiff (workingpath, union (command_line_path, octave_path));
+      default_path = parsepath (pathdef ());
+    endif
+    ## This is the path we'd like to preserve when octave is run.
+    path_to_preserve = workingpath(sort(n));
+
+    ## Determine the path to Octave's user and sytem wide pkgs.
+    [pkg_user, pkg_system] = pkg ("list");
+    pkg_user_path = cell (1, numel (pkg_user));
+    pkg_system_path = cell (1, numel (pkg_system));
+    for n = 1:numel(pkg_user)
+      pkg_user_path{n} = pkg_user{n}.archprefix;
+    endfor
+    for n = 1:numel(pkg_system)
+      pkg_system_path{n} = pkg_system{n}.archprefix;
+    endfor
+    pkg_path = union (pkg_user_path, pkg_system_path);
+
+    ## Rely on Octave's initialization to include the pkg path elements.
+    if (! isempty (pkg_path))
+      [tmp, n] = setdiff (path_to_preserve, strcat (pkg_path, ":"));
+      path_to_preserve = path_to_preserve(sort(n));
+    endif
+
+    ## Split the path to be saved into two groups. Those path elements that
+    ## belong at the beginning and those at the end.
+    if (! isempty (default_path))
+      n1 = strmatch (default_path{1}, path_to_preserve, "exact");
+      n2 = strmatch (default_path{end}, path_to_preserve, "exact");
+      n_middle = round (0.5*(n1+n2));
+      [tmp, n] = setdiff (path_to_preserve, default_path);
+      path_to_save = path_to_preserve(sort (n));
+      ## Remove pwd
+      path_to_save = path_to_save (! strcmpi (path_to_save, strcat (".", pathsep)));
+      n = ones (size (path_to_save));
+      for m = 1:numel(path_to_save)
+        n(m) = strmatch (path_to_save{m}, path_to_preserve);
+      endfor
+      path_to_save_begin = path_to_save(n <= n_middle);
+      path_to_save_end   = path_to_save(n > n_middle);
+    else
+      path_to_save_begin = path_to_preserve;
+      path_to_save_end   = {};
+    endif
+    path_to_save_begin = cell2mat (path_to_save_begin);
+    path_to_save_end   = cell2mat (path_to_save_end);
+
     ## Use single quotes for PATH argument to avoid string escape
     ## processing.  Since we are using single quotes around the arg,
     ## double any single quote characters found in the string.
-    fprintf (fid, "\n%s\n  path ('%s');\n%s\n",
-	     beginstring, strrep (path (), "'", "''"), endstring);
+    fprintf (fid, "%s\n", beginstring)
+    if (! isempty (path_to_save_begin))
+      n = find (path_to_save_begin != pathsep, 1, "last");
+      fprintf (fid, "  addpath ('%s', '-begin');\n",
+               strrep (path_to_save_begin(1:n), "'", "''"))
+    endif
+    if (! isempty (path_to_save_end))
+      n = find (path_to_save_end != pathsep, 1, "last");
+      fprintf (fid, "  addpath ('%s', '-end');\n",
+               strrep (path_to_save_end(1:n), "'", "''"))
+    endif
+    fprintf (fid, "%s\n", endstring)
 
     for i = 1:length (post)
       fprintf (fid, "%s\n", post{i});
@@ -137,3 +207,9 @@
   endif
   
 endfunction  
+
+function path_elements = parsepath (p)
+  pat = sprintf ("([^%s]+[%s$])", pathsep, pathsep);
+  [jnk1, jnk2, jnk3, path_elements] = regexpi (strcat (p, pathsep), pat);
+endfunction
+
--- a/src/ChangeLog	Sat Jan 24 12:09:25 2009 -0500
+++ b/src/ChangeLog	Sat Jan 24 14:53:44 2009 -0500
@@ -1,3 +1,8 @@
+2008-12-25 Ben Abbott <bpabbott@mac.com>
+
+	* load-path.cc: New function commandlinepath.
+	* load-path.h: Make command_line_path public.
+
 2009-01-24 Ben Abbott <bpabbott@mac.com>
 
 	* DLD-FUNCTIONS/eigs.cc: eigs.cc: skip tests if ARPACK is missing.
--- a/src/load-path.cc	Sat Jan 24 12:09:25 2009 -0500
+++ b/src/load-path.cc	Sat Jan 24 14:53:44 2009 -0500
@@ -1772,6 +1772,17 @@
   return retval;
 }
 
+DEFUN (commandlinepath, , ,
+    "-*- texinfo -*-\n\
+@deftypefn {Built-in Function} {} commandlinepath (@dots{})\n\
+Return the command line path variable.\n\
+\n\
+@seealso{path, addpath, rmpath, genpath, pathdef, savepath, pathsep}\n\
+@end deftypefn")
+{
+  return octave_value (load_path::get_command_line_path ());
+}
+
 DEFUN (restoredefaultpath, , ,
     "-*- texinfo -*-\n\
 @deftypefn {Built-in Function} {} restoredefaultpath (@dots{})\n\
--- a/src/load-path.h	Sat Jan 24 12:09:25 2009 -0500
+++ b/src/load-path.h	Sat Jan 24 14:53:44 2009 -0500
@@ -218,6 +218,11 @@
       command_line_path += dir_path::path_sep_str () + p;
   }
 
+  static std::string get_command_line_path (void)
+  {
+    return instance_ok () ? instance->do_get_command_line_path () : std::string ();
+  }
+
   static std::string system_path (void)
   {
     return instance_ok () ? instance->do_system_path () : std::string ();
@@ -486,6 +491,8 @@
 
   std::string do_system_path (void) const { return sys_path; }
 
+  std::string do_get_command_line_path (void) const { return command_line_path; }
+
   void add_to_fcn_map (const dir_info& di, bool at_end) const;
 
   void add_to_private_fcn_map (const dir_info& di) const;