5801
|
1 ## Copyright (C) 2005 S�ren Hauberg |
|
2 ## |
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2 of the License, or |
|
6 ## (at your option) any later version. |
|
7 ## |
|
8 ## This program is distributed in the hope that it will be useful, |
|
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 ## GNU General Public License for more details. |
|
12 ## |
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this program; if not, write to the Free Software |
|
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
16 |
|
17 ## -*- texinfo -*- |
6032
|
18 ## @deftypefn {Command} pkg @var{command} @var{pkg_name} |
|
19 ## @deftypefnx {Command} pkg @var{command} @var{option} @var{pkg_name} |
|
20 ## This command interacts with the package manager. Different actions will |
|
21 ## be taking depending on the value of @var{command}. |
|
22 ## |
|
23 ## @table @samp |
|
24 ## @item install |
6070
|
25 ## Install named packages. For example, |
|
26 ## @example |
|
27 ## pkg install image-1.0.0.tar.gz |
|
28 ## @end example |
|
29 ## @noindent |
|
30 ## installs the package found in the file @code{image-1.0.0.tar.gz}. |
|
31 ## |
|
32 ## If @var{option} is @code{-nodeps} the package manager will disable the |
|
33 ## dependency checking. That way it is possible to install a package even |
|
34 ## if it depends on another package that's not installed on the system. |
|
35 ## @strong{Use this option with care.} |
6032
|
36 ## @item uninstall |
6070
|
37 ## Uninstall named packages. For example, |
|
38 ## @example |
|
39 ## pkg uninstall image |
|
40 ## @end example |
|
41 ## @noindent |
|
42 ## removes the @code{image} package from the system. If another installed |
|
43 ## package depends on the @code{image} package an error will be issued. |
|
44 ## The package can be uninstalled anyway by using the @code{-nodeps} option. |
6032
|
45 ## @item load |
6070
|
46 ## Add named packages to the path. After loading a package it is |
|
47 ## possible to use the functions provided by the package. For example, |
|
48 ## @example |
|
49 ## pkg load image |
|
50 ## @end example |
|
51 ## @noindent |
|
52 ## adds the @code{image} package to the path. It is possible to load all |
|
53 ## installed packages at once with the command |
|
54 ## @example |
|
55 ## pkg load all |
|
56 ## @end example |
6203
|
57 ## @item unload |
|
58 ## Removes named packages from the path. After unloading a package it is |
|
59 ## no longer possible to use the functions provided by the package. |
|
60 ## This command behaves like the @code{load} command. |
6032
|
61 ## @item list |
6070
|
62 ## Show a list of the currently installed packages. By requesting one or two |
|
63 ## output argument it is possible to get a list of the currently installed |
|
64 ## packages. For example, |
|
65 ## @example |
|
66 ## installed_packages = pkg list; |
|
67 ## @end example |
|
68 ## @noindent |
|
69 ## returns a cell array containing a structure for each installed package. |
|
70 ## The command |
|
71 ## @example |
|
72 ## [@var{user_packages}, @var{system_packages}] = pkg list |
|
73 ## @end example |
|
74 ## @noindent |
|
75 ## splits the list of installed packages into those who are installed by |
|
76 ## the current user, and those installed by the system administrator. |
6034
|
77 ## @item prefix |
6070
|
78 ## Set the installation prefix directory. For example, |
|
79 ## @example |
|
80 ## pkg prefix ~/my_octave_packages |
|
81 ## @end example |
|
82 ## @noindent |
|
83 ## sets the installation prefix to @code{~/my_octave_packages}. |
|
84 ## Packages will be installed in this directory. |
6034
|
85 ## |
6070
|
86 ## It is possible to get the current installation prefix by requesting an |
|
87 ## output argument. For example, |
|
88 ## @example |
|
89 ## p = pkg prefix |
|
90 ## @end example |
6189
|
91 ## @item local_list |
|
92 ## Set the file in which to look for information on the locally |
|
93 ## installed packages. Locally installed packages are those that are |
|
94 ## typically available only to the current user. For example |
|
95 ## @example |
|
96 ## pkg local_list ~/.octave_packages |
|
97 ## @end example |
|
98 ## It is possible to get the current value of local_list with the following |
|
99 ## @example |
|
100 ## pkg local_list |
|
101 ## @end example |
|
102 ## @item global_list |
|
103 ## Set the file in which to look for, for information on the globally |
|
104 ## installed packages. Globally installed packages are those that are |
|
105 ## typically available to all users. For example |
|
106 ## @example |
|
107 ## pkg global_list /usr/share/octave/octave_packages |
|
108 ## @end example |
|
109 ## It is possible to get the current value of global_list with the following |
|
110 ## @example |
|
111 ## pkg global_list |
|
112 ## @end example |
6032
|
113 ## @end table |
5801
|
114 ## @end deftypefn |
5947
|
115 |
|
116 ## PKG_ADD: mark_as_command pkg |
|
117 |
5801
|
118 function [local_packages, global_packages] = pkg(varargin) |
6233
|
119 ## Installation prefix (XXX: what should these be on windows?) |
6034
|
120 persistent prefix = -1; |
6189
|
121 persistent local_list = tilde_expand("~/.octave_packages"); |
|
122 persistent global_list = fullfile (OCTAVE_HOME (), "/share/octave/octave_packages"); |
6233
|
123 mlock; |
6189
|
124 |
6034
|
125 if (prefix == -1) |
|
126 if (issuperuser()) |
6233
|
127 prefix = fullfile (OCTAVE_HOME (), "/share/octave/packages"); |
6034
|
128 else |
6233
|
129 prefix = "~/octave"; |
6034
|
130 endif |
|
131 endif |
|
132 prefix = tilde_expand(prefix); |
6189
|
133 |
5801
|
134 ## Handle input |
|
135 if (length(varargin) == 0 || !iscellstr(varargin)) |
5928
|
136 print_usage(); |
5801
|
137 endif |
|
138 files = {}; |
|
139 deps = true; |
|
140 action = "none"; |
|
141 for i = 1:length(varargin) |
|
142 switch (varargin{i}) |
|
143 case "-nodeps" |
|
144 deps = false; |
6203
|
145 case {"list", "install", "uninstall", "load", "unload", ... |
|
146 "prefix", "local_list", "global_list"} |
5801
|
147 action = varargin{i}; |
|
148 otherwise |
|
149 files{end+1} = varargin{i}; |
|
150 endswitch |
|
151 endfor |
|
152 |
|
153 ## Take action |
|
154 switch (action) |
|
155 case "list" |
|
156 if (nargout == 0) |
6189
|
157 installed_packages(local_list, global_list); |
5801
|
158 elseif (nargout == 1) |
6189
|
159 local_packages = installed_packages(local_list, global_list); |
5801
|
160 elseif (nargout == 2) |
6189
|
161 [local_packages, global_packages] = installed_packages(local_list, global_list); |
5801
|
162 else |
6042
|
163 error("Too many output arguments requested."); |
5801
|
164 endif |
|
165 case "install" |
|
166 if (length(files) == 0) |
6042
|
167 error("You must specify at least one filename when calling 'pkg install'"); |
5801
|
168 endif |
6189
|
169 install(files, deps, prefix, local_list, global_list); |
5801
|
170 case "uninstall" |
|
171 if (length(files) == 0) |
6042
|
172 error("You must specify at least one package when calling 'pkg uninstall'"); |
5801
|
173 endif |
6189
|
174 uninstall(files, deps, local_list, global_list); |
5801
|
175 case "load" |
|
176 if (length(files) == 0) |
6042
|
177 error("You must specify at least one package or 'all' when calling 'pkg load'"); |
5801
|
178 endif |
6189
|
179 load_packages(files, deps, local_list, global_list); |
6203
|
180 case "unload" |
|
181 if (length(files) == 0) |
|
182 error("You must specify at least one package or 'all' when calling 'pkg unload'"); |
|
183 endif |
|
184 unload_packages(files, deps, local_list, global_list); |
6034
|
185 case "prefix" |
|
186 if (length(files) == 0 && nargout == 0) |
6035
|
187 disp(prefix); |
6034
|
188 elseif (length(files) == 0 && nargout == 1) |
|
189 local_packages = prefix; |
|
190 elseif (length(files) == 1 && nargout == 0 && ischar(files{1})) |
|
191 prefix = files{1}; |
6233
|
192 #if (!strcmp(prefix(end), "/")) prefix(end+1) = "/"; endif |
6034
|
193 else |
6189
|
194 error("You must specify a prefix directory, or request an output argument"); |
|
195 endif |
|
196 case "local_list" |
|
197 if (length(files) == 0 && nargout == 0) |
|
198 disp(local_list); |
|
199 elseif (length(files) == 0 && nargout == 1) |
|
200 local_packages = local_list; |
|
201 elseif (length(files) == 1 && nargout == 0 && ischar(files{1})) |
|
202 local_list = files{1}; |
|
203 else |
|
204 error("You must specify a local_list file, or request an output argument"); |
|
205 endif |
|
206 case "global_list" |
|
207 if (length(files) == 0 && nargout == 0) |
|
208 disp(global_list); |
|
209 elseif (length(files) == 0 && nargout == 1) |
|
210 local_packages = global_list; |
|
211 elseif (length(files) == 1 && nargout == 0 && ischar(files{1})) |
|
212 global_list = files{1}; |
|
213 else |
|
214 error("You must specify a global_list file, or request an output argument"); |
6034
|
215 endif |
5801
|
216 otherwise |
6042
|
217 error("You must specify a valid action for 'pkg'. See 'help pkg' for details"); |
5801
|
218 endswitch |
|
219 endfunction |
|
220 |
6189
|
221 function install(files, handle_deps, prefix, local_list, global_list) |
6034
|
222 global_install = issuperuser(); |
5801
|
223 |
|
224 # Check that the directory in prefix exist. If it doesn't: create it! |
|
225 if (!exist(prefix, "dir")) |
|
226 warning("Creating installation directory %s", prefix); |
|
227 [status, msg] = mkdir(prefix); |
|
228 if (status != 1) |
6042
|
229 error("Could not create installation directory: %s", msg); |
5801
|
230 endif |
|
231 endif |
|
232 |
|
233 ## Get the list of installed packages |
6189
|
234 [local_packages, global_packages] = installed_packages(local_list, |
|
235 global_list); |
|
236 installed_packages = {local_packages{:}, global_packages{:}}; |
5801
|
237 |
|
238 if (global_install) |
|
239 packages = global_packages; |
|
240 else |
|
241 packages = local_packages; |
|
242 endif |
|
243 |
|
244 ## Uncompress the packages and read the DESCRIPTION files |
5947
|
245 tmpdirs = packdirs = descriptions = {}; |
5801
|
246 try |
|
247 ## Unpack the package files and read the DESCRIPTION files |
|
248 packages_to_uninstall = []; |
|
249 for i = 1:length(files) |
|
250 tgz = files{i}; |
|
251 |
|
252 ## Create a temporary directory |
|
253 tmpdir = tmpnam(); |
5947
|
254 tmpdirs{end+1} = tmpdir; |
5801
|
255 [status, msg] = mkdir(tmpdir); |
|
256 if (status != 1) |
6042
|
257 error("Couldn't create temporary directory: %s", msg); |
5801
|
258 endif |
|
259 |
|
260 ## Uncompress the package |
|
261 untar(tgz, tmpdir); |
5928
|
262 |
5947
|
263 ## Get the name of the directories produced by tar |
5801
|
264 [dirlist, err, msg] = readdir(tmpdir); |
|
265 if (err) |
6042
|
266 error("Couldn't read directory produced by tar: %s", msg); |
5801
|
267 endif |
5987
|
268 |
|
269 if (length(dirlist) > 3) |
|
270 error("Bundles of packages are not allowed") |
|
271 endif |
5947
|
272 |
|
273 for k = 3:length(dirlist) # the two first entries of dirlist are "." and ".." |
6233
|
274 packdir = fullfile(tmpdir, dirlist{k}); |
5947
|
275 packdirs{end+1} = packdir; |
5801
|
276 |
5947
|
277 ## Make sure the package contains necessary files |
|
278 verify_directory(packdir); |
5928
|
279 |
5947
|
280 ## Read the DESCRIPTION file |
6233
|
281 filename = fullfile(packdir, "DESCRIPTION"); |
5947
|
282 desc = get_description(filename); |
5928
|
283 |
6233
|
284 ## Verify that package name corresponds with filename |
|
285 [dummy, nm] = fileparts(tgz); |
|
286 if ((length(nm) >= length(desc.name)) && |
|
287 ! strcmp(desc.name,nm(1:length(desc.name)))) |
|
288 error("Package name '%s' doesn't correspond to its filename '%s'", desc.name, nm); |
|
289 endif |
5971
|
290 |
5947
|
291 ## Set default installation directory |
6233
|
292 desc.dir = fullfile(prefix, [desc.name "-" desc.version]); |
5801
|
293 |
5947
|
294 ## Save desc |
|
295 descriptions{end+1} = desc; |
5801
|
296 |
5947
|
297 ## Are any of the new packages already installed? |
|
298 ## If so we'll remove the old version. |
|
299 for j = 1:length(packages) |
|
300 if (strcmp(packages{j}.name, desc.name)) |
|
301 packages_to_uninstall(end+1) = j; |
|
302 endif |
|
303 endfor |
|
304 endfor |
5801
|
305 endfor |
|
306 catch |
|
307 ## Something went wrong, delete tmpdirs |
|
308 for i = 1:length(tmpdirs) |
5928
|
309 rm_rf(tmpdirs{i}); |
5801
|
310 endfor |
|
311 error(lasterr()(8:end)); |
|
312 end_try_catch |
|
313 |
|
314 ## Check dependencies |
|
315 if (handle_deps) |
|
316 ok = true; |
|
317 error_text = ""; |
|
318 for i = 1:length(descriptions) |
|
319 desc = descriptions{i}; |
|
320 idx1 = complement(packages_to_uninstall, 1:length(installed_packages)); |
|
321 idx2 = complement(i, 1:length(descriptions)); |
|
322 pseudo_installed_packages = {installed_packages{idx1} descriptions{idx2}}; |
|
323 bad_deps = get_unsatisfied_deps(desc, pseudo_installed_packages); |
|
324 ## Are there any unsatisfied dependencies? |
|
325 if (!isempty(bad_deps)) |
|
326 ok = false; |
|
327 for i = 1:length(bad_deps) |
|
328 dep = bad_deps{i}; |
|
329 error_text = [error_text " " desc.name " needs " ... |
|
330 dep.package " " dep.operator " " ... |
|
331 dep.version "\n"]; |
|
332 endfor |
|
333 endif |
|
334 endfor |
|
335 |
|
336 ## Did we find any unsatisfied dependencies? |
|
337 if (!ok) |
|
338 error("The following dependencies where unsatisfied:\n %s", error_text); |
|
339 endif |
|
340 endif |
|
341 |
|
342 ## Prepare each package for installation |
|
343 try |
|
344 for i = 1:length(descriptions) |
|
345 desc = descriptions{i}; |
|
346 pdir = packdirs{i}; |
|
347 prepare_installation (desc, pdir); |
|
348 configure_make (desc, pdir); |
|
349 endfor |
|
350 catch |
|
351 ## Something went wrong, delete tmpdirs |
|
352 for i = 1:length(tmpdirs) |
5928
|
353 rm_rf(tmpdirs{i}); |
5801
|
354 endfor |
|
355 error(lasterr()(8:end)); |
|
356 end_try_catch |
|
357 |
|
358 ## Uninstall the packages that will be replaced |
|
359 try |
|
360 for i = packages_to_uninstall |
6189
|
361 uninstall({installed_packages{i}.name}, false, local_list, |
|
362 global_list); |
5801
|
363 endfor |
|
364 catch |
|
365 ## Something went wrong, delete tmpdirs |
|
366 for i = 1:length(tmpdirs) |
5928
|
367 rm_rf(tmpdirs{i}); |
5801
|
368 endfor |
|
369 error(lasterr()(8:end)); |
|
370 end_try_catch |
|
371 |
|
372 ## Install each package |
|
373 try |
|
374 for i = 1:length(descriptions) |
|
375 desc = descriptions{i}; |
|
376 pdir = packdirs{i}; |
5971
|
377 copy_files(desc, pdir); |
|
378 create_pkgadddel(desc, pdir, "PKG_ADD"); |
|
379 create_pkgadddel(desc, pdir, "PKG_DEL"); |
5801
|
380 finish_installation (desc, pdir) |
|
381 endfor |
|
382 catch |
|
383 ## Something went wrong, delete tmpdirs |
|
384 for i = 1:length(tmpdirs) |
5928
|
385 rm_rf(tmpdirs{i}); |
5801
|
386 endfor |
5971
|
387 for i = 1:length(descriptions) |
|
388 rm_rf(descriptions{i}.dir); |
|
389 endfor |
5801
|
390 error(lasterr()(8:end)); |
|
391 end_try_catch |
|
392 |
5971
|
393 ## Check if the installed directory is empty. If it is remove it |
|
394 ## from the list |
|
395 for i = length(descriptions):-1:1 |
|
396 if (dirempty(descriptions{i}.dir,{"packinfo","doc"})) |
|
397 rm_rf(descriptions{i}.dir); |
|
398 descriptions(i) = []; |
|
399 endif |
|
400 endfor |
|
401 |
|
402 ## Add the packages to the package list |
5801
|
403 try |
|
404 if (global_install) |
|
405 idx = complement(packages_to_uninstall, 1:length(global_packages)); |
|
406 global_packages = {global_packages{idx} descriptions{:}}; |
|
407 save(global_list, "global_packages"); |
|
408 else |
|
409 idx = complement(packages_to_uninstall, 1:length(local_packages)); |
|
410 local_packages = {local_packages{idx} descriptions{:}}; |
|
411 save(local_list, "local_packages"); |
|
412 endif |
|
413 catch |
|
414 ## Something went wrong, delete tmpdirs |
|
415 for i = 1:length(tmpdirs) |
5928
|
416 rm_rf(tmpdirs{i}); |
5801
|
417 endfor |
|
418 for i = 1:length(descriptions) |
5928
|
419 rm_rf(descriptions{i}.dir); |
5801
|
420 endfor |
|
421 if (global_install) |
6042
|
422 error("Couldn't append to %s: %s", global_list, lasterr()(8:end)); |
5801
|
423 else |
6042
|
424 error("Couldn't append to %s: %s", local_list, lasterr()(8:end)); |
5801
|
425 endif |
|
426 end_try_catch |
|
427 |
|
428 ## All is well, let's clean up |
|
429 for i = 1:length(tmpdirs) |
5928
|
430 [status, msg] = rm_rf(tmpdirs{i}); |
5801
|
431 if (status != 1) |
|
432 warning("Couldn't clean up after my self: %s\n", msg); |
|
433 endif |
|
434 endfor |
|
435 |
|
436 ## Add the newly installed packages to the path, so the user |
5971
|
437 ## can begin usings them. |
|
438 if (length(descriptions) > 0) |
|
439 dirs = cell(1, length(descriptions)); |
|
440 for i = 1:length(descriptions) |
5801
|
441 dirs{i} = descriptions{i}.dir; |
5971
|
442 endfor |
|
443 addpath(dirs{:}); |
|
444 endif |
5801
|
445 endfunction |
|
446 |
6189
|
447 function uninstall(pkgnames, handle_deps, local_list, global_list) |
5801
|
448 ## Get the list of installed packages |
6189
|
449 [local_packages, global_packages] = installed_packages(local_list, |
|
450 global_list); |
5801
|
451 if (issuperuser()) |
6189
|
452 installed_packages = {local_packages{:}, global_packages{:}}; |
5801
|
453 else |
|
454 installed_packages = local_packages; |
|
455 endif |
6042
|
456 |
5801
|
457 num_packages = length(installed_packages); |
|
458 delete_idx = []; |
|
459 for i = 1:num_packages |
|
460 cur_name = installed_packages{i}.name; |
|
461 if (any(strcmp(cur_name, pkgnames))) |
|
462 delete_idx(end+1) = i; |
|
463 endif |
|
464 endfor |
6042
|
465 |
5801
|
466 ## Are all the packages that should be uninstalled already installed? |
|
467 if (length(delete_idx) != length(pkgnames)) |
6233
|
468 if (issuperuser()) |
|
469 ## Try again for a locally installed package |
|
470 installed_packages = local_packages |
6189
|
471 |
6233
|
472 num_packages = length(installed_packages); |
|
473 delete_idx = []; |
|
474 for i = 1:num_packages |
|
475 cur_name = installed_packages{i}.name; |
|
476 if (any(strcmp(cur_name, pkgnames))) |
|
477 delete_idx(end+1) = i; |
|
478 endif |
|
479 endfor |
|
480 if (length(delete_idx) != length(pkgnames)) |
|
481 ## XXX: We should have a better error message |
|
482 error("Some of the packages you want to uninstall are not installed."); |
|
483 endif |
|
484 else |
|
485 ## XXX: We should have a better error message |
|
486 error("Some of the packages you want to uninstall are not installed."); |
|
487 endif |
5801
|
488 endif |
|
489 |
|
490 ## Compute the packages that will remain installed |
|
491 idx = complement(delete_idx, 1:num_packages); |
|
492 remaining_packages = {installed_packages{idx}}; |
|
493 |
|
494 ## Check dependencies |
|
495 if (handle_deps) |
|
496 error_text = ""; |
|
497 for i = 1:length(remaining_packages) |
|
498 desc = remaining_packages{i}; |
|
499 bad_deps = get_unsatisfied_deps(desc, remaining_packages); |
|
500 |
|
501 ## Will the uninstallation break any dependencies? |
|
502 if (!isempty(bad_deps)) |
|
503 for i = 1:length(bad_deps) |
|
504 dep = bad_deps{i}; |
|
505 error_text = [error_text " " desc.name " needs " ... |
|
506 dep.package " " dep.operator " " ... |
|
507 dep.version "\n"]; |
|
508 endfor |
|
509 endif |
|
510 endfor |
|
511 |
6042
|
512 if (! isempty(error_text)) |
5801
|
513 error("The following dependencies where unsatisfied:\n %s", error_text); |
|
514 endif |
|
515 endif |
|
516 |
|
517 ## Delete the directories containing the packages |
|
518 for i = delete_idx |
|
519 desc = installed_packages{i}; |
|
520 ## If an 'on_uninstall.m' exist, call it! |
6233
|
521 if (exist(fullfile(desc.dir, "packinfo", "on_uninstall.m"), "file")) |
5801
|
522 try |
|
523 wd = pwd(); |
6233
|
524 cd(fullfile(desc.dir, "packinfo")); |
5801
|
525 on_uninstall(desc); |
|
526 cd(wd); |
|
527 catch |
|
528 # XXX: Should this rather be an error? |
6042
|
529 warning("The 'on_uninstall' script retsurned the following error: %s", lasterr); |
5801
|
530 cd(wd); |
|
531 end_try_catch |
|
532 endif |
|
533 ## Do the actual deletion |
5928
|
534 rmpath(desc.dir); |
6233
|
535 if (exist (desc.dir, "dir")) |
|
536 [status, msg] = rm_rf(desc.dir); |
|
537 if (status != 1) |
|
538 error("Couldn't delete directory %s: %s", desc.dir, msg); |
|
539 endif |
|
540 else |
|
541 warning("Directory %s previously lost", desc.dir); |
|
542 endif |
5801
|
543 endfor |
|
544 |
|
545 ## Write a new ~/.octave_packages |
|
546 if (issuperuser()) |
|
547 if (length(remaining_packages) == 0) |
|
548 unlink(global_list); |
|
549 else |
|
550 global_packages = remaining_packages; |
|
551 save(global_list, "global_packages"); |
|
552 endif |
|
553 else |
|
554 if (length(remaining_packages) == 0) |
|
555 unlink(local_list); |
|
556 else |
|
557 local_packages = remaining_packages; |
|
558 save(local_list, "local_packages"); |
|
559 endif |
|
560 endif |
|
561 |
|
562 endfunction |
|
563 |
|
564 ########################################################## |
|
565 ## A U X I L A R Y F U N C T I O N S ## |
|
566 ########################################################## |
|
567 |
|
568 function prepare_installation(desc, packdir) |
|
569 ## Is there a pre_install to call? |
|
570 if (exist([packdir "pre_install.m"], "file")) |
|
571 wd = pwd(); |
|
572 try |
|
573 cd(packdir); |
|
574 pre_install(desc); |
|
575 cd(wd); |
|
576 catch |
|
577 cd(wd); |
6042
|
578 error("The pre-install function returned the following error: %s", lasterr); |
5801
|
579 end_try_catch |
|
580 endif |
|
581 |
5993
|
582 ## If the directory "inst" doesn't exist, we create it |
|
583 if (!exist([packdir "inst"], "dir")) |
5801
|
584 [status, msg] = mkdir([packdir "inst"]); |
|
585 if (status != 1) |
5928
|
586 rm_rf(desc.dir); |
6042
|
587 error("The 'inst' directory did not exist and could not be created: %s", msg); |
5801
|
588 endif |
|
589 endif |
|
590 endfunction |
|
591 |
|
592 function configure_make (desc, packdir) |
|
593 ## Perform ./configure, make, make install in "src" |
|
594 if (exist([packdir "src"], "dir")) |
|
595 src = [packdir "src/"]; |
|
596 ## configure |
6233
|
597 if (exist(fullfile(src, "configure"), "file")) |
5928
|
598 [status, output] = system(["cd " src " ;./configure --prefix=" desc.dir]); |
5801
|
599 if (status != 0) |
5928
|
600 rm_rf(desc.dir); |
6042
|
601 error("The configure script returned the following error: %s", output); |
5801
|
602 endif |
|
603 endif |
|
604 |
|
605 ## make |
6233
|
606 if (exist(fullfile(src, "Makefile"), "file")) |
5928
|
607 [status, output] = system(["export INSTALLDIR=" desc.dir "; make -C " src]); |
5801
|
608 if (status != 0) |
5928
|
609 rm_rf(desc.dir); |
6042
|
610 error("'make' returned the following error: %s", output); |
5801
|
611 endif |
|
612 %# make install |
5928
|
613 %[status, output] = system(["export INSTALLDIR=" desc.dir "; make install -C " src]); |
5801
|
614 %if (status != 0) |
5928
|
615 % rm_rf(desc.dir); |
6042
|
616 % error("'make install' returned the following error: %s", output); |
5801
|
617 %endif |
|
618 endif |
|
619 |
|
620 ## Copy files to "inst" (this is instead of 'make install') |
6233
|
621 files = fullfile(src, "FILES"); |
|
622 instdir = fullfile(packdir, "inst"); |
5801
|
623 if (exist(files, "file")) |
6233
|
624 ## Get file names |
5801
|
625 [fid, msg] = fopen(files, "r"); |
|
626 if (fid < 0) |
6042
|
627 error("Couldn't open %s: %s", files, msg); |
5801
|
628 endif |
5928
|
629 filenames = char(fread(fid))'; |
5801
|
630 fclose(fid); |
6233
|
631 ## Copy the files |
|
632 fn = split_by(filenames, "\n"); |
|
633 for i = 1:length(fn) |
|
634 fn{i} = fullfile(src, fn{i}); |
|
635 endfor |
|
636 filenames = sprintf("%s ", fn{:}); |
5801
|
637 else |
6233
|
638 m = dir(fullfile(src, "*.m")); |
|
639 oct = dir(fullfile(src, "*.oct")); |
|
640 filenames = ""; |
5801
|
641 if (length(m) > 0) |
6233
|
642 filenames = sprintf([src "%s "], m.name); |
5801
|
643 endif |
|
644 if (length(oct) > 0) |
6233
|
645 filenames = [filenames " " sprintf([src "%s "], oct.name)]; |
5928
|
646 endif |
5801
|
647 endif |
6233
|
648 |
|
649 if (!all(isspace(filenames))) |
|
650 mkdir(instdir); |
|
651 [status, output] = copyfile(filenames, instdir); |
|
652 if (status != 1) |
|
653 rm_rf(desc.dir); |
|
654 error("Couldn't copy files from 'src' to 'inst': %s", output); |
|
655 endif |
5801
|
656 endif |
|
657 endif |
|
658 endfunction |
|
659 |
5971
|
660 function pkg = extract_pkg (nm, pat) |
5955
|
661 fid = fopen (nm, "rt"); |
5971
|
662 pkg = ""; |
5955
|
663 if (fid >= 0) |
|
664 while (! feof(fid)) |
|
665 ln = fgetl (fid); |
|
666 if (ln > 0) |
6143
|
667 t = regexp(ln, pat, "tokens"); |
5955
|
668 if (!isempty(t)) |
5971
|
669 pkg = [pkg, "\n", t{1}{1}]; |
5955
|
670 endif |
|
671 endif |
|
672 endwhile |
5971
|
673 if (!isempty(pkg)) |
|
674 pkg = [pkg, "\n"]; |
5955
|
675 endif |
|
676 fclose (fid); |
|
677 endif |
|
678 endfunction |
|
679 |
5971
|
680 function create_pkgadddel (desc, packdir, nm) |
6233
|
681 pkg = fullfile(desc.dir, nm); |
5971
|
682 fid = fopen(pkg, "wt"); |
5976
|
683 |
5955
|
684 if (fid >= 0) |
5971
|
685 ## Search all dot-m files for PKG commands |
6233
|
686 lst = dir (fullfile(packdir, "inst", "*.m")); |
5955
|
687 for i=1:length(lst) |
6233
|
688 nam = fullfile(packdir, "inst", lst(i).name); |
5976
|
689 fwrite (fid, extract_pkg (nam, ['^[#%][#%]* *' nm ': *(.*)$'])); |
5955
|
690 endfor |
|
691 |
5971
|
692 ## Search all C++ source files for PKG commands |
6233
|
693 lst = dir (fullfile(packdir, "src", "*.cc")); |
5955
|
694 for i=1:length(lst) |
6233
|
695 nam = fullfile(packdir, "src", lst(i).name); |
5976
|
696 fwrite (fid, extract_pkg (nam, ['^//* *' nm ': *(.*)$'])); |
|
697 fwrite (fid, extract_pkg (nam, ['^/\** *' nm ': *(.*) *\*/$'])); |
5955
|
698 endfor |
|
699 |
5971
|
700 ## Add developer included PKG commands |
6233
|
701 packdirnm = fullfile(packdir, "nm"); |
|
702 if (exist(packdirnm, "file")) |
|
703 fid2 = fopen(packdirnm,"rt"); |
5955
|
704 if (fid2 >= 0) |
|
705 while (! feof(fid2)) |
|
706 ln = fgets (fid2); |
|
707 if (ln > 0) |
|
708 fwrite(fid, ln); |
|
709 endif |
|
710 endwhile |
5971
|
711 fclose(fid2); |
5955
|
712 endif |
|
713 endif |
|
714 fclose(fid); |
5971
|
715 |
|
716 ## If the file is empty remove it |
|
717 t = dir (pkg); |
|
718 if (t.bytes <= 0) |
|
719 unlink (pkg); |
|
720 endif |
5955
|
721 endif |
|
722 endfunction |
|
723 |
5971
|
724 function copy_files (desc, packdir, bindir) |
6020
|
725 ## Create the installation directory |
|
726 if (! exist (desc.dir, "dir")) |
6233
|
727 [status, output] = mkdir (desc.dir); |
|
728 if (status != 1) |
|
729 error("Couldn't create installation directory %s : %s", |
|
730 desc.dir, output); |
|
731 endif |
6020
|
732 endif |
|
733 |
5801
|
734 ## Copy the files from "inst" to installdir |
6233
|
735 instdir = fullfile(packdir, "inst"); |
|
736 if (!dirempty(instdir)) |
|
737 [status, output] = copyfile(fullfile(instdir, "*"), desc.dir); |
|
738 if (status != 1) |
5971
|
739 rm_rf(desc.dir); |
6042
|
740 error("Couldn't copy files to the installation directory"); |
5971
|
741 endif |
5801
|
742 endif |
|
743 |
|
744 ## Create the "packinfo" directory |
6233
|
745 packinfo = fullfile(desc.dir, "packinfo"); |
5801
|
746 [status, msg] = mkdir (packinfo); |
|
747 if (status != 1) |
5928
|
748 rm_rf(desc.dir); |
6042
|
749 error("Couldn't create packinfo directory: %s", msg); |
5801
|
750 endif |
|
751 |
|
752 ## Copy DESCRIPTION |
6233
|
753 [status, output] = copyfile(fullfile(packdir, "DESCRIPTION"), packinfo); |
|
754 if (status != 1) |
5928
|
755 rm_rf(desc.dir); |
6042
|
756 error("Couldn't copy DESCRIPTION: %s", output); |
5801
|
757 endif |
|
758 |
5993
|
759 ## Copy COPYING |
6233
|
760 [status, output] = copyfile(fullfile(packdir, "COPYING"), packinfo); |
|
761 if (status != 1) |
5993
|
762 rm_rf(desc.dir); |
6042
|
763 error("Couldn't copy COPYING: %s", output); |
5993
|
764 endif |
|
765 |
5801
|
766 ## Is there an INDEX file to copy or should we generate one? |
6233
|
767 fINDEX = fullfile(packdir, "INDEX"); |
|
768 if (exist(fINDEX, "file")) |
|
769 [status, output] = copyfile(fINDEX, packinfo); |
|
770 if (status != 1) |
5928
|
771 rm_rf(desc.dir); |
6042
|
772 error("Couldn't copy INDEX file: %s", output); |
5801
|
773 endif |
|
774 else |
|
775 try |
6233
|
776 write_INDEX(desc, fullfile(packdir, "inst"), fINDEX); |
5801
|
777 catch |
5928
|
778 rm_rf(desc.dir); |
5801
|
779 error(lasterr); |
|
780 end_try_catch |
|
781 endif |
|
782 |
|
783 ## Is there an 'on_uninstall.m' to install? |
6233
|
784 fon_uninstall = fullfile(packdir, "on_uninstall.m"); |
|
785 if (exist(fon_uninstall, "file")) |
|
786 [status, output] = copyfile(fon_uninstall, packinfo); |
|
787 if (status != 1) |
5928
|
788 rm_rf(desc.dir); |
6042
|
789 error("Couldn't copy on_uninstall.m: %s", output); |
5801
|
790 endif |
|
791 endif |
5971
|
792 |
|
793 ## Is there a doc/ directory that needs to be installed |
6233
|
794 docdir = fullfile(packdir, "doc"); |
|
795 if (exist(docdir, "dir") && !dirempty(docdir)) |
|
796 [status, output] = copyfile(docdir, desc.dir); |
5971
|
797 endif |
|
798 |
|
799 ## Is there a bin/ directory that needs to be installed |
6233
|
800 bindir = fullfile(packdir, "bin"); |
|
801 if (exist(bindir, "dir") && !dirempty(bindir)) |
|
802 [status, output] = copyfile(bindir, desc.dir); |
5971
|
803 endif |
5801
|
804 endfunction |
|
805 |
|
806 function finish_installation (desc, packdir) |
|
807 ## Is there a post-install to call? |
|
808 if (exist([packdir "post_install.m"], "file")) |
|
809 wd = pwd(); |
|
810 try |
|
811 cd(packdir); |
|
812 post_install(desc); |
|
813 cd(wd); |
|
814 catch |
|
815 cd(wd); |
5928
|
816 rm_rf(desc.dir); |
6042
|
817 error("The post_install function returned the following error: %s", lasterr); |
5801
|
818 end_try_catch |
|
819 endif |
|
820 endfunction |
|
821 |
|
822 function out = issuperuser() |
|
823 out = strcmp(getenv("USER"), "root"); |
|
824 endfunction |
|
825 |
|
826 ## This function makes sure the package contains the |
|
827 ## essential files. |
|
828 function verify_directory(dir) |
|
829 needed_files = {"COPYING", "DESCRIPTION"}; |
|
830 for f = needed_files |
6233
|
831 if (!exist(fullfile(dir, f{1}), "file")) |
5801
|
832 error("Package is missing file: %s", f{1}); |
|
833 endif |
|
834 endfor |
|
835 endfunction |
|
836 |
|
837 ## This function parses the DESCRIPTION file |
|
838 function desc = get_description(filename) |
6042
|
839 [fid, msg] = fopen(filename, "r"); |
5801
|
840 if (fid == -1) |
6042
|
841 error("The DESCRIPTION file %s could not be read: %s", filename, msg); |
5801
|
842 endif |
|
843 |
|
844 desc = struct(); |
|
845 |
|
846 line = fgetl(fid); |
|
847 while (line != -1) |
|
848 ## Comments |
|
849 if (line(1) == "#") |
|
850 # Do nothing |
|
851 ## Continuation lines |
|
852 elseif (isspace(line(1))) |
|
853 if (exist("keyword", "var") && isfield(desc, keyword)) |
|
854 desc.(keyword) = [desc.(keyword) " " rstrip(line)]; |
|
855 endif |
|
856 ## Keyword/value pair |
|
857 else |
|
858 colon = find(line == ":"); |
|
859 if (length(colon) == 0) |
|
860 disp("Skipping line."); |
|
861 else |
|
862 colon = colon(1); |
|
863 keyword = tolower(strip(line(1:colon-1))); |
|
864 value = strip(line(colon+1:end)); |
|
865 if (length(value) == 0) |
|
866 fclose(fid); |
6042
|
867 error("The keyword %s have empty value", desc.keywords{end}); |
5801
|
868 endif |
|
869 desc.(keyword) = value; |
|
870 endif |
|
871 endif |
|
872 line = fgetl(fid); |
|
873 endwhile |
|
874 fclose(fid); |
|
875 |
|
876 ## Make sure all is okay |
|
877 needed_fields = {"name", "version", "date", "title", ... |
|
878 "author", "maintainer", "description"}; |
|
879 for f = needed_fields |
|
880 if (!isfield(desc, f{1})) |
6042
|
881 error("Description is missing needed field %s", f{1}); |
5801
|
882 endif |
|
883 endfor |
|
884 desc.version = fix_version(desc.version); |
|
885 if (isfield(desc, "depends")) |
|
886 desc.depends = fix_depends(desc.depends); |
|
887 else |
|
888 desc.depends = ""; |
|
889 endif |
|
890 desc.name = tolower(desc.name); |
|
891 endfunction |
|
892 |
|
893 ## Makes sure the version string v is a valid x.y.z version string |
|
894 ## Examples: "0.1" => "0.1.0", "monkey" => error(...) |
|
895 function out = fix_version(v) |
|
896 dots = find(v == "."); |
|
897 if (length(dots) == 1) |
|
898 major = str2num(v(1:dots-1)); |
|
899 minor = str2num(v(dots+1:end)); |
|
900 if (length(major) != 0 && length(minor) != 0) |
|
901 out = sprintf("%d.%d.0", major, minor); |
|
902 return |
|
903 endif |
|
904 elseif (length(dots) == 2) |
|
905 major = str2num(v(1:dots(1)-1)); |
|
906 minor = str2num(v(dots(1)+1:dots(2)-1)); |
|
907 rev = str2num(v(dots(2)+1:end)); |
|
908 if (length(major) != 0 && length(minor) != 0 && length(rev) != 0) |
|
909 out = sprintf("%d.%d.%d", major, minor, rev); |
|
910 return |
|
911 endif |
|
912 endif |
6042
|
913 error("Bad version string: %s", v); |
5801
|
914 endfunction |
|
915 |
|
916 ## Makes sure the depends field is of the right format. |
|
917 ## This function returns a cell of structures with the following fields: |
|
918 ## package, version, operator |
|
919 function deps_cell = fix_depends(depends) |
|
920 deps = split_by(tolower(depends), ","); |
|
921 deps_cell = cell(1, length(deps)); |
|
922 |
|
923 ## For each dependency |
|
924 for i = 1:length(deps) |
|
925 dep = deps{i}; |
|
926 lpar = find(dep == "("); |
|
927 rpar = find(dep == ")"); |
|
928 ## Does the dependency specify a version |
|
929 ## Example: package(>= version) |
|
930 if (length(lpar) == 1 && length(rpar) == 1) |
|
931 package = tolower(strip(dep(1:lpar-1))); |
|
932 sub = dep( lpar(1)+1:rpar(1)-1 ); |
|
933 parts = split_by(sub, " "); |
|
934 idx = []; |
|
935 for r = 1:size(parts,1) |
|
936 if (length(parts{r}) > 0) |
|
937 idx(end+1) = r; |
|
938 endif |
|
939 endfor |
|
940 |
|
941 if (length(idx) != 2) |
|
942 error(["There's something wrong with the DESCRIPTION file. " ... |
|
943 "The dependency %s has the wrong syntax.\n"], dep); |
|
944 endif |
|
945 operator = parts{idx(1)}; |
|
946 if (!any(strcmp(operator, {">=", "<=", "=="}))) ## XXX: I belive we also support ">" and "<" |
6042
|
947 error("Unsupported operator: %s", operator); |
5801
|
948 endif |
|
949 version = fix_version(parts{idx(2)}); |
|
950 |
|
951 ## If no version is specified for the dependency |
|
952 ## we say that the version should be greater than |
|
953 ## or equal to 0.0.0 |
|
954 else |
|
955 package = tolower(strip(dep)); |
|
956 operator = ">="; |
|
957 version = "0.0.0"; |
|
958 endif |
|
959 deps_cell{i} = struct("package", package, "operator", operator, "version", version); |
|
960 endfor |
|
961 endfunction |
|
962 |
|
963 ## Strips the text of spaces from the right |
|
964 ## Example: " hello world " => " hello world" (XXX: is this the same as deblank?) |
|
965 function text = rstrip(text) |
|
966 chars = find(!isspace(text)); |
|
967 if (length(chars) > 0) |
|
968 text = text(chars(1):end); # XXX: shouldn't it be text = text(1:chars(end)); |
|
969 else |
|
970 text = ""; |
|
971 endif |
|
972 endfunction |
|
973 |
|
974 ## Strips the text of spaces from the left and the right |
|
975 ## Example: " hello world " => "hello world" |
|
976 function text = strip(text) |
|
977 chars = find(!isspace(text)); |
|
978 if (length(chars) > 0) |
|
979 text = text(chars(1):chars(end)); |
|
980 else |
|
981 text = ""; |
|
982 endif |
|
983 endfunction |
|
984 |
|
985 ## Splits the text into a cell array of strings by sep |
|
986 ## Example: "A, B" => {"A", "B"} (with sep = ",") |
|
987 function out = split_by(text, sep) |
|
988 text_matrix = split(text, sep); |
|
989 num_words = size(text_matrix,1); |
|
990 out = cell(num_words, 1); |
|
991 for i = 1:num_words |
|
992 out{i} = strip(text_matrix(i, :)); |
|
993 endfor |
|
994 endfunction |
|
995 |
|
996 ## Creates an INDEX file for a package that doesn't provide one. |
|
997 ## 'desc' describes the package. |
|
998 ## 'dir' is the 'inst' direcotyr in temporary directory. |
|
999 ## 'INDEX' is the name (including path) of resulting INDEX file. |
|
1000 function write_INDEX(desc, dir, INDEX) |
|
1001 ## Get names of functions in dir |
|
1002 [files, err, msg] = readdir(dir); |
|
1003 if (err) |
6042
|
1004 error("Couldn't read directory %s: %s", dir, msg); |
5801
|
1005 endif |
|
1006 |
|
1007 functions = {}; |
|
1008 for i = 1:length(files) |
|
1009 file = files{i}; |
|
1010 lf = length(file); |
|
1011 if (lf > 2 && strcmp( file(end-1:end), ".m" )) |
|
1012 functions{end+1} = file(1:end-2); |
|
1013 elseif (lf > 4 && strcmp( file(end-3:end), ".oct" )) |
|
1014 functions{end+1} = file(1:end-4); |
|
1015 endif |
|
1016 endfor |
|
1017 |
|
1018 ## Does desc have a categories field? |
|
1019 if (!isfield(desc, "categories")) |
6042
|
1020 error("The DESCRIPTION file must have a Categories field, when no INDEX file is given."); |
5801
|
1021 endif |
|
1022 categories = split_by(desc.categories, ","); |
|
1023 if (length(categories) < 1) |
6042
|
1024 error("The Category field is empty."); |
5801
|
1025 endif |
|
1026 |
|
1027 ## Write INDEX |
|
1028 fid = fopen(INDEX, "w"); |
|
1029 if (fid == -1) |
6042
|
1030 error("Couldn't open %s for writing.", INDEX); |
5801
|
1031 endif |
|
1032 fprintf(fid, "%s >> %s\n", desc.name, desc.title); |
|
1033 fprintf(fid, "%s\n", categories{1}); |
|
1034 fprintf(fid, " %s\n", functions{:}); |
|
1035 fclose(fid); |
|
1036 endfunction |
|
1037 |
|
1038 function bad_deps = get_unsatisfied_deps(desc, installed_packages) |
|
1039 bad_deps = {}; |
|
1040 |
|
1041 ## For each dependency |
|
1042 for i = 1:length(desc.depends) |
|
1043 dep = desc.depends{i}; |
|
1044 |
|
1045 ## Is the current dependency Octave? |
|
1046 if (strcmp(dep.package, "octave")) |
|
1047 if (!compare_versions(OCTAVE_VERSION, dep.version, dep.operator)) |
|
1048 bad_deps{end+1} = dep; |
|
1049 endif |
|
1050 ## Is the current dependency not Octave? |
|
1051 else |
|
1052 ok = false; |
|
1053 for i = 1:length(installed_packages) |
|
1054 cur_name = installed_packages{i}.name; |
|
1055 cur_version = installed_packages{i}.version; |
|
1056 if (strcmp(dep.package, cur_name) && compare_versions(cur_version, dep.version, dep.operator)) |
|
1057 ok = true; |
|
1058 break; |
|
1059 endif |
|
1060 endfor |
|
1061 if (!ok) |
|
1062 bad_deps{end+1} = dep; |
|
1063 endif |
|
1064 endif |
|
1065 endfor |
|
1066 endfunction |
|
1067 |
6189
|
1068 function [out1, out2] = installed_packages(local_list, global_list) |
5801
|
1069 ## Get the list of installed packages |
|
1070 try |
|
1071 local_packages = load(local_list).local_packages; |
|
1072 catch |
|
1073 local_packages = {}; |
|
1074 end_try_catch |
|
1075 try |
6189
|
1076 if (strcmp(local_list, global_list)) |
|
1077 global_packages = {}; |
|
1078 else |
|
1079 global_packages = load(global_list).global_packages; |
|
1080 endif |
5801
|
1081 catch |
|
1082 global_packages = {}; |
|
1083 end_try_catch |
|
1084 installed_packages = {local_packages{:} global_packages{:}}; |
|
1085 |
|
1086 ## Should we return something? |
|
1087 if (nargout == 2) |
|
1088 out1 = local_packages; |
|
1089 out2 = global_packages; |
|
1090 return; |
|
1091 elseif (nargout == 1) |
|
1092 out1 = installed_packages; |
|
1093 return; |
|
1094 endif |
|
1095 |
|
1096 ## We shouldn't return something, so we'll print something |
|
1097 num_packages = length(installed_packages); |
|
1098 if (num_packages == 0) |
|
1099 printf("No packages installed.\n"); |
|
1100 return; |
|
1101 endif |
|
1102 |
5987
|
1103 ## Compute the maximal lengths of name, version, and dir |
5801
|
1104 h1 = "Package Name"; |
|
1105 h2 = "Version"; |
|
1106 h3 = "Installation directory"; |
5987
|
1107 max_name_length = length(h1); |
|
1108 max_version_length = length(h2); |
|
1109 names = cell(num_packages,1); |
|
1110 for i = 1:num_packages |
|
1111 max_name_length = max(max_name_length, |
|
1112 length(installed_packages{i}.name)); |
|
1113 max_version_length = max(max_version_length, |
|
1114 length(installed_packages{i}.version)); |
|
1115 names{i} = installed_packages{i}.name; |
|
1116 endfor |
|
1117 h1 = postpad (h1, max_name_length,' '); |
|
1118 h2 = postpad (h2, max_version_length, ' ');; |
|
1119 |
|
1120 ## Print a header |
5801
|
1121 header = sprintf("%s | %s | %s\n", h1, h2, h3); |
|
1122 printf(header); |
|
1123 tmp = sprintf(repmat("-", 1, length(header)-1)); |
|
1124 tmp(length(h1)+2) = "+"; tmp(length(h1)+length(h2)+5) = "+"; |
|
1125 printf("%s\n", tmp); |
|
1126 |
|
1127 ## Print the packages |
|
1128 format = sprintf("%%%ds | %%%ds | %%s\n", max_name_length, max_version_length); |
5987
|
1129 [dummy, idx] = sort(names); |
5801
|
1130 for i = 1:num_packages |
5987
|
1131 cur_name = installed_packages{idx(i)}.name; |
|
1132 cur_version = installed_packages{idx(i)}.version; |
|
1133 cur_dir = installed_packages{idx(i)}.dir; |
5801
|
1134 printf(format, cur_name, cur_version, cur_dir); |
|
1135 endfor |
|
1136 endfunction |
|
1137 |
6189
|
1138 function load_packages(files, handle_deps, local_list, global_list) |
|
1139 installed_packages = installed_packages(local_list, global_list); |
5801
|
1140 num_packages = length(installed_packages); |
|
1141 |
6037
|
1142 ## Read package names and installdirs into a more convenient format |
|
1143 pnames = pdirs = cell(1, num_packages); |
|
1144 for i = 1:num_packages |
|
1145 pnames{i} = installed_packages{i}.name; |
|
1146 pdirs{i} = installed_packages{i}.dir; |
|
1147 pdeps{i} = installed_packages{i}.depends; |
|
1148 endfor |
|
1149 |
|
1150 ## load all |
5801
|
1151 if (length(files) == 1 && strcmp(files{1}, "all")) |
6037
|
1152 dirs = pdirs; |
|
1153 ## load package_name1 ... |
5801
|
1154 else |
6037
|
1155 dirs = {}; |
|
1156 for i = 1:length(files) |
|
1157 idx = strcmp(pnames, files{i}); |
|
1158 if (!any(idx)) |
6042
|
1159 error("Package %s is not installed", files{i}); |
6037
|
1160 endif |
|
1161 dirs{end+1} = pdirs{idx}; |
|
1162 if (handle_deps) |
|
1163 pdep = pdeps{idx}; |
|
1164 for j = 1:length(pdep) |
|
1165 depname = pdep{j}.package; |
|
1166 if (strcmp(depname, "octave")) continue; endif |
|
1167 idx = strcmp(pnames, depname); |
|
1168 if (!any(idx)) |
|
1169 error("Package %s could not be loaded since it depends on %s", ... |
|
1170 files{i}, depname); |
|
1171 endif |
|
1172 dirs{end+1} = pdirs{idx}; |
|
1173 endfor |
5971
|
1174 endif |
5801
|
1175 endfor |
6037
|
1176 dirs = unique(dirs); |
5801
|
1177 endif |
6037
|
1178 |
|
1179 ## Load the packages |
6025
|
1180 if (length(dirs) > 0) |
|
1181 addpath(dirs{:}); |
|
1182 endif |
5971
|
1183 |
|
1184 ## Add local binaries, if any, to the EXEC_PATH |
|
1185 for i = 1:length(dirs) |
6233
|
1186 if (exist (fullfile(dirs{i}, "bin"), "dir")) |
|
1187 EXEC_PATH ([fullfile(dirs{i}, "bin") ":" EXEC_PATH()]); |
5971
|
1188 endif |
|
1189 endfor |
5801
|
1190 endfunction |
5928
|
1191 |
6203
|
1192 function unload_packages(files, handle_deps, local_list, global_list) |
|
1193 installed_packages = installed_packages(local_list, global_list); |
|
1194 num_packages = length(installed_packages); |
|
1195 |
|
1196 ## Read package names and installdirs into a more convenient format |
|
1197 pnames = pdirs = cell(1, num_packages); |
|
1198 for i = 1:num_packages |
|
1199 pnames{i} = installed_packages{i}.name; |
|
1200 pdirs{i} = installed_packages{i}.dir; |
|
1201 pdeps{i} = installed_packages{i}.depends; |
|
1202 endfor |
|
1203 |
|
1204 ## Get the current octave path |
|
1205 p = split_by(path(), pathsep()); |
|
1206 |
|
1207 ## unload all |
|
1208 if (length(files) == 1 && strcmp(files{1}, "all")) |
|
1209 dirs = pdirs; |
|
1210 ## unload package_name1 ... |
|
1211 else |
|
1212 dirs = {}; |
|
1213 for i = 1:length(files) |
|
1214 idx = strcmp(pnames, files{i}); |
|
1215 if (!any(idx)) |
|
1216 error("Package %s is not installed", files{i}); |
|
1217 endif |
|
1218 dirs{end+1} = pdirs{idx}; |
|
1219 endfor |
|
1220 endif |
|
1221 |
|
1222 ## Unload the packages |
|
1223 for i = 1:length(dirs) |
|
1224 d = dirs{i}; |
|
1225 idx = strcmp(p, d); |
|
1226 if (any(idx)) |
|
1227 rmpath(d); |
|
1228 # XXX: We should also check if we need to remove items from EXEC_PATH |
|
1229 endif |
|
1230 endfor |
|
1231 endfunction |
|
1232 |
5928
|
1233 function [status_out, msg_out] = rm_rf (dir) |
|
1234 crr = confirm_recursive_rmdir (); |
|
1235 unwind_protect |
|
1236 confirm_recursive_rmdir (false); |
|
1237 [status, msg] = rmdir (dir, "s"); |
|
1238 unwind_protect_cleanup |
|
1239 confirm_recursive_rmdir (crr); |
|
1240 end_unwind_protect |
|
1241 if (nargout > 0) |
|
1242 status_out = status; |
|
1243 endif |
|
1244 if (nargout > 1) |
|
1245 msg_out = msg; |
|
1246 endif |
|
1247 endfunction |
5971
|
1248 |
|
1249 function emp = dirempty (nm, ign) |
|
1250 if (nargin < 2) |
|
1251 ign = {".",".."}; |
|
1252 else |
|
1253 ign = [{".",".."},ign]; |
|
1254 endif |
|
1255 l = dir (nm); |
|
1256 for i=1:length(l) |
|
1257 found = false; |
|
1258 for j=1:length(ign) |
|
1259 if (strcmp(l(i).name,ign{j})) |
|
1260 found = true; |
|
1261 break; |
|
1262 endif |
|
1263 endfor |
|
1264 if (!found) |
|
1265 emp = false; |
|
1266 return |
|
1267 endif |
|
1268 endfor |
|
1269 emp = true; |
|
1270 return; |
|
1271 endfunction |