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