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 |
6234
|
248 files = glob(files); |
5801
|
249 packages_to_uninstall = []; |
|
250 for i = 1:length(files) |
|
251 tgz = files{i}; |
|
252 |
|
253 ## Create a temporary directory |
|
254 tmpdir = tmpnam(); |
5947
|
255 tmpdirs{end+1} = tmpdir; |
5801
|
256 [status, msg] = mkdir(tmpdir); |
|
257 if (status != 1) |
6042
|
258 error("Couldn't create temporary directory: %s", msg); |
5801
|
259 endif |
|
260 |
|
261 ## Uncompress the package |
|
262 untar(tgz, tmpdir); |
5928
|
263 |
5947
|
264 ## Get the name of the directories produced by tar |
5801
|
265 [dirlist, err, msg] = readdir(tmpdir); |
|
266 if (err) |
6042
|
267 error("Couldn't read directory produced by tar: %s", msg); |
5801
|
268 endif |
5987
|
269 |
|
270 if (length(dirlist) > 3) |
|
271 error("Bundles of packages are not allowed") |
|
272 endif |
5947
|
273 |
|
274 for k = 3:length(dirlist) # the two first entries of dirlist are "." and ".." |
6233
|
275 packdir = fullfile(tmpdir, dirlist{k}); |
5947
|
276 packdirs{end+1} = packdir; |
5801
|
277 |
5947
|
278 ## Make sure the package contains necessary files |
|
279 verify_directory(packdir); |
5928
|
280 |
5947
|
281 ## Read the DESCRIPTION file |
6233
|
282 filename = fullfile(packdir, "DESCRIPTION"); |
5947
|
283 desc = get_description(filename); |
5928
|
284 |
6233
|
285 ## Verify that package name corresponds with filename |
|
286 [dummy, nm] = fileparts(tgz); |
|
287 if ((length(nm) >= length(desc.name)) && |
|
288 ! strcmp(desc.name,nm(1:length(desc.name)))) |
|
289 error("Package name '%s' doesn't correspond to its filename '%s'", desc.name, nm); |
|
290 endif |
5971
|
291 |
5947
|
292 ## Set default installation directory |
6233
|
293 desc.dir = fullfile(prefix, [desc.name "-" desc.version]); |
5801
|
294 |
5947
|
295 ## Save desc |
|
296 descriptions{end+1} = desc; |
5801
|
297 |
5947
|
298 ## Are any of the new packages already installed? |
|
299 ## If so we'll remove the old version. |
|
300 for j = 1:length(packages) |
|
301 if (strcmp(packages{j}.name, desc.name)) |
|
302 packages_to_uninstall(end+1) = j; |
|
303 endif |
|
304 endfor |
|
305 endfor |
5801
|
306 endfor |
|
307 catch |
|
308 ## Something went wrong, delete tmpdirs |
|
309 for i = 1:length(tmpdirs) |
5928
|
310 rm_rf(tmpdirs{i}); |
5801
|
311 endfor |
|
312 error(lasterr()(8:end)); |
|
313 end_try_catch |
|
314 |
|
315 ## Check dependencies |
|
316 if (handle_deps) |
|
317 ok = true; |
|
318 error_text = ""; |
|
319 for i = 1:length(descriptions) |
|
320 desc = descriptions{i}; |
|
321 idx1 = complement(packages_to_uninstall, 1:length(installed_packages)); |
|
322 idx2 = complement(i, 1:length(descriptions)); |
|
323 pseudo_installed_packages = {installed_packages{idx1} descriptions{idx2}}; |
|
324 bad_deps = get_unsatisfied_deps(desc, pseudo_installed_packages); |
|
325 ## Are there any unsatisfied dependencies? |
|
326 if (!isempty(bad_deps)) |
|
327 ok = false; |
|
328 for i = 1:length(bad_deps) |
|
329 dep = bad_deps{i}; |
|
330 error_text = [error_text " " desc.name " needs " ... |
|
331 dep.package " " dep.operator " " ... |
|
332 dep.version "\n"]; |
|
333 endfor |
|
334 endif |
|
335 endfor |
|
336 |
|
337 ## Did we find any unsatisfied dependencies? |
|
338 if (!ok) |
|
339 error("The following dependencies where unsatisfied:\n %s", error_text); |
|
340 endif |
|
341 endif |
|
342 |
|
343 ## Prepare each package for installation |
|
344 try |
|
345 for i = 1:length(descriptions) |
|
346 desc = descriptions{i}; |
|
347 pdir = packdirs{i}; |
|
348 prepare_installation (desc, pdir); |
|
349 configure_make (desc, pdir); |
|
350 endfor |
|
351 catch |
|
352 ## Something went wrong, delete tmpdirs |
|
353 for i = 1:length(tmpdirs) |
5928
|
354 rm_rf(tmpdirs{i}); |
5801
|
355 endfor |
|
356 error(lasterr()(8:end)); |
|
357 end_try_catch |
|
358 |
|
359 ## Uninstall the packages that will be replaced |
|
360 try |
|
361 for i = packages_to_uninstall |
6189
|
362 uninstall({installed_packages{i}.name}, false, local_list, |
6245
|
363 global_list); |
5801
|
364 endfor |
|
365 catch |
|
366 ## Something went wrong, delete tmpdirs |
|
367 for i = 1:length(tmpdirs) |
5928
|
368 rm_rf(tmpdirs{i}); |
5801
|
369 endfor |
|
370 error(lasterr()(8:end)); |
|
371 end_try_catch |
|
372 |
|
373 ## Install each package |
|
374 try |
|
375 for i = 1:length(descriptions) |
|
376 desc = descriptions{i}; |
|
377 pdir = packdirs{i}; |
6245
|
378 copy_files(desc, pdir); |
|
379 create_pkgadddel(desc, pdir, "PKG_ADD"); |
|
380 create_pkgadddel(desc, pdir, "PKG_DEL"); |
5801
|
381 finish_installation (desc, pdir) |
|
382 endfor |
|
383 catch |
|
384 ## Something went wrong, delete tmpdirs |
|
385 for i = 1:length(tmpdirs) |
5928
|
386 rm_rf(tmpdirs{i}); |
5801
|
387 endfor |
5971
|
388 for i = 1:length(descriptions) |
|
389 rm_rf(descriptions{i}.dir); |
|
390 endfor |
5801
|
391 error(lasterr()(8:end)); |
|
392 end_try_catch |
|
393 |
5971
|
394 ## Check if the installed directory is empty. If it is remove it |
|
395 ## from the list |
|
396 for i = length(descriptions):-1:1 |
|
397 if (dirempty(descriptions{i}.dir,{"packinfo","doc"})) |
6245
|
398 rm_rf(descriptions{i}.dir); |
|
399 descriptions(i) = []; |
5971
|
400 endif |
|
401 endfor |
|
402 |
|
403 ## Add the packages to the package list |
5801
|
404 try |
|
405 if (global_install) |
|
406 idx = complement(packages_to_uninstall, 1:length(global_packages)); |
|
407 global_packages = {global_packages{idx} descriptions{:}}; |
|
408 save(global_list, "global_packages"); |
|
409 else |
|
410 idx = complement(packages_to_uninstall, 1:length(local_packages)); |
|
411 local_packages = {local_packages{idx} descriptions{:}}; |
|
412 save(local_list, "local_packages"); |
|
413 endif |
|
414 catch |
|
415 ## Something went wrong, delete tmpdirs |
|
416 for i = 1:length(tmpdirs) |
5928
|
417 rm_rf(tmpdirs{i}); |
5801
|
418 endfor |
|
419 for i = 1:length(descriptions) |
5928
|
420 rm_rf(descriptions{i}.dir); |
5801
|
421 endfor |
|
422 if (global_install) |
6042
|
423 error("Couldn't append to %s: %s", global_list, lasterr()(8:end)); |
5801
|
424 else |
6042
|
425 error("Couldn't append to %s: %s", local_list, lasterr()(8:end)); |
5801
|
426 endif |
|
427 end_try_catch |
|
428 |
|
429 ## All is well, let's clean up |
|
430 for i = 1:length(tmpdirs) |
5928
|
431 [status, msg] = rm_rf(tmpdirs{i}); |
5801
|
432 if (status != 1) |
|
433 warning("Couldn't clean up after my self: %s\n", msg); |
|
434 endif |
|
435 endfor |
|
436 |
|
437 ## Add the newly installed packages to the path, so the user |
5971
|
438 ## can begin usings them. |
|
439 if (length(descriptions) > 0) |
|
440 dirs = cell(1, length(descriptions)); |
|
441 for i = 1:length(descriptions) |
5801
|
442 dirs{i} = descriptions{i}.dir; |
5971
|
443 endfor |
|
444 addpath(dirs{:}); |
|
445 endif |
5801
|
446 endfunction |
|
447 |
6189
|
448 function uninstall(pkgnames, handle_deps, local_list, global_list) |
5801
|
449 ## Get the list of installed packages |
6189
|
450 [local_packages, global_packages] = installed_packages(local_list, |
|
451 global_list); |
5801
|
452 if (issuperuser()) |
6189
|
453 installed_packages = {local_packages{:}, global_packages{:}}; |
5801
|
454 else |
|
455 installed_packages = local_packages; |
|
456 endif |
6042
|
457 |
5801
|
458 num_packages = length(installed_packages); |
|
459 delete_idx = []; |
|
460 for i = 1:num_packages |
|
461 cur_name = installed_packages{i}.name; |
|
462 if (any(strcmp(cur_name, pkgnames))) |
|
463 delete_idx(end+1) = i; |
|
464 endif |
|
465 endfor |
6042
|
466 |
5801
|
467 ## Are all the packages that should be uninstalled already installed? |
|
468 if (length(delete_idx) != length(pkgnames)) |
6233
|
469 if (issuperuser()) |
|
470 ## Try again for a locally installed package |
|
471 installed_packages = local_packages |
6189
|
472 |
6233
|
473 num_packages = length(installed_packages); |
|
474 delete_idx = []; |
|
475 for i = 1:num_packages |
|
476 cur_name = installed_packages{i}.name; |
|
477 if (any(strcmp(cur_name, pkgnames))) |
|
478 delete_idx(end+1) = i; |
|
479 endif |
|
480 endfor |
|
481 if (length(delete_idx) != length(pkgnames)) |
|
482 ## XXX: We should have a better error message |
|
483 error("Some of the packages you want to uninstall are not installed."); |
|
484 endif |
|
485 else |
|
486 ## XXX: We should have a better error message |
|
487 error("Some of the packages you want to uninstall are not installed."); |
|
488 endif |
5801
|
489 endif |
|
490 |
|
491 ## Compute the packages that will remain installed |
|
492 idx = complement(delete_idx, 1:num_packages); |
|
493 remaining_packages = {installed_packages{idx}}; |
|
494 |
|
495 ## Check dependencies |
|
496 if (handle_deps) |
|
497 error_text = ""; |
|
498 for i = 1:length(remaining_packages) |
|
499 desc = remaining_packages{i}; |
|
500 bad_deps = get_unsatisfied_deps(desc, remaining_packages); |
|
501 |
|
502 ## Will the uninstallation break any dependencies? |
|
503 if (!isempty(bad_deps)) |
|
504 for i = 1:length(bad_deps) |
|
505 dep = bad_deps{i}; |
|
506 error_text = [error_text " " desc.name " needs " ... |
|
507 dep.package " " dep.operator " " ... |
|
508 dep.version "\n"]; |
|
509 endfor |
|
510 endif |
|
511 endfor |
|
512 |
6042
|
513 if (! isempty(error_text)) |
5801
|
514 error("The following dependencies where unsatisfied:\n %s", error_text); |
|
515 endif |
|
516 endif |
|
517 |
|
518 ## Delete the directories containing the packages |
|
519 for i = delete_idx |
|
520 desc = installed_packages{i}; |
|
521 ## If an 'on_uninstall.m' exist, call it! |
6233
|
522 if (exist(fullfile(desc.dir, "packinfo", "on_uninstall.m"), "file")) |
5801
|
523 try |
|
524 wd = pwd(); |
6233
|
525 cd(fullfile(desc.dir, "packinfo")); |
5801
|
526 on_uninstall(desc); |
|
527 cd(wd); |
|
528 catch |
|
529 # XXX: Should this rather be an error? |
6042
|
530 warning("The 'on_uninstall' script retsurned the following error: %s", lasterr); |
5801
|
531 cd(wd); |
|
532 end_try_catch |
|
533 endif |
|
534 ## Do the actual deletion |
5928
|
535 rmpath(desc.dir); |
6233
|
536 if (exist (desc.dir, "dir")) |
|
537 [status, msg] = rm_rf(desc.dir); |
|
538 if (status != 1) |
|
539 error("Couldn't delete directory %s: %s", desc.dir, msg); |
|
540 endif |
|
541 else |
|
542 warning("Directory %s previously lost", desc.dir); |
|
543 endif |
5801
|
544 endfor |
|
545 |
|
546 ## Write a new ~/.octave_packages |
|
547 if (issuperuser()) |
|
548 if (length(remaining_packages) == 0) |
|
549 unlink(global_list); |
|
550 else |
|
551 global_packages = remaining_packages; |
|
552 save(global_list, "global_packages"); |
|
553 endif |
|
554 else |
|
555 if (length(remaining_packages) == 0) |
|
556 unlink(local_list); |
|
557 else |
|
558 local_packages = remaining_packages; |
|
559 save(local_list, "local_packages"); |
|
560 endif |
|
561 endif |
|
562 |
|
563 endfunction |
|
564 |
|
565 ########################################################## |
|
566 ## A U X I L A R Y F U N C T I O N S ## |
|
567 ########################################################## |
|
568 |
|
569 function prepare_installation(desc, packdir) |
|
570 ## Is there a pre_install to call? |
|
571 if (exist([packdir "pre_install.m"], "file")) |
|
572 wd = pwd(); |
|
573 try |
|
574 cd(packdir); |
|
575 pre_install(desc); |
|
576 cd(wd); |
|
577 catch |
|
578 cd(wd); |
6042
|
579 error("The pre-install function returned the following error: %s", lasterr); |
5801
|
580 end_try_catch |
|
581 endif |
|
582 |
5993
|
583 ## If the directory "inst" doesn't exist, we create it |
|
584 if (!exist([packdir "inst"], "dir")) |
5801
|
585 [status, msg] = mkdir([packdir "inst"]); |
|
586 if (status != 1) |
5928
|
587 rm_rf(desc.dir); |
6042
|
588 error("The 'inst' directory did not exist and could not be created: %s", msg); |
5801
|
589 endif |
|
590 endif |
|
591 endfunction |
|
592 |
|
593 function configure_make (desc, packdir) |
|
594 ## Perform ./configure, make, make install in "src" |
|
595 if (exist([packdir "src"], "dir")) |
|
596 src = [packdir "src/"]; |
|
597 ## configure |
6233
|
598 if (exist(fullfile(src, "configure"), "file")) |
5928
|
599 [status, output] = system(["cd " src " ;./configure --prefix=" desc.dir]); |
5801
|
600 if (status != 0) |
5928
|
601 rm_rf(desc.dir); |
6042
|
602 error("The configure script returned the following error: %s", output); |
5801
|
603 endif |
|
604 endif |
|
605 |
|
606 ## make |
6233
|
607 if (exist(fullfile(src, "Makefile"), "file")) |
5928
|
608 [status, output] = system(["export INSTALLDIR=" desc.dir "; make -C " src]); |
5801
|
609 if (status != 0) |
5928
|
610 rm_rf(desc.dir); |
6042
|
611 error("'make' returned the following error: %s", output); |
5801
|
612 endif |
|
613 %# make install |
5928
|
614 %[status, output] = system(["export INSTALLDIR=" desc.dir "; make install -C " src]); |
5801
|
615 %if (status != 0) |
5928
|
616 % rm_rf(desc.dir); |
6042
|
617 % error("'make install' returned the following error: %s", output); |
5801
|
618 %endif |
|
619 endif |
|
620 |
|
621 ## Copy files to "inst" (this is instead of 'make install') |
6233
|
622 files = fullfile(src, "FILES"); |
|
623 instdir = fullfile(packdir, "inst"); |
5801
|
624 if (exist(files, "file")) |
6233
|
625 ## Get file names |
5801
|
626 [fid, msg] = fopen(files, "r"); |
|
627 if (fid < 0) |
6042
|
628 error("Couldn't open %s: %s", files, msg); |
5801
|
629 endif |
5928
|
630 filenames = char(fread(fid))'; |
5801
|
631 fclose(fid); |
6233
|
632 ## Copy the files |
|
633 fn = split_by(filenames, "\n"); |
|
634 for i = 1:length(fn) |
|
635 fn{i} = fullfile(src, fn{i}); |
|
636 endfor |
|
637 filenames = sprintf("%s ", fn{:}); |
5801
|
638 else |
6233
|
639 m = dir(fullfile(src, "*.m")); |
|
640 oct = dir(fullfile(src, "*.oct")); |
|
641 filenames = ""; |
5801
|
642 if (length(m) > 0) |
6233
|
643 filenames = sprintf([src "%s "], m.name); |
5801
|
644 endif |
|
645 if (length(oct) > 0) |
6233
|
646 filenames = [filenames " " sprintf([src "%s "], oct.name)]; |
5928
|
647 endif |
5801
|
648 endif |
6233
|
649 |
|
650 if (!all(isspace(filenames))) |
|
651 mkdir(instdir); |
|
652 [status, output] = copyfile(filenames, instdir); |
|
653 if (status != 1) |
|
654 rm_rf(desc.dir); |
|
655 error("Couldn't copy files from 'src' to 'inst': %s", output); |
|
656 endif |
5801
|
657 endif |
|
658 endif |
|
659 endfunction |
|
660 |
5971
|
661 function pkg = extract_pkg (nm, pat) |
5955
|
662 fid = fopen (nm, "rt"); |
5971
|
663 pkg = ""; |
5955
|
664 if (fid >= 0) |
|
665 while (! feof(fid)) |
|
666 ln = fgetl (fid); |
|
667 if (ln > 0) |
6143
|
668 t = regexp(ln, pat, "tokens"); |
5955
|
669 if (!isempty(t)) |
5971
|
670 pkg = [pkg, "\n", t{1}{1}]; |
5955
|
671 endif |
|
672 endif |
|
673 endwhile |
5971
|
674 if (!isempty(pkg)) |
|
675 pkg = [pkg, "\n"]; |
5955
|
676 endif |
|
677 fclose (fid); |
|
678 endif |
|
679 endfunction |
|
680 |
5971
|
681 function create_pkgadddel (desc, packdir, nm) |
6233
|
682 pkg = fullfile(desc.dir, nm); |
5971
|
683 fid = fopen(pkg, "wt"); |
5976
|
684 |
5955
|
685 if (fid >= 0) |
5971
|
686 ## Search all dot-m files for PKG commands |
6233
|
687 lst = dir (fullfile(packdir, "inst", "*.m")); |
5955
|
688 for i=1:length(lst) |
6233
|
689 nam = fullfile(packdir, "inst", lst(i).name); |
5976
|
690 fwrite (fid, extract_pkg (nam, ['^[#%][#%]* *' nm ': *(.*)$'])); |
5955
|
691 endfor |
|
692 |
5971
|
693 ## Search all C++ source files for PKG commands |
6233
|
694 lst = dir (fullfile(packdir, "src", "*.cc")); |
5955
|
695 for i=1:length(lst) |
6233
|
696 nam = fullfile(packdir, "src", lst(i).name); |
5976
|
697 fwrite (fid, extract_pkg (nam, ['^//* *' nm ': *(.*)$'])); |
|
698 fwrite (fid, extract_pkg (nam, ['^/\** *' nm ': *(.*) *\*/$'])); |
5955
|
699 endfor |
|
700 |
5971
|
701 ## Add developer included PKG commands |
6245
|
702 packdirnm = fullfile(packdir, nm); |
6233
|
703 if (exist(packdirnm, "file")) |
|
704 fid2 = fopen(packdirnm,"rt"); |
5955
|
705 if (fid2 >= 0) |
|
706 while (! feof(fid2)) |
|
707 ln = fgets (fid2); |
|
708 if (ln > 0) |
|
709 fwrite(fid, ln); |
|
710 endif |
|
711 endwhile |
5971
|
712 fclose(fid2); |
5955
|
713 endif |
|
714 endif |
|
715 fclose(fid); |
5971
|
716 |
|
717 ## If the file is empty remove it |
|
718 t = dir (pkg); |
|
719 if (t.bytes <= 0) |
|
720 unlink (pkg); |
|
721 endif |
5955
|
722 endif |
|
723 endfunction |
|
724 |
5971
|
725 function copy_files (desc, packdir, bindir) |
6020
|
726 ## Create the installation directory |
|
727 if (! exist (desc.dir, "dir")) |
6233
|
728 [status, output] = mkdir (desc.dir); |
|
729 if (status != 1) |
|
730 error("Couldn't create installation directory %s : %s", |
|
731 desc.dir, output); |
|
732 endif |
6020
|
733 endif |
|
734 |
5801
|
735 ## Copy the files from "inst" to installdir |
6233
|
736 instdir = fullfile(packdir, "inst"); |
|
737 if (!dirempty(instdir)) |
|
738 [status, output] = copyfile(fullfile(instdir, "*"), desc.dir); |
|
739 if (status != 1) |
5971
|
740 rm_rf(desc.dir); |
6042
|
741 error("Couldn't copy files to the installation directory"); |
5971
|
742 endif |
5801
|
743 endif |
|
744 |
|
745 ## Create the "packinfo" directory |
6233
|
746 packinfo = fullfile(desc.dir, "packinfo"); |
5801
|
747 [status, msg] = mkdir (packinfo); |
|
748 if (status != 1) |
5928
|
749 rm_rf(desc.dir); |
6042
|
750 error("Couldn't create packinfo directory: %s", msg); |
5801
|
751 endif |
|
752 |
|
753 ## Copy DESCRIPTION |
6233
|
754 [status, output] = copyfile(fullfile(packdir, "DESCRIPTION"), packinfo); |
|
755 if (status != 1) |
5928
|
756 rm_rf(desc.dir); |
6042
|
757 error("Couldn't copy DESCRIPTION: %s", output); |
5801
|
758 endif |
|
759 |
5993
|
760 ## Copy COPYING |
6233
|
761 [status, output] = copyfile(fullfile(packdir, "COPYING"), packinfo); |
|
762 if (status != 1) |
5993
|
763 rm_rf(desc.dir); |
6042
|
764 error("Couldn't copy COPYING: %s", output); |
5993
|
765 endif |
|
766 |
5801
|
767 ## Is there an INDEX file to copy or should we generate one? |
6233
|
768 fINDEX = fullfile(packdir, "INDEX"); |
|
769 if (exist(fINDEX, "file")) |
|
770 [status, output] = copyfile(fINDEX, packinfo); |
|
771 if (status != 1) |
5928
|
772 rm_rf(desc.dir); |
6042
|
773 error("Couldn't copy INDEX file: %s", output); |
5801
|
774 endif |
|
775 else |
|
776 try |
6233
|
777 write_INDEX(desc, fullfile(packdir, "inst"), fINDEX); |
5801
|
778 catch |
5928
|
779 rm_rf(desc.dir); |
5801
|
780 error(lasterr); |
|
781 end_try_catch |
|
782 endif |
|
783 |
|
784 ## Is there an 'on_uninstall.m' to install? |
6233
|
785 fon_uninstall = fullfile(packdir, "on_uninstall.m"); |
|
786 if (exist(fon_uninstall, "file")) |
|
787 [status, output] = copyfile(fon_uninstall, packinfo); |
|
788 if (status != 1) |
5928
|
789 rm_rf(desc.dir); |
6042
|
790 error("Couldn't copy on_uninstall.m: %s", output); |
5801
|
791 endif |
|
792 endif |
5971
|
793 |
|
794 ## Is there a doc/ directory that needs to be installed |
6233
|
795 docdir = fullfile(packdir, "doc"); |
|
796 if (exist(docdir, "dir") && !dirempty(docdir)) |
|
797 [status, output] = copyfile(docdir, desc.dir); |
5971
|
798 endif |
|
799 |
|
800 ## Is there a bin/ directory that needs to be installed |
6233
|
801 bindir = fullfile(packdir, "bin"); |
|
802 if (exist(bindir, "dir") && !dirempty(bindir)) |
|
803 [status, output] = copyfile(bindir, desc.dir); |
5971
|
804 endif |
5801
|
805 endfunction |
|
806 |
|
807 function finish_installation (desc, packdir) |
|
808 ## Is there a post-install to call? |
|
809 if (exist([packdir "post_install.m"], "file")) |
|
810 wd = pwd(); |
|
811 try |
|
812 cd(packdir); |
|
813 post_install(desc); |
|
814 cd(wd); |
|
815 catch |
|
816 cd(wd); |
5928
|
817 rm_rf(desc.dir); |
6042
|
818 error("The post_install function returned the following error: %s", lasterr); |
5801
|
819 end_try_catch |
|
820 endif |
|
821 endfunction |
|
822 |
|
823 function out = issuperuser() |
|
824 out = strcmp(getenv("USER"), "root"); |
|
825 endfunction |
|
826 |
|
827 ## This function makes sure the package contains the |
|
828 ## essential files. |
|
829 function verify_directory(dir) |
|
830 needed_files = {"COPYING", "DESCRIPTION"}; |
|
831 for f = needed_files |
6233
|
832 if (!exist(fullfile(dir, f{1}), "file")) |
5801
|
833 error("Package is missing file: %s", f{1}); |
|
834 endif |
|
835 endfor |
|
836 endfunction |
|
837 |
|
838 ## This function parses the DESCRIPTION file |
|
839 function desc = get_description(filename) |
6042
|
840 [fid, msg] = fopen(filename, "r"); |
5801
|
841 if (fid == -1) |
6042
|
842 error("The DESCRIPTION file %s could not be read: %s", filename, msg); |
5801
|
843 endif |
|
844 |
|
845 desc = struct(); |
|
846 |
|
847 line = fgetl(fid); |
|
848 while (line != -1) |
|
849 ## Comments |
|
850 if (line(1) == "#") |
|
851 # Do nothing |
|
852 ## Continuation lines |
|
853 elseif (isspace(line(1))) |
|
854 if (exist("keyword", "var") && isfield(desc, keyword)) |
|
855 desc.(keyword) = [desc.(keyword) " " rstrip(line)]; |
|
856 endif |
|
857 ## Keyword/value pair |
|
858 else |
|
859 colon = find(line == ":"); |
|
860 if (length(colon) == 0) |
|
861 disp("Skipping line."); |
|
862 else |
|
863 colon = colon(1); |
|
864 keyword = tolower(strip(line(1:colon-1))); |
|
865 value = strip(line(colon+1:end)); |
|
866 if (length(value) == 0) |
|
867 fclose(fid); |
6042
|
868 error("The keyword %s have empty value", desc.keywords{end}); |
5801
|
869 endif |
|
870 desc.(keyword) = value; |
|
871 endif |
|
872 endif |
|
873 line = fgetl(fid); |
|
874 endwhile |
|
875 fclose(fid); |
|
876 |
|
877 ## Make sure all is okay |
|
878 needed_fields = {"name", "version", "date", "title", ... |
|
879 "author", "maintainer", "description"}; |
|
880 for f = needed_fields |
|
881 if (!isfield(desc, f{1})) |
6042
|
882 error("Description is missing needed field %s", f{1}); |
5801
|
883 endif |
|
884 endfor |
|
885 desc.version = fix_version(desc.version); |
|
886 if (isfield(desc, "depends")) |
|
887 desc.depends = fix_depends(desc.depends); |
|
888 else |
|
889 desc.depends = ""; |
|
890 endif |
|
891 desc.name = tolower(desc.name); |
|
892 endfunction |
|
893 |
|
894 ## Makes sure the version string v is a valid x.y.z version string |
|
895 ## Examples: "0.1" => "0.1.0", "monkey" => error(...) |
|
896 function out = fix_version(v) |
|
897 dots = find(v == "."); |
|
898 if (length(dots) == 1) |
|
899 major = str2num(v(1:dots-1)); |
|
900 minor = str2num(v(dots+1:end)); |
|
901 if (length(major) != 0 && length(minor) != 0) |
|
902 out = sprintf("%d.%d.0", major, minor); |
|
903 return |
|
904 endif |
|
905 elseif (length(dots) == 2) |
|
906 major = str2num(v(1:dots(1)-1)); |
|
907 minor = str2num(v(dots(1)+1:dots(2)-1)); |
|
908 rev = str2num(v(dots(2)+1:end)); |
|
909 if (length(major) != 0 && length(minor) != 0 && length(rev) != 0) |
|
910 out = sprintf("%d.%d.%d", major, minor, rev); |
|
911 return |
|
912 endif |
|
913 endif |
6042
|
914 error("Bad version string: %s", v); |
5801
|
915 endfunction |
|
916 |
|
917 ## Makes sure the depends field is of the right format. |
|
918 ## This function returns a cell of structures with the following fields: |
|
919 ## package, version, operator |
|
920 function deps_cell = fix_depends(depends) |
|
921 deps = split_by(tolower(depends), ","); |
|
922 deps_cell = cell(1, length(deps)); |
|
923 |
|
924 ## For each dependency |
|
925 for i = 1:length(deps) |
|
926 dep = deps{i}; |
|
927 lpar = find(dep == "("); |
|
928 rpar = find(dep == ")"); |
|
929 ## Does the dependency specify a version |
|
930 ## Example: package(>= version) |
|
931 if (length(lpar) == 1 && length(rpar) == 1) |
|
932 package = tolower(strip(dep(1:lpar-1))); |
|
933 sub = dep( lpar(1)+1:rpar(1)-1 ); |
|
934 parts = split_by(sub, " "); |
|
935 idx = []; |
|
936 for r = 1:size(parts,1) |
|
937 if (length(parts{r}) > 0) |
|
938 idx(end+1) = r; |
|
939 endif |
|
940 endfor |
|
941 |
|
942 if (length(idx) != 2) |
|
943 error(["There's something wrong with the DESCRIPTION file. " ... |
|
944 "The dependency %s has the wrong syntax.\n"], dep); |
|
945 endif |
|
946 operator = parts{idx(1)}; |
|
947 if (!any(strcmp(operator, {">=", "<=", "=="}))) ## XXX: I belive we also support ">" and "<" |
6042
|
948 error("Unsupported operator: %s", operator); |
5801
|
949 endif |
|
950 version = fix_version(parts{idx(2)}); |
|
951 |
|
952 ## If no version is specified for the dependency |
|
953 ## we say that the version should be greater than |
|
954 ## or equal to 0.0.0 |
|
955 else |
|
956 package = tolower(strip(dep)); |
|
957 operator = ">="; |
|
958 version = "0.0.0"; |
|
959 endif |
|
960 deps_cell{i} = struct("package", package, "operator", operator, "version", version); |
|
961 endfor |
|
962 endfunction |
|
963 |
|
964 ## Strips the text of spaces from the right |
|
965 ## Example: " hello world " => " hello world" (XXX: is this the same as deblank?) |
|
966 function text = rstrip(text) |
|
967 chars = find(!isspace(text)); |
|
968 if (length(chars) > 0) |
|
969 text = text(chars(1):end); # XXX: shouldn't it be text = text(1:chars(end)); |
|
970 else |
|
971 text = ""; |
|
972 endif |
|
973 endfunction |
|
974 |
|
975 ## Strips the text of spaces from the left and the right |
|
976 ## Example: " hello world " => "hello world" |
|
977 function text = strip(text) |
|
978 chars = find(!isspace(text)); |
|
979 if (length(chars) > 0) |
|
980 text = text(chars(1):chars(end)); |
|
981 else |
|
982 text = ""; |
|
983 endif |
|
984 endfunction |
|
985 |
|
986 ## Splits the text into a cell array of strings by sep |
|
987 ## Example: "A, B" => {"A", "B"} (with sep = ",") |
|
988 function out = split_by(text, sep) |
|
989 text_matrix = split(text, sep); |
|
990 num_words = size(text_matrix,1); |
|
991 out = cell(num_words, 1); |
|
992 for i = 1:num_words |
|
993 out{i} = strip(text_matrix(i, :)); |
|
994 endfor |
|
995 endfunction |
|
996 |
|
997 ## Creates an INDEX file for a package that doesn't provide one. |
|
998 ## 'desc' describes the package. |
|
999 ## 'dir' is the 'inst' direcotyr in temporary directory. |
|
1000 ## 'INDEX' is the name (including path) of resulting INDEX file. |
|
1001 function write_INDEX(desc, dir, INDEX) |
|
1002 ## Get names of functions in dir |
|
1003 [files, err, msg] = readdir(dir); |
|
1004 if (err) |
6042
|
1005 error("Couldn't read directory %s: %s", dir, msg); |
5801
|
1006 endif |
|
1007 |
|
1008 functions = {}; |
|
1009 for i = 1:length(files) |
|
1010 file = files{i}; |
|
1011 lf = length(file); |
|
1012 if (lf > 2 && strcmp( file(end-1:end), ".m" )) |
|
1013 functions{end+1} = file(1:end-2); |
|
1014 elseif (lf > 4 && strcmp( file(end-3:end), ".oct" )) |
|
1015 functions{end+1} = file(1:end-4); |
|
1016 endif |
|
1017 endfor |
|
1018 |
|
1019 ## Does desc have a categories field? |
|
1020 if (!isfield(desc, "categories")) |
6042
|
1021 error("The DESCRIPTION file must have a Categories field, when no INDEX file is given."); |
5801
|
1022 endif |
|
1023 categories = split_by(desc.categories, ","); |
|
1024 if (length(categories) < 1) |
6042
|
1025 error("The Category field is empty."); |
5801
|
1026 endif |
|
1027 |
|
1028 ## Write INDEX |
|
1029 fid = fopen(INDEX, "w"); |
|
1030 if (fid == -1) |
6042
|
1031 error("Couldn't open %s for writing.", INDEX); |
5801
|
1032 endif |
|
1033 fprintf(fid, "%s >> %s\n", desc.name, desc.title); |
|
1034 fprintf(fid, "%s\n", categories{1}); |
|
1035 fprintf(fid, " %s\n", functions{:}); |
|
1036 fclose(fid); |
|
1037 endfunction |
|
1038 |
|
1039 function bad_deps = get_unsatisfied_deps(desc, installed_packages) |
|
1040 bad_deps = {}; |
|
1041 |
|
1042 ## For each dependency |
|
1043 for i = 1:length(desc.depends) |
|
1044 dep = desc.depends{i}; |
|
1045 |
|
1046 ## Is the current dependency Octave? |
|
1047 if (strcmp(dep.package, "octave")) |
|
1048 if (!compare_versions(OCTAVE_VERSION, dep.version, dep.operator)) |
|
1049 bad_deps{end+1} = dep; |
|
1050 endif |
|
1051 ## Is the current dependency not Octave? |
|
1052 else |
|
1053 ok = false; |
|
1054 for i = 1:length(installed_packages) |
|
1055 cur_name = installed_packages{i}.name; |
|
1056 cur_version = installed_packages{i}.version; |
|
1057 if (strcmp(dep.package, cur_name) && compare_versions(cur_version, dep.version, dep.operator)) |
|
1058 ok = true; |
|
1059 break; |
|
1060 endif |
|
1061 endfor |
|
1062 if (!ok) |
|
1063 bad_deps{end+1} = dep; |
|
1064 endif |
|
1065 endif |
|
1066 endfor |
|
1067 endfunction |
|
1068 |
6189
|
1069 function [out1, out2] = installed_packages(local_list, global_list) |
5801
|
1070 ## Get the list of installed packages |
|
1071 try |
|
1072 local_packages = load(local_list).local_packages; |
|
1073 catch |
|
1074 local_packages = {}; |
|
1075 end_try_catch |
|
1076 try |
6189
|
1077 if (strcmp(local_list, global_list)) |
|
1078 global_packages = {}; |
|
1079 else |
|
1080 global_packages = load(global_list).global_packages; |
|
1081 endif |
5801
|
1082 catch |
|
1083 global_packages = {}; |
|
1084 end_try_catch |
|
1085 installed_packages = {local_packages{:} global_packages{:}}; |
|
1086 |
|
1087 ## Should we return something? |
|
1088 if (nargout == 2) |
|
1089 out1 = local_packages; |
|
1090 out2 = global_packages; |
|
1091 return; |
|
1092 elseif (nargout == 1) |
|
1093 out1 = installed_packages; |
|
1094 return; |
|
1095 endif |
|
1096 |
|
1097 ## We shouldn't return something, so we'll print something |
|
1098 num_packages = length(installed_packages); |
|
1099 if (num_packages == 0) |
|
1100 printf("No packages installed.\n"); |
|
1101 return; |
|
1102 endif |
|
1103 |
5987
|
1104 ## Compute the maximal lengths of name, version, and dir |
5801
|
1105 h1 = "Package Name"; |
|
1106 h2 = "Version"; |
|
1107 h3 = "Installation directory"; |
5987
|
1108 max_name_length = length(h1); |
|
1109 max_version_length = length(h2); |
|
1110 names = cell(num_packages,1); |
|
1111 for i = 1:num_packages |
|
1112 max_name_length = max(max_name_length, |
|
1113 length(installed_packages{i}.name)); |
|
1114 max_version_length = max(max_version_length, |
|
1115 length(installed_packages{i}.version)); |
|
1116 names{i} = installed_packages{i}.name; |
|
1117 endfor |
|
1118 h1 = postpad (h1, max_name_length,' '); |
|
1119 h2 = postpad (h2, max_version_length, ' ');; |
|
1120 |
|
1121 ## Print a header |
5801
|
1122 header = sprintf("%s | %s | %s\n", h1, h2, h3); |
|
1123 printf(header); |
|
1124 tmp = sprintf(repmat("-", 1, length(header)-1)); |
|
1125 tmp(length(h1)+2) = "+"; tmp(length(h1)+length(h2)+5) = "+"; |
|
1126 printf("%s\n", tmp); |
|
1127 |
|
1128 ## Print the packages |
|
1129 format = sprintf("%%%ds | %%%ds | %%s\n", max_name_length, max_version_length); |
5987
|
1130 [dummy, idx] = sort(names); |
5801
|
1131 for i = 1:num_packages |
5987
|
1132 cur_name = installed_packages{idx(i)}.name; |
|
1133 cur_version = installed_packages{idx(i)}.version; |
|
1134 cur_dir = installed_packages{idx(i)}.dir; |
5801
|
1135 printf(format, cur_name, cur_version, cur_dir); |
|
1136 endfor |
|
1137 endfunction |
|
1138 |
6189
|
1139 function load_packages(files, handle_deps, local_list, global_list) |
|
1140 installed_packages = installed_packages(local_list, global_list); |
5801
|
1141 num_packages = length(installed_packages); |
|
1142 |
6037
|
1143 ## Read package names and installdirs into a more convenient format |
|
1144 pnames = pdirs = cell(1, num_packages); |
|
1145 for i = 1:num_packages |
|
1146 pnames{i} = installed_packages{i}.name; |
|
1147 pdirs{i} = installed_packages{i}.dir; |
|
1148 pdeps{i} = installed_packages{i}.depends; |
|
1149 endfor |
|
1150 |
|
1151 ## load all |
5801
|
1152 if (length(files) == 1 && strcmp(files{1}, "all")) |
6037
|
1153 dirs = pdirs; |
|
1154 ## load package_name1 ... |
5801
|
1155 else |
6037
|
1156 dirs = {}; |
|
1157 for i = 1:length(files) |
|
1158 idx = strcmp(pnames, files{i}); |
|
1159 if (!any(idx)) |
6042
|
1160 error("Package %s is not installed", files{i}); |
6037
|
1161 endif |
|
1162 dirs{end+1} = pdirs{idx}; |
|
1163 if (handle_deps) |
|
1164 pdep = pdeps{idx}; |
|
1165 for j = 1:length(pdep) |
|
1166 depname = pdep{j}.package; |
|
1167 if (strcmp(depname, "octave")) continue; endif |
|
1168 idx = strcmp(pnames, depname); |
|
1169 if (!any(idx)) |
|
1170 error("Package %s could not be loaded since it depends on %s", ... |
|
1171 files{i}, depname); |
|
1172 endif |
|
1173 dirs{end+1} = pdirs{idx}; |
|
1174 endfor |
5971
|
1175 endif |
5801
|
1176 endfor |
6037
|
1177 dirs = unique(dirs); |
5801
|
1178 endif |
6037
|
1179 |
|
1180 ## Load the packages |
6025
|
1181 if (length(dirs) > 0) |
|
1182 addpath(dirs{:}); |
|
1183 endif |
5971
|
1184 |
|
1185 ## Add local binaries, if any, to the EXEC_PATH |
|
1186 for i = 1:length(dirs) |
6233
|
1187 if (exist (fullfile(dirs{i}, "bin"), "dir")) |
|
1188 EXEC_PATH ([fullfile(dirs{i}, "bin") ":" EXEC_PATH()]); |
5971
|
1189 endif |
|
1190 endfor |
5801
|
1191 endfunction |
5928
|
1192 |
6203
|
1193 function unload_packages(files, handle_deps, local_list, global_list) |
|
1194 installed_packages = installed_packages(local_list, global_list); |
|
1195 num_packages = length(installed_packages); |
|
1196 |
|
1197 ## Read package names and installdirs into a more convenient format |
|
1198 pnames = pdirs = cell(1, num_packages); |
|
1199 for i = 1:num_packages |
|
1200 pnames{i} = installed_packages{i}.name; |
|
1201 pdirs{i} = installed_packages{i}.dir; |
|
1202 pdeps{i} = installed_packages{i}.depends; |
|
1203 endfor |
|
1204 |
|
1205 ## Get the current octave path |
|
1206 p = split_by(path(), pathsep()); |
|
1207 |
|
1208 ## unload all |
|
1209 if (length(files) == 1 && strcmp(files{1}, "all")) |
|
1210 dirs = pdirs; |
|
1211 ## unload package_name1 ... |
|
1212 else |
|
1213 dirs = {}; |
|
1214 for i = 1:length(files) |
|
1215 idx = strcmp(pnames, files{i}); |
|
1216 if (!any(idx)) |
|
1217 error("Package %s is not installed", files{i}); |
|
1218 endif |
|
1219 dirs{end+1} = pdirs{idx}; |
|
1220 endfor |
|
1221 endif |
|
1222 |
|
1223 ## Unload the packages |
|
1224 for i = 1:length(dirs) |
|
1225 d = dirs{i}; |
|
1226 idx = strcmp(p, d); |
|
1227 if (any(idx)) |
|
1228 rmpath(d); |
|
1229 # XXX: We should also check if we need to remove items from EXEC_PATH |
|
1230 endif |
|
1231 endfor |
|
1232 endfunction |
|
1233 |
5928
|
1234 function [status_out, msg_out] = rm_rf (dir) |
|
1235 crr = confirm_recursive_rmdir (); |
|
1236 unwind_protect |
|
1237 confirm_recursive_rmdir (false); |
|
1238 [status, msg] = rmdir (dir, "s"); |
|
1239 unwind_protect_cleanup |
|
1240 confirm_recursive_rmdir (crr); |
|
1241 end_unwind_protect |
|
1242 if (nargout > 0) |
|
1243 status_out = status; |
|
1244 endif |
|
1245 if (nargout > 1) |
|
1246 msg_out = msg; |
|
1247 endif |
|
1248 endfunction |
5971
|
1249 |
|
1250 function emp = dirempty (nm, ign) |
|
1251 if (nargin < 2) |
|
1252 ign = {".",".."}; |
|
1253 else |
|
1254 ign = [{".",".."},ign]; |
|
1255 endif |
|
1256 l = dir (nm); |
|
1257 for i=1:length(l) |
|
1258 found = false; |
|
1259 for j=1:length(ign) |
|
1260 if (strcmp(l(i).name,ign{j})) |
|
1261 found = true; |
|
1262 break; |
|
1263 endif |
|
1264 endfor |
|
1265 if (!found) |
|
1266 emp = false; |
|
1267 return |
|
1268 endif |
|
1269 endfor |
|
1270 emp = true; |
|
1271 return; |
|
1272 endfunction |