Mercurial > hg > octave-lyh
annotate scripts/path/savepath.m @ 8920:eb63fbe60fab
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 07 Mar 2009 10:41:27 -0500 |
parents | 5dd06f19e9be |
children | fca0dc2fb042 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2005, 2006, 2007, 2008, 2009 Bill Denney |
5732 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
5732 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
5732 | 18 |
19 ## -*- texinfo -*- | |
20 ## @deftypefn {Function File} {} savepath (@var{file}) | |
8586
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
21 ## Save the the portion of the current function search path, that is |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
22 ## not set during Octave's initialization process, to @var{file}. |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
23 ## If @var{file} is omitted, @file{~/.octaverc} is used. If successful, |
5804 | 24 ## @code{savepath} returns 0. |
5814 | 25 ## @seealso{path, addpath, rmpath, genpath, pathdef, pathsep} |
5732 | 26 ## @end deftypefn |
27 | |
28 ## Author: Bill Denney <bill@givebillmoney.com> | |
29 | |
30 function varargout = savepath (savefile) | |
31 | |
32 retval = 1; | |
33 | |
34 beginstring = "## Begin savepath auto-created section, do not edit"; | |
35 endstring = "## End savepath auto-created section"; | |
36 | |
5733 | 37 if (nargin == 0) |
8608 | 38 savefile = fullfile ("~", ".octaverc"); |
5732 | 39 endif |
40 | |
5733 | 41 ## parse the file if it exists to see if we should replace a section |
42 ## or create a section | |
5732 | 43 startline = 0; |
44 endline = 0; | |
45 filelines = {}; | |
46 if (exist (savefile) == 2) | |
5733 | 47 ## read in all lines of the file |
5732 | 48 [fid, msg] = fopen (savefile, "rt"); |
49 if (fid < 0) | |
50 error ("savepath: could not open savefile, %s: %s", savefile, msg); | |
51 endif | |
7392 | 52 unwind_protect |
53 linenum = 0; | |
54 while (linenum >= 0) | |
55 result = fgetl (fid); | |
56 if (isnumeric (result)) | |
57 ## end at the end of file | |
58 linenum = -1; | |
59 else | |
60 linenum = linenum + 1; | |
61 filelines{linenum} = result; | |
62 ## find the first and last lines if they exist in the file | |
63 if (strcmp (result, beginstring)) | |
64 startline = linenum; | |
65 elseif (strcmp (result, endstring)) | |
66 endline = linenum; | |
67 endif | |
68 endif | |
69 endwhile | |
70 unwind_protect_cleanup | |
71 closeread = fclose (fid); | |
72 if (closeread < 0) | |
73 error ("savepath: could not close savefile after reading, %s", | |
74 savefile); | |
5732 | 75 endif |
7392 | 76 end_unwind_protect |
5732 | 77 endif |
78 | |
5733 | 79 if (startline > endline || (startline > 0 && endline == 0)) |
5735 | 80 error ("savepath: unable to parse file, %s", savefile); |
5732 | 81 endif |
82 | |
5733 | 83 ## put the current savepath lines into the file |
84 if (isempty (filelines) | |
85 || (startline == 1 && endline == length (filelines))) | |
86 ## savepath is the entire file | |
5732 | 87 pre = post = {}; |
88 elseif (endline == 0) | |
5733 | 89 ## drop the savepath statements at the end of the file |
5732 | 90 pre = filelines; |
91 post = {}; | |
92 elseif (startline == 1) | |
93 pre = {}; | |
94 post = filelines(endline+1:end); | |
8608 | 95 elseif (endline == length (filelines)) |
5732 | 96 pre = filelines(1:startline-1); |
97 post = {}; | |
98 else | |
5733 | 99 ## insert in the middle |
5732 | 100 pre = filelines(1:startline-1); |
101 post = filelines(endline+1:end); | |
102 endif | |
103 | |
5733 | 104 ## write the results |
5732 | 105 [fid, msg] = fopen (savefile, "wt"); |
106 if (fid < 0) | |
107 error ("savepath: unable to open file for writing, %s, %s", savefile, msg); | |
7151 | 108 endif |
7392 | 109 unwind_protect |
110 for i = 1:length (pre) | |
111 fprintf (fid, "%s\n", pre{i}) | |
112 endfor | |
5736 | 113 |
8586
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
114 ## Remove the portion of the path defined via the command line |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
115 ## and/or the environment. |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
116 workingpath = parsepath (path); |
8609
fcf762ba66cf
load-path.cc (Fcommand_line_path): rename from Fcommandlinepath
John W. Eaton <jwe@octave.org>
parents:
8608
diff
changeset
|
117 command_line_path = parsepath (command_line_path ()); |
8586
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
118 octave_path = parsepath (getenv ("OCTAVE_PATH")); |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
119 if (isempty (pathdef ())) |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
120 ## This occurs when running octave via run-octave. In this instance |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
121 ## the entire path is specified via the command line and pathdef() |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
122 ## is empty. |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
123 [tmp, n] = setdiff (workingpath, octave_path); |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
124 default_path = command_line_path; |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
125 else |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
126 [tmp, n] = setdiff (workingpath, union (command_line_path, octave_path)); |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
127 default_path = parsepath (pathdef ()); |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
128 endif |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
129 ## This is the path we'd like to preserve when octave is run. |
8608 | 130 path_to_preserve = workingpath (sort (n)); |
8586
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
131 |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
132 ## Determine the path to Octave's user and sytem wide pkgs. |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
133 [pkg_user, pkg_system] = pkg ("list"); |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
134 pkg_user_path = cell (1, numel (pkg_user)); |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
135 pkg_system_path = cell (1, numel (pkg_system)); |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
136 for n = 1:numel(pkg_user) |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
137 pkg_user_path{n} = pkg_user{n}.archprefix; |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
138 endfor |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
139 for n = 1:numel(pkg_system) |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
140 pkg_system_path{n} = pkg_system{n}.archprefix; |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
141 endfor |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
142 pkg_path = union (pkg_user_path, pkg_system_path); |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
143 |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
144 ## Rely on Octave's initialization to include the pkg path elements. |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
145 if (! isempty (pkg_path)) |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
146 [tmp, n] = setdiff (path_to_preserve, strcat (pkg_path, ":")); |
8608 | 147 path_to_preserve = path_to_preserve (sort (n)); |
8586
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
148 endif |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
149 |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
150 ## Split the path to be saved into two groups. Those path elements that |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
151 ## belong at the beginning and those at the end. |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
152 if (! isempty (default_path)) |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
153 n1 = strmatch (default_path{1}, path_to_preserve, "exact"); |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
154 n2 = strmatch (default_path{end}, path_to_preserve, "exact"); |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
155 n_middle = round (0.5*(n1+n2)); |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
156 [tmp, n] = setdiff (path_to_preserve, default_path); |
8608 | 157 path_to_save = path_to_preserve (sort (n)); |
8586
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
158 ## Remove pwd |
8608 | 159 path_to_save = path_to_save (! strcmpi (path_to_save, |
160 strcat (".", pathsep))); | |
8586
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
161 n = ones (size (path_to_save)); |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
162 for m = 1:numel(path_to_save) |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
163 n(m) = strmatch (path_to_save{m}, path_to_preserve); |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
164 endfor |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
165 path_to_save_begin = path_to_save(n <= n_middle); |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
166 path_to_save_end = path_to_save(n > n_middle); |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
167 else |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
168 path_to_save_begin = path_to_preserve; |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
169 path_to_save_end = {}; |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
170 endif |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
171 path_to_save_begin = cell2mat (path_to_save_begin); |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
172 path_to_save_end = cell2mat (path_to_save_end); |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
173 |
7392 | 174 ## Use single quotes for PATH argument to avoid string escape |
7393 | 175 ## processing. Since we are using single quotes around the arg, |
176 ## double any single quote characters found in the string. | |
8586
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
177 fprintf (fid, "%s\n", beginstring) |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
178 if (! isempty (path_to_save_begin)) |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
179 n = find (path_to_save_begin != pathsep, 1, "last"); |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
180 fprintf (fid, " addpath ('%s', '-begin');\n", |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
181 strrep (path_to_save_begin(1:n), "'", "''")) |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
182 endif |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
183 if (! isempty (path_to_save_end)) |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
184 n = find (path_to_save_end != pathsep, 1, "last"); |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
185 fprintf (fid, " addpath ('%s', '-end');\n", |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
186 strrep (path_to_save_end(1:n), "'", "''")) |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
187 endif |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
188 fprintf (fid, "%s\n", endstring) |
5736 | 189 |
7392 | 190 for i = 1:length (post) |
191 fprintf (fid, "%s\n", post{i}); | |
192 endfor | |
193 unwind_protect_cleanup | |
194 closeread = fclose (fid); | |
195 if (closeread < 0) | |
196 error ("savepath: could not close savefile after writing, %s", savefile); | |
197 elseif (nargin == 0) | |
198 warning ("savepath: current path saved to %s", savefile); | |
199 endif | |
200 end_unwind_protect | |
5732 | 201 |
202 retval = 0; | |
203 | |
204 if (nargout == 1) | |
205 varargout{1} = retval; | |
206 endif | |
207 | |
208 endfunction | |
8586
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
209 |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
210 function path_elements = parsepath (p) |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
211 pat = sprintf ("([^%s]+[%s$])", pathsep, pathsep); |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
212 [jnk1, jnk2, jnk3, path_elements] = regexpi (strcat (p, pathsep), pat); |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
213 endfunction |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
214 |