comparison scripts/pkg/pkg.m @ 6189:0d23b0c0ce1a

[project @ 2006-11-29 21:01:05 by dbateman]
author dbateman
date Wed, 29 Nov 2006 21:01:05 +0000
parents 47f0cab13a02
children 512d72ee321f
comparison
equal deleted inserted replaced
6188:160958073cde 6189:0d23b0c0ce1a
82 ## It is possible to get the current installation prefix by requesting an 82 ## It is possible to get the current installation prefix by requesting an
83 ## output argument. For example, 83 ## output argument. For example,
84 ## @example 84 ## @example
85 ## p = pkg prefix 85 ## p = pkg prefix
86 ## @end example 86 ## @end example
87 ## @item local_list
88 ## Set the file in which to look for information on the locally
89 ## installed packages. Locally installed packages are those that are
90 ## typically available only to the current user. For example
91 ## @example
92 ## pkg local_list ~/.octave_packages
93 ## @end example
94 ## It is possible to get the current value of local_list with the following
95 ## @example
96 ## pkg local_list
97 ## @end example
98 ## @item global_list
99 ## Set the file in which to look for, for information on the globally
100 ## installed packages. Globally installed packages are those that are
101 ## typically available to all users. For example
102 ## @example
103 ## pkg global_list /usr/share/octave/octave_packages
104 ## @end example
105 ## It is possible to get the current value of global_list with the following
106 ## @example
107 ## pkg global_list
108 ## @end example
87 ## @end table 109 ## @end table
88 ## @end deftypefn 110 ## @end deftypefn
89 111
90 ## PKG_ADD: mark_as_command pkg 112 ## PKG_ADD: mark_as_command pkg
91 113
92 function [local_packages, global_packages] = pkg(varargin) 114 function [local_packages, global_packages] = pkg(varargin)
93 ## Installation prefix 115 ## Installation prefix
94 persistent prefix = -1; 116 persistent prefix = -1;
117 persistent local_list = tilde_expand("~/.octave_packages");
118 persistent global_list = fullfile (OCTAVE_HOME (), "/share/octave/octave_packages");
119
95 if (prefix == -1) 120 if (prefix == -1)
96 if (issuperuser()) 121 if (issuperuser())
97 prefix = fullfile (OCTAVE_HOME (), "/share/octave/packages/"); 122 prefix = fullfile (OCTAVE_HOME (), "/share/octave/packages/");
98 else 123 else
99 prefix = "~/octave/"; 124 prefix = "~/octave/";
100 endif 125 endif
101 endif 126 endif
102 prefix = tilde_expand(prefix); 127 prefix = tilde_expand(prefix);
103 128
104 ## Handle input 129 ## Handle input
105 if (length(varargin) == 0 || !iscellstr(varargin)) 130 if (length(varargin) == 0 || !iscellstr(varargin))
106 print_usage(); 131 print_usage();
107 endif 132 endif
108 files = {}; 133 files = {};
110 action = "none"; 135 action = "none";
111 for i = 1:length(varargin) 136 for i = 1:length(varargin)
112 switch (varargin{i}) 137 switch (varargin{i})
113 case "-nodeps" 138 case "-nodeps"
114 deps = false; 139 deps = false;
115 case {"list", "install", "uninstall", "load", "prefix"} 140 case {"list", "install", "uninstall", "load", "prefix", "local_list", "global_list"}
116 action = varargin{i}; 141 action = varargin{i};
117 otherwise 142 otherwise
118 files{end+1} = varargin{i}; 143 files{end+1} = varargin{i};
119 endswitch 144 endswitch
120 endfor 145 endfor
121 146
122 ## Take action 147 ## Take action
123 switch (action) 148 switch (action)
124 case "list" 149 case "list"
125 if (nargout == 0) 150 if (nargout == 0)
126 installed_packages(); 151 installed_packages(local_list, global_list);
127 elseif (nargout == 1) 152 elseif (nargout == 1)
128 local_packages = installed_packages(); 153 local_packages = installed_packages(local_list, global_list);
129 elseif (nargout == 2) 154 elseif (nargout == 2)
130 [local_packages, global_packages] = installed_packages(); 155 [local_packages, global_packages] = installed_packages(local_list, global_list);
131 else 156 else
132 error("Too many output arguments requested."); 157 error("Too many output arguments requested.");
133 endif 158 endif
134 case "install" 159 case "install"
135 if (length(files) == 0) 160 if (length(files) == 0)
136 error("You must specify at least one filename when calling 'pkg install'"); 161 error("You must specify at least one filename when calling 'pkg install'");
137 endif 162 endif
138 install(files, deps, prefix); 163 install(files, deps, prefix, local_list, global_list);
139 case "uninstall" 164 case "uninstall"
140 if (length(files) == 0) 165 if (length(files) == 0)
141 error("You must specify at least one package when calling 'pkg uninstall'"); 166 error("You must specify at least one package when calling 'pkg uninstall'");
142 endif 167 endif
143 uninstall(files, deps); 168 uninstall(files, deps, local_list, global_list);
144 case "load" 169 case "load"
145 if (length(files) == 0) 170 if (length(files) == 0)
146 error("You must specify at least one package or 'all' when calling 'pkg load'"); 171 error("You must specify at least one package or 'all' when calling 'pkg load'");
147 endif 172 endif
148 load_packages(files, deps); 173 load_packages(files, deps, local_list, global_list);
149 case "prefix" 174 case "prefix"
150 if (length(files) == 0 && nargout == 0) 175 if (length(files) == 0 && nargout == 0)
151 disp(prefix); 176 disp(prefix);
152 elseif (length(files) == 0 && nargout == 1) 177 elseif (length(files) == 0 && nargout == 1)
153 local_packages = prefix; 178 local_packages = prefix;
154 elseif (length(files) == 1 && nargout == 0 && ischar(files{1})) 179 elseif (length(files) == 1 && nargout == 0 && ischar(files{1}))
155 prefix = files{1}; 180 prefix = files{1};
156 if (!strcmp(prefix(end), "/")) prefix(end+1) = "/"; endif 181 if (!strcmp(prefix(end), "/")) prefix(end+1) = "/"; endif
157 else 182 else
158 error("You must specify a prefix directory, or request an output arguement"); 183 error("You must specify a prefix directory, or request an output argument");
184 endif
185 case "local_list"
186 if (length(files) == 0 && nargout == 0)
187 disp(local_list);
188 elseif (length(files) == 0 && nargout == 1)
189 local_packages = local_list;
190 elseif (length(files) == 1 && nargout == 0 && ischar(files{1}))
191 local_list = files{1};
192 else
193 error("You must specify a local_list file, or request an output argument");
194 endif
195 case "global_list"
196 if (length(files) == 0 && nargout == 0)
197 disp(global_list);
198 elseif (length(files) == 0 && nargout == 1)
199 local_packages = global_list;
200 elseif (length(files) == 1 && nargout == 0 && ischar(files{1}))
201 global_list = files{1};
202 else
203 error("You must specify a global_list file, or request an output argument");
159 endif 204 endif
160 otherwise 205 otherwise
161 error("You must specify a valid action for 'pkg'. See 'help pkg' for details"); 206 error("You must specify a valid action for 'pkg'. See 'help pkg' for details");
162 endswitch 207 endswitch
163 endfunction 208 endfunction
164 209
165 function install(files, handle_deps, prefix) 210 function install(files, handle_deps, prefix, local_list, global_list)
166 ## Set parameters depending on wether or not the installation
167 ## is system-wide (global) or local.
168 local_list = tilde_expand("~/.octave_packages");
169 global_list = fullfile (OCTAVE_HOME (), "/share/octave/octave_packages");
170
171 global_install = issuperuser(); 211 global_install = issuperuser();
172 212
173 # Check that the directory in prefix exist. If it doesn't: create it! 213 # Check that the directory in prefix exist. If it doesn't: create it!
174 if (!exist(prefix, "dir")) 214 if (!exist(prefix, "dir"))
175 warning("Creating installation directory %s", prefix); 215 warning("Creating installation directory %s", prefix);
178 error("Could not create installation directory: %s", msg); 218 error("Could not create installation directory: %s", msg);
179 endif 219 endif
180 endif 220 endif
181 221
182 ## Get the list of installed packages 222 ## Get the list of installed packages
183 [local_packages, global_packages] = installed_packages(); 223 [local_packages, global_packages] = installed_packages(local_list,
184 installed_packages = {local_packages{:} global_packages{:}}; 224 global_list);
225 installed_packages = {local_packages{:}, global_packages{:}};
185 226
186 if (global_install) 227 if (global_install)
187 packages = global_packages; 228 packages = global_packages;
188 else 229 else
189 packages = local_packages; 230 packages = local_packages;
304 end_try_catch 345 end_try_catch
305 346
306 ## Uninstall the packages that will be replaced 347 ## Uninstall the packages that will be replaced
307 try 348 try
308 for i = packages_to_uninstall 349 for i = packages_to_uninstall
309 uninstall({installed_packages{i}.name}, false); 350 uninstall({installed_packages{i}.name}, false, local_list,
351 global_list);
310 endfor 352 endfor
311 catch 353 catch
312 ## Something went wrong, delete tmpdirs 354 ## Something went wrong, delete tmpdirs
313 for i = 1:length(tmpdirs) 355 for i = 1:length(tmpdirs)
314 rm_rf(tmpdirs{i}); 356 rm_rf(tmpdirs{i});
389 endfor 431 endfor
390 addpath(dirs{:}); 432 addpath(dirs{:});
391 endif 433 endif
392 endfunction 434 endfunction
393 435
394 function uninstall(pkgnames, handle_deps) 436 function uninstall(pkgnames, handle_deps, local_list, global_list)
395 local_list = tilde_expand("~/.octave_packages");
396 global_list = fullfile (OCTAVE_HOME (), "/share/octave/octave_packages");
397 ## Get the list of installed packages 437 ## Get the list of installed packages
398 [local_packages, global_packages] = installed_packages(); 438 [local_packages, global_packages] = installed_packages(local_list,
439 global_list);
399 if (issuperuser()) 440 if (issuperuser())
400 installed_packages = global_packages; 441 installed_packages = {local_packages{:}, global_packages{:}};
401 else 442 else
402 installed_packages = local_packages; 443 installed_packages = local_packages;
403 endif 444 endif
404 445
405 num_packages = length(installed_packages); 446 num_packages = length(installed_packages);
411 endif 452 endif
412 endfor 453 endfor
413 454
414 ## Are all the packages that should be uninstalled already installed? 455 ## Are all the packages that should be uninstalled already installed?
415 if (length(delete_idx) != length(pkgnames)) 456 if (length(delete_idx) != length(pkgnames))
416 # XXX: We should have a better error message 457 delete_idx
417 error("Some of the packages you want to uninstall are not installed."); 458 pkgnames
459
460 if (issuperuser())
461 ## Try again for a locally installed package
462 installed_packages = local_packages
463
464 num_packages = length(installed_packages);
465 delete_idx = [];
466 for i = 1:num_packages
467 cur_name = installed_packages{i}.name;
468 if (any(strcmp(cur_name, pkgnames)))
469 delete_idx(end+1) = i;
470 endif
471 endfor
472 if (length(delete_idx) != length(pkgnames))
473 ## XXX: We should have a better error message
474 error("Some of the packages you want to uninstall are not installed.");
475 endif
476 else
477 ## XXX: We should have a better error message
478 error("Some of the packages you want to uninstall are not installed.");
479 endif
418 endif 480 endif
419 481
420 ## Compute the packages that will remain installed 482 ## Compute the packages that will remain installed
421 idx = complement(delete_idx, 1:num_packages); 483 idx = complement(delete_idx, 1:num_packages);
422 remaining_packages = {installed_packages{idx}}; 484 remaining_packages = {installed_packages{idx}};
980 endif 1042 endif
981 endif 1043 endif
982 endfor 1044 endfor
983 endfunction 1045 endfunction
984 1046
985 function [out1, out2] = installed_packages() 1047 function [out1, out2] = installed_packages(local_list, global_list)
986 local_list = tilde_expand("~/.octave_packages");
987 global_list = fullfile (OCTAVE_HOME (), "/share/octave/octave_packages");
988 ## Get the list of installed packages 1048 ## Get the list of installed packages
989 try 1049 try
990 local_packages = load(local_list).local_packages; 1050 local_packages = load(local_list).local_packages;
991 catch 1051 catch
992 local_packages = {}; 1052 local_packages = {};
993 end_try_catch 1053 end_try_catch
994 try 1054 try
995 global_packages = load(global_list).global_packages; 1055 if (strcmp(local_list, global_list))
1056 global_packages = {};
1057 else
1058 global_packages = load(global_list).global_packages;
1059 endif
996 catch 1060 catch
997 global_packages = {}; 1061 global_packages = {};
998 end_try_catch 1062 end_try_catch
999 installed_packages = {local_packages{:} global_packages{:}}; 1063 installed_packages = {local_packages{:} global_packages{:}};
1000 1064
1048 cur_dir = installed_packages{idx(i)}.dir; 1112 cur_dir = installed_packages{idx(i)}.dir;
1049 printf(format, cur_name, cur_version, cur_dir); 1113 printf(format, cur_name, cur_version, cur_dir);
1050 endfor 1114 endfor
1051 endfunction 1115 endfunction
1052 1116
1053 function load_packages(files, handle_deps) 1117 function load_packages(files, handle_deps, local_list, global_list)
1054 installed_packages = installed_packages(); 1118 installed_packages = installed_packages(local_list, global_list);
1055 num_packages = length(installed_packages); 1119 num_packages = length(installed_packages);
1056 1120
1057 ## Read package names and installdirs into a more convenient format 1121 ## Read package names and installdirs into a more convenient format
1058 pnames = pdirs = cell(1, num_packages); 1122 pnames = pdirs = cell(1, num_packages);
1059 for i = 1:num_packages 1123 for i = 1:num_packages