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