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