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