Mercurial > hg > octave-lyh
annotate scripts/path/savepath.m @ 8608:5cc1fba0a7d6
style and ChangeLog fixes
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 27 Jan 2009 22:59:51 -0500 |
parents | 31ab3b83bc8a |
children | fcf762ba66cf |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 2005, 2006, 2007 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 | |
5736 | 30 ##PKGADD: mark_as_command savepath |
31 | |
5732 | 32 function varargout = savepath (savefile) |
33 | |
34 retval = 1; | |
35 | |
36 beginstring = "## Begin savepath auto-created section, do not edit"; | |
37 endstring = "## End savepath auto-created section"; | |
38 | |
5733 | 39 if (nargin == 0) |
8608 | 40 savefile = fullfile ("~", ".octaverc"); |
5732 | 41 endif |
42 | |
5733 | 43 ## parse the file if it exists to see if we should replace a section |
44 ## or create a section | |
5732 | 45 startline = 0; |
46 endline = 0; | |
47 filelines = {}; | |
48 if (exist (savefile) == 2) | |
5733 | 49 ## read in all lines of the file |
5732 | 50 [fid, msg] = fopen (savefile, "rt"); |
51 if (fid < 0) | |
52 error ("savepath: could not open savefile, %s: %s", savefile, msg); | |
53 endif | |
7392 | 54 unwind_protect |
55 linenum = 0; | |
56 while (linenum >= 0) | |
57 result = fgetl (fid); | |
58 if (isnumeric (result)) | |
59 ## end at the end of file | |
60 linenum = -1; | |
61 else | |
62 linenum = linenum + 1; | |
63 filelines{linenum} = result; | |
64 ## find the first and last lines if they exist in the file | |
65 if (strcmp (result, beginstring)) | |
66 startline = linenum; | |
67 elseif (strcmp (result, endstring)) | |
68 endline = linenum; | |
69 endif | |
70 endif | |
71 endwhile | |
72 unwind_protect_cleanup | |
73 closeread = fclose (fid); | |
74 if (closeread < 0) | |
75 error ("savepath: could not close savefile after reading, %s", | |
76 savefile); | |
5732 | 77 endif |
7392 | 78 end_unwind_protect |
5732 | 79 endif |
80 | |
5733 | 81 if (startline > endline || (startline > 0 && endline == 0)) |
5735 | 82 error ("savepath: unable to parse file, %s", savefile); |
5732 | 83 endif |
84 | |
5733 | 85 ## put the current savepath lines into the file |
86 if (isempty (filelines) | |
87 || (startline == 1 && endline == length (filelines))) | |
88 ## savepath is the entire file | |
5732 | 89 pre = post = {}; |
90 elseif (endline == 0) | |
5733 | 91 ## drop the savepath statements at the end of the file |
5732 | 92 pre = filelines; |
93 post = {}; | |
94 elseif (startline == 1) | |
95 pre = {}; | |
96 post = filelines(endline+1:end); | |
8608 | 97 elseif (endline == length (filelines)) |
5732 | 98 pre = filelines(1:startline-1); |
99 post = {}; | |
100 else | |
5733 | 101 ## insert in the middle |
5732 | 102 pre = filelines(1:startline-1); |
103 post = filelines(endline+1:end); | |
104 endif | |
105 | |
5733 | 106 ## write the results |
5732 | 107 [fid, msg] = fopen (savefile, "wt"); |
108 if (fid < 0) | |
109 error ("savepath: unable to open file for writing, %s, %s", savefile, msg); | |
7151 | 110 endif |
7392 | 111 unwind_protect |
112 for i = 1:length (pre) | |
113 fprintf (fid, "%s\n", pre{i}) | |
114 endfor | |
5736 | 115 |
8586
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
116 ## 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
|
117 ## and/or the environment. |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
118 workingpath = parsepath (path); |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
119 command_line_path = parsepath (commandlinepath ()); |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
120 octave_path = parsepath (getenv ("OCTAVE_PATH")); |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
121 if (isempty (pathdef ())) |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
122 ## 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
|
123 ## 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
|
124 ## is empty. |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
125 [tmp, n] = setdiff (workingpath, octave_path); |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
126 default_path = command_line_path; |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
127 else |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
128 [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
|
129 default_path = parsepath (pathdef ()); |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
130 endif |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
131 ## This is the path we'd like to preserve when octave is run. |
8608 | 132 path_to_preserve = workingpath (sort (n)); |
8586
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
133 |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
134 ## 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
|
135 [pkg_user, pkg_system] = pkg ("list"); |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
136 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
|
137 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
|
138 for n = 1:numel(pkg_user) |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
139 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
|
140 endfor |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
141 for n = 1:numel(pkg_system) |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
142 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
|
143 endfor |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
144 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
|
145 |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
146 ## 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
|
147 if (! isempty (pkg_path)) |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
148 [tmp, n] = setdiff (path_to_preserve, strcat (pkg_path, ":")); |
8608 | 149 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
|
150 endif |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
151 |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
152 ## 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
|
153 ## 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
|
154 if (! isempty (default_path)) |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
155 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
|
156 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
|
157 n_middle = round (0.5*(n1+n2)); |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
158 [tmp, n] = setdiff (path_to_preserve, default_path); |
8608 | 159 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
|
160 ## Remove pwd |
8608 | 161 path_to_save = path_to_save (! strcmpi (path_to_save, |
162 strcat (".", pathsep))); | |
8586
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
163 n = ones (size (path_to_save)); |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
164 for m = 1:numel(path_to_save) |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
165 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
|
166 endfor |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
167 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
|
168 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
|
169 else |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
170 path_to_save_begin = path_to_preserve; |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
171 path_to_save_end = {}; |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
172 endif |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
173 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
|
174 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
|
175 |
7392 | 176 ## Use single quotes for PATH argument to avoid string escape |
7393 | 177 ## processing. Since we are using single quotes around the arg, |
178 ## 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
|
179 fprintf (fid, "%s\n", beginstring) |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
180 if (! isempty (path_to_save_begin)) |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
181 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
|
182 fprintf (fid, " addpath ('%s', '-begin');\n", |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
183 strrep (path_to_save_begin(1:n), "'", "''")) |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
184 endif |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
185 if (! isempty (path_to_save_end)) |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
186 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
|
187 fprintf (fid, " addpath ('%s', '-end');\n", |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
188 strrep (path_to_save_end(1:n), "'", "''")) |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
189 endif |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
190 fprintf (fid, "%s\n", endstring) |
5736 | 191 |
7392 | 192 for i = 1:length (post) |
193 fprintf (fid, "%s\n", post{i}); | |
194 endfor | |
195 unwind_protect_cleanup | |
196 closeread = fclose (fid); | |
197 if (closeread < 0) | |
198 error ("savepath: could not close savefile after writing, %s", savefile); | |
199 elseif (nargin == 0) | |
200 warning ("savepath: current path saved to %s", savefile); | |
201 endif | |
202 end_unwind_protect | |
5732 | 203 |
204 retval = 0; | |
205 | |
206 if (nargout == 1) | |
207 varargout{1} = retval; | |
208 endif | |
209 | |
210 endfunction | |
8586
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
211 |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
212 function path_elements = parsepath (p) |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
213 pat = sprintf ("([^%s]+[%s$])", pathsep, pathsep); |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
214 [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
|
215 endfunction |
31ab3b83bc8a
savepath: Respect cmd-line and env paths.
Ben Abbott <bpabbott@mac.com>
parents:
7393
diff
changeset
|
216 |