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"); |
6925
|
194 archprefix = fullfile (OCTAVE_HOME (), "libexec", "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 ()); |
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) = []; |
|
1076 idx1 = cellfun ("isempty", regexp (filenames, '^.*\.mex')); |
|
1077 idx2 = cellfun ("isempty", regexp (filenames, '^.*\.oct')); |
|
1078 mex = filenames; |
|
1079 mex(idx1 != 0) = []; |
|
1080 oct = filenames; |
|
1081 oct(idx2 != 0) = []; |
6614
|
1082 archindependent = filenames; |
6655
|
1083 archindependent(idx1 == 0 | idx2 == 0) = []; |
6614
|
1084 archdependent = [oct, mex]; |
6496
|
1085 else |
|
1086 m = dir (fullfile (src, "*.m")); |
|
1087 oct = dir (fullfile (src, "*.oct")); |
|
1088 mex = dir (fullfile (src, "*.mex")); |
6614
|
1089 archdependent = ""; |
|
1090 archindependent = ""; |
6496
|
1091 filenames = ""; |
|
1092 if (length (m) > 0) |
|
1093 filenames = sprintf (fullfile (src, "%s "), m.name); |
6614
|
1094 archindependent = sprintf (fullfile (src, "%s "), m.name); |
6496
|
1095 endif |
|
1096 if (length (oct) > 0) |
6675
|
1097 filenames = strcat (filenames, " ", sprintf(fullfile(src, "%s "), ... |
|
1098 oct.name)); |
|
1099 archdependent = strcat (archdependent, " ", ... |
|
1100 sprintf(fullfile(src, "%s "), oct.name)); |
6496
|
1101 endif |
|
1102 if (length (mex) > 0) |
6675
|
1103 filenames = strcat (filenames, " ", sprintf(fullfile(src, "%s "), ... |
|
1104 mex.name)); |
|
1105 archdependent = strcat (archdependent, " ", ... |
|
1106 sprintf(fullfile(src, "%s "), mex.name)); |
6496
|
1107 endif |
6614
|
1108 filenames = split_by (filenames, " "); |
|
1109 archdependent = split_by (archdependent, " "); |
|
1110 archindependent = split_by (archindependent, " "); |
5971
|
1111 endif |
5801
|
1112 |
6655
|
1113 ## Copy the files |
6496
|
1114 if (! all (isspace (filenames))) |
6655
|
1115 if (! exist (instdir, "dir")) |
|
1116 mkdir (instdir); |
|
1117 endif |
6614
|
1118 if (! all (isspace (archindependent))) |
6634
|
1119 if (verbose) |
|
1120 printf ("copyfile"); |
|
1121 printf (" %s", archindependent{:}); |
|
1122 printf ("%s\n", instdir); |
|
1123 endif |
6614
|
1124 [status, output] = copyfile (archindependent, instdir); |
|
1125 if (status != 1) |
6496
|
1126 rm_rf (desc.dir); |
|
1127 error ("Couldn't copy files from 'src' to 'inst': %s", output); |
6614
|
1128 endif |
|
1129 endif |
|
1130 if (! all (isspace (archdependent))) |
6634
|
1131 if (verbose) |
|
1132 printf ("copyfile"); |
|
1133 printf (" %s", archdependent{:}); |
|
1134 printf (" %s\n", archdir); |
|
1135 endif |
6655
|
1136 if (! exist (archdir, "dir")) |
|
1137 mkdir (archdir); |
|
1138 endif |
6614
|
1139 [status, output] = copyfile (archdependent, archdir); |
|
1140 if (status != 1) |
|
1141 rm_rf (desc.dir); |
|
1142 error ("Couldn't copy files from 'src' to 'inst': %s", output); |
|
1143 endif |
|
1144 endif |
5801
|
1145 endif |
6496
|
1146 endif |
5801
|
1147 endfunction |
|
1148 |
5971
|
1149 function pkg = extract_pkg (nm, pat) |
5955
|
1150 fid = fopen (nm, "rt"); |
5971
|
1151 pkg = ""; |
5955
|
1152 if (fid >= 0) |
6496
|
1153 while (! feof (fid)) |
5955
|
1154 ln = fgetl (fid); |
|
1155 if (ln > 0) |
6496
|
1156 t = regexp (ln, pat, "tokens"); |
|
1157 if (! isempty (t)) |
|
1158 pkg = strcat (pkg, "\n", t{1}{1}); |
5955
|
1159 endif |
|
1160 endif |
|
1161 endwhile |
6496
|
1162 if (! isempty (pkg)) |
|
1163 pkg = strcat (pkg, "\n"); |
5955
|
1164 endif |
|
1165 fclose (fid); |
|
1166 endif |
|
1167 endfunction |
|
1168 |
6925
|
1169 function create_pkgadddel (desc, packdir, nm, global_install) |
6675
|
1170 instpkg = fullfile (desc.dir, nm); |
|
1171 instfid = fopen (instpkg, "wt"); |
|
1172 ## If it is exists, most of the PKG_* file should go into the |
|
1173 ## architecture dependent directory so that the autoload/mfilename |
|
1174 ## commands work as expected. The only part that doesn't is the |
|
1175 ## part in the main directory. |
6925
|
1176 archdir = fullfile (getarchprefix (desc), strcat (desc.name, "-", |
|
1177 desc.version), getarch ()); |
|
1178 if (exist (getarchdir (desc, global_install), "dir")) |
|
1179 archpkg = fullfile (getarchdir (desc, global_install), nm); |
|
1180 archfid = fopen (archpkg, "at"); |
6675
|
1181 else |
|
1182 archpkg = instpkg; |
|
1183 archfid = instfid; |
|
1184 endif |
6634
|
1185 |
6675
|
1186 if (archfid >= 0 && instfid >= 0) |
5971
|
1187 ## Search all dot-m files for PKG commands |
6233
|
1188 lst = dir (fullfile(packdir, "inst", "*.m")); |
6496
|
1189 for i = 1:length (lst) |
6233
|
1190 nam = fullfile(packdir, "inst", lst(i).name); |
6675
|
1191 fwrite (instfid, extract_pkg (nam, ['^[#%][#%]* *' nm ': *(.*)$'])); |
5955
|
1192 endfor |
|
1193 |
5971
|
1194 ## Search all C++ source files for PKG commands |
6233
|
1195 lst = dir (fullfile(packdir, "src", "*.cc")); |
6496
|
1196 for i = 1:length (lst) |
6233
|
1197 nam = fullfile(packdir, "src", lst(i).name); |
6675
|
1198 fwrite (archfid, extract_pkg (nam, ['^//* *' nm ': *(.*)$'])); |
|
1199 fwrite (archfid, extract_pkg (nam, ['^/\** *' nm ': *(.*) *\*/$'])); |
5955
|
1200 endfor |
|
1201 |
5971
|
1202 ## Add developer included PKG commands |
6496
|
1203 packdirnm = fullfile (packdir, nm); |
|
1204 if (exist (packdirnm, "file")) |
6675
|
1205 fid = fopen (packdirnm, "rt"); |
|
1206 if (fid >= 0) |
|
1207 while (! feof (fid)) |
|
1208 ln = fgets (fid); |
5955
|
1209 if (ln > 0) |
6675
|
1210 fwrite (archfid, ln); |
5955
|
1211 endif |
|
1212 endwhile |
6675
|
1213 fclose (fid); |
5955
|
1214 endif |
|
1215 endif |
5971
|
1216 |
6675
|
1217 ## If the files is empty remove it |
|
1218 fclose (instfid); |
|
1219 t = dir (instpkg); |
5971
|
1220 if (t.bytes <= 0) |
6675
|
1221 unlink (instpkg); |
|
1222 endif |
|
1223 |
|
1224 if (instfid != archfid) |
|
1225 fclose (archfid); |
|
1226 t = dir (archpkg); |
|
1227 if (t.bytes <= 0) |
|
1228 unlink (archpkg); |
|
1229 endif |
5971
|
1230 endif |
5955
|
1231 endif |
|
1232 endfunction |
|
1233 |
6925
|
1234 function copy_files (desc, packdir, global_install) |
6496
|
1235 ## Create the installation directory |
|
1236 if (! exist (desc.dir, "dir")) |
|
1237 [status, output] = mkdir (desc.dir); |
|
1238 if (status != 1) |
|
1239 error ("couldn't create installation directory %s : %s", |
|
1240 desc.dir, output); |
6020
|
1241 endif |
6496
|
1242 endif |
6020
|
1243 |
6925
|
1244 octfiledir = getarchdir (desc); |
|
1245 |
6496
|
1246 ## Copy the files from "inst" to installdir |
|
1247 instdir = fullfile (packdir, "inst"); |
|
1248 if (! dirempty (instdir)) |
|
1249 [status, output] = copyfile (fullfile (instdir, "*"), desc.dir); |
|
1250 if (status != 1) |
|
1251 rm_rf (desc.dir); |
|
1252 error ("couldn't copy files to the installation directory"); |
5801
|
1253 endif |
6925
|
1254 if (exist (fullfile (desc.dir, getarch ()), "dir") && |
|
1255 ! strcmp (fullfile (desc.dir, getarch ()), octfiledir)) |
|
1256 if (! exist (octfiledir, "dir")) |
|
1257 ## Can be required to create upto three levels of dirs |
|
1258 octm1 = fileparts (octfiledir); |
|
1259 if (! exist (octm1, "dir")) |
|
1260 octm2 = fileparts (octm1); |
|
1261 if (! exist (octm2, "dir")) |
|
1262 octm3 = fileparts (octm2); |
|
1263 if (! exist (octm3, "dir")) |
|
1264 [status, output] = mkdir (octm3); |
|
1265 if (status != 1) |
|
1266 rm_rf (desc.dir); |
|
1267 error ("couldn't create installation directory %s : %s", |
|
1268 octm3, output); |
|
1269 endif |
|
1270 endif |
|
1271 [status, output] = mkdir (octm2); |
|
1272 if (status != 1) |
|
1273 rm_rf (desc.dir); |
|
1274 error ("couldn't create installation directory %s : %s", |
|
1275 octm2, output); |
|
1276 endif |
|
1277 endif |
|
1278 [status, output] = mkdir (octm1); |
|
1279 if (status != 1) |
|
1280 rm_rf (desc.dir); |
|
1281 error ("couldn't create installation directory %s : %s", |
|
1282 octm1, output); |
|
1283 endif |
|
1284 endif |
|
1285 [status, output] = mkdir (octfiledir); |
|
1286 if (status != 1) |
|
1287 rm_rf (desc.dir); |
|
1288 error ("couldn't create installation directory %s : %s", |
|
1289 octfiledir, output); |
|
1290 endif |
|
1291 endif |
|
1292 [status, output] = movefile (fullfile (desc.dir, getarch (), "*"), |
|
1293 octfiledir); |
|
1294 rm_rf (fullfile (desc.dir, getarch ())); |
|
1295 |
|
1296 if (status != 1) |
|
1297 rm_rf (desc.dir); |
|
1298 rm_rf (octfiledir); |
|
1299 error ("couldn't copy files to the installation directory"); |
|
1300 endif |
|
1301 endif |
|
1302 |
6496
|
1303 endif |
5801
|
1304 |
6496
|
1305 ## Create the "packinfo" directory |
|
1306 packinfo = fullfile (desc.dir, "packinfo"); |
|
1307 [status, msg] = mkdir (packinfo); |
|
1308 if (status != 1) |
|
1309 rm_rf (desc.dir); |
6925
|
1310 rm_rf (octfiledir); |
6496
|
1311 error ("couldn't create packinfo directory: %s", msg); |
|
1312 endif |
5801
|
1313 |
6496
|
1314 ## Copy DESCRIPTION |
|
1315 [status, output] = copyfile (fullfile (packdir, "DESCRIPTION"), packinfo); |
|
1316 if (status != 1) |
6925
|
1317 rm_rf (desc.dir); |
|
1318 rm_rf (octfiledir); |
|
1319 error ("couldn't copy DESCRIPTION: %s", output); |
6496
|
1320 endif |
5801
|
1321 |
6496
|
1322 ## Copy COPYING |
|
1323 [status, output] = copyfile (fullfile (packdir, "COPYING"), packinfo); |
|
1324 if (status != 1) |
6925
|
1325 rm_rf (desc.dir); |
|
1326 rm_rf (octfiledir); |
|
1327 error ("couldn't copy COPYING: %s", output); |
6496
|
1328 endif |
5993
|
1329 |
6496
|
1330 ## If the file ChangeLog exists, copy it |
|
1331 fChangeLog = fullfile(packdir, "ChangeLog"); |
|
1332 if (exist (fChangeLog, "file")) |
|
1333 [status, output] = copyfile (fChangeLog, packinfo); |
|
1334 if (status != 1) |
|
1335 rm_rf (desc.dir); |
6925
|
1336 rm_rf (octfiledir); |
6496
|
1337 error ("couldn't copy ChangeLog file: %s", output); |
6254
|
1338 endif |
6496
|
1339 endif |
6254
|
1340 |
6496
|
1341 ## Is there an INDEX file to copy or should we generate one? |
|
1342 fINDEX = fullfile (packdir, "INDEX"); |
|
1343 if (exist(fINDEX, "file")) |
|
1344 [status, output] = copyfile (fINDEX, packinfo); |
|
1345 if (status != 1) |
|
1346 rm_rf (desc.dir); |
6925
|
1347 rm_rf (octfiledir); |
6496
|
1348 error ("couldn't copy INDEX file: %s", output); |
5801
|
1349 endif |
6496
|
1350 else |
|
1351 try |
|
1352 write_INDEX (desc, fullfile (packdir, "inst"), |
6925
|
1353 fullfile (packinfo, "INDEX"), global_install); |
6496
|
1354 catch |
|
1355 rm_rf (desc.dir); |
6925
|
1356 rm_rf (octfiledir); |
6695
|
1357 rethrow (lasterror ()); |
6496
|
1358 end_try_catch |
|
1359 endif |
5971
|
1360 |
6496
|
1361 ## Is there an 'on_uninstall.m' to install? |
|
1362 fon_uninstall = fullfile(packdir, "on_uninstall.m"); |
|
1363 if (exist (fon_uninstall, "file")) |
|
1364 [status, output] = copyfile (fon_uninstall, packinfo); |
|
1365 if (status != 1) |
|
1366 rm_rf (desc.dir); |
6925
|
1367 rm_rf (octfiledir); |
6496
|
1368 error ("couldn't copy on_uninstall.m: %s", output); |
5971
|
1369 endif |
6496
|
1370 endif |
5971
|
1371 |
6496
|
1372 ## Is there a doc/ directory that needs to be installed |
|
1373 docdir = fullfile (packdir, "doc"); |
|
1374 if (exist (docdir, "dir") && ! dirempty (docdir)) |
6925
|
1375 [status, output] = copyfile (docdir, desc.dir); |
6496
|
1376 endif |
|
1377 |
|
1378 ## Is there a bin/ directory that needs to be installed |
|
1379 bindir = fullfile (packdir, "bin"); |
|
1380 if (exist (bindir, "dir") && ! dirempty (bindir)) |
6925
|
1381 [status, output] = copyfile (bindir, desc.dir); |
6496
|
1382 endif |
5801
|
1383 endfunction |
|
1384 |
6925
|
1385 function finish_installation (desc, packdir, global_install) |
6496
|
1386 ## Is there a post-install to call? |
|
1387 if (exist (fullfile (packdir, "post_install.m"), "file")) |
|
1388 wd = pwd (); |
|
1389 try |
|
1390 cd (packdir); |
|
1391 post_install (desc); |
|
1392 cd (wd); |
|
1393 catch |
|
1394 cd (wd); |
|
1395 rm_rf (desc.dir); |
6925
|
1396 rm_rf (getarchdir (desc), global_install); |
6695
|
1397 rethrow (lasterror ()); |
6496
|
1398 end_try_catch |
|
1399 endif |
5801
|
1400 endfunction |
|
1401 |
|
1402 ## This function makes sure the package contains the |
|
1403 ## essential files. |
6496
|
1404 function verify_directory (dir) |
|
1405 needed_files = {"COPYING", "DESCRIPTION"}; |
|
1406 for f = needed_files |
|
1407 if (! exist (fullfile (dir, f{1}), "file")) |
|
1408 error ("package is missing file: %s", f{1}); |
|
1409 endif |
|
1410 endfor |
5801
|
1411 endfunction |
|
1412 |
|
1413 ## This function parses the DESCRIPTION file |
6496
|
1414 function desc = get_description (filename) |
|
1415 [fid, msg] = fopen (filename, "r"); |
|
1416 if (fid == -1) |
|
1417 error ("the DESCRIPTION file %s could not be read: %s", filename, msg); |
|
1418 endif |
|
1419 |
|
1420 desc = struct (); |
5801
|
1421 |
6496
|
1422 line = fgetl (fid); |
|
1423 while (line != -1) |
|
1424 if (line(1) == "#") |
|
1425 ## Comments, do nothing |
|
1426 elseif (isspace(line(1))) |
|
1427 ## Continuation lines |
|
1428 if (exist ("keyword", "var") && isfield (desc, keyword)) |
|
1429 desc.(keyword) = strcat (desc.(keyword), " ", rstrip(line)); |
|
1430 endif |
5801
|
1431 else |
6496
|
1432 ## Keyword/value pair |
|
1433 colon = find (line == ":"); |
|
1434 if (length (colon) == 0) |
|
1435 disp ("skipping line"); |
|
1436 else |
|
1437 colon = colon(1); |
|
1438 keyword = tolower (strip (line(1:colon-1))); |
|
1439 value = strip (line (colon+1:end)); |
|
1440 if (length (value) == 0) |
|
1441 fclose (fid); |
|
1442 error ("the keyword %s has an empty value", desc.keywords{end}); |
|
1443 endif |
|
1444 desc.(keyword) = value; |
|
1445 endif |
5801
|
1446 endif |
6496
|
1447 line = fgetl (fid); |
|
1448 endwhile |
|
1449 fclose (fid); |
|
1450 |
|
1451 ## Make sure all is okay |
|
1452 needed_fields = {"name", "version", "date", "title", ... |
|
1453 "author", "maintainer", "description"}; |
|
1454 for f = needed_fields |
|
1455 if (! isfield (desc, f{1})) |
|
1456 error ("description is missing needed field %s", f{1}); |
|
1457 endif |
|
1458 endfor |
|
1459 desc.version = fix_version (desc.version); |
|
1460 if (isfield (desc, "depends")) |
|
1461 desc.depends = fix_depends (desc.depends); |
|
1462 else |
|
1463 desc.depends = ""; |
|
1464 endif |
|
1465 desc.name = tolower (desc.name); |
5801
|
1466 endfunction |
|
1467 |
|
1468 ## Makes sure the version string v is a valid x.y.z version string |
|
1469 ## Examples: "0.1" => "0.1.0", "monkey" => error(...) |
6496
|
1470 function out = fix_version (v) |
|
1471 dots = find (v == "."); |
|
1472 if (length (dots) == 1) |
|
1473 major = str2num (v(1:dots-1)); |
|
1474 minor = str2num (v(dots+1:end)); |
|
1475 if (length (major) != 0 && length (minor) != 0) |
|
1476 out = sprintf ("%d.%d.0", major, minor); |
|
1477 return; |
5801
|
1478 endif |
6496
|
1479 elseif (length (dots) == 2) |
|
1480 major = str2num (v(1:dots(1)-1)); |
|
1481 minor = str2num (v(dots(1)+1:dots(2)-1)); |
|
1482 rev = str2num (v(dots(2)+1:end)); |
|
1483 if (length (major) != 0 && length (minor) != 0 && length (rev) != 0) |
|
1484 out = sprintf ("%d.%d.%d", major, minor, rev); |
|
1485 return; |
|
1486 endif |
|
1487 endif |
|
1488 error ("bad version string: %s", v); |
5801
|
1489 endfunction |
|
1490 |
|
1491 ## Makes sure the depends field is of the right format. |
|
1492 ## This function returns a cell of structures with the following fields: |
|
1493 ## package, version, operator |
6496
|
1494 function deps_cell = fix_depends (depends) |
|
1495 deps = split_by (tolower (depends), ","); |
|
1496 deps_cell = cell (1, length (deps)); |
|
1497 |
|
1498 ## For each dependency |
|
1499 for i = 1:length (deps) |
|
1500 dep = deps{i}; |
|
1501 lpar = find (dep == "("); |
|
1502 rpar = find (dep == ")"); |
|
1503 ## Does the dependency specify a version |
|
1504 ## Example: package(>= version) |
|
1505 if (length (lpar) == 1 && length (rpar) == 1) |
|
1506 package = tolower (strip (dep(1:lpar-1))); |
|
1507 sub = dep(lpar(1)+1:rpar(1)-1); |
|
1508 parts = split_by (sub, " "); |
|
1509 idx = []; |
|
1510 for r = 1:size (parts, 1) |
|
1511 if (length (parts{r}) > 0) |
|
1512 idx(end+1) = r; |
|
1513 endif |
|
1514 endfor |
|
1515 |
|
1516 if (length (idx) != 2) |
|
1517 error ("incorrect syntax for dependency `%s' in the DESCRIPTION file\n", |
|
1518 dep); |
|
1519 endif |
|
1520 operator = parts{idx(1)}; |
|
1521 if (! any (strcmp (operator, {">", ">=", "<=", "<", "=="}))) |
|
1522 error ("unsupported operator: %s", operator); |
|
1523 endif |
|
1524 version = fix_version (parts{idx(2)}); |
|
1525 |
|
1526 ## If no version is specified for the dependency |
|
1527 ## we say that the version should be greater than |
|
1528 ## or equal to 0.0.0 |
|
1529 else |
|
1530 package = tolower (strip (dep)); |
|
1531 operator = ">="; |
|
1532 version = "0.0.0"; |
|
1533 endif |
|
1534 deps_cell{i} = struct ("package", package, "operator", operator, |
|
1535 "version", version); |
|
1536 endfor |
5801
|
1537 endfunction |
|
1538 |
|
1539 ## Strips the text of spaces from the right |
|
1540 ## Example: " hello world " => " hello world" (XXX: is this the same as deblank?) |
6496
|
1541 function text = rstrip (text) |
|
1542 chars = find (! isspace (text)); |
|
1543 if (length (chars) > 0) |
|
1544 ## XXX: shouldn't it be text = text(1:chars(end)); |
|
1545 text = text (chars(1):end); |
|
1546 else |
|
1547 text = ""; |
|
1548 endif |
5801
|
1549 endfunction |
|
1550 |
|
1551 ## Strips the text of spaces from the left and the right |
|
1552 ## Example: " hello world " => "hello world" |
6496
|
1553 function text = strip (text) |
|
1554 chars = find (! isspace (text)); |
|
1555 if (length (chars) > 0) |
|
1556 text = text(chars(1):chars(end)); |
|
1557 else |
|
1558 text = ""; |
|
1559 endif |
5801
|
1560 endfunction |
|
1561 |
|
1562 ## Splits the text into a cell array of strings by sep |
|
1563 ## Example: "A, B" => {"A", "B"} (with sep = ",") |
6496
|
1564 function out = split_by (text, sep) |
|
1565 text_matrix = split (text, sep); |
|
1566 num_words = size (text_matrix, 1); |
|
1567 out = cell (num_words, 1); |
|
1568 for i = 1:num_words |
|
1569 out{i} = strip (text_matrix(i, :)); |
|
1570 endfor |
5801
|
1571 endfunction |
|
1572 |
|
1573 ## Creates an INDEX file for a package that doesn't provide one. |
|
1574 ## 'desc' describes the package. |
|
1575 ## 'dir' is the 'inst' direcotyr in temporary directory. |
|
1576 ## 'INDEX' is the name (including path) of resulting INDEX file. |
6925
|
1577 function write_INDEX (desc, dir, INDEX, global_install) |
6496
|
1578 ## Get names of functions in dir |
|
1579 [files, err, msg] = readdir (dir); |
|
1580 if (err) |
|
1581 error ("couldn't read directory %s: %s", dir, msg); |
|
1582 endif |
|
1583 |
6634
|
1584 ## Check for architecture dependent files |
6925
|
1585 tmpdir = getarchdir (desc); |
6634
|
1586 if (exist (tmpdir, "dir")) |
|
1587 [files2, err, msg] = readdir (tmpdir); |
|
1588 if (err) |
|
1589 error ("couldn't read directory %s: %s", tmpdir, msg); |
|
1590 endif |
|
1591 files = [files; files2]; |
|
1592 endif |
|
1593 |
6496
|
1594 functions = {}; |
|
1595 for i = 1:length (files) |
|
1596 file = files{i}; |
|
1597 lf = length (file); |
|
1598 if (lf > 2 && strcmp (file(end-1:end), ".m")) |
|
1599 functions{end+1} = file(1:end-2); |
|
1600 elseif (lf > 4 && strcmp (file(end-3:end), ".oct")) |
|
1601 functions{end+1} = file(1:end-4); |
5801
|
1602 endif |
6496
|
1603 endfor |
|
1604 |
|
1605 ## Does desc have a categories field? |
|
1606 if (! isfield (desc, "categories")) |
|
1607 error ("the DESCRIPTION file must have a Categories field, when no INDEX file is given"); |
|
1608 endif |
|
1609 categories = split_by (desc.categories, ","); |
|
1610 if (length (categories) < 1) |
|
1611 error ("the Category field is empty"); |
|
1612 endif |
|
1613 |
|
1614 ## Write INDEX |
|
1615 fid = fopen (INDEX, "w"); |
|
1616 if (fid == -1) |
|
1617 error ("couldn't open %s for writing.", INDEX); |
|
1618 endif |
|
1619 fprintf (fid, "%s >> %s\n", desc.name, desc.title); |
|
1620 fprintf (fid, "%s\n", categories{1}); |
|
1621 fprintf (fid, " %s\n", functions{:}); |
|
1622 fclose (fid); |
5801
|
1623 endfunction |
|
1624 |
6820
|
1625 function bad_deps = get_unsatisfied_deps (desc, installed_pkgs_lst) |
6496
|
1626 bad_deps = {}; |
5801
|
1627 |
6496
|
1628 ## For each dependency |
|
1629 for i = 1:length (desc.depends) |
|
1630 dep = desc.depends{i}; |
5801
|
1631 |
6496
|
1632 ## Is the current dependency Octave? |
|
1633 if (strcmp (dep.package, "octave")) |
|
1634 if (! compare_versions (OCTAVE_VERSION, dep.version, dep.operator)) |
|
1635 bad_deps{end+1} = dep; |
6258
|
1636 endif |
6496
|
1637 ## Is the current dependency not Octave? |
|
1638 else |
|
1639 ok = false; |
6820
|
1640 for i = 1:length (installed_pkgs_lst) |
|
1641 cur_name = installed_pkgs_lst{i}.name; |
|
1642 cur_version = installed_pkgs_lst{i}.version; |
6496
|
1643 if (strcmp (dep.package, cur_name) |
|
1644 && compare_versions (cur_version, dep.version, dep.operator)) |
|
1645 ok = true; |
|
1646 break; |
6258
|
1647 endif |
|
1648 endfor |
6496
|
1649 if (! ok) |
|
1650 bad_deps{end+1} = dep; |
|
1651 endif |
5801
|
1652 endif |
6496
|
1653 endfor |
|
1654 endfunction |
|
1655 |
|
1656 function [out1, out2] = installed_packages (local_list, global_list) |
|
1657 ## Get the list of installed packages |
|
1658 try |
|
1659 local_packages = load (local_list).local_packages; |
|
1660 catch |
|
1661 local_packages = {}; |
|
1662 end_try_catch |
|
1663 try |
6675
|
1664 global_packages = load (global_list).global_packages; |
6496
|
1665 catch |
|
1666 global_packages = {}; |
|
1667 end_try_catch |
6820
|
1668 installed_pkgs_lst = {local_packages{:}, global_packages{:}}; |
6496
|
1669 |
|
1670 ## Eliminate duplicates in the installed package list. |
|
1671 ## Locally installed packages take precedence |
|
1672 dup = []; |
6820
|
1673 for i = 1:length (installed_pkgs_lst) |
6496
|
1674 if (find (dup, i)) |
|
1675 continue; |
5801
|
1676 endif |
6820
|
1677 for j = (i+1):length (installed_pkgs_lst) |
6496
|
1678 if (find (dup, j)) |
|
1679 continue; |
|
1680 endif |
6820
|
1681 if (strcmp (installed_pkgs_lst{i}.name, installed_pkgs_lst{j}.name)) |
6496
|
1682 dup = [dup, j]; |
|
1683 endif |
5987
|
1684 endfor |
6496
|
1685 endfor |
|
1686 if (! isempty(dup)) |
6820
|
1687 installed_pkgs_lst(dup) = []; |
6496
|
1688 endif |
|
1689 |
6616
|
1690 ## Now check if the package is loaded |
6645
|
1691 tmppath = strrep (path(), "\\", "/"); |
6820
|
1692 for i = 1:length (installed_pkgs_lst) |
|
1693 if (findstr (tmppath, strrep (installed_pkgs_lst{i}.dir, "\\", "/"))) |
|
1694 installed_pkgs_lst{i}.loaded = true; |
6616
|
1695 else |
6820
|
1696 installed_pkgs_lst{i}.loaded = false; |
6616
|
1697 endif |
|
1698 endfor |
6675
|
1699 for i = 1:length (local_packages) |
6776
|
1700 if (findstr (tmppath, strrep (local_packages{i}.dir, "\\", "/"))) |
6675
|
1701 local_packages{i}.loaded = true; |
|
1702 else |
|
1703 local_packages{i}.loaded = false; |
|
1704 endif |
|
1705 endfor |
|
1706 for i = 1:length (global_packages) |
6776
|
1707 if (findstr (tmppath, strrep (global_packages{i}.dir, "\\", "/"))) |
6675
|
1708 global_packages{i}.loaded = true; |
|
1709 else |
|
1710 global_packages{i}.loaded = false; |
|
1711 endif |
|
1712 endfor |
6616
|
1713 |
6496
|
1714 ## Should we return something? |
|
1715 if (nargout == 2) |
|
1716 out1 = local_packages; |
|
1717 out2 = global_packages; |
|
1718 return; |
|
1719 elseif (nargout == 1) |
6820
|
1720 out1 = installed_pkgs_lst; |
6496
|
1721 return; |
|
1722 endif |
|
1723 |
|
1724 ## We shouldn't return something, so we'll print something |
6820
|
1725 num_packages = length (installed_pkgs_lst); |
6496
|
1726 if (num_packages == 0) |
|
1727 printf ("no packages installed.\n"); |
|
1728 return; |
|
1729 endif |
|
1730 |
|
1731 ## Compute the maximal lengths of name, version, and dir |
|
1732 h1 = "Package Name"; |
|
1733 h2 = "Version"; |
|
1734 h3 = "Installation directory"; |
|
1735 max_name_length = length (h1); |
|
1736 max_version_length = length (h2); |
|
1737 names = cell (num_packages, 1); |
|
1738 for i = 1:num_packages |
|
1739 max_name_length = max (max_name_length, |
6820
|
1740 length (installed_pkgs_lst{i}.name)); |
6496
|
1741 max_version_length = max (max_version_length, |
6820
|
1742 length (installed_pkgs_lst{i}.version)); |
|
1743 names{i} = installed_pkgs_lst{i}.name; |
6496
|
1744 endfor |
6698
|
1745 max_dir_length = terminal_size()(2) - max_name_length - ... |
|
1746 max_version_length - 7; |
|
1747 if (max_dir_length < 20) |
|
1748 max_dir_length = Inf; |
|
1749 endif |
|
1750 |
6616
|
1751 h1 = postpad (h1, max_name_length + 1, " "); |
6496
|
1752 h2 = postpad (h2, max_version_length, " ");; |
|
1753 |
|
1754 ## Print a header |
|
1755 header = sprintf("%s | %s | %s\n", h1, h2, h3); |
|
1756 printf (header); |
|
1757 tmp = sprintf (repmat ("-", 1, length(header)-1)); |
|
1758 tmp(length(h1)+2) = "+"; |
|
1759 tmp(length(h1)+length(h2)+5) = "+"; |
|
1760 printf ("%s\n", tmp); |
|
1761 |
|
1762 ## Print the packages |
6616
|
1763 format = sprintf ("%%%ds %%1s| %%%ds | %%s\n", max_name_length, |
6496
|
1764 max_version_length); |
|
1765 [dummy, idx] = sort (names); |
|
1766 for i = 1:num_packages |
6820
|
1767 cur_name = installed_pkgs_lst{idx(i)}.name; |
|
1768 cur_version = installed_pkgs_lst{idx(i)}.version; |
|
1769 cur_dir = installed_pkgs_lst{idx(i)}.dir; |
6698
|
1770 if (length (cur_dir) > max_dir_length) |
|
1771 first_char = length (cur_dir) - max_dir_length + 4; |
|
1772 first_filesep = strfind (cur_dir(first_char:end), filesep()); |
|
1773 if (! isempty (first_filesep)) |
|
1774 cur_dir = strcat ("...", |
|
1775 cur_dir((first_char + first_filesep(1) - 1):end)); |
|
1776 else |
|
1777 cur_dir = strcat ("...", cur_dir(first_char:end)); |
|
1778 endif |
|
1779 endif |
6820
|
1780 if (installed_pkgs_lst{idx(i)}.loaded) |
6616
|
1781 cur_loaded = "*"; |
|
1782 else |
|
1783 cur_loaded = " "; |
|
1784 endif |
|
1785 printf (format, cur_name, cur_loaded, cur_version, cur_dir); |
6496
|
1786 endfor |
5801
|
1787 endfunction |
|
1788 |
6496
|
1789 function load_packages (files, handle_deps, local_list, global_list) |
6820
|
1790 installed_pkgs_lst = installed_packages (local_list, global_list); |
|
1791 num_packages = length (installed_pkgs_lst); |
6496
|
1792 |
|
1793 ## Read package names and installdirs into a more convenient format |
|
1794 pnames = pdirs = cell (1, num_packages); |
|
1795 for i = 1:num_packages |
6820
|
1796 pnames{i} = installed_pkgs_lst{i}.name; |
|
1797 pdirs{i} = installed_pkgs_lst{i}.dir; |
6496
|
1798 endfor |
|
1799 |
|
1800 ## load all |
|
1801 if (length (files) == 1 && strcmp (files{1}, "all")) |
6820
|
1802 idx = [1:length(installed_pkgs_lst)]; |
6496
|
1803 ## load auto |
6695
|
1804 elseif (length (files) == 1 && strcmp (files{1}, "auto")) |
|
1805 idx = []; |
6820
|
1806 for i = 1:length (installed_pkgs_lst) |
6496
|
1807 if (exist (fullfile (pdirs{i}, "packinfo", ".autoload"), "file")) |
6695
|
1808 idx (end + 1) = i; |
6496
|
1809 endif |
6037
|
1810 endfor |
6496
|
1811 ## load package_name1 ... |
|
1812 else |
6695
|
1813 idx = []; |
6496
|
1814 for i = 1:length (files) |
6695
|
1815 idx2 = find (strcmp (pnames, files{i})); |
|
1816 if (! any (idx2)) |
6496
|
1817 error ("package %s is not installed", files{i}); |
|
1818 endif |
6695
|
1819 idx (end + 1) = idx2; |
6496
|
1820 endfor |
|
1821 endif |
6037
|
1822 |
6695
|
1823 ## Load the packages, but take care of the ordering of dependencies |
6925
|
1824 load_packages_and_dependencies (idx, handle_deps, installed_pkgs_lst, true); |
5801
|
1825 endfunction |
5928
|
1826 |
6496
|
1827 function unload_packages (files, handle_deps, local_list, global_list) |
6820
|
1828 installed_pkgs_lst = installed_packages (local_list, global_list); |
|
1829 num_packages = length (installed_pkgs_lst); |
6496
|
1830 |
|
1831 ## Read package names and installdirs into a more convenient format |
|
1832 pnames = pdirs = cell (1, num_packages); |
|
1833 for i = 1:num_packages |
6820
|
1834 pnames{i} = installed_pkgs_lst{i}.name; |
|
1835 pdirs{i} = installed_pkgs_lst{i}.dir; |
|
1836 pdeps{i} = installed_pkgs_lst{i}.depends; |
6496
|
1837 endfor |
|
1838 |
|
1839 ## Get the current octave path |
|
1840 p = split_by (path(), pathsep ()); |
6203
|
1841 |
6496
|
1842 ## unload all |
|
1843 if (length (files) == 1 && strcmp (files{1}, "all")) |
|
1844 dirs = pdirs; |
6925
|
1845 desc = installed_pkgs_lst; |
6496
|
1846 ## unload package_name1 ... |
|
1847 else |
|
1848 dirs = {}; |
6925
|
1849 desc = {}; |
6496
|
1850 for i = 1:length (files) |
6925
|
1851 idx = strcmp (pnames, files {i}); |
6496
|
1852 if (! any (idx)) |
|
1853 error ("package %s is not installed", files{i}); |
|
1854 endif |
6925
|
1855 dirs {end + 1} = pdirs {idx}; |
|
1856 desc {end + 1} = installed_pkgs_lst {idx}; |
6496
|
1857 endfor |
|
1858 endif |
|
1859 |
6614
|
1860 ## Check for architecture dependent directories |
|
1861 archdirs = {}; |
|
1862 for i = 1:length (dirs) |
6925
|
1863 tmpdir = getarchdir (desc {i}); |
6614
|
1864 if (exist (tmpdir, "dir")) |
|
1865 archdirs{end + 1} = dirs{i}; |
|
1866 archdirs{end + 1} = tmpdir; |
6925
|
1867 else |
|
1868 archdirs{end+ 1} = dirs {i}; |
6614
|
1869 endif |
|
1870 endfor |
|
1871 |
6496
|
1872 ## Unload the packages |
6925
|
1873 for i = 1:length (archdirs) |
|
1874 d = archdirs{i}; |
6496
|
1875 idx = strcmp (p, d); |
|
1876 if (any (idx)) |
|
1877 rmpath (d); |
|
1878 ## XXX: We should also check if we need to remove items from EXEC_PATH |
6203
|
1879 endif |
6496
|
1880 endfor |
6203
|
1881 endfunction |
|
1882 |
5928
|
1883 function [status_out, msg_out] = rm_rf (dir) |
6925
|
1884 if (exist (dir)) |
|
1885 crr = confirm_recursive_rmdir (); |
|
1886 unwind_protect |
|
1887 confirm_recursive_rmdir (false); |
|
1888 [status, msg] = rmdir (dir, "s"); |
|
1889 unwind_protect_cleanup |
|
1890 confirm_recursive_rmdir (crr); |
|
1891 end_unwind_protect |
|
1892 else |
|
1893 status = 1; |
|
1894 msg = ""; |
|
1895 endif |
5928
|
1896 if (nargout > 0) |
|
1897 status_out = status; |
|
1898 endif |
|
1899 if (nargout > 1) |
|
1900 msg_out = msg; |
|
1901 endif |
|
1902 endfunction |
5971
|
1903 |
|
1904 function emp = dirempty (nm, ign) |
6925
|
1905 if (exist (nm, "dir")) |
|
1906 if (nargin < 2) |
|
1907 ign = {".", ".."}; |
|
1908 else |
|
1909 ign = [{".", ".."}, ign]; |
|
1910 endif |
|
1911 l = dir (nm); |
|
1912 for i = 1:length (l) |
|
1913 found = false; |
|
1914 for j = 1:length (ign) |
|
1915 if (strcmp (l(i).name, ign{j})) |
|
1916 found = true; |
|
1917 break; |
|
1918 endif |
|
1919 endfor |
|
1920 if (! found) |
|
1921 emp = false; |
|
1922 return |
5971
|
1923 endif |
|
1924 endfor |
6925
|
1925 emp = true; |
|
1926 else |
|
1927 emp = true; |
|
1928 endif |
5971
|
1929 endfunction |
6614
|
1930 |
|
1931 function arch = getarch () |
6675
|
1932 persistent _arch = strcat (octave_config_info("canonical_host_type"), ... |
|
1933 "-", octave_config_info("api_version")); |
6614
|
1934 arch = _arch; |
|
1935 endfunction |
6645
|
1936 |
6925
|
1937 function archprefix = getarchprefix (desc, global_install) |
|
1938 if ((nargin == 2 && global_install) || (nargin < 2 && issuperuser ())) |
|
1939 archprefix = fullfile (octave_config_info ("libexecdir"), "octave", |
|
1940 "packages", strcat(desc.name, "-", desc.version)); |
|
1941 else |
|
1942 archprefix = desc.dir; |
|
1943 endif |
|
1944 endfunction |
|
1945 |
|
1946 function archdir = getarchdir (desc) |
|
1947 archdir = fullfile (desc.archprefix, getarch()); |
|
1948 endfunction |
|
1949 |
|
1950 function s = issuperuser () |
|
1951 if ((ispc () && ! isunix ()) || (geteuid() == 0)) |
|
1952 s = true; |
|
1953 else |
|
1954 s = false; |
|
1955 endif |
|
1956 endfunction |
|
1957 |
6645
|
1958 function [status, output] = shell (cmd) |
|
1959 persistent have_sh; |
|
1960 |
|
1961 cmd = strrep (cmd, "\\", "/"); |
|
1962 if (ispc () && ! isunix ()) |
|
1963 if (isempty(have_sh)) |
|
1964 if (system("sh.exe -c \"exit\"")) |
|
1965 have_sh = false; |
|
1966 else |
|
1967 have_sh = true; |
|
1968 endif |
|
1969 endif |
|
1970 if (have_sh) |
6675
|
1971 [status, output] = system (strcat ("sh.exe -c \"", cmd, "\"")); |
6645
|
1972 else |
|
1973 error ("Can not find the command shell") |
|
1974 endif |
|
1975 else |
|
1976 [status, output] = system (cmd); |
|
1977 endif |
|
1978 endfunction |
6695
|
1979 |
|
1980 function newdesc = save_order (desc) |
|
1981 newdesc = {}; |
|
1982 for i = 1 : length(desc) |
|
1983 deps = desc{i}.depends; |
|
1984 if (isempty (deps) || (length (deps) == 1 && |
|
1985 strcmp(deps{1}.package, "octave"))) |
|
1986 newdesc {end + 1} = desc{i}; |
|
1987 else |
|
1988 tmpdesc = {}; |
|
1989 for k = 1 : length (deps) |
|
1990 for j = 1 : length (desc) |
|
1991 if (strcmp (desc{j}.name, deps{k}.package)) |
|
1992 tmpdesc {end + 1} = desc{j}; |
|
1993 break; |
|
1994 endif |
|
1995 endfor |
|
1996 endfor |
|
1997 if (! isempty (tmpdesc)) |
|
1998 newdesc = {newdesc{:}, save_order(tmpdesc){:}, desc{i}}; |
|
1999 else |
|
2000 newdesc {end + 1} = desc{i}; |
|
2001 endif |
|
2002 endif |
|
2003 endfor |
|
2004 ## Eliminate the duplicates |
|
2005 idx = []; |
|
2006 for i = 1 : length (newdesc) |
|
2007 for j = (i + 1) : length (newdesc) |
|
2008 if (strcmp (newdesc{i}.name, newdesc{j}.name)) |
|
2009 idx (end + 1) = j; |
|
2010 endif |
|
2011 endfor |
|
2012 endfor |
|
2013 newdesc(idx) = []; |
|
2014 endfunction |
|
2015 |
6925
|
2016 function load_packages_and_dependencies (idx, handle_deps, installed_pkgs_lst, |
|
2017 global_install) |
6820
|
2018 idx = load_package_dirs (idx, [], handle_deps, installed_pkgs_lst); |
6695
|
2019 dirs = {}; |
|
2020 execpath = EXEC_PATH (); |
|
2021 for i = idx; |
6820
|
2022 ndir = installed_pkgs_lst{i}.dir; |
6695
|
2023 dirs {end + 1} = ndir; |
|
2024 if (exist (fullfile (dirs{end}, "bin"), "dir")) |
|
2025 execpath = strcat (fullfile(dirs{end}, "bin"), ":", execpath); |
|
2026 endif |
6925
|
2027 tmpdir = getarchdir (installed_pkgs_lst {i}); |
6695
|
2028 if (exist (tmpdir, "dir")) |
|
2029 dirs{end + 1} = tmpdir; |
|
2030 endif |
|
2031 endfor |
|
2032 |
|
2033 ## Load the packages |
|
2034 if (length (dirs) > 0) |
|
2035 addpath (dirs{:}); |
|
2036 endif |
|
2037 |
|
2038 ## Add the binaries to exec_path |
|
2039 if (! strcmp (EXEC_PATH, execpath)) |
|
2040 EXEC_PATH (execpath); |
|
2041 endif |
|
2042 endfunction |
|
2043 |
6820
|
2044 function idx = load_package_dirs (lidx, idx, handle_deps, installed_pkgs_lst) |
6695
|
2045 for i = lidx |
6820
|
2046 if (isfield (installed_pkgs_lst{i}, "loaded") && |
|
2047 installed_pkgs_lst{i}.loaded) |
6695
|
2048 continue; |
|
2049 else |
|
2050 if (handle_deps) |
6820
|
2051 deps = installed_pkgs_lst{i}.depends; |
6695
|
2052 if ((length (deps) > 1) || (length (deps) == 1 && |
|
2053 ! strcmp(deps{1}.package, "octave"))) |
|
2054 tmplidx = []; |
|
2055 for k = 1 : length (deps) |
6820
|
2056 for j = 1 : length (installed_pkgs_lst) |
|
2057 if (strcmp (installed_pkgs_lst{j}.name, deps{k}.package)) |
6695
|
2058 tmplidx (end + 1) = j; |
|
2059 break; |
|
2060 endif |
|
2061 endfor |
|
2062 endfor |
|
2063 idx = load_package_dirs (tmplidx, idx, handle_deps, |
6820
|
2064 installed_pkgs_lst); |
6695
|
2065 endif |
|
2066 endif |
|
2067 if (isempty (find(idx == i))) |
|
2068 idx (end + 1) = i; |
|
2069 endif |
|
2070 endif |
|
2071 endfor |
|
2072 endfunction |