5801
|
1 ## Copyright (C) 2005 S�ren Hauberg |
|
2 ## |
6440
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
5801
|
15 ## You should have received a copy of the GNU General Public License |
6440
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
5801
|
19 |
|
20 ## -*- texinfo -*- |
6032
|
21 ## @deftypefn {Command} pkg @var{command} @var{pkg_name} |
|
22 ## @deftypefnx {Command} pkg @var{command} @var{option} @var{pkg_name} |
|
23 ## This command interacts with the package manager. Different actions will |
6378
|
24 ## be taken depending on the value of @var{command}. |
6032
|
25 ## |
|
26 ## @table @samp |
|
27 ## @item install |
6070
|
28 ## Install named packages. For example, |
|
29 ## @example |
|
30 ## pkg install image-1.0.0.tar.gz |
|
31 ## @end example |
|
32 ## @noindent |
|
33 ## installs the package found in the file @code{image-1.0.0.tar.gz}. |
|
34 ## |
6645
|
35 ## The @var{option} variable can contain options that affect the manner |
|
36 ## in which a package is installed. These options can be one or more of |
|
37 ## |
|
38 ## @table @code |
|
39 ## @item -nodeps |
|
40 ## The package manager will disable the dependency checking. That way it |
|
41 ## is possible to install a package even if it depends on another package |
|
42 ## that's not installed on the system. @strong{Use this option with care.} |
|
43 ## |
|
44 ## @item -noauto |
|
45 ## The package manager will not automatically load the installed package |
|
46 ## when starting Octave, even if the package requests that it is. |
6258
|
47 ## |
6645
|
48 ## @item -auto |
|
49 ## The package manager will automatically load the installed package when |
|
50 ## starting Octave, even if the package requests that it isn't. |
|
51 ## |
|
52 ## @item -local |
|
53 ## A local installation is forced, even if the user has system privileges. |
6258
|
54 ## |
6645
|
55 ## @item -global |
|
56 ## A global installation is forced, even if the user doesn't normally have |
|
57 ## system privileges |
6614
|
58 ## |
6645
|
59 ## @item -verbose |
|
60 ## The package manager will print the output of all of the commands that are |
|
61 ## performed. |
|
62 ## @end table |
|
63 ## |
6032
|
64 ## @item uninstall |
6070
|
65 ## Uninstall named packages. For example, |
|
66 ## @example |
|
67 ## pkg uninstall image |
|
68 ## @end example |
|
69 ## @noindent |
|
70 ## removes the @code{image} package from the system. If another installed |
|
71 ## package depends on the @code{image} package an error will be issued. |
|
72 ## The package can be uninstalled anyway by using the @code{-nodeps} option. |
6032
|
73 ## @item load |
6070
|
74 ## Add named packages to the path. After loading a package it is |
|
75 ## possible to use the functions provided by the package. For example, |
|
76 ## @example |
|
77 ## pkg load image |
|
78 ## @end example |
|
79 ## @noindent |
|
80 ## adds the @code{image} package to the path. It is possible to load all |
|
81 ## installed packages at once with the command |
|
82 ## @example |
|
83 ## pkg load all |
|
84 ## @end example |
6203
|
85 ## @item unload |
|
86 ## Removes named packages from the path. After unloading a package it is |
|
87 ## no longer possible to use the functions provided by the package. |
|
88 ## This command behaves like the @code{load} command. |
6032
|
89 ## @item list |
6070
|
90 ## Show a list of the currently installed packages. By requesting one or two |
|
91 ## output argument it is possible to get a list of the currently installed |
|
92 ## packages. For example, |
|
93 ## @example |
|
94 ## installed_packages = pkg list; |
|
95 ## @end example |
|
96 ## @noindent |
|
97 ## returns a cell array containing a structure for each installed package. |
|
98 ## The command |
|
99 ## @example |
|
100 ## [@var{user_packages}, @var{system_packages}] = pkg list |
|
101 ## @end example |
|
102 ## @noindent |
|
103 ## splits the list of installed packages into those who are installed by |
|
104 ## the current user, and those installed by the system administrator. |
6034
|
105 ## @item prefix |
6070
|
106 ## Set the installation prefix directory. For example, |
|
107 ## @example |
|
108 ## pkg prefix ~/my_octave_packages |
|
109 ## @end example |
|
110 ## @noindent |
|
111 ## sets the installation prefix to @code{~/my_octave_packages}. |
|
112 ## Packages will be installed in this directory. |
6034
|
113 ## |
6070
|
114 ## It is possible to get the current installation prefix by requesting an |
|
115 ## output argument. For example, |
|
116 ## @example |
|
117 ## p = pkg prefix |
|
118 ## @end example |
6189
|
119 ## @item local_list |
|
120 ## Set the file in which to look for information on the locally |
|
121 ## installed packages. Locally installed packages are those that are |
|
122 ## typically available only to the current user. For example |
|
123 ## @example |
|
124 ## pkg local_list ~/.octave_packages |
|
125 ## @end example |
|
126 ## It is possible to get the current value of local_list with the following |
|
127 ## @example |
|
128 ## pkg local_list |
|
129 ## @end example |
|
130 ## @item global_list |
|
131 ## Set the file in which to look for, for information on the globally |
|
132 ## installed packages. Globally installed packages are those that are |
|
133 ## typically available to all users. For example |
|
134 ## @example |
|
135 ## pkg global_list /usr/share/octave/octave_packages |
|
136 ## @end example |
|
137 ## It is possible to get the current value of global_list with the following |
|
138 ## @example |
|
139 ## pkg global_list |
|
140 ## @end example |
6645
|
141 ## @item rebuild |
|
142 ## Rebuilds the package database from the installed directories. This can |
|
143 ## be used in cases where for some reason the package database is corrupted. |
|
144 ## It can also take the @code{-auto} and @code{-noauto} options to allow the |
|
145 ## autolaoding state of a package to be changed. For example |
|
146 ## |
|
147 ## @example |
|
148 ## pkg rebuild -noauto image |
|
149 ## @end example |
|
150 ## |
|
151 ## will remove the autoloading status of the image package. |
6032
|
152 ## @end table |
5801
|
153 ## @end deftypefn |
5947
|
154 |
|
155 ## PKG_ADD: mark_as_command pkg |
6258
|
156 ## PKG_ADD: pkg ("load", "auto"); |
5947
|
157 |
6496
|
158 function [local_packages, global_packages] = pkg (varargin) |
|
159 ## Installation prefix (XXX: what should these be on windows?) |
6645
|
160 persistent user_prefix = false; |
6496
|
161 persistent prefix = -1; |
|
162 persistent local_list = tilde_expand (fullfile("~", ".octave_packages")); |
|
163 persistent global_list = fullfile (OCTAVE_HOME (), "share", "octave", |
|
164 "octave_packages"); |
|
165 mlock (); |
6189
|
166 |
6645
|
167 global_install = issuperuser (); |
6496
|
168 if (prefix == -1) |
6645
|
169 if (global_install) |
6496
|
170 prefix = fullfile (OCTAVE_HOME (), "share", "octave", "packages"); |
|
171 else |
|
172 prefix = fullfile ("~", "octave"); |
5801
|
173 endif |
6645
|
174 prefix = tilde_expand (prefix); |
6496
|
175 endif |
|
176 |
|
177 ## Handle input |
|
178 if (length (varargin) == 0 || ! iscellstr (varargin)) |
|
179 print_usage (); |
|
180 endif |
|
181 files = {}; |
|
182 deps = true; |
|
183 auto = 0; |
|
184 action = "none"; |
6614
|
185 verbose = false; |
6496
|
186 for i = 1:length (varargin) |
|
187 switch (varargin{i}) |
|
188 case "-nodeps" |
|
189 deps = false; |
|
190 case "-noauto" |
|
191 auto = -1; |
|
192 case "-auto" |
|
193 auto = 1; |
6614
|
194 case "-verbose" |
|
195 verbose = true; |
6645
|
196 case "-local" |
|
197 global_install = false; |
|
198 if (! user_prefix) |
|
199 prefix = fullfile ("~", "octave"); |
|
200 endif |
|
201 case "-global" |
|
202 global_install = true; |
|
203 if (! user_prefix) |
|
204 prefix = fullfile (OCTAVE_HOME (), "share", "octave", "packages"); |
|
205 endif |
6496
|
206 case {"list", "install", "uninstall", "load", "unload", ... |
6645
|
207 "prefix", "local_list", "global_list", "rebuild"} |
6496
|
208 action = varargin{i}; |
|
209 otherwise |
|
210 files{end+1} = varargin{i}; |
5801
|
211 endswitch |
6496
|
212 endfor |
|
213 |
|
214 ## Take action |
|
215 switch (action) |
|
216 case "list" |
|
217 if (nargout == 0) |
|
218 installed_packages (local_list, global_list); |
|
219 elseif (nargout == 1) |
|
220 local_packages = installed_packages (local_list, global_list); |
|
221 elseif (nargout == 2) |
|
222 [local_packages, global_packages] = installed_packages (local_list, |
|
223 global_list); |
|
224 else |
|
225 error ("too many output arguments requested"); |
|
226 endif |
|
227 |
|
228 case "install" |
|
229 if (length (files) == 0) |
|
230 error ("you must specify at least one filename when calling 'pkg install'"); |
|
231 endif |
6645
|
232 install (files, deps, auto, prefix, verbose, local_list, |
|
233 global_list, global_install); |
6496
|
234 |
|
235 case "uninstall" |
|
236 if (length (files) == 0) |
|
237 error ("you must specify at least one package when calling 'pkg uninstall'"); |
|
238 endif |
6645
|
239 uninstall (files, deps, verbose, local_list, |
|
240 global_list, global_install); |
6496
|
241 |
|
242 case "load" |
|
243 if (length (files) == 0) |
|
244 error ("you must specify at least one package, 'all' or 'auto' when calling 'pkg load'"); |
|
245 endif |
|
246 load_packages (files, deps, local_list, global_list); |
|
247 |
|
248 case "unload" |
|
249 if (length (files) == 0) |
|
250 error ("you must specify at least one package or 'all' when calling 'pkg unload'"); |
|
251 endif |
|
252 unload_packages (files, deps, local_list, global_list); |
|
253 |
|
254 case "prefix" |
|
255 if (length (files) == 0 && nargout == 0) |
|
256 disp (prefix); |
|
257 elseif (length (files) == 0 && nargout == 1) |
|
258 local_packages = prefix; |
|
259 elseif (length (files) == 1 && nargout == 0 && ischar (files{1})) |
|
260 prefix = files{1}; |
|
261 ## if (!strcmp(prefix(end), filesep)) |
|
262 ## prefix(end+1) = filesep; |
|
263 ## endif |
6645
|
264 prefix = tilde_expand (prefix); |
|
265 user_prefix = true; |
6496
|
266 else |
|
267 error ("you must specify a prefix directory, or request an output argument"); |
|
268 endif |
|
269 |
|
270 case "local_list" |
|
271 if (length (files) == 0 && nargout == 0) |
|
272 disp (local_list); |
|
273 elseif (length (files) == 0 && nargout == 1) |
|
274 local_packages = local_list; |
|
275 elseif (length (files) == 1 && nargout == 0 && ischar (files{1})) |
|
276 local_list = files{1}; |
|
277 else |
|
278 error ("you must specify a local_list file, or request an output argument"); |
|
279 endif |
|
280 |
|
281 case "global_list" |
|
282 if (length (files) == 0 && nargout == 0) |
|
283 disp(global_list); |
|
284 elseif (length (files) == 0 && nargout == 1) |
|
285 local_packages = global_list; |
|
286 elseif (length (files) == 1 && nargout == 0 && ischar (files{1})) |
|
287 global_list = files{1}; |
|
288 else |
|
289 error ("you must specify a global_list file, or request an output argument"); |
|
290 endif |
6645
|
291 |
|
292 case "rebuild" |
|
293 if (global_install) |
6663
|
294 global_packages = rebuild (prefix, global_list, files, auto, verbose); |
6645
|
295 save (global_list, "global_packages"); |
|
296 local_packages = global_packages; |
|
297 else |
|
298 local_packages = rebuild (prefix, local_list, files, auto, verbose); |
|
299 save (local_list, "local_packages"); |
|
300 endif |
|
301 |
6496
|
302 otherwise |
|
303 error ("you must specify a valid action for 'pkg'. See 'help pkg' for details"); |
|
304 endswitch |
|
305 endfunction |
|
306 |
6645
|
307 function descriptions = rebuild (prefix, list, files, auto, verbose) |
|
308 if (isempty (files)) |
|
309 [dirlist, err, msg] = readdir (prefix); |
|
310 if (err) |
|
311 error ("couldn't read directory %s: %s", prefix, msg); |
|
312 endif |
|
313 ## the two first entries of dirlist are "." and ".." |
|
314 dirlist([1,2]) = []; |
|
315 else |
|
316 old_descriptions = installed_packages (list, list); |
|
317 wd = pwd (); |
6663
|
318 unwind_protect |
|
319 cd (prefix); |
|
320 dirlist = glob (cellfun(@(x) [x, '-*'], files, 'UniformOutput', 0)) |
|
321 unwind_protect_cleanup |
|
322 cd (wd); |
|
323 end_unwind_protect |
6645
|
324 endif |
|
325 descriptions = {}; |
|
326 for k = 1:length (dirlist) |
|
327 descfile = fullfile (prefix, dirlist{k}, "packinfo", "DESCRIPTION"); |
|
328 if (verbose) |
|
329 printf ("recreating package description from %s\n", dirlist{k}); |
|
330 endif |
|
331 if (exist (descfile, "file")) |
|
332 desc = get_description (descfile); |
|
333 desc.dir = fullfile (prefix, dirlist{k}); |
|
334 if (auto != 0) |
|
335 if (exist (fullfile (desc.dir, "packinfo", ".autoload"), "file")) |
|
336 unlink (fullfile (desc.dir, "packinfo", ".autoload")); |
|
337 endif |
|
338 if (auto < 0) |
|
339 desc.autoload = 0; |
|
340 elseif (auto > 0) |
|
341 desc.autoload = 1; |
|
342 fclose (fopen (fullfile (desc.dir, "packinfo", ".autoload"), "wt")); |
|
343 endif |
|
344 else |
|
345 if (exist (fullfile (desc.dir, "packinfo", ".autoload"), "file")) |
|
346 desc.autoload = 1; |
|
347 else |
|
348 desc.autoload = 0; |
|
349 endif |
|
350 endif |
|
351 descriptions{end + 1} = desc; |
|
352 elseif (verbose) |
|
353 warning ("directory %s is not a valid package", dirlist{k}); |
|
354 endif |
|
355 endfor |
|
356 |
6663
|
357 if (! isempty (files)) |
6645
|
358 ## We are rebuilding for a particular package(s) so we should take |
|
359 ## care to keep the other untouched packages in the descriptions |
6663
|
360 descriptions = {descriptions{:}, old_descriptions{:}}; |
6645
|
361 |
|
362 dup = []; |
|
363 for i = 1:length (descriptions) |
|
364 if (find (dup, i)) |
|
365 continue; |
|
366 endif |
|
367 for j = (i+1):length (descriptions) |
|
368 if (find (dup, j)) |
|
369 continue; |
|
370 endif |
|
371 if (strcmp (descriptions{i}.name, descriptions{j}.name)) |
|
372 dup = [dup, j]; |
|
373 endif |
|
374 endfor |
|
375 endfor |
|
376 if (! isempty (dup)) |
|
377 descriptions (dup) = []; |
|
378 endif |
|
379 endif |
|
380 endfunction |
|
381 |
6496
|
382 function auto = isautoload (desc) |
|
383 auto = false; |
|
384 if (isfield (desc{1}, "autoload")) |
|
385 a = desc{1}.autoload; |
|
386 if ((isnumeric (a) && a > 0) |
6500
|
387 || (ischar (a) && (strcmpi (a, "true") |
|
388 || strcmpi (a, "on") |
|
389 || strcmpi (a, "yes") |
|
390 || strcmpi (a, "1")))) |
6496
|
391 auto = true; |
|
392 endif |
|
393 endif |
5801
|
394 endfunction |
|
395 |
6645
|
396 function install (files, handle_deps, autoload, prefix, verbose, local_list, global_list, global_install) |
6496
|
397 |
|
398 # Check that the directory in prefix exist. If it doesn't: create it! |
|
399 if (! exist (prefix, "dir")) |
|
400 warning ("creating installation directory %s", prefix); |
|
401 [status, msg] = mkdir (prefix); |
|
402 if (status != 1) |
|
403 error ("could not create installation directory: %s", msg); |
|
404 endif |
|
405 endif |
|
406 |
|
407 ## Get the list of installed packages |
|
408 [local_packages, global_packages] = installed_packages (local_list, |
|
409 global_list); |
|
410 |
|
411 installed_packages = {local_packages{:}, global_packages{:}}; |
|
412 |
|
413 if (global_install) |
|
414 packages = global_packages; |
|
415 else |
|
416 packages = local_packages; |
|
417 endif |
|
418 |
|
419 ## Uncompress the packages and read the DESCRIPTION files |
|
420 tmpdirs = packdirs = descriptions = {}; |
|
421 try |
|
422 ## Unpack the package files and read the DESCRIPTION files |
|
423 files = glob (files); |
|
424 packages_to_uninstall = []; |
|
425 for i = 1:length (files) |
|
426 tgz = files{i}; |
|
427 |
6645
|
428 if (exist (tgz, "file")) |
|
429 ## Create a temporary directory |
|
430 tmpdir = tmpnam (); |
|
431 tmpdirs{end+1} = tmpdir; |
|
432 if (verbose) |
|
433 printf ("mkdir (%s)\n", tmpdir); |
|
434 endif |
|
435 [status, msg] = mkdir (tmpdir); |
|
436 if (status != 1) |
|
437 error ("couldn't create temporary directory: %s", msg); |
|
438 endif |
6496
|
439 |
6645
|
440 ## Uncompress the package |
|
441 if (verbose) |
|
442 printf ("untar (%s, %s)\n", tgz, tmpdir); |
|
443 endif |
|
444 untar (tgz, tmpdir); |
6496
|
445 |
6645
|
446 ## Get the name of the directories produced by tar |
|
447 [dirlist, err, msg] = readdir (tmpdir); |
|
448 if (err) |
|
449 error ("couldn't read directory produced by tar: %s", msg); |
|
450 endif |
6496
|
451 |
6645
|
452 if (length (dirlist) > 3) |
|
453 error ("bundles of packages are not allowed") |
6496
|
454 endif |
|
455 |
6645
|
456 ## the two first entries of dirlist are "." and ".." |
|
457 for k = 3:length (dirlist) |
|
458 packdir = fullfile (tmpdir, dirlist{k}); |
|
459 packdirs{end+1} = packdir; |
6496
|
460 |
6645
|
461 ## Make sure the package contains necessary files |
|
462 verify_directory (packdir); |
|
463 |
|
464 ## Read the DESCRIPTION file |
|
465 filename = fullfile (packdir, "DESCRIPTION"); |
|
466 desc = get_description (filename); |
6496
|
467 |
6645
|
468 ## Verify that package name corresponds with filename |
|
469 [dummy, nm] = fileparts (tgz); |
|
470 if ((length (nm) >= length (desc.name)) |
|
471 && ! strcmp (desc.name, nm(1:length(desc.name)))) |
|
472 error ("package name '%s' doesn't correspond to its filename '%s'", desc.name, nm); |
6496
|
473 endif |
6645
|
474 |
|
475 ## Set default installation directory |
|
476 desc.dir = fullfile (prefix, strcat (desc.name, "-", desc.version)); |
|
477 |
|
478 ## Save desc |
|
479 descriptions{end+1} = desc; |
|
480 |
|
481 ## Are any of the new packages already installed? |
|
482 ## If so we'll remove the old version. |
|
483 for j = 1:length (packages) |
|
484 if (strcmp (packages{j}.name, desc.name)) |
|
485 packages_to_uninstall(end+1) = j; |
|
486 endif |
|
487 endfor |
|
488 endfor |
|
489 else |
|
490 warning ("file %s does not exist", tgz); |
|
491 endif |
6496
|
492 endfor |
|
493 catch |
|
494 ## Something went wrong, delete tmpdirs |
|
495 for i = 1:length (tmpdirs) |
|
496 rm_rf (tmpdirs{i}); |
|
497 endfor |
6655
|
498 rethrow (lasterror ()); |
6496
|
499 end_try_catch |
|
500 |
|
501 ## Check dependencies |
|
502 if (handle_deps) |
|
503 ok = true; |
|
504 error_text = ""; |
|
505 for i = 1:length (descriptions) |
|
506 desc = descriptions{i}; |
|
507 idx1 = complement (packages_to_uninstall, 1:length(installed_packages)); |
|
508 idx2 = complement (i, 1:length(descriptions)); |
|
509 pseudo_installed_packages = {installed_packages{idx1}, descriptions{idx2}}; |
|
510 bad_deps = get_unsatisfied_deps (desc, pseudo_installed_packages); |
|
511 ## Are there any unsatisfied dependencies? |
|
512 if (! isempty (bad_deps)) |
|
513 ok = false; |
|
514 for i = 1:length (bad_deps) |
|
515 dep = bad_deps{i}; |
|
516 error_text = strcat (error_text, " ", desc.name, " needs ", |
|
517 dep.package, " ", dep.operator, " ", |
|
518 dep.version, "\n"); |
|
519 endfor |
|
520 endif |
|
521 endfor |
|
522 |
|
523 ## Did we find any unsatisfied dependencies? |
|
524 if (! ok) |
|
525 error ("the following dependencies where unsatisfied:\n %s", error_text); |
|
526 endif |
|
527 endif |
|
528 |
|
529 ## Prepare each package for installation |
|
530 try |
|
531 for i = 1:length (descriptions) |
|
532 desc = descriptions{i}; |
|
533 pdir = packdirs{i}; |
|
534 prepare_installation (desc, pdir); |
6614
|
535 configure_make (desc, pdir, verbose); |
6496
|
536 endfor |
|
537 catch |
|
538 ## Something went wrong, delete tmpdirs |
|
539 for i = 1:length (tmpdirs) |
|
540 rm_rf (tmpdirs{i}); |
|
541 endfor |
6655
|
542 rethrow (lasterror ()); |
6496
|
543 end_try_catch |
|
544 |
|
545 ## Uninstall the packages that will be replaced |
|
546 try |
|
547 for i = packages_to_uninstall |
6614
|
548 uninstall ({installed_packages{i}.name}, false, verbose, local_list, |
6645
|
549 global_list, global_install); |
6496
|
550 endfor |
|
551 catch |
|
552 ## Something went wrong, delete tmpdirs |
|
553 for i = 1:length (tmpdirs) |
|
554 rm_rf (tmpdirs{i}); |
|
555 endfor |
6655
|
556 rethrow (lasterror ()); |
6496
|
557 end_try_catch |
|
558 |
|
559 ## Install each package |
|
560 try |
|
561 for i = 1:length (descriptions) |
|
562 desc = descriptions{i}; |
|
563 pdir = packdirs{i}; |
|
564 copy_files (desc, pdir); |
|
565 create_pkgadddel (desc, pdir, "PKG_ADD"); |
|
566 create_pkgadddel (desc, pdir, "PKG_DEL"); |
|
567 finish_installation (desc, pdir) |
|
568 endfor |
|
569 catch |
|
570 ## Something went wrong, delete tmpdirs |
|
571 for i = 1:length (tmpdirs) |
|
572 rm_rf (tmpdirs{i}); |
|
573 endfor |
|
574 for i = 1:length (descriptions) |
|
575 rm_rf (descriptions{i}.dir); |
|
576 endfor |
6655
|
577 rethrow (lasterror ()); |
6496
|
578 end_try_catch |
|
579 |
|
580 ## Check if the installed directory is empty. If it is remove it |
|
581 ## from the list |
|
582 for i = length (descriptions):-1:1 |
|
583 if (dirempty (descriptions{i}.dir, {"packinfo", "doc"})) |
6655
|
584 warning ("package %s is empty\n", descriptions{i}.name); |
6496
|
585 rm_rf (descriptions{i}.dir); |
|
586 descriptions(i) = []; |
|
587 endif |
|
588 endfor |
|
589 |
|
590 ## If the package requested that it is autoloaded, or the installer |
|
591 ## requested that it is, then mark the package as autoloaded. |
|
592 for i = length (descriptions):-1:1 |
|
593 if (autoload > 0 || (autoload == 0 && isautoload (descriptions(i)))) |
|
594 fclose (fopen (fullfile (descriptions{i}.dir, "packinfo", |
|
595 ".autoload"), "wt")); |
|
596 endif |
|
597 endfor |
|
598 |
|
599 ## Add the packages to the package list |
|
600 try |
|
601 if (global_install) |
|
602 idx = complement (packages_to_uninstall, 1:length(global_packages)); |
|
603 global_packages = {global_packages{idx}, descriptions{:}}; |
|
604 save (global_list, "global_packages"); |
|
605 else |
|
606 idx = complement (packages_to_uninstall, 1:length(local_packages)); |
|
607 local_packages = {local_packages{idx}, descriptions{:}}; |
|
608 save (local_list, "local_packages"); |
|
609 endif |
|
610 catch |
|
611 ## Something went wrong, delete tmpdirs |
|
612 for i = 1:length (tmpdirs) |
|
613 rm_rf (tmpdirs{i}); |
|
614 endfor |
|
615 for i = 1:length (descriptions) |
|
616 rm_rf (descriptions{i}.dir); |
|
617 endfor |
|
618 if (global_install) |
|
619 error ("couldn't append to %s: %s", global_list, lasterr()(8:end)); |
|
620 else |
|
621 error ("couldn't append to %s: %s", local_list, lasterr()(8:end)); |
|
622 endif |
|
623 end_try_catch |
|
624 |
|
625 ## All is well, let's clean up |
|
626 for i = 1:length (tmpdirs) |
|
627 [status, msg] = rm_rf (tmpdirs{i}); |
|
628 if (status != 1) |
|
629 warning ("couldn't clean up after my self: %s\n", msg); |
|
630 endif |
|
631 endfor |
|
632 |
|
633 ## Add the newly installed packages to the path, so the user |
|
634 ## can begin usings them. |
6614
|
635 arch = getarch(); |
6496
|
636 if (length (descriptions) > 0) |
6614
|
637 dirs = {}; |
6496
|
638 for i = 1:length (descriptions) |
6614
|
639 dirs{end + 1} = descriptions{i}.dir; |
|
640 if (exist (fullfile (descriptions{i}.dir, arch), "dir")) |
|
641 dirs{end + 1} = fullfile (descriptions{i}.dir, arch); |
|
642 endif |
6496
|
643 endfor |
|
644 addpath (dirs{:}); |
|
645 endif |
|
646 endfunction |
|
647 |
6645
|
648 function uninstall (pkgnames, handle_deps, verbose, local_list, |
|
649 global_list, global_install) |
6496
|
650 ## Get the list of installed packages |
|
651 [local_packages, global_packages] = installed_packages(local_list, |
|
652 global_list); |
6645
|
653 if (global_install) |
6496
|
654 installed_packages = {local_packages{:}, global_packages{:}}; |
|
655 else |
|
656 installed_packages = local_packages; |
|
657 endif |
|
658 |
|
659 num_packages = length (installed_packages); |
|
660 delete_idx = []; |
|
661 for i = 1:num_packages |
|
662 cur_name = installed_packages{i}.name; |
|
663 if (any (strcmp (cur_name, pkgnames))) |
|
664 delete_idx(end+1) = i; |
|
665 endif |
|
666 endfor |
|
667 |
|
668 ## Are all the packages that should be uninstalled already installed? |
|
669 if (length (delete_idx) != length (pkgnames)) |
6645
|
670 if (global_install) |
6496
|
671 ## Try again for a locally installed package |
6655
|
672 installed_packages = local_packages; |
6496
|
673 |
|
674 num_packages = length (installed_packages); |
|
675 delete_idx = []; |
|
676 for i = 1:num_packages |
|
677 cur_name = installed_packages{i}.name; |
|
678 if (any (strcmp (cur_name, pkgnames))) |
|
679 delete_idx(end+1) = i; |
|
680 endif |
|
681 endfor |
|
682 if (length (delete_idx) != length (pkgnames)) |
|
683 ## XXX: We should have a better error message |
6655
|
684 warning ("some of the packages you want to uninstall are not installed"); |
6496
|
685 endif |
|
686 else |
|
687 ## XXX: We should have a better error message |
6655
|
688 warning ("some of the packages you want to uninstall are not installed."); |
6496
|
689 endif |
|
690 endif |
|
691 |
|
692 ## Compute the packages that will remain installed |
|
693 idx = complement (delete_idx, 1:num_packages); |
|
694 remaining_packages = {installed_packages{idx}}; |
|
695 |
|
696 ## Check dependencies |
|
697 if (handle_deps) |
|
698 error_text = ""; |
|
699 for i = 1:length (remaining_packages) |
|
700 desc = remaining_packages{i}; |
|
701 bad_deps = get_unsatisfied_deps (desc, remaining_packages); |
|
702 |
|
703 ## Will the uninstallation break any dependencies? |
|
704 if (! isempty (bad_deps)) |
|
705 for i = 1:length (bad_deps) |
|
706 dep = bad_deps{i}; |
|
707 error_text = strcat (error_text, " ", desc.name, " needs ", |
|
708 dep.package, " ", dep.operator, " ", |
|
709 dep.version, "\n"); |
|
710 endfor |
|
711 endif |
|
712 endfor |
|
713 |
|
714 if (! isempty (error_text)) |
|
715 error ("the following dependencies where unsatisfied:\n %s", error_text); |
|
716 endif |
|
717 endif |
|
718 |
|
719 ## Delete the directories containing the packages |
|
720 for i = delete_idx |
|
721 desc = installed_packages{i}; |
|
722 ## If an 'on_uninstall.m' exist, call it! |
|
723 if (exist (fullfile (desc.dir, "packinfo", "on_uninstall.m"), "file")) |
6614
|
724 wd = pwd (); |
|
725 cd (fullfile(desc.dir, "packinfo")); |
|
726 on_uninstall (desc); |
|
727 cd (wd); |
6496
|
728 endif |
|
729 ## Do the actual deletion |
|
730 rmpath (desc.dir); |
6614
|
731 if (exist (fullfile (desc.dir, getarch()), "dir")) |
|
732 rmpath (fullfile (desc.dir, getarch ())); |
|
733 endif |
6496
|
734 if (exist (desc.dir, "dir")) |
|
735 [status, msg] = rm_rf (desc.dir); |
|
736 if (status != 1) |
|
737 error ("couldn't delete directory %s: %s", desc.dir, msg); |
|
738 endif |
|
739 else |
|
740 warning ("directory %s previously lost", desc.dir); |
|
741 endif |
|
742 endfor |
|
743 |
|
744 ## Write a new ~/.octave_packages |
6645
|
745 if (global_install) |
6496
|
746 if (length (remaining_packages) == 0) |
|
747 unlink (global_list); |
|
748 else |
|
749 global_packages = remaining_packages; |
|
750 save (global_list, "global_packages"); |
|
751 endif |
|
752 else |
|
753 if (length (remaining_packages) == 0) |
|
754 unlink (local_list); |
|
755 else |
|
756 local_packages = remaining_packages; |
|
757 save (local_list, "local_packages"); |
|
758 endif |
|
759 endif |
|
760 |
|
761 endfunction |
|
762 |
|
763 ########################################################## |
|
764 ## A U X I L I A R Y F U N C T I O N S ## |
|
765 ########################################################## |
|
766 |
|
767 function prepare_installation (desc, packdir) |
|
768 ## Is there a pre_install to call? |
|
769 if (exist (fullfile (packdir, "pre_install.m"), "file")) |
|
770 wd = pwd (); |
|
771 try |
|
772 cd (packdir); |
|
773 pre_install (desc); |
|
774 cd (wd); |
|
775 catch |
|
776 cd (wd); |
|
777 error ("the pre-install function returned the following error: %s", |
|
778 lasterr ()); |
|
779 end_try_catch |
|
780 endif |
|
781 |
|
782 ## If the directory "inst" doesn't exist, we create it |
|
783 inst_dir = fullfile (packdir, "inst"); |
|
784 if (! exist (inst_dir, "dir")) |
|
785 [status, msg] = mkdir (inst_dir); |
|
786 if (status != 1) |
|
787 rm_rf (desc.dir); |
|
788 error ("the 'inst' directory did not exist and could not be created: %s", |
|
789 msg); |
6258
|
790 endif |
|
791 endif |
|
792 endfunction |
|
793 |
6614
|
794 function configure_make (desc, packdir, verbose) |
6496
|
795 ## Perform ./configure, make, make install in "src" |
|
796 if (exist (fullfile (packdir, "src"), "dir")) |
|
797 src = fullfile (packdir, "src"); |
|
798 ## configure |
|
799 if (exist (fullfile (src, "configure"), "file")) |
6645
|
800 [status, output] = shell (strcat ("cd ", src, "; ./configure --prefix=\"", |
|
801 desc.dir, "\"", |
|
802 " CC=", octave_config_info ("CC"), |
|
803 " CXX=", octave_config_info ("CXX"), |
|
804 " AR=", octave_config_info ("AR"), |
|
805 " RANLIB=", octave_config_info ("RANLIB"))); |
6614
|
806 if (verbose) |
|
807 printf("%s", output); |
|
808 endif |
6496
|
809 if (status != 0) |
|
810 rm_rf (desc.dir); |
|
811 error ("the configure script returned the following error: %s", output); |
|
812 endif |
5801
|
813 endif |
|
814 |
6496
|
815 ## make |
|
816 if (exist (fullfile (src, "Makefile"), "file")) |
6645
|
817 [status, output] = shell (strcat ("export INSTALLDIR=\"", desc.dir, |
|
818 "\"; make -C ", src)); |
6614
|
819 if (verbose) |
|
820 printf("%s", output); |
|
821 endif |
6496
|
822 if (status != 0) |
|
823 rm_rf (desc.dir); |
|
824 error ("'make' returned the following error: %s", output); |
|
825 endif |
|
826 %# make install |
|
827 %[status, output] = system(["export INSTALLDIR=" desc.dir "; make install -C " src]); |
6614
|
828 %if (verbose) |
|
829 % printf("%s", output); |
|
830 %endif |
6496
|
831 %if (status != 0) |
|
832 % rm_rf(desc.dir); |
|
833 % error("'make install' returned the following error: %s", output); |
|
834 %endif |
5801
|
835 endif |
|
836 |
6614
|
837 ## Copy files to "inst" and "inst/arch" (this is instead of 'make install') |
6496
|
838 files = fullfile (src, "FILES"); |
|
839 instdir = fullfile (packdir, "inst"); |
6614
|
840 archdir = fullfile (packdir, "inst", getarch ()); |
6655
|
841 ## Get file names |
6496
|
842 if (exist (files, "file")) |
|
843 [fid, msg] = fopen (files, "r"); |
|
844 if (fid < 0) |
|
845 error ("couldn't open %s: %s", files, msg); |
5971
|
846 endif |
6496
|
847 filenames = char (fread (fid))'; |
|
848 fclose (fid); |
|
849 if (filenames(end) == "\n") |
|
850 filenames(end) = []; |
6258
|
851 endif |
6655
|
852 filenames = split_by (filenames, "\n"); |
6496
|
853 delete_idx = []; |
6655
|
854 for i = 1:length (filenames) |
|
855 if (! all (isspace (filenames{i}))) |
|
856 filenames{i} = fullfile (src, filenames{i}); |
6496
|
857 else |
|
858 delete_idx(end+1) = i; |
|
859 endif |
5971
|
860 endfor |
6655
|
861 filenames(delete_idx) = []; |
|
862 idx1 = cellfun ("isempty", regexp (filenames, '^.*\.mex')); |
|
863 idx2 = cellfun ("isempty", regexp (filenames, '^.*\.oct')); |
|
864 mex = filenames; |
|
865 mex(idx1 != 0) = []; |
|
866 oct = filenames; |
|
867 oct(idx2 != 0) = []; |
6614
|
868 archindependent = filenames; |
6655
|
869 archindependent(idx1 == 0 | idx2 == 0) = []; |
6614
|
870 archdependent = [oct, mex]; |
6496
|
871 else |
|
872 m = dir (fullfile (src, "*.m")); |
|
873 oct = dir (fullfile (src, "*.oct")); |
|
874 mex = dir (fullfile (src, "*.mex")); |
6614
|
875 archdependent = ""; |
|
876 archindependent = ""; |
6496
|
877 filenames = ""; |
|
878 if (length (m) > 0) |
|
879 filenames = sprintf (fullfile (src, "%s "), m.name); |
6614
|
880 archindependent = sprintf (fullfile (src, "%s "), m.name); |
6496
|
881 endif |
|
882 if (length (oct) > 0) |
|
883 filenames = [filenames, " ", sprintf(fullfile(src, "%s "), oct.name)]; |
6614
|
884 archdependent = [archdependent, " ", ... |
|
885 sprintf(fullfile(src, "%s "), oct.name)]; |
6496
|
886 endif |
|
887 if (length (mex) > 0) |
|
888 filenames = [filenames, " ", sprintf(fullfile(src, "%s "), mex.name)]; |
6614
|
889 archdependent = [archdependent, " ", ... |
|
890 sprintf(fullfile(src, "%s "), mex.name)]; |
6496
|
891 endif |
6614
|
892 filenames = split_by (filenames, " "); |
|
893 archdependent = split_by (archdependent, " "); |
|
894 archindependent = split_by (archindependent, " "); |
5971
|
895 endif |
5801
|
896 |
6655
|
897 ## Copy the files |
6496
|
898 if (! all (isspace (filenames))) |
6655
|
899 if (! exist (instdir, "dir")) |
|
900 mkdir (instdir); |
|
901 endif |
6614
|
902 if (! all (isspace (archindependent))) |
6634
|
903 if (verbose) |
|
904 printf ("copyfile"); |
|
905 printf (" %s", archindependent{:}); |
|
906 printf ("%s\n", instdir); |
|
907 endif |
6614
|
908 [status, output] = copyfile (archindependent, instdir); |
|
909 if (status != 1) |
6496
|
910 rm_rf (desc.dir); |
|
911 error ("Couldn't copy files from 'src' to 'inst': %s", output); |
6614
|
912 endif |
|
913 endif |
|
914 if (! all (isspace (archdependent))) |
6634
|
915 if (verbose) |
|
916 printf ("copyfile"); |
|
917 printf (" %s", archdependent{:}); |
|
918 printf (" %s\n", archdir); |
|
919 endif |
6655
|
920 if (! exist (archdir, "dir")) |
|
921 mkdir (archdir); |
|
922 endif |
6614
|
923 [status, output] = copyfile (archdependent, archdir); |
|
924 if (status != 1) |
|
925 rm_rf (desc.dir); |
|
926 error ("Couldn't copy files from 'src' to 'inst': %s", output); |
|
927 endif |
|
928 endif |
5801
|
929 endif |
6496
|
930 endif |
5801
|
931 endfunction |
|
932 |
5971
|
933 function pkg = extract_pkg (nm, pat) |
5955
|
934 fid = fopen (nm, "rt"); |
5971
|
935 pkg = ""; |
5955
|
936 if (fid >= 0) |
6496
|
937 while (! feof (fid)) |
5955
|
938 ln = fgetl (fid); |
|
939 if (ln > 0) |
6496
|
940 t = regexp (ln, pat, "tokens"); |
|
941 if (! isempty (t)) |
|
942 pkg = strcat (pkg, "\n", t{1}{1}); |
5955
|
943 endif |
|
944 endif |
|
945 endwhile |
6496
|
946 if (! isempty (pkg)) |
|
947 pkg = strcat (pkg, "\n"); |
5955
|
948 endif |
|
949 fclose (fid); |
|
950 endif |
|
951 endfunction |
|
952 |
5971
|
953 function create_pkgadddel (desc, packdir, nm) |
6634
|
954 archdir = fullfile (desc.dir, getarch ()); |
|
955 |
|
956 ## If it is exists, the PKG_* files should go into the architecture |
|
957 ## dependent directory so that the autoload/mfilename commands work |
|
958 ## as expected... |
|
959 if (exist (archdir, "dir")) |
|
960 pkg = fullfile (desc.dir, getarch(), nm); |
|
961 else |
|
962 pkg = fullfile (desc.dir, nm); |
|
963 endif |
6496
|
964 fid = fopen (pkg, "wt"); |
5976
|
965 |
5955
|
966 if (fid >= 0) |
5971
|
967 ## Search all dot-m files for PKG commands |
6233
|
968 lst = dir (fullfile(packdir, "inst", "*.m")); |
6496
|
969 for i = 1:length (lst) |
6233
|
970 nam = fullfile(packdir, "inst", lst(i).name); |
5976
|
971 fwrite (fid, extract_pkg (nam, ['^[#%][#%]* *' nm ': *(.*)$'])); |
5955
|
972 endfor |
|
973 |
5971
|
974 ## Search all C++ source files for PKG commands |
6233
|
975 lst = dir (fullfile(packdir, "src", "*.cc")); |
6496
|
976 for i = 1:length (lst) |
6233
|
977 nam = fullfile(packdir, "src", lst(i).name); |
5976
|
978 fwrite (fid, extract_pkg (nam, ['^//* *' nm ': *(.*)$'])); |
|
979 fwrite (fid, extract_pkg (nam, ['^/\** *' nm ': *(.*) *\*/$'])); |
5955
|
980 endfor |
|
981 |
5971
|
982 ## Add developer included PKG commands |
6496
|
983 packdirnm = fullfile (packdir, nm); |
|
984 if (exist (packdirnm, "file")) |
|
985 fid2 = fopen (packdirnm, "rt"); |
5955
|
986 if (fid2 >= 0) |
6496
|
987 while (! feof (fid2)) |
5955
|
988 ln = fgets (fid2); |
|
989 if (ln > 0) |
6496
|
990 fwrite (fid, ln); |
5955
|
991 endif |
|
992 endwhile |
6496
|
993 fclose (fid2); |
5955
|
994 endif |
|
995 endif |
6496
|
996 fclose (fid); |
5971
|
997 |
|
998 ## If the file is empty remove it |
|
999 t = dir (pkg); |
|
1000 if (t.bytes <= 0) |
|
1001 unlink (pkg); |
|
1002 endif |
5955
|
1003 endif |
|
1004 endfunction |
|
1005 |
5971
|
1006 function copy_files (desc, packdir, bindir) |
6496
|
1007 ## Create the installation directory |
|
1008 if (! exist (desc.dir, "dir")) |
|
1009 [status, output] = mkdir (desc.dir); |
|
1010 if (status != 1) |
|
1011 error ("couldn't create installation directory %s : %s", |
|
1012 desc.dir, output); |
6020
|
1013 endif |
6496
|
1014 endif |
6020
|
1015 |
6496
|
1016 ## Copy the files from "inst" to installdir |
|
1017 instdir = fullfile (packdir, "inst"); |
|
1018 if (! dirempty (instdir)) |
|
1019 [status, output] = copyfile (fullfile (instdir, "*"), desc.dir); |
|
1020 if (status != 1) |
|
1021 rm_rf (desc.dir); |
|
1022 error ("couldn't copy files to the installation directory"); |
5801
|
1023 endif |
6496
|
1024 endif |
5801
|
1025 |
6496
|
1026 ## Create the "packinfo" directory |
|
1027 packinfo = fullfile (desc.dir, "packinfo"); |
|
1028 [status, msg] = mkdir (packinfo); |
|
1029 if (status != 1) |
|
1030 rm_rf (desc.dir); |
|
1031 error ("couldn't create packinfo directory: %s", msg); |
|
1032 endif |
5801
|
1033 |
6496
|
1034 ## Copy DESCRIPTION |
|
1035 [status, output] = copyfile (fullfile (packdir, "DESCRIPTION"), packinfo); |
|
1036 if (status != 1) |
|
1037 rm_rf (desc.dir); |
|
1038 error ("couldn't copy DESCRIPTION: %s", output); |
|
1039 endif |
5801
|
1040 |
6496
|
1041 ## Copy COPYING |
|
1042 [status, output] = copyfile (fullfile (packdir, "COPYING"), packinfo); |
|
1043 if (status != 1) |
|
1044 rm_rf (desc.dir); |
|
1045 error ("couldn't copy COPYING: %s", output); |
|
1046 endif |
5993
|
1047 |
6496
|
1048 ## If the file ChangeLog exists, copy it |
|
1049 fChangeLog = fullfile(packdir, "ChangeLog"); |
|
1050 if (exist (fChangeLog, "file")) |
|
1051 [status, output] = copyfile (fChangeLog, packinfo); |
|
1052 if (status != 1) |
|
1053 rm_rf (desc.dir); |
|
1054 error ("couldn't copy ChangeLog file: %s", output); |
6254
|
1055 endif |
6496
|
1056 endif |
6254
|
1057 |
6496
|
1058 ## Is there an INDEX file to copy or should we generate one? |
|
1059 fINDEX = fullfile (packdir, "INDEX"); |
|
1060 if (exist(fINDEX, "file")) |
|
1061 [status, output] = copyfile (fINDEX, packinfo); |
|
1062 if (status != 1) |
|
1063 rm_rf (desc.dir); |
|
1064 error ("couldn't copy INDEX file: %s", output); |
5801
|
1065 endif |
6496
|
1066 else |
|
1067 try |
|
1068 write_INDEX (desc, fullfile (packdir, "inst"), |
|
1069 fullfile (packinfo, "INDEX")); |
|
1070 catch |
|
1071 rm_rf (desc.dir); |
|
1072 error (lasterr ()); |
|
1073 end_try_catch |
|
1074 endif |
5971
|
1075 |
6496
|
1076 ## Is there an 'on_uninstall.m' to install? |
|
1077 fon_uninstall = fullfile(packdir, "on_uninstall.m"); |
|
1078 if (exist (fon_uninstall, "file")) |
|
1079 [status, output] = copyfile (fon_uninstall, packinfo); |
|
1080 if (status != 1) |
|
1081 rm_rf (desc.dir); |
|
1082 error ("couldn't copy on_uninstall.m: %s", output); |
5971
|
1083 endif |
6496
|
1084 endif |
5971
|
1085 |
6496
|
1086 ## Is there a doc/ directory that needs to be installed |
|
1087 docdir = fullfile (packdir, "doc"); |
|
1088 if (exist (docdir, "dir") && ! dirempty (docdir)) |
|
1089 [status, output] = copyfile (docdir, desc.dir); |
|
1090 endif |
|
1091 |
|
1092 ## Is there a bin/ directory that needs to be installed |
|
1093 bindir = fullfile (packdir, "bin"); |
|
1094 if (exist (bindir, "dir") && ! dirempty (bindir)) |
|
1095 [status, output] = copyfile (bindir, desc.dir); |
|
1096 endif |
5801
|
1097 endfunction |
|
1098 |
|
1099 function finish_installation (desc, packdir) |
6496
|
1100 ## Is there a post-install to call? |
|
1101 if (exist (fullfile (packdir, "post_install.m"), "file")) |
|
1102 wd = pwd (); |
|
1103 try |
|
1104 cd (packdir); |
|
1105 post_install (desc); |
|
1106 cd (wd); |
|
1107 catch |
|
1108 cd (wd); |
|
1109 rm_rf (desc.dir); |
|
1110 error ("the post_install function returned the following error: %s", |
|
1111 lasterr ()); |
|
1112 end_try_catch |
|
1113 endif |
5801
|
1114 endfunction |
|
1115 |
6496
|
1116 function out = issuperuser () |
6645
|
1117 if (ispc () && ! isunix ()) |
|
1118 out = 1; |
|
1119 else |
|
1120 ## Need to be a bit presistent in probing superuser |
|
1121 out = (geteuid() == 0); |
|
1122 endif |
5801
|
1123 endfunction |
|
1124 |
|
1125 ## This function makes sure the package contains the |
|
1126 ## essential files. |
6496
|
1127 function verify_directory (dir) |
|
1128 needed_files = {"COPYING", "DESCRIPTION"}; |
|
1129 for f = needed_files |
|
1130 if (! exist (fullfile (dir, f{1}), "file")) |
|
1131 error ("package is missing file: %s", f{1}); |
|
1132 endif |
|
1133 endfor |
5801
|
1134 endfunction |
|
1135 |
|
1136 ## This function parses the DESCRIPTION file |
6496
|
1137 function desc = get_description (filename) |
|
1138 [fid, msg] = fopen (filename, "r"); |
|
1139 if (fid == -1) |
|
1140 error ("the DESCRIPTION file %s could not be read: %s", filename, msg); |
|
1141 endif |
|
1142 |
|
1143 desc = struct (); |
5801
|
1144 |
6496
|
1145 line = fgetl (fid); |
|
1146 while (line != -1) |
|
1147 if (line(1) == "#") |
|
1148 ## Comments, do nothing |
|
1149 elseif (isspace(line(1))) |
|
1150 ## Continuation lines |
|
1151 if (exist ("keyword", "var") && isfield (desc, keyword)) |
|
1152 desc.(keyword) = strcat (desc.(keyword), " ", rstrip(line)); |
|
1153 endif |
5801
|
1154 else |
6496
|
1155 ## Keyword/value pair |
|
1156 colon = find (line == ":"); |
|
1157 if (length (colon) == 0) |
|
1158 disp ("skipping line"); |
|
1159 else |
|
1160 colon = colon(1); |
|
1161 keyword = tolower (strip (line(1:colon-1))); |
|
1162 value = strip (line (colon+1:end)); |
|
1163 if (length (value) == 0) |
|
1164 fclose (fid); |
|
1165 error ("the keyword %s has an empty value", desc.keywords{end}); |
|
1166 endif |
|
1167 desc.(keyword) = value; |
|
1168 endif |
5801
|
1169 endif |
6496
|
1170 line = fgetl (fid); |
|
1171 endwhile |
|
1172 fclose (fid); |
|
1173 |
|
1174 ## Make sure all is okay |
|
1175 needed_fields = {"name", "version", "date", "title", ... |
|
1176 "author", "maintainer", "description"}; |
|
1177 for f = needed_fields |
|
1178 if (! isfield (desc, f{1})) |
|
1179 error ("description is missing needed field %s", f{1}); |
|
1180 endif |
|
1181 endfor |
|
1182 desc.version = fix_version (desc.version); |
|
1183 if (isfield (desc, "depends")) |
|
1184 desc.depends = fix_depends (desc.depends); |
|
1185 else |
|
1186 desc.depends = ""; |
|
1187 endif |
|
1188 desc.name = tolower (desc.name); |
5801
|
1189 endfunction |
|
1190 |
|
1191 ## Makes sure the version string v is a valid x.y.z version string |
|
1192 ## Examples: "0.1" => "0.1.0", "monkey" => error(...) |
6496
|
1193 function out = fix_version (v) |
|
1194 dots = find (v == "."); |
|
1195 if (length (dots) == 1) |
|
1196 major = str2num (v(1:dots-1)); |
|
1197 minor = str2num (v(dots+1:end)); |
|
1198 if (length (major) != 0 && length (minor) != 0) |
|
1199 out = sprintf ("%d.%d.0", major, minor); |
|
1200 return; |
5801
|
1201 endif |
6496
|
1202 elseif (length (dots) == 2) |
|
1203 major = str2num (v(1:dots(1)-1)); |
|
1204 minor = str2num (v(dots(1)+1:dots(2)-1)); |
|
1205 rev = str2num (v(dots(2)+1:end)); |
|
1206 if (length (major) != 0 && length (minor) != 0 && length (rev) != 0) |
|
1207 out = sprintf ("%d.%d.%d", major, minor, rev); |
|
1208 return; |
|
1209 endif |
|
1210 endif |
|
1211 error ("bad version string: %s", v); |
5801
|
1212 endfunction |
|
1213 |
|
1214 ## Makes sure the depends field is of the right format. |
|
1215 ## This function returns a cell of structures with the following fields: |
|
1216 ## package, version, operator |
6496
|
1217 function deps_cell = fix_depends (depends) |
|
1218 deps = split_by (tolower (depends), ","); |
|
1219 deps_cell = cell (1, length (deps)); |
|
1220 |
|
1221 ## For each dependency |
|
1222 for i = 1:length (deps) |
|
1223 dep = deps{i}; |
|
1224 lpar = find (dep == "("); |
|
1225 rpar = find (dep == ")"); |
|
1226 ## Does the dependency specify a version |
|
1227 ## Example: package(>= version) |
|
1228 if (length (lpar) == 1 && length (rpar) == 1) |
|
1229 package = tolower (strip (dep(1:lpar-1))); |
|
1230 sub = dep(lpar(1)+1:rpar(1)-1); |
|
1231 parts = split_by (sub, " "); |
|
1232 idx = []; |
|
1233 for r = 1:size (parts, 1) |
|
1234 if (length (parts{r}) > 0) |
|
1235 idx(end+1) = r; |
|
1236 endif |
|
1237 endfor |
|
1238 |
|
1239 if (length (idx) != 2) |
|
1240 error ("incorrect syntax for dependency `%s' in the DESCRIPTION file\n", |
|
1241 dep); |
|
1242 endif |
|
1243 operator = parts{idx(1)}; |
|
1244 if (! any (strcmp (operator, {">", ">=", "<=", "<", "=="}))) |
|
1245 error ("unsupported operator: %s", operator); |
|
1246 endif |
|
1247 version = fix_version (parts{idx(2)}); |
|
1248 |
|
1249 ## If no version is specified for the dependency |
|
1250 ## we say that the version should be greater than |
|
1251 ## or equal to 0.0.0 |
|
1252 else |
|
1253 package = tolower (strip (dep)); |
|
1254 operator = ">="; |
|
1255 version = "0.0.0"; |
|
1256 endif |
|
1257 deps_cell{i} = struct ("package", package, "operator", operator, |
|
1258 "version", version); |
|
1259 endfor |
5801
|
1260 endfunction |
|
1261 |
|
1262 ## Strips the text of spaces from the right |
|
1263 ## Example: " hello world " => " hello world" (XXX: is this the same as deblank?) |
6496
|
1264 function text = rstrip (text) |
|
1265 chars = find (! isspace (text)); |
|
1266 if (length (chars) > 0) |
|
1267 ## XXX: shouldn't it be text = text(1:chars(end)); |
|
1268 text = text (chars(1):end); |
|
1269 else |
|
1270 text = ""; |
|
1271 endif |
5801
|
1272 endfunction |
|
1273 |
|
1274 ## Strips the text of spaces from the left and the right |
|
1275 ## Example: " hello world " => "hello world" |
6496
|
1276 function text = strip (text) |
|
1277 chars = find (! isspace (text)); |
|
1278 if (length (chars) > 0) |
|
1279 text = text(chars(1):chars(end)); |
|
1280 else |
|
1281 text = ""; |
|
1282 endif |
5801
|
1283 endfunction |
|
1284 |
|
1285 ## Splits the text into a cell array of strings by sep |
|
1286 ## Example: "A, B" => {"A", "B"} (with sep = ",") |
6496
|
1287 function out = split_by (text, sep) |
|
1288 text_matrix = split (text, sep); |
|
1289 num_words = size (text_matrix, 1); |
|
1290 out = cell (num_words, 1); |
|
1291 for i = 1:num_words |
|
1292 out{i} = strip (text_matrix(i, :)); |
|
1293 endfor |
5801
|
1294 endfunction |
|
1295 |
|
1296 ## Creates an INDEX file for a package that doesn't provide one. |
|
1297 ## 'desc' describes the package. |
|
1298 ## 'dir' is the 'inst' direcotyr in temporary directory. |
|
1299 ## 'INDEX' is the name (including path) of resulting INDEX file. |
6496
|
1300 function write_INDEX (desc, dir, INDEX) |
|
1301 ## Get names of functions in dir |
|
1302 [files, err, msg] = readdir (dir); |
|
1303 if (err) |
|
1304 error ("couldn't read directory %s: %s", dir, msg); |
|
1305 endif |
|
1306 |
6634
|
1307 ## Check for architecture dependent files |
|
1308 arch = getarch(); |
|
1309 tmpdir = fullfile (dir, arch); |
|
1310 if (exist (tmpdir, "dir")) |
|
1311 [files2, err, msg] = readdir (tmpdir); |
|
1312 if (err) |
|
1313 error ("couldn't read directory %s: %s", tmpdir, msg); |
|
1314 endif |
|
1315 files = [files; files2]; |
|
1316 endif |
|
1317 |
6496
|
1318 functions = {}; |
|
1319 for i = 1:length (files) |
|
1320 file = files{i}; |
|
1321 lf = length (file); |
|
1322 if (lf > 2 && strcmp (file(end-1:end), ".m")) |
|
1323 functions{end+1} = file(1:end-2); |
|
1324 elseif (lf > 4 && strcmp (file(end-3:end), ".oct")) |
|
1325 functions{end+1} = file(1:end-4); |
5801
|
1326 endif |
6496
|
1327 endfor |
|
1328 |
|
1329 ## Does desc have a categories field? |
|
1330 if (! isfield (desc, "categories")) |
|
1331 error ("the DESCRIPTION file must have a Categories field, when no INDEX file is given"); |
|
1332 endif |
|
1333 categories = split_by (desc.categories, ","); |
|
1334 if (length (categories) < 1) |
|
1335 error ("the Category field is empty"); |
|
1336 endif |
|
1337 |
|
1338 ## Write INDEX |
|
1339 fid = fopen (INDEX, "w"); |
|
1340 if (fid == -1) |
|
1341 error ("couldn't open %s for writing.", INDEX); |
|
1342 endif |
|
1343 fprintf (fid, "%s >> %s\n", desc.name, desc.title); |
|
1344 fprintf (fid, "%s\n", categories{1}); |
|
1345 fprintf (fid, " %s\n", functions{:}); |
|
1346 fclose (fid); |
5801
|
1347 endfunction |
|
1348 |
6496
|
1349 function bad_deps = get_unsatisfied_deps (desc, installed_packages) |
|
1350 bad_deps = {}; |
5801
|
1351 |
6496
|
1352 ## For each dependency |
|
1353 for i = 1:length (desc.depends) |
|
1354 dep = desc.depends{i}; |
5801
|
1355 |
6496
|
1356 ## Is the current dependency Octave? |
|
1357 if (strcmp (dep.package, "octave")) |
|
1358 if (! compare_versions (OCTAVE_VERSION, dep.version, dep.operator)) |
|
1359 bad_deps{end+1} = dep; |
6258
|
1360 endif |
6496
|
1361 ## Is the current dependency not Octave? |
|
1362 else |
|
1363 ok = false; |
|
1364 for i = 1:length (installed_packages) |
|
1365 cur_name = installed_packages{i}.name; |
|
1366 cur_version = installed_packages{i}.version; |
|
1367 if (strcmp (dep.package, cur_name) |
|
1368 && compare_versions (cur_version, dep.version, dep.operator)) |
|
1369 ok = true; |
|
1370 break; |
6258
|
1371 endif |
|
1372 endfor |
6496
|
1373 if (! ok) |
|
1374 bad_deps{end+1} = dep; |
|
1375 endif |
5801
|
1376 endif |
6496
|
1377 endfor |
|
1378 endfunction |
|
1379 |
|
1380 function [out1, out2] = installed_packages (local_list, global_list) |
|
1381 ## Get the list of installed packages |
|
1382 try |
|
1383 local_packages = load (local_list).local_packages; |
|
1384 catch |
|
1385 local_packages = {}; |
|
1386 end_try_catch |
|
1387 try |
|
1388 if (strcmp(local_list, global_list)) |
|
1389 global_packages = {}; |
|
1390 else |
|
1391 global_packages = load (global_list).global_packages; |
|
1392 endif |
|
1393 catch |
|
1394 global_packages = {}; |
|
1395 end_try_catch |
|
1396 installed_packages = {local_packages{:}, global_packages{:}}; |
|
1397 |
|
1398 ## Eliminate duplicates in the installed package list. |
|
1399 ## Locally installed packages take precedence |
|
1400 dup = []; |
|
1401 for i = 1:length (installed_packages) |
|
1402 if (find (dup, i)) |
|
1403 continue; |
5801
|
1404 endif |
6496
|
1405 for j = (i+1):length (installed_packages) |
|
1406 if (find (dup, j)) |
|
1407 continue; |
|
1408 endif |
|
1409 if (strcmp (installed_packages{i}.name, installed_packages{j}.name)) |
|
1410 dup = [dup, j]; |
|
1411 endif |
5987
|
1412 endfor |
6496
|
1413 endfor |
|
1414 if (! isempty(dup)) |
|
1415 installed_packages(dup) = []; |
|
1416 endif |
|
1417 |
6616
|
1418 ## Now check if the package is loaded |
6645
|
1419 tmppath = strrep (path(), "\\", "/"); |
6616
|
1420 for i = 1:length (installed_packages) |
6645
|
1421 if (regexp (tmppath, strrep (installed_packages{i}.dir, "\\", "/"))) |
6616
|
1422 installed_packages{i}.loaded = true; |
|
1423 else |
|
1424 installed_packages{i}.loaded = false; |
|
1425 endif |
|
1426 endfor |
|
1427 |
6496
|
1428 ## Should we return something? |
|
1429 if (nargout == 2) |
|
1430 out1 = local_packages; |
|
1431 out2 = global_packages; |
|
1432 return; |
|
1433 elseif (nargout == 1) |
|
1434 out1 = installed_packages; |
|
1435 return; |
|
1436 endif |
|
1437 |
|
1438 ## We shouldn't return something, so we'll print something |
|
1439 num_packages = length (installed_packages); |
|
1440 if (num_packages == 0) |
|
1441 printf ("no packages installed.\n"); |
|
1442 return; |
|
1443 endif |
|
1444 |
|
1445 ## Compute the maximal lengths of name, version, and dir |
|
1446 h1 = "Package Name"; |
|
1447 h2 = "Version"; |
|
1448 h3 = "Installation directory"; |
|
1449 max_name_length = length (h1); |
|
1450 max_version_length = length (h2); |
|
1451 names = cell (num_packages, 1); |
|
1452 for i = 1:num_packages |
|
1453 max_name_length = max (max_name_length, |
|
1454 length (installed_packages{i}.name)); |
|
1455 max_version_length = max (max_version_length, |
|
1456 length (installed_packages{i}.version)); |
|
1457 names{i} = installed_packages{i}.name; |
|
1458 endfor |
6616
|
1459 h1 = postpad (h1, max_name_length + 1, " "); |
6496
|
1460 h2 = postpad (h2, max_version_length, " ");; |
|
1461 |
|
1462 ## Print a header |
|
1463 header = sprintf("%s | %s | %s\n", h1, h2, h3); |
|
1464 printf (header); |
|
1465 tmp = sprintf (repmat ("-", 1, length(header)-1)); |
|
1466 tmp(length(h1)+2) = "+"; |
|
1467 tmp(length(h1)+length(h2)+5) = "+"; |
|
1468 printf ("%s\n", tmp); |
|
1469 |
|
1470 ## Print the packages |
6616
|
1471 format = sprintf ("%%%ds %%1s| %%%ds | %%s\n", max_name_length, |
6496
|
1472 max_version_length); |
|
1473 [dummy, idx] = sort (names); |
|
1474 for i = 1:num_packages |
|
1475 cur_name = installed_packages{idx(i)}.name; |
|
1476 cur_version = installed_packages{idx(i)}.version; |
|
1477 cur_dir = installed_packages{idx(i)}.dir; |
6616
|
1478 if (installed_packages{idx(i)}.loaded) |
|
1479 cur_loaded = "*"; |
|
1480 else |
|
1481 cur_loaded = " "; |
|
1482 endif |
|
1483 printf (format, cur_name, cur_loaded, cur_version, cur_dir); |
6496
|
1484 endfor |
5801
|
1485 endfunction |
|
1486 |
6496
|
1487 function load_packages (files, handle_deps, local_list, global_list) |
|
1488 installed_packages = installed_packages (local_list, global_list); |
|
1489 num_packages = length (installed_packages); |
|
1490 |
|
1491 ## Read package names and installdirs into a more convenient format |
|
1492 pnames = pdirs = cell (1, num_packages); |
|
1493 for i = 1:num_packages |
|
1494 pnames{i} = installed_packages{i}.name; |
|
1495 pdirs{i} = installed_packages{i}.dir; |
|
1496 pdeps{i} = installed_packages{i}.depends; |
|
1497 endfor |
|
1498 |
|
1499 ## load all |
|
1500 if (length (files) == 1 && strcmp (files{1}, "all")) |
|
1501 dirs = pdirs; |
|
1502 ## load auto |
|
1503 elseif (length (files) == 1 && strcmp (files{1}, "auto")) |
|
1504 dirs = {}; |
|
1505 for i = 1:length (installed_packages) |
|
1506 if (exist (fullfile (pdirs{i}, "packinfo", ".autoload"), "file")) |
|
1507 dirs{end+1} = pdirs{i}; |
|
1508 endif |
6037
|
1509 endfor |
6496
|
1510 ## load package_name1 ... |
|
1511 else |
|
1512 dirs = {}; |
|
1513 for i = 1:length (files) |
|
1514 idx = strcmp (pnames, files{i}); |
|
1515 if (! any (idx)) |
|
1516 error ("package %s is not installed", files{i}); |
|
1517 endif |
|
1518 dirs{end+1} = pdirs{idx}; |
|
1519 if (handle_deps) |
|
1520 pdep = pdeps{idx}; |
|
1521 for j = 1:length (pdep) |
|
1522 depname = pdep{j}.package; |
|
1523 if (strcmp (depname, "octave")) |
|
1524 continue; |
|
1525 endif |
|
1526 idx = strcmp (pnames, depname); |
|
1527 if (! any (idx)) |
|
1528 error ("package %s could not be loaded since it depends on %s", |
|
1529 files{i}, depname); |
|
1530 endif |
|
1531 dirs{end+1} = pdirs{idx}; |
|
1532 endfor |
|
1533 endif |
|
1534 endfor |
|
1535 dirs = unique(dirs); |
|
1536 endif |
6037
|
1537 |
6614
|
1538 ## Check for architecture dependent directories |
|
1539 arch = getarch(); |
|
1540 archdirs = {}; |
|
1541 for i = 1:length (dirs) |
|
1542 tmpdir = fullfile (dirs{i}, arch); |
|
1543 if (exist (tmpdir, "dir")) |
|
1544 archdirs{end + 1} = dirs{i}; |
|
1545 archdirs{end + 1} = tmpdir; |
|
1546 endif |
|
1547 endfor |
|
1548 if (length (archdirs) > 0) |
|
1549 dirs = archdirs; |
|
1550 endif |
|
1551 |
6496
|
1552 ## Load the packages |
|
1553 if (length (dirs) > 0) |
|
1554 addpath (dirs{:}); |
|
1555 endif |
5971
|
1556 |
6496
|
1557 ## Add local binaries, if any, to the EXEC_PATH |
|
1558 for i = 1:length (dirs) |
|
1559 if (exist (fullfile (dirs{i}, "bin"), "dir")) |
|
1560 EXEC_PATH (strcat (fullfile(dirs{i}, "bin"), ":", EXEC_PATH ())); |
|
1561 endif |
|
1562 endfor |
5801
|
1563 endfunction |
5928
|
1564 |
6496
|
1565 function unload_packages (files, handle_deps, local_list, global_list) |
|
1566 installed_packages = installed_packages (local_list, global_list); |
|
1567 num_packages = length (installed_packages); |
|
1568 |
|
1569 ## Read package names and installdirs into a more convenient format |
|
1570 pnames = pdirs = cell (1, num_packages); |
|
1571 for i = 1:num_packages |
|
1572 pnames{i} = installed_packages{i}.name; |
|
1573 pdirs{i} = installed_packages{i}.dir; |
|
1574 pdeps{i} = installed_packages{i}.depends; |
|
1575 endfor |
|
1576 |
|
1577 ## Get the current octave path |
|
1578 p = split_by (path(), pathsep ()); |
6203
|
1579 |
6496
|
1580 ## unload all |
|
1581 if (length (files) == 1 && strcmp (files{1}, "all")) |
|
1582 dirs = pdirs; |
|
1583 ## unload package_name1 ... |
|
1584 else |
|
1585 dirs = {}; |
|
1586 for i = 1:length (files) |
|
1587 idx = strcmp (pnames, files{i}); |
|
1588 if (! any (idx)) |
|
1589 error ("package %s is not installed", files{i}); |
|
1590 endif |
|
1591 dirs{end+1} = pdirs{idx}; |
|
1592 endfor |
|
1593 endif |
|
1594 |
6614
|
1595 ## Check for architecture dependent directories |
|
1596 arch = getarch(); |
|
1597 archdirs = {}; |
|
1598 for i = 1:length (dirs) |
|
1599 tmpdir = fullfile (dirs{i}, arch); |
|
1600 if (exist (tmpdir, "dir")) |
|
1601 archdirs{end + 1} = dirs{i}; |
|
1602 archdirs{end + 1} = tmpdir; |
|
1603 endif |
|
1604 endfor |
|
1605 if (length (archdirs) > 0) |
|
1606 dirs = archdirs; |
|
1607 endif |
|
1608 |
6496
|
1609 ## Unload the packages |
|
1610 for i = 1:length (dirs) |
|
1611 d = dirs{i}; |
|
1612 idx = strcmp (p, d); |
|
1613 if (any (idx)) |
|
1614 rmpath (d); |
|
1615 ## XXX: We should also check if we need to remove items from EXEC_PATH |
6203
|
1616 endif |
6496
|
1617 endfor |
6203
|
1618 endfunction |
|
1619 |
5928
|
1620 function [status_out, msg_out] = rm_rf (dir) |
|
1621 crr = confirm_recursive_rmdir (); |
|
1622 unwind_protect |
|
1623 confirm_recursive_rmdir (false); |
|
1624 [status, msg] = rmdir (dir, "s"); |
|
1625 unwind_protect_cleanup |
|
1626 confirm_recursive_rmdir (crr); |
|
1627 end_unwind_protect |
|
1628 if (nargout > 0) |
|
1629 status_out = status; |
|
1630 endif |
|
1631 if (nargout > 1) |
|
1632 msg_out = msg; |
|
1633 endif |
|
1634 endfunction |
5971
|
1635 |
|
1636 function emp = dirempty (nm, ign) |
|
1637 if (nargin < 2) |
6496
|
1638 ign = {".", ".."}; |
5971
|
1639 else |
6496
|
1640 ign = [{".", ".."}, ign]; |
5971
|
1641 endif |
|
1642 l = dir (nm); |
6496
|
1643 for i = 1:length (l) |
5971
|
1644 found = false; |
6496
|
1645 for j = 1:length (ign) |
|
1646 if (strcmp (l(i).name, ign{j})) |
5971
|
1647 found = true; |
|
1648 break; |
|
1649 endif |
|
1650 endfor |
6496
|
1651 if (! found) |
5971
|
1652 emp = false; |
|
1653 return |
|
1654 endif |
|
1655 endfor |
|
1656 emp = true; |
|
1657 endfunction |
6614
|
1658 |
|
1659 function arch = getarch () |
|
1660 persistent _arch = [octave_config_info("canonical_host_type"), "-", ... |
|
1661 octave_config_info("api_version")]; |
|
1662 arch = _arch; |
|
1663 endfunction |
6645
|
1664 |
|
1665 function [status, output] = shell (cmd) |
|
1666 |
|
1667 persistent have_sh; |
|
1668 |
|
1669 cmd = strrep (cmd, "\\", "/"); |
|
1670 disp (cmd); |
|
1671 if (ispc () && ! isunix ()) |
|
1672 if (isempty(have_sh)) |
|
1673 if (system("sh.exe -c \"exit\"")) |
|
1674 have_sh = false; |
|
1675 else |
|
1676 have_sh = true; |
|
1677 endif |
|
1678 endif |
|
1679 if (have_sh) |
|
1680 [status, output] = system (["sh.exe -c \"", cmd, "\""]); |
|
1681 else |
|
1682 error ("Can not find the command shell") |
|
1683 endif |
|
1684 else |
|
1685 [status, output] = system (cmd); |
|
1686 endif |
|
1687 disp(output); |
|
1688 |
|
1689 endfunction |