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) |
|
294 global_packages = rebuild (prefix, global_list, auto, verbose); |
|
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 (); |
|
318 cd (prefix); |
|
319 dirlist = glob (cellfun(@(x) [x, '-*'], files, 'UniformOutput', 0)) |
|
320 cd (wd); |
|
321 endif |
|
322 descriptions = {}; |
|
323 for k = 1:length (dirlist) |
|
324 descfile = fullfile (prefix, dirlist{k}, "packinfo", "DESCRIPTION"); |
|
325 if (verbose) |
|
326 printf ("recreating package description from %s\n", dirlist{k}); |
|
327 endif |
|
328 if (exist (descfile, "file")) |
|
329 desc = get_description (descfile); |
|
330 desc.dir = fullfile (prefix, dirlist{k}); |
|
331 if (auto != 0) |
|
332 if (exist (fullfile (desc.dir, "packinfo", ".autoload"), "file")) |
|
333 unlink (fullfile (desc.dir, "packinfo", ".autoload")); |
|
334 endif |
|
335 if (auto < 0) |
|
336 desc.autoload = 0; |
|
337 elseif (auto > 0) |
|
338 desc.autoload = 1; |
|
339 fclose (fopen (fullfile (desc.dir, "packinfo", ".autoload"), "wt")); |
|
340 endif |
|
341 else |
|
342 if (exist (fullfile (desc.dir, "packinfo", ".autoload"), "file")) |
|
343 desc.autoload = 1; |
|
344 else |
|
345 desc.autoload = 0; |
|
346 endif |
|
347 endif |
|
348 descriptions{end + 1} = desc; |
|
349 elseif (verbose) |
|
350 warning ("directory %s is not a valid package", dirlist{k}); |
|
351 endif |
|
352 endfor |
|
353 |
|
354 if (isempty (files)) |
|
355 ## We are rebuilding for a particular package(s) so we should take |
|
356 ## care to keep the other untouched packages in the descriptions |
|
357 descriptions = {desriptions{:}, old_desriptions{:}}; |
|
358 |
|
359 dup = []; |
|
360 for i = 1:length (descriptions) |
|
361 if (find (dup, i)) |
|
362 continue; |
|
363 endif |
|
364 for j = (i+1):length (descriptions) |
|
365 if (find (dup, j)) |
|
366 continue; |
|
367 endif |
|
368 if (strcmp (descriptions{i}.name, descriptions{j}.name)) |
|
369 dup = [dup, j]; |
|
370 endif |
|
371 endfor |
|
372 endfor |
|
373 if (! isempty (dup)) |
|
374 descriptions (dup) = []; |
|
375 endif |
|
376 endif |
|
377 endfunction |
|
378 |
6496
|
379 function auto = isautoload (desc) |
|
380 auto = false; |
|
381 if (isfield (desc{1}, "autoload")) |
|
382 a = desc{1}.autoload; |
|
383 if ((isnumeric (a) && a > 0) |
6500
|
384 || (ischar (a) && (strcmpi (a, "true") |
|
385 || strcmpi (a, "on") |
|
386 || strcmpi (a, "yes") |
|
387 || strcmpi (a, "1")))) |
6496
|
388 auto = true; |
|
389 endif |
|
390 endif |
5801
|
391 endfunction |
|
392 |
6645
|
393 function install (files, handle_deps, autoload, prefix, verbose, local_list, global_list, global_install) |
6496
|
394 |
|
395 # Check that the directory in prefix exist. If it doesn't: create it! |
|
396 if (! exist (prefix, "dir")) |
|
397 warning ("creating installation directory %s", prefix); |
|
398 [status, msg] = mkdir (prefix); |
|
399 if (status != 1) |
|
400 error ("could not create installation directory: %s", msg); |
|
401 endif |
|
402 endif |
|
403 |
|
404 ## Get the list of installed packages |
|
405 [local_packages, global_packages] = installed_packages (local_list, |
|
406 global_list); |
|
407 |
|
408 installed_packages = {local_packages{:}, global_packages{:}}; |
|
409 |
|
410 if (global_install) |
|
411 packages = global_packages; |
|
412 else |
|
413 packages = local_packages; |
|
414 endif |
|
415 |
|
416 ## Uncompress the packages and read the DESCRIPTION files |
|
417 tmpdirs = packdirs = descriptions = {}; |
|
418 try |
|
419 ## Unpack the package files and read the DESCRIPTION files |
|
420 files = glob (files); |
|
421 packages_to_uninstall = []; |
|
422 for i = 1:length (files) |
|
423 tgz = files{i}; |
|
424 |
6645
|
425 if (exist (tgz, "file")) |
|
426 ## Create a temporary directory |
|
427 tmpdir = tmpnam (); |
|
428 tmpdirs{end+1} = tmpdir; |
|
429 if (verbose) |
|
430 printf ("mkdir (%s)\n", tmpdir); |
|
431 endif |
|
432 [status, msg] = mkdir (tmpdir); |
|
433 if (status != 1) |
|
434 error ("couldn't create temporary directory: %s", msg); |
|
435 endif |
6496
|
436 |
6645
|
437 ## Uncompress the package |
|
438 if (verbose) |
|
439 printf ("untar (%s, %s)\n", tgz, tmpdir); |
|
440 endif |
|
441 untar (tgz, tmpdir); |
6496
|
442 |
6645
|
443 ## Get the name of the directories produced by tar |
|
444 [dirlist, err, msg] = readdir (tmpdir); |
|
445 if (err) |
|
446 error ("couldn't read directory produced by tar: %s", msg); |
|
447 endif |
6496
|
448 |
6645
|
449 if (length (dirlist) > 3) |
|
450 error ("bundles of packages are not allowed") |
6496
|
451 endif |
|
452 |
6645
|
453 ## the two first entries of dirlist are "." and ".." |
|
454 for k = 3:length (dirlist) |
|
455 packdir = fullfile (tmpdir, dirlist{k}); |
|
456 packdirs{end+1} = packdir; |
6496
|
457 |
6645
|
458 ## Make sure the package contains necessary files |
|
459 verify_directory (packdir); |
|
460 |
|
461 ## Read the DESCRIPTION file |
|
462 filename = fullfile (packdir, "DESCRIPTION"); |
|
463 desc = get_description (filename); |
6496
|
464 |
6645
|
465 ## Verify that package name corresponds with filename |
|
466 [dummy, nm] = fileparts (tgz); |
|
467 if ((length (nm) >= length (desc.name)) |
|
468 && ! strcmp (desc.name, nm(1:length(desc.name)))) |
|
469 error ("package name '%s' doesn't correspond to its filename '%s'", desc.name, nm); |
6496
|
470 endif |
6645
|
471 |
|
472 ## Set default installation directory |
|
473 desc.dir = fullfile (prefix, strcat (desc.name, "-", desc.version)); |
|
474 |
|
475 ## Save desc |
|
476 descriptions{end+1} = desc; |
|
477 |
|
478 ## Are any of the new packages already installed? |
|
479 ## If so we'll remove the old version. |
|
480 for j = 1:length (packages) |
|
481 if (strcmp (packages{j}.name, desc.name)) |
|
482 packages_to_uninstall(end+1) = j; |
|
483 endif |
|
484 endfor |
|
485 endfor |
|
486 else |
|
487 warning ("file %s does not exist", tgz); |
|
488 endif |
6496
|
489 endfor |
|
490 catch |
|
491 ## Something went wrong, delete tmpdirs |
|
492 for i = 1:length (tmpdirs) |
|
493 rm_rf (tmpdirs{i}); |
|
494 endfor |
|
495 error (lasterr()(8:end)); |
|
496 end_try_catch |
|
497 |
|
498 ## Check dependencies |
|
499 if (handle_deps) |
|
500 ok = true; |
|
501 error_text = ""; |
|
502 for i = 1:length (descriptions) |
|
503 desc = descriptions{i}; |
|
504 idx1 = complement (packages_to_uninstall, 1:length(installed_packages)); |
|
505 idx2 = complement (i, 1:length(descriptions)); |
|
506 pseudo_installed_packages = {installed_packages{idx1}, descriptions{idx2}}; |
|
507 bad_deps = get_unsatisfied_deps (desc, pseudo_installed_packages); |
|
508 ## Are there any unsatisfied dependencies? |
|
509 if (! isempty (bad_deps)) |
|
510 ok = false; |
|
511 for i = 1:length (bad_deps) |
|
512 dep = bad_deps{i}; |
|
513 error_text = strcat (error_text, " ", desc.name, " needs ", |
|
514 dep.package, " ", dep.operator, " ", |
|
515 dep.version, "\n"); |
|
516 endfor |
|
517 endif |
|
518 endfor |
|
519 |
|
520 ## Did we find any unsatisfied dependencies? |
|
521 if (! ok) |
|
522 error ("the following dependencies where unsatisfied:\n %s", error_text); |
|
523 endif |
|
524 endif |
|
525 |
|
526 ## Prepare each package for installation |
|
527 try |
|
528 for i = 1:length (descriptions) |
|
529 desc = descriptions{i}; |
|
530 pdir = packdirs{i}; |
|
531 prepare_installation (desc, pdir); |
6614
|
532 configure_make (desc, pdir, verbose); |
6496
|
533 endfor |
|
534 catch |
|
535 ## Something went wrong, delete tmpdirs |
|
536 for i = 1:length (tmpdirs) |
|
537 rm_rf (tmpdirs{i}); |
|
538 endfor |
|
539 error (lasterr()(8:end)); |
|
540 end_try_catch |
|
541 |
|
542 ## Uninstall the packages that will be replaced |
|
543 try |
|
544 for i = packages_to_uninstall |
6614
|
545 uninstall ({installed_packages{i}.name}, false, verbose, local_list, |
6645
|
546 global_list, global_install); |
6496
|
547 endfor |
|
548 catch |
|
549 ## Something went wrong, delete tmpdirs |
|
550 for i = 1:length (tmpdirs) |
|
551 rm_rf (tmpdirs{i}); |
|
552 endfor |
|
553 error (lasterr()(8:end)); |
|
554 end_try_catch |
|
555 |
|
556 ## Install each package |
|
557 try |
|
558 for i = 1:length (descriptions) |
|
559 desc = descriptions{i}; |
|
560 pdir = packdirs{i}; |
|
561 copy_files (desc, pdir); |
|
562 create_pkgadddel (desc, pdir, "PKG_ADD"); |
|
563 create_pkgadddel (desc, pdir, "PKG_DEL"); |
|
564 finish_installation (desc, pdir) |
|
565 endfor |
|
566 catch |
|
567 ## Something went wrong, delete tmpdirs |
|
568 for i = 1:length (tmpdirs) |
|
569 rm_rf (tmpdirs{i}); |
|
570 endfor |
|
571 for i = 1:length (descriptions) |
|
572 rm_rf (descriptions{i}.dir); |
|
573 endfor |
|
574 error (lasterr()(8:end)); |
|
575 end_try_catch |
|
576 |
|
577 ## Check if the installed directory is empty. If it is remove it |
|
578 ## from the list |
|
579 for i = length (descriptions):-1:1 |
|
580 if (dirempty (descriptions{i}.dir, {"packinfo", "doc"})) |
|
581 rm_rf (descriptions{i}.dir); |
|
582 descriptions(i) = []; |
|
583 endif |
|
584 endfor |
|
585 |
|
586 ## If the package requested that it is autoloaded, or the installer |
|
587 ## requested that it is, then mark the package as autoloaded. |
|
588 for i = length (descriptions):-1:1 |
|
589 if (autoload > 0 || (autoload == 0 && isautoload (descriptions(i)))) |
|
590 fclose (fopen (fullfile (descriptions{i}.dir, "packinfo", |
|
591 ".autoload"), "wt")); |
|
592 endif |
|
593 endfor |
|
594 |
|
595 ## Add the packages to the package list |
|
596 try |
|
597 if (global_install) |
|
598 idx = complement (packages_to_uninstall, 1:length(global_packages)); |
|
599 global_packages = {global_packages{idx}, descriptions{:}}; |
|
600 save (global_list, "global_packages"); |
|
601 else |
|
602 idx = complement (packages_to_uninstall, 1:length(local_packages)); |
|
603 local_packages = {local_packages{idx}, descriptions{:}}; |
|
604 save (local_list, "local_packages"); |
|
605 endif |
|
606 catch |
|
607 ## Something went wrong, delete tmpdirs |
|
608 for i = 1:length (tmpdirs) |
|
609 rm_rf (tmpdirs{i}); |
|
610 endfor |
|
611 for i = 1:length (descriptions) |
|
612 rm_rf (descriptions{i}.dir); |
|
613 endfor |
|
614 if (global_install) |
|
615 error ("couldn't append to %s: %s", global_list, lasterr()(8:end)); |
|
616 else |
|
617 error ("couldn't append to %s: %s", local_list, lasterr()(8:end)); |
|
618 endif |
|
619 end_try_catch |
|
620 |
|
621 ## All is well, let's clean up |
|
622 for i = 1:length (tmpdirs) |
|
623 [status, msg] = rm_rf (tmpdirs{i}); |
|
624 if (status != 1) |
|
625 warning ("couldn't clean up after my self: %s\n", msg); |
|
626 endif |
|
627 endfor |
|
628 |
|
629 ## Add the newly installed packages to the path, so the user |
|
630 ## can begin usings them. |
6614
|
631 arch = getarch(); |
6496
|
632 if (length (descriptions) > 0) |
6614
|
633 dirs = {}; |
6496
|
634 for i = 1:length (descriptions) |
6614
|
635 dirs{end + 1} = descriptions{i}.dir; |
|
636 if (exist (fullfile (descriptions{i}.dir, arch), "dir")) |
|
637 dirs{end + 1} = fullfile (descriptions{i}.dir, arch); |
|
638 endif |
6496
|
639 endfor |
|
640 addpath (dirs{:}); |
|
641 endif |
|
642 endfunction |
|
643 |
6645
|
644 function uninstall (pkgnames, handle_deps, verbose, local_list, |
|
645 global_list, global_install) |
6496
|
646 ## Get the list of installed packages |
|
647 [local_packages, global_packages] = installed_packages(local_list, |
|
648 global_list); |
6645
|
649 if (global_install) |
6496
|
650 installed_packages = {local_packages{:}, global_packages{:}}; |
|
651 else |
|
652 installed_packages = local_packages; |
|
653 endif |
|
654 |
|
655 num_packages = length (installed_packages); |
|
656 delete_idx = []; |
|
657 for i = 1:num_packages |
|
658 cur_name = installed_packages{i}.name; |
|
659 if (any (strcmp (cur_name, pkgnames))) |
|
660 delete_idx(end+1) = i; |
|
661 endif |
|
662 endfor |
|
663 |
|
664 ## Are all the packages that should be uninstalled already installed? |
|
665 if (length (delete_idx) != length (pkgnames)) |
6645
|
666 if (global_install) |
6496
|
667 ## Try again for a locally installed package |
|
668 installed_packages = local_packages |
|
669 |
|
670 num_packages = length (installed_packages); |
|
671 delete_idx = []; |
|
672 for i = 1:num_packages |
|
673 cur_name = installed_packages{i}.name; |
|
674 if (any (strcmp (cur_name, pkgnames))) |
|
675 delete_idx(end+1) = i; |
|
676 endif |
|
677 endfor |
|
678 if (length (delete_idx) != length (pkgnames)) |
|
679 ## XXX: We should have a better error message |
|
680 error ("some of the packages you want to uninstall are not installed"); |
|
681 endif |
|
682 else |
|
683 ## XXX: We should have a better error message |
|
684 error ("some of the packages you want to uninstall are not installed."); |
|
685 endif |
|
686 endif |
|
687 |
|
688 ## Compute the packages that will remain installed |
|
689 idx = complement (delete_idx, 1:num_packages); |
|
690 remaining_packages = {installed_packages{idx}}; |
|
691 |
|
692 ## Check dependencies |
|
693 if (handle_deps) |
|
694 error_text = ""; |
|
695 for i = 1:length (remaining_packages) |
|
696 desc = remaining_packages{i}; |
|
697 bad_deps = get_unsatisfied_deps (desc, remaining_packages); |
|
698 |
|
699 ## Will the uninstallation break any dependencies? |
|
700 if (! isempty (bad_deps)) |
|
701 for i = 1:length (bad_deps) |
|
702 dep = bad_deps{i}; |
|
703 error_text = strcat (error_text, " ", desc.name, " needs ", |
|
704 dep.package, " ", dep.operator, " ", |
|
705 dep.version, "\n"); |
|
706 endfor |
|
707 endif |
|
708 endfor |
|
709 |
|
710 if (! isempty (error_text)) |
|
711 error ("the following dependencies where unsatisfied:\n %s", error_text); |
|
712 endif |
|
713 endif |
|
714 |
|
715 ## Delete the directories containing the packages |
|
716 for i = delete_idx |
|
717 desc = installed_packages{i}; |
|
718 ## If an 'on_uninstall.m' exist, call it! |
|
719 if (exist (fullfile (desc.dir, "packinfo", "on_uninstall.m"), "file")) |
6614
|
720 wd = pwd (); |
|
721 cd (fullfile(desc.dir, "packinfo")); |
|
722 on_uninstall (desc); |
|
723 cd (wd); |
6496
|
724 endif |
|
725 ## Do the actual deletion |
|
726 rmpath (desc.dir); |
6614
|
727 if (exist (fullfile (desc.dir, getarch()), "dir")) |
|
728 rmpath (fullfile (desc.dir, getarch ())); |
|
729 endif |
6496
|
730 if (exist (desc.dir, "dir")) |
|
731 [status, msg] = rm_rf (desc.dir); |
|
732 if (status != 1) |
|
733 error ("couldn't delete directory %s: %s", desc.dir, msg); |
|
734 endif |
|
735 else |
|
736 warning ("directory %s previously lost", desc.dir); |
|
737 endif |
|
738 endfor |
|
739 |
|
740 ## Write a new ~/.octave_packages |
6645
|
741 if (global_install) |
6496
|
742 if (length (remaining_packages) == 0) |
|
743 unlink (global_list); |
|
744 else |
|
745 global_packages = remaining_packages; |
|
746 save (global_list, "global_packages"); |
|
747 endif |
|
748 else |
|
749 if (length (remaining_packages) == 0) |
|
750 unlink (local_list); |
|
751 else |
|
752 local_packages = remaining_packages; |
|
753 save (local_list, "local_packages"); |
|
754 endif |
|
755 endif |
|
756 |
|
757 endfunction |
|
758 |
|
759 ########################################################## |
|
760 ## A U X I L I A R Y F U N C T I O N S ## |
|
761 ########################################################## |
|
762 |
|
763 function prepare_installation (desc, packdir) |
|
764 ## Is there a pre_install to call? |
|
765 if (exist (fullfile (packdir, "pre_install.m"), "file")) |
|
766 wd = pwd (); |
|
767 try |
|
768 cd (packdir); |
|
769 pre_install (desc); |
|
770 cd (wd); |
|
771 catch |
|
772 cd (wd); |
|
773 error ("the pre-install function returned the following error: %s", |
|
774 lasterr ()); |
|
775 end_try_catch |
|
776 endif |
|
777 |
|
778 ## If the directory "inst" doesn't exist, we create it |
|
779 inst_dir = fullfile (packdir, "inst"); |
|
780 if (! exist (inst_dir, "dir")) |
|
781 [status, msg] = mkdir (inst_dir); |
|
782 if (status != 1) |
|
783 rm_rf (desc.dir); |
|
784 error ("the 'inst' directory did not exist and could not be created: %s", |
|
785 msg); |
6258
|
786 endif |
|
787 endif |
|
788 endfunction |
|
789 |
6614
|
790 function configure_make (desc, packdir, verbose) |
6496
|
791 ## Perform ./configure, make, make install in "src" |
|
792 if (exist (fullfile (packdir, "src"), "dir")) |
|
793 src = fullfile (packdir, "src"); |
|
794 ## configure |
|
795 if (exist (fullfile (src, "configure"), "file")) |
6645
|
796 [status, output] = shell (strcat ("cd ", src, "; ./configure --prefix=\"", |
|
797 desc.dir, "\"", |
|
798 " CC=", octave_config_info ("CC"), |
|
799 " CXX=", octave_config_info ("CXX"), |
|
800 " AR=", octave_config_info ("AR"), |
|
801 " RANLIB=", octave_config_info ("RANLIB"))); |
6614
|
802 if (verbose) |
|
803 printf("%s", output); |
|
804 endif |
6496
|
805 if (status != 0) |
|
806 rm_rf (desc.dir); |
|
807 error ("the configure script returned the following error: %s", output); |
|
808 endif |
5801
|
809 endif |
|
810 |
6496
|
811 ## make |
|
812 if (exist (fullfile (src, "Makefile"), "file")) |
6645
|
813 [status, output] = shell (strcat ("export INSTALLDIR=\"", desc.dir, |
|
814 "\"; make -C ", src)); |
6614
|
815 if (verbose) |
|
816 printf("%s", output); |
|
817 endif |
6496
|
818 if (status != 0) |
|
819 rm_rf (desc.dir); |
|
820 error ("'make' returned the following error: %s", output); |
|
821 endif |
|
822 %# make install |
|
823 %[status, output] = system(["export INSTALLDIR=" desc.dir "; make install -C " src]); |
6614
|
824 %if (verbose) |
|
825 % printf("%s", output); |
|
826 %endif |
6496
|
827 %if (status != 0) |
|
828 % rm_rf(desc.dir); |
|
829 % error("'make install' returned the following error: %s", output); |
|
830 %endif |
5801
|
831 endif |
|
832 |
6614
|
833 ## Copy files to "inst" and "inst/arch" (this is instead of 'make install') |
6496
|
834 files = fullfile (src, "FILES"); |
|
835 instdir = fullfile (packdir, "inst"); |
6614
|
836 archdir = fullfile (packdir, "inst", getarch ()); |
6496
|
837 if (exist (files, "file")) |
|
838 ## Get file names |
|
839 [fid, msg] = fopen (files, "r"); |
|
840 if (fid < 0) |
|
841 error ("couldn't open %s: %s", files, msg); |
5971
|
842 endif |
6496
|
843 filenames = char (fread (fid))'; |
|
844 fclose (fid); |
|
845 if (filenames(end) == "\n") |
|
846 filenames(end) = []; |
6258
|
847 endif |
6496
|
848 ## Copy the files |
|
849 fn = split_by (filenames, "\n"); |
|
850 delete_idx = []; |
|
851 for i = 1:length (fn) |
|
852 if (! all (isspace (fn{i}))) |
|
853 fn{i} = fullfile (src, fn{i}); |
|
854 else |
|
855 delete_idx(end+1) = i; |
|
856 endif |
5971
|
857 endfor |
6496
|
858 fn(delete_idx) = []; |
|
859 filenames = sprintf ("%s ", fn{:}); |
6614
|
860 |
|
861 filenames = split_by (filenames, " "); |
|
862 archindependent = filenames; |
|
863 mex = regexp (filenames, '^.*\.mex'); |
|
864 archindependent(cellfun ("isempty", mex) == 0) = []; |
|
865 mex (cellfun ("isempty", mex)) = []; |
|
866 mex = cellfun (@(x) x(1), mex); |
|
867 oct = regexp (filenames, '^.*\.oct'); |
|
868 archindependent(cellfun ("isempty", oct) == 0) = []; |
|
869 oct (cellfun ("isempty", oct)) = []; |
|
870 oct = cellfun (@(x) x(1), oct); |
|
871 archdependent = [oct, mex]; |
6496
|
872 else |
|
873 m = dir (fullfile (src, "*.m")); |
|
874 oct = dir (fullfile (src, "*.oct")); |
|
875 mex = dir (fullfile (src, "*.mex")); |
6614
|
876 archdependent = ""; |
|
877 archindependent = ""; |
6496
|
878 filenames = ""; |
|
879 if (length (m) > 0) |
|
880 filenames = sprintf (fullfile (src, "%s "), m.name); |
6614
|
881 archindependent = sprintf (fullfile (src, "%s "), m.name); |
6496
|
882 endif |
|
883 if (length (oct) > 0) |
|
884 filenames = [filenames, " ", sprintf(fullfile(src, "%s "), oct.name)]; |
6614
|
885 archdependent = [archdependent, " ", ... |
|
886 sprintf(fullfile(src, "%s "), oct.name)]; |
6496
|
887 endif |
|
888 if (length (mex) > 0) |
|
889 filenames = [filenames, " ", sprintf(fullfile(src, "%s "), mex.name)]; |
6614
|
890 archdependent = [archdependent, " ", ... |
|
891 sprintf(fullfile(src, "%s "), mex.name)]; |
6496
|
892 endif |
6614
|
893 filenames = split_by (filenames, " "); |
|
894 archdependent = split_by (archdependent, " "); |
|
895 archindependent = split_by (archindependent, " "); |
5971
|
896 endif |
5801
|
897 |
6496
|
898 if (! all (isspace (filenames))) |
|
899 mkdir (instdir); |
6614
|
900 if (! all (isspace (archindependent))) |
6634
|
901 if (verbose) |
|
902 printf ("copyfile"); |
|
903 printf (" %s", archindependent{:}); |
|
904 printf ("%s\n", instdir); |
|
905 endif |
6614
|
906 [status, output] = copyfile (archindependent, instdir); |
|
907 if (status != 1) |
6496
|
908 rm_rf (desc.dir); |
|
909 error ("Couldn't copy files from 'src' to 'inst': %s", output); |
6614
|
910 endif |
|
911 endif |
|
912 if (! all (isspace (archdependent))) |
6634
|
913 if (verbose) |
|
914 printf ("copyfile"); |
|
915 printf (" %s", archdependent{:}); |
|
916 printf (" %s\n", archdir); |
|
917 endif |
6614
|
918 mkdir (archdir); |
|
919 [status, output] = copyfile (archdependent, archdir); |
|
920 if (status != 1) |
|
921 rm_rf (desc.dir); |
|
922 error ("Couldn't copy files from 'src' to 'inst': %s", output); |
|
923 endif |
|
924 endif |
5801
|
925 endif |
6496
|
926 endif |
5801
|
927 endfunction |
|
928 |
5971
|
929 function pkg = extract_pkg (nm, pat) |
5955
|
930 fid = fopen (nm, "rt"); |
5971
|
931 pkg = ""; |
5955
|
932 if (fid >= 0) |
6496
|
933 while (! feof (fid)) |
5955
|
934 ln = fgetl (fid); |
|
935 if (ln > 0) |
6496
|
936 t = regexp (ln, pat, "tokens"); |
|
937 if (! isempty (t)) |
|
938 pkg = strcat (pkg, "\n", t{1}{1}); |
5955
|
939 endif |
|
940 endif |
|
941 endwhile |
6496
|
942 if (! isempty (pkg)) |
|
943 pkg = strcat (pkg, "\n"); |
5955
|
944 endif |
|
945 fclose (fid); |
|
946 endif |
|
947 endfunction |
|
948 |
5971
|
949 function create_pkgadddel (desc, packdir, nm) |
6634
|
950 archdir = fullfile (desc.dir, getarch ()); |
|
951 |
|
952 ## If it is exists, the PKG_* files should go into the architecture |
|
953 ## dependent directory so that the autoload/mfilename commands work |
|
954 ## as expected... |
|
955 if (exist (archdir, "dir")) |
|
956 pkg = fullfile (desc.dir, getarch(), nm); |
|
957 else |
|
958 pkg = fullfile (desc.dir, nm); |
|
959 endif |
6496
|
960 fid = fopen (pkg, "wt"); |
5976
|
961 |
5955
|
962 if (fid >= 0) |
5971
|
963 ## Search all dot-m files for PKG commands |
6233
|
964 lst = dir (fullfile(packdir, "inst", "*.m")); |
6496
|
965 for i = 1:length (lst) |
6233
|
966 nam = fullfile(packdir, "inst", lst(i).name); |
5976
|
967 fwrite (fid, extract_pkg (nam, ['^[#%][#%]* *' nm ': *(.*)$'])); |
5955
|
968 endfor |
|
969 |
5971
|
970 ## Search all C++ source files for PKG commands |
6233
|
971 lst = dir (fullfile(packdir, "src", "*.cc")); |
6496
|
972 for i = 1:length (lst) |
6233
|
973 nam = fullfile(packdir, "src", lst(i).name); |
5976
|
974 fwrite (fid, extract_pkg (nam, ['^//* *' nm ': *(.*)$'])); |
|
975 fwrite (fid, extract_pkg (nam, ['^/\** *' nm ': *(.*) *\*/$'])); |
5955
|
976 endfor |
|
977 |
5971
|
978 ## Add developer included PKG commands |
6496
|
979 packdirnm = fullfile (packdir, nm); |
|
980 if (exist (packdirnm, "file")) |
|
981 fid2 = fopen (packdirnm, "rt"); |
5955
|
982 if (fid2 >= 0) |
6496
|
983 while (! feof (fid2)) |
5955
|
984 ln = fgets (fid2); |
|
985 if (ln > 0) |
6496
|
986 fwrite (fid, ln); |
5955
|
987 endif |
|
988 endwhile |
6496
|
989 fclose (fid2); |
5955
|
990 endif |
|
991 endif |
6496
|
992 fclose (fid); |
5971
|
993 |
|
994 ## If the file is empty remove it |
|
995 t = dir (pkg); |
|
996 if (t.bytes <= 0) |
|
997 unlink (pkg); |
|
998 endif |
5955
|
999 endif |
|
1000 endfunction |
|
1001 |
5971
|
1002 function copy_files (desc, packdir, bindir) |
6496
|
1003 ## Create the installation directory |
|
1004 if (! exist (desc.dir, "dir")) |
|
1005 [status, output] = mkdir (desc.dir); |
|
1006 if (status != 1) |
|
1007 error ("couldn't create installation directory %s : %s", |
|
1008 desc.dir, output); |
6020
|
1009 endif |
6496
|
1010 endif |
6020
|
1011 |
6496
|
1012 ## Copy the files from "inst" to installdir |
|
1013 instdir = fullfile (packdir, "inst"); |
|
1014 if (! dirempty (instdir)) |
|
1015 [status, output] = copyfile (fullfile (instdir, "*"), desc.dir); |
|
1016 if (status != 1) |
|
1017 rm_rf (desc.dir); |
|
1018 error ("couldn't copy files to the installation directory"); |
5801
|
1019 endif |
6496
|
1020 endif |
5801
|
1021 |
6496
|
1022 ## Create the "packinfo" directory |
|
1023 packinfo = fullfile (desc.dir, "packinfo"); |
|
1024 [status, msg] = mkdir (packinfo); |
|
1025 if (status != 1) |
|
1026 rm_rf (desc.dir); |
|
1027 error ("couldn't create packinfo directory: %s", msg); |
|
1028 endif |
5801
|
1029 |
6496
|
1030 ## Copy DESCRIPTION |
|
1031 [status, output] = copyfile (fullfile (packdir, "DESCRIPTION"), packinfo); |
|
1032 if (status != 1) |
|
1033 rm_rf (desc.dir); |
|
1034 error ("couldn't copy DESCRIPTION: %s", output); |
|
1035 endif |
5801
|
1036 |
6496
|
1037 ## Copy COPYING |
|
1038 [status, output] = copyfile (fullfile (packdir, "COPYING"), packinfo); |
|
1039 if (status != 1) |
|
1040 rm_rf (desc.dir); |
|
1041 error ("couldn't copy COPYING: %s", output); |
|
1042 endif |
5993
|
1043 |
6496
|
1044 ## If the file ChangeLog exists, copy it |
|
1045 fChangeLog = fullfile(packdir, "ChangeLog"); |
|
1046 if (exist (fChangeLog, "file")) |
|
1047 [status, output] = copyfile (fChangeLog, packinfo); |
|
1048 if (status != 1) |
|
1049 rm_rf (desc.dir); |
|
1050 error ("couldn't copy ChangeLog file: %s", output); |
6254
|
1051 endif |
6496
|
1052 endif |
6254
|
1053 |
6496
|
1054 ## Is there an INDEX file to copy or should we generate one? |
|
1055 fINDEX = fullfile (packdir, "INDEX"); |
|
1056 if (exist(fINDEX, "file")) |
|
1057 [status, output] = copyfile (fINDEX, packinfo); |
|
1058 if (status != 1) |
|
1059 rm_rf (desc.dir); |
|
1060 error ("couldn't copy INDEX file: %s", output); |
5801
|
1061 endif |
6496
|
1062 else |
|
1063 try |
|
1064 write_INDEX (desc, fullfile (packdir, "inst"), |
|
1065 fullfile (packinfo, "INDEX")); |
|
1066 catch |
|
1067 rm_rf (desc.dir); |
|
1068 error (lasterr ()); |
|
1069 end_try_catch |
|
1070 endif |
5971
|
1071 |
6496
|
1072 ## Is there an 'on_uninstall.m' to install? |
|
1073 fon_uninstall = fullfile(packdir, "on_uninstall.m"); |
|
1074 if (exist (fon_uninstall, "file")) |
|
1075 [status, output] = copyfile (fon_uninstall, packinfo); |
|
1076 if (status != 1) |
|
1077 rm_rf (desc.dir); |
|
1078 error ("couldn't copy on_uninstall.m: %s", output); |
5971
|
1079 endif |
6496
|
1080 endif |
5971
|
1081 |
6496
|
1082 ## Is there a doc/ directory that needs to be installed |
|
1083 docdir = fullfile (packdir, "doc"); |
|
1084 if (exist (docdir, "dir") && ! dirempty (docdir)) |
|
1085 [status, output] = copyfile (docdir, desc.dir); |
|
1086 endif |
|
1087 |
|
1088 ## Is there a bin/ directory that needs to be installed |
|
1089 bindir = fullfile (packdir, "bin"); |
|
1090 if (exist (bindir, "dir") && ! dirempty (bindir)) |
|
1091 [status, output] = copyfile (bindir, desc.dir); |
|
1092 endif |
5801
|
1093 endfunction |
|
1094 |
|
1095 function finish_installation (desc, packdir) |
6496
|
1096 ## Is there a post-install to call? |
|
1097 if (exist (fullfile (packdir, "post_install.m"), "file")) |
|
1098 wd = pwd (); |
|
1099 try |
|
1100 cd (packdir); |
|
1101 post_install (desc); |
|
1102 cd (wd); |
|
1103 catch |
|
1104 cd (wd); |
|
1105 rm_rf (desc.dir); |
|
1106 error ("the post_install function returned the following error: %s", |
|
1107 lasterr ()); |
|
1108 end_try_catch |
|
1109 endif |
5801
|
1110 endfunction |
|
1111 |
6496
|
1112 function out = issuperuser () |
6645
|
1113 if (ispc () && ! isunix ()) |
|
1114 out = 1; |
|
1115 else |
|
1116 ## Need to be a bit presistent in probing superuser |
|
1117 out = (geteuid() == 0); |
|
1118 endif |
5801
|
1119 endfunction |
|
1120 |
|
1121 ## This function makes sure the package contains the |
|
1122 ## essential files. |
6496
|
1123 function verify_directory (dir) |
|
1124 needed_files = {"COPYING", "DESCRIPTION"}; |
|
1125 for f = needed_files |
|
1126 if (! exist (fullfile (dir, f{1}), "file")) |
|
1127 error ("package is missing file: %s", f{1}); |
|
1128 endif |
|
1129 endfor |
5801
|
1130 endfunction |
|
1131 |
|
1132 ## This function parses the DESCRIPTION file |
6496
|
1133 function desc = get_description (filename) |
|
1134 [fid, msg] = fopen (filename, "r"); |
|
1135 if (fid == -1) |
|
1136 error ("the DESCRIPTION file %s could not be read: %s", filename, msg); |
|
1137 endif |
|
1138 |
|
1139 desc = struct (); |
5801
|
1140 |
6496
|
1141 line = fgetl (fid); |
|
1142 while (line != -1) |
|
1143 if (line(1) == "#") |
|
1144 ## Comments, do nothing |
|
1145 elseif (isspace(line(1))) |
|
1146 ## Continuation lines |
|
1147 if (exist ("keyword", "var") && isfield (desc, keyword)) |
|
1148 desc.(keyword) = strcat (desc.(keyword), " ", rstrip(line)); |
|
1149 endif |
5801
|
1150 else |
6496
|
1151 ## Keyword/value pair |
|
1152 colon = find (line == ":"); |
|
1153 if (length (colon) == 0) |
|
1154 disp ("skipping line"); |
|
1155 else |
|
1156 colon = colon(1); |
|
1157 keyword = tolower (strip (line(1:colon-1))); |
|
1158 value = strip (line (colon+1:end)); |
|
1159 if (length (value) == 0) |
|
1160 fclose (fid); |
|
1161 error ("the keyword %s has an empty value", desc.keywords{end}); |
|
1162 endif |
|
1163 desc.(keyword) = value; |
|
1164 endif |
5801
|
1165 endif |
6496
|
1166 line = fgetl (fid); |
|
1167 endwhile |
|
1168 fclose (fid); |
|
1169 |
|
1170 ## Make sure all is okay |
|
1171 needed_fields = {"name", "version", "date", "title", ... |
|
1172 "author", "maintainer", "description"}; |
|
1173 for f = needed_fields |
|
1174 if (! isfield (desc, f{1})) |
|
1175 error ("description is missing needed field %s", f{1}); |
|
1176 endif |
|
1177 endfor |
|
1178 desc.version = fix_version (desc.version); |
|
1179 if (isfield (desc, "depends")) |
|
1180 desc.depends = fix_depends (desc.depends); |
|
1181 else |
|
1182 desc.depends = ""; |
|
1183 endif |
|
1184 desc.name = tolower (desc.name); |
5801
|
1185 endfunction |
|
1186 |
|
1187 ## Makes sure the version string v is a valid x.y.z version string |
|
1188 ## Examples: "0.1" => "0.1.0", "monkey" => error(...) |
6496
|
1189 function out = fix_version (v) |
|
1190 dots = find (v == "."); |
|
1191 if (length (dots) == 1) |
|
1192 major = str2num (v(1:dots-1)); |
|
1193 minor = str2num (v(dots+1:end)); |
|
1194 if (length (major) != 0 && length (minor) != 0) |
|
1195 out = sprintf ("%d.%d.0", major, minor); |
|
1196 return; |
5801
|
1197 endif |
6496
|
1198 elseif (length (dots) == 2) |
|
1199 major = str2num (v(1:dots(1)-1)); |
|
1200 minor = str2num (v(dots(1)+1:dots(2)-1)); |
|
1201 rev = str2num (v(dots(2)+1:end)); |
|
1202 if (length (major) != 0 && length (minor) != 0 && length (rev) != 0) |
|
1203 out = sprintf ("%d.%d.%d", major, minor, rev); |
|
1204 return; |
|
1205 endif |
|
1206 endif |
|
1207 error ("bad version string: %s", v); |
5801
|
1208 endfunction |
|
1209 |
|
1210 ## Makes sure the depends field is of the right format. |
|
1211 ## This function returns a cell of structures with the following fields: |
|
1212 ## package, version, operator |
6496
|
1213 function deps_cell = fix_depends (depends) |
|
1214 deps = split_by (tolower (depends), ","); |
|
1215 deps_cell = cell (1, length (deps)); |
|
1216 |
|
1217 ## For each dependency |
|
1218 for i = 1:length (deps) |
|
1219 dep = deps{i}; |
|
1220 lpar = find (dep == "("); |
|
1221 rpar = find (dep == ")"); |
|
1222 ## Does the dependency specify a version |
|
1223 ## Example: package(>= version) |
|
1224 if (length (lpar) == 1 && length (rpar) == 1) |
|
1225 package = tolower (strip (dep(1:lpar-1))); |
|
1226 sub = dep(lpar(1)+1:rpar(1)-1); |
|
1227 parts = split_by (sub, " "); |
|
1228 idx = []; |
|
1229 for r = 1:size (parts, 1) |
|
1230 if (length (parts{r}) > 0) |
|
1231 idx(end+1) = r; |
|
1232 endif |
|
1233 endfor |
|
1234 |
|
1235 if (length (idx) != 2) |
|
1236 error ("incorrect syntax for dependency `%s' in the DESCRIPTION file\n", |
|
1237 dep); |
|
1238 endif |
|
1239 operator = parts{idx(1)}; |
|
1240 if (! any (strcmp (operator, {">", ">=", "<=", "<", "=="}))) |
|
1241 error ("unsupported operator: %s", operator); |
|
1242 endif |
|
1243 version = fix_version (parts{idx(2)}); |
|
1244 |
|
1245 ## If no version is specified for the dependency |
|
1246 ## we say that the version should be greater than |
|
1247 ## or equal to 0.0.0 |
|
1248 else |
|
1249 package = tolower (strip (dep)); |
|
1250 operator = ">="; |
|
1251 version = "0.0.0"; |
|
1252 endif |
|
1253 deps_cell{i} = struct ("package", package, "operator", operator, |
|
1254 "version", version); |
|
1255 endfor |
5801
|
1256 endfunction |
|
1257 |
|
1258 ## Strips the text of spaces from the right |
|
1259 ## Example: " hello world " => " hello world" (XXX: is this the same as deblank?) |
6496
|
1260 function text = rstrip (text) |
|
1261 chars = find (! isspace (text)); |
|
1262 if (length (chars) > 0) |
|
1263 ## XXX: shouldn't it be text = text(1:chars(end)); |
|
1264 text = text (chars(1):end); |
|
1265 else |
|
1266 text = ""; |
|
1267 endif |
5801
|
1268 endfunction |
|
1269 |
|
1270 ## Strips the text of spaces from the left and the right |
|
1271 ## Example: " hello world " => "hello world" |
6496
|
1272 function text = strip (text) |
|
1273 chars = find (! isspace (text)); |
|
1274 if (length (chars) > 0) |
|
1275 text = text(chars(1):chars(end)); |
|
1276 else |
|
1277 text = ""; |
|
1278 endif |
5801
|
1279 endfunction |
|
1280 |
|
1281 ## Splits the text into a cell array of strings by sep |
|
1282 ## Example: "A, B" => {"A", "B"} (with sep = ",") |
6496
|
1283 function out = split_by (text, sep) |
|
1284 text_matrix = split (text, sep); |
|
1285 num_words = size (text_matrix, 1); |
|
1286 out = cell (num_words, 1); |
|
1287 for i = 1:num_words |
|
1288 out{i} = strip (text_matrix(i, :)); |
|
1289 endfor |
5801
|
1290 endfunction |
|
1291 |
|
1292 ## Creates an INDEX file for a package that doesn't provide one. |
|
1293 ## 'desc' describes the package. |
|
1294 ## 'dir' is the 'inst' direcotyr in temporary directory. |
|
1295 ## 'INDEX' is the name (including path) of resulting INDEX file. |
6496
|
1296 function write_INDEX (desc, dir, INDEX) |
|
1297 ## Get names of functions in dir |
|
1298 [files, err, msg] = readdir (dir); |
|
1299 if (err) |
|
1300 error ("couldn't read directory %s: %s", dir, msg); |
|
1301 endif |
|
1302 |
6634
|
1303 ## Check for architecture dependent files |
|
1304 arch = getarch(); |
|
1305 tmpdir = fullfile (dir, arch); |
|
1306 if (exist (tmpdir, "dir")) |
|
1307 [files2, err, msg] = readdir (tmpdir); |
|
1308 if (err) |
|
1309 error ("couldn't read directory %s: %s", tmpdir, msg); |
|
1310 endif |
|
1311 files = [files; files2]; |
|
1312 endif |
|
1313 |
6496
|
1314 functions = {}; |
|
1315 for i = 1:length (files) |
|
1316 file = files{i}; |
|
1317 lf = length (file); |
|
1318 if (lf > 2 && strcmp (file(end-1:end), ".m")) |
|
1319 functions{end+1} = file(1:end-2); |
|
1320 elseif (lf > 4 && strcmp (file(end-3:end), ".oct")) |
|
1321 functions{end+1} = file(1:end-4); |
5801
|
1322 endif |
6496
|
1323 endfor |
|
1324 |
|
1325 ## Does desc have a categories field? |
|
1326 if (! isfield (desc, "categories")) |
|
1327 error ("the DESCRIPTION file must have a Categories field, when no INDEX file is given"); |
|
1328 endif |
|
1329 categories = split_by (desc.categories, ","); |
|
1330 if (length (categories) < 1) |
|
1331 error ("the Category field is empty"); |
|
1332 endif |
|
1333 |
|
1334 ## Write INDEX |
|
1335 fid = fopen (INDEX, "w"); |
|
1336 if (fid == -1) |
|
1337 error ("couldn't open %s for writing.", INDEX); |
|
1338 endif |
|
1339 fprintf (fid, "%s >> %s\n", desc.name, desc.title); |
|
1340 fprintf (fid, "%s\n", categories{1}); |
|
1341 fprintf (fid, " %s\n", functions{:}); |
|
1342 fclose (fid); |
5801
|
1343 endfunction |
|
1344 |
6496
|
1345 function bad_deps = get_unsatisfied_deps (desc, installed_packages) |
|
1346 bad_deps = {}; |
5801
|
1347 |
6496
|
1348 ## For each dependency |
|
1349 for i = 1:length (desc.depends) |
|
1350 dep = desc.depends{i}; |
5801
|
1351 |
6496
|
1352 ## Is the current dependency Octave? |
|
1353 if (strcmp (dep.package, "octave")) |
|
1354 if (! compare_versions (OCTAVE_VERSION, dep.version, dep.operator)) |
|
1355 bad_deps{end+1} = dep; |
6258
|
1356 endif |
6496
|
1357 ## Is the current dependency not Octave? |
|
1358 else |
|
1359 ok = false; |
|
1360 for i = 1:length (installed_packages) |
|
1361 cur_name = installed_packages{i}.name; |
|
1362 cur_version = installed_packages{i}.version; |
|
1363 if (strcmp (dep.package, cur_name) |
|
1364 && compare_versions (cur_version, dep.version, dep.operator)) |
|
1365 ok = true; |
|
1366 break; |
6258
|
1367 endif |
|
1368 endfor |
6496
|
1369 if (! ok) |
|
1370 bad_deps{end+1} = dep; |
|
1371 endif |
5801
|
1372 endif |
6496
|
1373 endfor |
|
1374 endfunction |
|
1375 |
|
1376 function [out1, out2] = installed_packages (local_list, global_list) |
|
1377 ## Get the list of installed packages |
|
1378 try |
|
1379 local_packages = load (local_list).local_packages; |
|
1380 catch |
|
1381 local_packages = {}; |
|
1382 end_try_catch |
|
1383 try |
|
1384 if (strcmp(local_list, global_list)) |
|
1385 global_packages = {}; |
|
1386 else |
|
1387 global_packages = load (global_list).global_packages; |
|
1388 endif |
|
1389 catch |
|
1390 global_packages = {}; |
|
1391 end_try_catch |
|
1392 installed_packages = {local_packages{:}, global_packages{:}}; |
|
1393 |
|
1394 ## Eliminate duplicates in the installed package list. |
|
1395 ## Locally installed packages take precedence |
|
1396 dup = []; |
|
1397 for i = 1:length (installed_packages) |
|
1398 if (find (dup, i)) |
|
1399 continue; |
5801
|
1400 endif |
6496
|
1401 for j = (i+1):length (installed_packages) |
|
1402 if (find (dup, j)) |
|
1403 continue; |
|
1404 endif |
|
1405 if (strcmp (installed_packages{i}.name, installed_packages{j}.name)) |
|
1406 dup = [dup, j]; |
|
1407 endif |
5987
|
1408 endfor |
6496
|
1409 endfor |
|
1410 if (! isempty(dup)) |
|
1411 installed_packages(dup) = []; |
|
1412 endif |
|
1413 |
6616
|
1414 ## Now check if the package is loaded |
6645
|
1415 tmppath = strrep (path(), "\\", "/"); |
6616
|
1416 for i = 1:length (installed_packages) |
6645
|
1417 if (regexp (tmppath, strrep (installed_packages{i}.dir, "\\", "/"))) |
6616
|
1418 installed_packages{i}.loaded = true; |
|
1419 else |
|
1420 installed_packages{i}.loaded = false; |
|
1421 endif |
|
1422 endfor |
|
1423 |
6496
|
1424 ## Should we return something? |
|
1425 if (nargout == 2) |
|
1426 out1 = local_packages; |
|
1427 out2 = global_packages; |
|
1428 return; |
|
1429 elseif (nargout == 1) |
|
1430 out1 = installed_packages; |
|
1431 return; |
|
1432 endif |
|
1433 |
|
1434 ## We shouldn't return something, so we'll print something |
|
1435 num_packages = length (installed_packages); |
|
1436 if (num_packages == 0) |
|
1437 printf ("no packages installed.\n"); |
|
1438 return; |
|
1439 endif |
|
1440 |
|
1441 ## Compute the maximal lengths of name, version, and dir |
|
1442 h1 = "Package Name"; |
|
1443 h2 = "Version"; |
|
1444 h3 = "Installation directory"; |
|
1445 max_name_length = length (h1); |
|
1446 max_version_length = length (h2); |
|
1447 names = cell (num_packages, 1); |
|
1448 for i = 1:num_packages |
|
1449 max_name_length = max (max_name_length, |
|
1450 length (installed_packages{i}.name)); |
|
1451 max_version_length = max (max_version_length, |
|
1452 length (installed_packages{i}.version)); |
|
1453 names{i} = installed_packages{i}.name; |
|
1454 endfor |
6616
|
1455 h1 = postpad (h1, max_name_length + 1, " "); |
6496
|
1456 h2 = postpad (h2, max_version_length, " ");; |
|
1457 |
|
1458 ## Print a header |
|
1459 header = sprintf("%s | %s | %s\n", h1, h2, h3); |
|
1460 printf (header); |
|
1461 tmp = sprintf (repmat ("-", 1, length(header)-1)); |
|
1462 tmp(length(h1)+2) = "+"; |
|
1463 tmp(length(h1)+length(h2)+5) = "+"; |
|
1464 printf ("%s\n", tmp); |
|
1465 |
|
1466 ## Print the packages |
6616
|
1467 format = sprintf ("%%%ds %%1s| %%%ds | %%s\n", max_name_length, |
6496
|
1468 max_version_length); |
|
1469 [dummy, idx] = sort (names); |
|
1470 for i = 1:num_packages |
|
1471 cur_name = installed_packages{idx(i)}.name; |
|
1472 cur_version = installed_packages{idx(i)}.version; |
|
1473 cur_dir = installed_packages{idx(i)}.dir; |
6616
|
1474 if (installed_packages{idx(i)}.loaded) |
|
1475 cur_loaded = "*"; |
|
1476 else |
|
1477 cur_loaded = " "; |
|
1478 endif |
|
1479 printf (format, cur_name, cur_loaded, cur_version, cur_dir); |
6496
|
1480 endfor |
5801
|
1481 endfunction |
|
1482 |
6496
|
1483 function load_packages (files, handle_deps, local_list, global_list) |
|
1484 installed_packages = installed_packages (local_list, global_list); |
|
1485 num_packages = length (installed_packages); |
|
1486 |
|
1487 ## Read package names and installdirs into a more convenient format |
|
1488 pnames = pdirs = cell (1, num_packages); |
|
1489 for i = 1:num_packages |
|
1490 pnames{i} = installed_packages{i}.name; |
|
1491 pdirs{i} = installed_packages{i}.dir; |
|
1492 pdeps{i} = installed_packages{i}.depends; |
|
1493 endfor |
|
1494 |
|
1495 ## load all |
|
1496 if (length (files) == 1 && strcmp (files{1}, "all")) |
|
1497 dirs = pdirs; |
|
1498 ## load auto |
|
1499 elseif (length (files) == 1 && strcmp (files{1}, "auto")) |
|
1500 dirs = {}; |
|
1501 for i = 1:length (installed_packages) |
|
1502 if (exist (fullfile (pdirs{i}, "packinfo", ".autoload"), "file")) |
|
1503 dirs{end+1} = pdirs{i}; |
|
1504 endif |
6037
|
1505 endfor |
6496
|
1506 ## load package_name1 ... |
|
1507 else |
|
1508 dirs = {}; |
|
1509 for i = 1:length (files) |
|
1510 idx = strcmp (pnames, files{i}); |
|
1511 if (! any (idx)) |
|
1512 error ("package %s is not installed", files{i}); |
|
1513 endif |
|
1514 dirs{end+1} = pdirs{idx}; |
|
1515 if (handle_deps) |
|
1516 pdep = pdeps{idx}; |
|
1517 for j = 1:length (pdep) |
|
1518 depname = pdep{j}.package; |
|
1519 if (strcmp (depname, "octave")) |
|
1520 continue; |
|
1521 endif |
|
1522 idx = strcmp (pnames, depname); |
|
1523 if (! any (idx)) |
|
1524 error ("package %s could not be loaded since it depends on %s", |
|
1525 files{i}, depname); |
|
1526 endif |
|
1527 dirs{end+1} = pdirs{idx}; |
|
1528 endfor |
|
1529 endif |
|
1530 endfor |
|
1531 dirs = unique(dirs); |
|
1532 endif |
6037
|
1533 |
6614
|
1534 ## Check for architecture dependent directories |
|
1535 arch = getarch(); |
|
1536 archdirs = {}; |
|
1537 for i = 1:length (dirs) |
|
1538 tmpdir = fullfile (dirs{i}, arch); |
|
1539 if (exist (tmpdir, "dir")) |
|
1540 archdirs{end + 1} = dirs{i}; |
|
1541 archdirs{end + 1} = tmpdir; |
|
1542 endif |
|
1543 endfor |
|
1544 if (length (archdirs) > 0) |
|
1545 dirs = archdirs; |
|
1546 endif |
|
1547 |
6496
|
1548 ## Load the packages |
|
1549 if (length (dirs) > 0) |
|
1550 addpath (dirs{:}); |
|
1551 endif |
5971
|
1552 |
6496
|
1553 ## Add local binaries, if any, to the EXEC_PATH |
|
1554 for i = 1:length (dirs) |
|
1555 if (exist (fullfile (dirs{i}, "bin"), "dir")) |
|
1556 EXEC_PATH (strcat (fullfile(dirs{i}, "bin"), ":", EXEC_PATH ())); |
|
1557 endif |
|
1558 endfor |
5801
|
1559 endfunction |
5928
|
1560 |
6496
|
1561 function unload_packages (files, handle_deps, local_list, global_list) |
|
1562 installed_packages = installed_packages (local_list, global_list); |
|
1563 num_packages = length (installed_packages); |
|
1564 |
|
1565 ## Read package names and installdirs into a more convenient format |
|
1566 pnames = pdirs = cell (1, num_packages); |
|
1567 for i = 1:num_packages |
|
1568 pnames{i} = installed_packages{i}.name; |
|
1569 pdirs{i} = installed_packages{i}.dir; |
|
1570 pdeps{i} = installed_packages{i}.depends; |
|
1571 endfor |
|
1572 |
|
1573 ## Get the current octave path |
|
1574 p = split_by (path(), pathsep ()); |
6203
|
1575 |
6496
|
1576 ## unload all |
|
1577 if (length (files) == 1 && strcmp (files{1}, "all")) |
|
1578 dirs = pdirs; |
|
1579 ## unload package_name1 ... |
|
1580 else |
|
1581 dirs = {}; |
|
1582 for i = 1:length (files) |
|
1583 idx = strcmp (pnames, files{i}); |
|
1584 if (! any (idx)) |
|
1585 error ("package %s is not installed", files{i}); |
|
1586 endif |
|
1587 dirs{end+1} = pdirs{idx}; |
|
1588 endfor |
|
1589 endif |
|
1590 |
6614
|
1591 ## Check for architecture dependent directories |
|
1592 arch = getarch(); |
|
1593 archdirs = {}; |
|
1594 for i = 1:length (dirs) |
|
1595 tmpdir = fullfile (dirs{i}, arch); |
|
1596 if (exist (tmpdir, "dir")) |
|
1597 archdirs{end + 1} = dirs{i}; |
|
1598 archdirs{end + 1} = tmpdir; |
|
1599 endif |
|
1600 endfor |
|
1601 if (length (archdirs) > 0) |
|
1602 dirs = archdirs; |
|
1603 endif |
|
1604 |
6496
|
1605 ## Unload the packages |
|
1606 for i = 1:length (dirs) |
|
1607 d = dirs{i}; |
|
1608 idx = strcmp (p, d); |
|
1609 if (any (idx)) |
|
1610 rmpath (d); |
|
1611 ## XXX: We should also check if we need to remove items from EXEC_PATH |
6203
|
1612 endif |
6496
|
1613 endfor |
6203
|
1614 endfunction |
|
1615 |
5928
|
1616 function [status_out, msg_out] = rm_rf (dir) |
|
1617 crr = confirm_recursive_rmdir (); |
|
1618 unwind_protect |
|
1619 confirm_recursive_rmdir (false); |
|
1620 [status, msg] = rmdir (dir, "s"); |
|
1621 unwind_protect_cleanup |
|
1622 confirm_recursive_rmdir (crr); |
|
1623 end_unwind_protect |
|
1624 if (nargout > 0) |
|
1625 status_out = status; |
|
1626 endif |
|
1627 if (nargout > 1) |
|
1628 msg_out = msg; |
|
1629 endif |
|
1630 endfunction |
5971
|
1631 |
|
1632 function emp = dirempty (nm, ign) |
|
1633 if (nargin < 2) |
6496
|
1634 ign = {".", ".."}; |
5971
|
1635 else |
6496
|
1636 ign = [{".", ".."}, ign]; |
5971
|
1637 endif |
|
1638 l = dir (nm); |
6496
|
1639 for i = 1:length (l) |
5971
|
1640 found = false; |
6496
|
1641 for j = 1:length (ign) |
|
1642 if (strcmp (l(i).name, ign{j})) |
5971
|
1643 found = true; |
|
1644 break; |
|
1645 endif |
|
1646 endfor |
6496
|
1647 if (! found) |
5971
|
1648 emp = false; |
|
1649 return |
|
1650 endif |
|
1651 endfor |
|
1652 emp = true; |
|
1653 endfunction |
6614
|
1654 |
|
1655 function arch = getarch () |
|
1656 persistent _arch = [octave_config_info("canonical_host_type"), "-", ... |
|
1657 octave_config_info("api_version")]; |
|
1658 arch = _arch; |
|
1659 endfunction |
6645
|
1660 |
|
1661 function [status, output] = shell (cmd) |
|
1662 |
|
1663 persistent have_sh; |
|
1664 |
|
1665 cmd = strrep (cmd, "\\", "/"); |
|
1666 disp (cmd); |
|
1667 if (ispc () && ! isunix ()) |
|
1668 if (isempty(have_sh)) |
|
1669 if (system("sh.exe -c \"exit\"")) |
|
1670 have_sh = false; |
|
1671 else |
|
1672 have_sh = true; |
|
1673 endif |
|
1674 endif |
|
1675 if (have_sh) |
|
1676 [status, output] = system (["sh.exe -c \"", cmd, "\""]); |
|
1677 else |
|
1678 error ("Can not find the command shell") |
|
1679 endif |
|
1680 else |
|
1681 [status, output] = system (cmd); |
|
1682 endif |
|
1683 disp(output); |
|
1684 |
|
1685 endfunction |