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