5801
|
1 ## Copyright (C) 2005 S�ren Hauberg |
|
2 ## |
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2 of the License, or |
|
6 ## (at your option) any later version. |
|
7 ## |
|
8 ## This program is distributed in the hope that it will be useful, |
|
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 ## GNU General Public License for more details. |
|
12 ## |
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this program; if not, write to the Free Software |
|
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
16 |
|
17 ## -*- texinfo -*- |
6032
|
18 ## @deftypefn {Command} pkg @var{command} @var{pkg_name} |
|
19 ## @deftypefnx {Command} pkg @var{command} @var{option} @var{pkg_name} |
|
20 ## This command interacts with the package manager. Different actions will |
|
21 ## be taking depending on the value of @var{command}. |
|
22 ## |
|
23 ## @table @samp |
|
24 ## @item install |
6070
|
25 ## Install named packages. For example, |
|
26 ## @example |
|
27 ## pkg install image-1.0.0.tar.gz |
|
28 ## @end example |
|
29 ## @noindent |
|
30 ## installs the package found in the file @code{image-1.0.0.tar.gz}. |
|
31 ## |
|
32 ## If @var{option} is @code{-nodeps} the package manager will disable the |
|
33 ## dependency checking. That way it is possible to install a package even |
|
34 ## if it depends on another package that's not installed on the system. |
|
35 ## @strong{Use this option with care.} |
6032
|
36 ## @item uninstall |
6070
|
37 ## Uninstall named packages. For example, |
|
38 ## @example |
|
39 ## pkg uninstall image |
|
40 ## @end example |
|
41 ## @noindent |
|
42 ## removes the @code{image} package from the system. If another installed |
|
43 ## package depends on the @code{image} package an error will be issued. |
|
44 ## The package can be uninstalled anyway by using the @code{-nodeps} option. |
6032
|
45 ## @item load |
6070
|
46 ## Add named packages to the path. After loading a package it is |
|
47 ## possible to use the functions provided by the package. For example, |
|
48 ## @example |
|
49 ## pkg load image |
|
50 ## @end example |
|
51 ## @noindent |
|
52 ## adds the @code{image} package to the path. It is possible to load all |
|
53 ## installed packages at once with the command |
|
54 ## @example |
|
55 ## pkg load all |
|
56 ## @end example |
6032
|
57 ## @item list |
6070
|
58 ## Show a list of the currently installed packages. By requesting one or two |
|
59 ## output argument it is possible to get a list of the currently installed |
|
60 ## packages. For example, |
|
61 ## @example |
|
62 ## installed_packages = pkg list; |
|
63 ## @end example |
|
64 ## @noindent |
|
65 ## returns a cell array containing a structure for each installed package. |
|
66 ## The command |
|
67 ## @example |
|
68 ## [@var{user_packages}, @var{system_packages}] = pkg list |
|
69 ## @end example |
|
70 ## @noindent |
|
71 ## splits the list of installed packages into those who are installed by |
|
72 ## the current user, and those installed by the system administrator. |
6034
|
73 ## @item prefix |
6070
|
74 ## Set the installation prefix directory. For example, |
|
75 ## @example |
|
76 ## pkg prefix ~/my_octave_packages |
|
77 ## @end example |
|
78 ## @noindent |
|
79 ## sets the installation prefix to @code{~/my_octave_packages}. |
|
80 ## Packages will be installed in this directory. |
6034
|
81 ## |
6070
|
82 ## It is possible to get the current installation prefix by requesting an |
|
83 ## output argument. For example, |
|
84 ## @example |
|
85 ## p = pkg prefix |
|
86 ## @end example |
6032
|
87 ## @end table |
5801
|
88 ## @end deftypefn |
5947
|
89 |
|
90 ## PKG_ADD: mark_as_command pkg |
|
91 |
5801
|
92 function [local_packages, global_packages] = pkg(varargin) |
6034
|
93 ## Installation prefix |
|
94 persistent prefix = -1; |
|
95 if (prefix == -1) |
|
96 if (issuperuser()) |
6036
|
97 prefix = fullfile (OCTAVE_HOME (), "/share/octave/packages/"); |
6034
|
98 else |
|
99 prefix = "~/octave/"; |
|
100 endif |
|
101 endif |
|
102 prefix = tilde_expand(prefix); |
|
103 |
5801
|
104 ## Handle input |
|
105 if (length(varargin) == 0 || !iscellstr(varargin)) |
5928
|
106 print_usage(); |
5801
|
107 endif |
|
108 files = {}; |
|
109 deps = true; |
|
110 action = "none"; |
|
111 for i = 1:length(varargin) |
|
112 switch (varargin{i}) |
|
113 case "-nodeps" |
|
114 deps = false; |
6034
|
115 case {"list", "install", "uninstall", "load", "prefix"} |
5801
|
116 action = varargin{i}; |
|
117 otherwise |
|
118 files{end+1} = varargin{i}; |
|
119 endswitch |
|
120 endfor |
|
121 |
|
122 ## Take action |
|
123 switch (action) |
|
124 case "list" |
|
125 if (nargout == 0) |
|
126 installed_packages(); |
|
127 elseif (nargout == 1) |
|
128 local_packages = installed_packages(); |
|
129 elseif (nargout == 2) |
|
130 [local_packages, global_packages] = installed_packages(); |
|
131 else |
6042
|
132 error("Too many output arguments requested."); |
5801
|
133 endif |
|
134 case "install" |
|
135 if (length(files) == 0) |
6042
|
136 error("You must specify at least one filename when calling 'pkg install'"); |
5801
|
137 endif |
6034
|
138 install(files, deps, prefix); |
5801
|
139 case "uninstall" |
|
140 if (length(files) == 0) |
6042
|
141 error("You must specify at least one package when calling 'pkg uninstall'"); |
5801
|
142 endif |
|
143 uninstall(files, deps); |
|
144 case "load" |
|
145 if (length(files) == 0) |
6042
|
146 error("You must specify at least one package or 'all' when calling 'pkg load'"); |
5801
|
147 endif |
|
148 load_packages(files, deps); |
6034
|
149 case "prefix" |
|
150 if (length(files) == 0 && nargout == 0) |
6035
|
151 disp(prefix); |
6034
|
152 elseif (length(files) == 0 && nargout == 1) |
|
153 local_packages = prefix; |
|
154 elseif (length(files) == 1 && nargout == 0 && ischar(files{1})) |
|
155 prefix = files{1}; |
|
156 if (!strcmp(prefix(end), "/")) prefix(end+1) = "/"; endif |
|
157 else |
|
158 error("You must specify a prefix directory, or request an output arguement"); |
|
159 endif |
5801
|
160 otherwise |
6042
|
161 error("You must specify a valid action for 'pkg'. See 'help pkg' for details"); |
5801
|
162 endswitch |
|
163 endfunction |
|
164 |
6034
|
165 function install(files, handle_deps, prefix) |
5801
|
166 ## Set parameters depending on wether or not the installation |
|
167 ## is system-wide (global) or local. |
|
168 local_list = tilde_expand("~/.octave_packages"); |
6036
|
169 global_list = fullfile (OCTAVE_HOME (), "/share/octave/octave_packages"); |
6034
|
170 |
|
171 global_install = issuperuser(); |
5801
|
172 |
|
173 # Check that the directory in prefix exist. If it doesn't: create it! |
|
174 if (!exist(prefix, "dir")) |
|
175 warning("Creating installation directory %s", prefix); |
|
176 [status, msg] = mkdir(prefix); |
|
177 if (status != 1) |
6042
|
178 error("Could not create installation directory: %s", msg); |
5801
|
179 endif |
|
180 endif |
|
181 |
|
182 ## Get the list of installed packages |
|
183 [local_packages, global_packages] = installed_packages(); |
|
184 installed_packages = {local_packages{:} global_packages{:}}; |
|
185 |
|
186 if (global_install) |
|
187 packages = global_packages; |
|
188 else |
|
189 packages = local_packages; |
|
190 endif |
|
191 |
|
192 ## Uncompress the packages and read the DESCRIPTION files |
5947
|
193 tmpdirs = packdirs = descriptions = {}; |
5801
|
194 try |
|
195 ## Unpack the package files and read the DESCRIPTION files |
|
196 packages_to_uninstall = []; |
|
197 for i = 1:length(files) |
|
198 tgz = files{i}; |
|
199 |
|
200 ## Create a temporary directory |
|
201 tmpdir = tmpnam(); |
5947
|
202 tmpdirs{end+1} = tmpdir; |
5801
|
203 [status, msg] = mkdir(tmpdir); |
|
204 if (status != 1) |
6042
|
205 error("Couldn't create temporary directory: %s", msg); |
5801
|
206 endif |
|
207 |
|
208 ## Uncompress the package |
|
209 untar(tgz, tmpdir); |
5928
|
210 |
5947
|
211 ## Get the name of the directories produced by tar |
5801
|
212 [dirlist, err, msg] = readdir(tmpdir); |
|
213 if (err) |
6042
|
214 error("Couldn't read directory produced by tar: %s", msg); |
5801
|
215 endif |
5987
|
216 |
|
217 if (length(dirlist) > 3) |
|
218 error("Bundles of packages are not allowed") |
|
219 endif |
5947
|
220 |
|
221 for k = 3:length(dirlist) # the two first entries of dirlist are "." and ".." |
|
222 packdir = [tmpdir "/" dirlist{k} "/"]; |
|
223 packdirs{end+1} = packdir; |
5801
|
224 |
5947
|
225 ## Make sure the package contains necessary files |
|
226 verify_directory(packdir); |
5928
|
227 |
5947
|
228 ## Read the DESCRIPTION file |
|
229 filename = [packdir "DESCRIPTION"]; |
|
230 desc = get_description(filename); |
5928
|
231 |
5971
|
232 ## Verify that package name corresponds with filename |
|
233 [dummy, nm] = fileparts(tgz); |
|
234 if ((length(nm) >= length(desc.name)) && |
|
235 ! strcmp(desc.name,nm(1:length(desc.name)))) |
6042
|
236 error("Package name '%s' doesn't correspond to its filename '%s'", desc.name, nm); |
5971
|
237 endif |
|
238 |
5947
|
239 ## Set default installation directory |
|
240 desc.dir = [prefix "/" desc.name "-" desc.version]; |
5801
|
241 |
5947
|
242 ## Save desc |
|
243 descriptions{end+1} = desc; |
5801
|
244 |
5947
|
245 ## Are any of the new packages already installed? |
|
246 ## If so we'll remove the old version. |
|
247 for j = 1:length(packages) |
|
248 if (strcmp(packages{j}.name, desc.name)) |
|
249 packages_to_uninstall(end+1) = j; |
|
250 endif |
|
251 endfor |
|
252 endfor |
5801
|
253 endfor |
|
254 catch |
|
255 ## Something went wrong, delete tmpdirs |
|
256 for i = 1:length(tmpdirs) |
5928
|
257 rm_rf(tmpdirs{i}); |
5801
|
258 endfor |
|
259 error(lasterr()(8:end)); |
|
260 end_try_catch |
|
261 |
|
262 ## Check dependencies |
|
263 if (handle_deps) |
|
264 ok = true; |
|
265 error_text = ""; |
|
266 for i = 1:length(descriptions) |
|
267 desc = descriptions{i}; |
|
268 idx1 = complement(packages_to_uninstall, 1:length(installed_packages)); |
|
269 idx2 = complement(i, 1:length(descriptions)); |
|
270 pseudo_installed_packages = {installed_packages{idx1} descriptions{idx2}}; |
|
271 bad_deps = get_unsatisfied_deps(desc, pseudo_installed_packages); |
|
272 ## Are there any unsatisfied dependencies? |
|
273 if (!isempty(bad_deps)) |
|
274 ok = false; |
|
275 for i = 1:length(bad_deps) |
|
276 dep = bad_deps{i}; |
|
277 error_text = [error_text " " desc.name " needs " ... |
|
278 dep.package " " dep.operator " " ... |
|
279 dep.version "\n"]; |
|
280 endfor |
|
281 endif |
|
282 endfor |
|
283 |
|
284 ## Did we find any unsatisfied dependencies? |
|
285 if (!ok) |
|
286 error("The following dependencies where unsatisfied:\n %s", error_text); |
|
287 endif |
|
288 endif |
|
289 |
|
290 ## Prepare each package for installation |
|
291 try |
|
292 for i = 1:length(descriptions) |
|
293 desc = descriptions{i}; |
|
294 pdir = packdirs{i}; |
|
295 prepare_installation (desc, pdir); |
|
296 configure_make (desc, pdir); |
|
297 endfor |
|
298 catch |
|
299 ## Something went wrong, delete tmpdirs |
|
300 for i = 1:length(tmpdirs) |
5928
|
301 rm_rf(tmpdirs{i}); |
5801
|
302 endfor |
|
303 error(lasterr()(8:end)); |
|
304 end_try_catch |
|
305 |
|
306 ## Uninstall the packages that will be replaced |
|
307 try |
|
308 for i = packages_to_uninstall |
|
309 uninstall({installed_packages{i}.name}, false); |
|
310 endfor |
|
311 catch |
|
312 ## Something went wrong, delete tmpdirs |
|
313 for i = 1:length(tmpdirs) |
5928
|
314 rm_rf(tmpdirs{i}); |
5801
|
315 endfor |
|
316 error(lasterr()(8:end)); |
|
317 end_try_catch |
|
318 |
|
319 ## Install each package |
|
320 try |
|
321 for i = 1:length(descriptions) |
|
322 desc = descriptions{i}; |
|
323 pdir = packdirs{i}; |
5971
|
324 copy_files(desc, pdir); |
|
325 create_pkgadddel(desc, pdir, "PKG_ADD"); |
|
326 create_pkgadddel(desc, pdir, "PKG_DEL"); |
5801
|
327 finish_installation (desc, pdir) |
|
328 endfor |
|
329 catch |
|
330 ## Something went wrong, delete tmpdirs |
|
331 for i = 1:length(tmpdirs) |
5928
|
332 rm_rf(tmpdirs{i}); |
5801
|
333 endfor |
5971
|
334 for i = 1:length(descriptions) |
|
335 rm_rf(descriptions{i}.dir); |
|
336 endfor |
5801
|
337 error(lasterr()(8:end)); |
|
338 end_try_catch |
|
339 |
5971
|
340 ## Check if the installed directory is empty. If it is remove it |
|
341 ## from the list |
|
342 for i = length(descriptions):-1:1 |
|
343 if (dirempty(descriptions{i}.dir,{"packinfo","doc"})) |
|
344 rm_rf(descriptions{i}.dir); |
|
345 descriptions(i) = []; |
|
346 endif |
|
347 endfor |
|
348 |
|
349 ## Add the packages to the package list |
5801
|
350 try |
|
351 if (global_install) |
|
352 idx = complement(packages_to_uninstall, 1:length(global_packages)); |
|
353 global_packages = {global_packages{idx} descriptions{:}}; |
|
354 save(global_list, "global_packages"); |
|
355 else |
|
356 idx = complement(packages_to_uninstall, 1:length(local_packages)); |
|
357 local_packages = {local_packages{idx} descriptions{:}}; |
|
358 save(local_list, "local_packages"); |
|
359 endif |
|
360 catch |
|
361 ## Something went wrong, delete tmpdirs |
|
362 for i = 1:length(tmpdirs) |
5928
|
363 rm_rf(tmpdirs{i}); |
5801
|
364 endfor |
|
365 for i = 1:length(descriptions) |
5928
|
366 rm_rf(descriptions{i}.dir); |
5801
|
367 endfor |
|
368 if (global_install) |
6042
|
369 error("Couldn't append to %s: %s", global_list, lasterr()(8:end)); |
5801
|
370 else |
6042
|
371 error("Couldn't append to %s: %s", local_list, lasterr()(8:end)); |
5801
|
372 endif |
|
373 end_try_catch |
|
374 |
|
375 ## All is well, let's clean up |
|
376 for i = 1:length(tmpdirs) |
5928
|
377 [status, msg] = rm_rf(tmpdirs{i}); |
5801
|
378 if (status != 1) |
|
379 warning("Couldn't clean up after my self: %s\n", msg); |
|
380 endif |
|
381 endfor |
|
382 |
|
383 ## Add the newly installed packages to the path, so the user |
5971
|
384 ## can begin usings them. |
|
385 if (length(descriptions) > 0) |
|
386 dirs = cell(1, length(descriptions)); |
|
387 for i = 1:length(descriptions) |
5801
|
388 dirs{i} = descriptions{i}.dir; |
5971
|
389 endfor |
|
390 addpath(dirs{:}); |
|
391 endif |
5801
|
392 endfunction |
|
393 |
|
394 function uninstall(pkgnames, handle_deps) |
|
395 local_list = tilde_expand("~/.octave_packages"); |
6036
|
396 global_list = fullfile (OCTAVE_HOME (), "/share/octave/octave_packages"); |
5801
|
397 ## Get the list of installed packages |
|
398 [local_packages, global_packages] = installed_packages(); |
|
399 if (issuperuser()) |
|
400 installed_packages = global_packages; |
|
401 else |
|
402 installed_packages = local_packages; |
|
403 endif |
6042
|
404 |
5801
|
405 num_packages = length(installed_packages); |
|
406 delete_idx = []; |
|
407 for i = 1:num_packages |
|
408 cur_name = installed_packages{i}.name; |
|
409 if (any(strcmp(cur_name, pkgnames))) |
|
410 delete_idx(end+1) = i; |
|
411 endif |
|
412 endfor |
6042
|
413 |
5801
|
414 ## Are all the packages that should be uninstalled already installed? |
|
415 if (length(delete_idx) != length(pkgnames)) |
|
416 # XXX: We should have a better error message |
6042
|
417 error("Some of the packages you want to uninstall are not installed."); |
5801
|
418 endif |
|
419 |
|
420 ## Compute the packages that will remain installed |
|
421 idx = complement(delete_idx, 1:num_packages); |
|
422 remaining_packages = {installed_packages{idx}}; |
|
423 |
|
424 ## Check dependencies |
|
425 if (handle_deps) |
|
426 error_text = ""; |
|
427 for i = 1:length(remaining_packages) |
|
428 desc = remaining_packages{i}; |
|
429 bad_deps = get_unsatisfied_deps(desc, remaining_packages); |
|
430 |
|
431 ## Will the uninstallation break any dependencies? |
|
432 if (!isempty(bad_deps)) |
|
433 for i = 1:length(bad_deps) |
|
434 dep = bad_deps{i}; |
|
435 error_text = [error_text " " desc.name " needs " ... |
|
436 dep.package " " dep.operator " " ... |
|
437 dep.version "\n"]; |
|
438 endfor |
|
439 endif |
|
440 endfor |
|
441 |
6042
|
442 if (! isempty(error_text)) |
5801
|
443 error("The following dependencies where unsatisfied:\n %s", error_text); |
|
444 endif |
|
445 endif |
|
446 |
|
447 ## Delete the directories containing the packages |
|
448 for i = delete_idx |
|
449 desc = installed_packages{i}; |
|
450 ## If an 'on_uninstall.m' exist, call it! |
|
451 if (exist([desc.dir "/packinfo/on_uninstall.m"], "file")) |
|
452 try |
|
453 wd = pwd(); |
|
454 cd([desc.dir "/packinfo"]); |
|
455 on_uninstall(desc); |
|
456 cd(wd); |
|
457 catch |
|
458 # XXX: Should this rather be an error? |
6042
|
459 warning("The 'on_uninstall' script retsurned the following error: %s", lasterr); |
5801
|
460 cd(wd); |
|
461 end_try_catch |
|
462 endif |
|
463 ## Do the actual deletion |
5928
|
464 rmpath(desc.dir); |
6055
|
465 if (exist (desc.dir, "dir")) |
|
466 [status, msg] = rm_rf(desc.dir); |
|
467 if (status != 1) |
6042
|
468 error("Couldn't delete directory %s: %s", desc.dir, msg); |
6055
|
469 endif |
|
470 else |
|
471 warning("Directory %s previously lost", desc.dir); |
|
472 endif |
5801
|
473 endfor |
|
474 |
|
475 ## Write a new ~/.octave_packages |
|
476 if (issuperuser()) |
|
477 if (length(remaining_packages) == 0) |
|
478 unlink(global_list); |
|
479 else |
|
480 global_packages = remaining_packages; |
|
481 save(global_list, "global_packages"); |
|
482 endif |
|
483 else |
|
484 if (length(remaining_packages) == 0) |
|
485 unlink(local_list); |
|
486 else |
|
487 local_packages = remaining_packages; |
|
488 save(local_list, "local_packages"); |
|
489 endif |
|
490 endif |
|
491 |
|
492 endfunction |
|
493 |
|
494 ########################################################## |
|
495 ## A U X I L A R Y F U N C T I O N S ## |
|
496 ########################################################## |
|
497 |
|
498 function prepare_installation(desc, packdir) |
|
499 ## Is there a pre_install to call? |
|
500 if (exist([packdir "pre_install.m"], "file")) |
|
501 wd = pwd(); |
|
502 try |
|
503 cd(packdir); |
|
504 pre_install(desc); |
|
505 cd(wd); |
|
506 catch |
|
507 cd(wd); |
6042
|
508 error("The pre-install function returned the following error: %s", lasterr); |
5801
|
509 end_try_catch |
|
510 endif |
|
511 |
5993
|
512 ## If the directory "inst" doesn't exist, we create it |
|
513 if (!exist([packdir "inst"], "dir")) |
5801
|
514 [status, msg] = mkdir([packdir "inst"]); |
|
515 if (status != 1) |
5928
|
516 rm_rf(desc.dir); |
6042
|
517 error("The 'inst' directory did not exist and could not be created: %s", msg); |
5801
|
518 endif |
|
519 endif |
|
520 endfunction |
|
521 |
|
522 function configure_make (desc, packdir) |
|
523 ## Perform ./configure, make, make install in "src" |
|
524 if (exist([packdir "src"], "dir")) |
|
525 src = [packdir "src/"]; |
|
526 ## configure |
|
527 if (exist([src "configure"], "file")) |
5928
|
528 [status, output] = system(["cd " src " ;./configure --prefix=" desc.dir]); |
5801
|
529 if (status != 0) |
5928
|
530 rm_rf(desc.dir); |
6042
|
531 error("The configure script returned the following error: %s", output); |
5801
|
532 endif |
|
533 endif |
|
534 |
|
535 ## make |
|
536 if (exist([src "Makefile"], "file")) |
5928
|
537 [status, output] = system(["export INSTALLDIR=" desc.dir "; make -C " src]); |
5801
|
538 if (status != 0) |
5928
|
539 rm_rf(desc.dir); |
6042
|
540 error("'make' returned the following error: %s", output); |
5801
|
541 endif |
|
542 %# make install |
5928
|
543 %[status, output] = system(["export INSTALLDIR=" desc.dir "; make install -C " src]); |
5801
|
544 %if (status != 0) |
5928
|
545 % rm_rf(desc.dir); |
6042
|
546 % error("'make install' returned the following error: %s", output); |
5801
|
547 %endif |
|
548 endif |
|
549 |
|
550 ## Copy files to "inst" (this is instead of 'make install') |
|
551 files = [src "FILES"]; |
|
552 if (exist(files, "file")) |
|
553 [fid, msg] = fopen(files, "r"); |
|
554 if (fid < 0) |
6042
|
555 error("Couldn't open %s: %s", files, msg); |
5801
|
556 endif |
5928
|
557 filenames = char(fread(fid))'; |
5801
|
558 filenames = strrep(filenames, "\n", " "); |
5928
|
559 [status, output] = system(["cd " src "; cp " filenames " " packdir "inst/"]); |
5801
|
560 fclose(fid); |
|
561 else |
|
562 m = dir([src "*.m"]); |
|
563 oct = dir([src "*.oct"]); |
|
564 f = ""; |
|
565 if (length(m) > 0) |
5928
|
566 f = sprintf([src "%s "], m.name); |
5801
|
567 endif |
|
568 if (length(oct) > 0) |
5928
|
569 f = [f " " sprintf([src "%s "], oct.name)]; |
5801
|
570 endif |
5928
|
571 |
|
572 if (!all(isspace(f))) |
|
573 [status, output] = system(["cp -R " f " " packdir "inst/"]); |
|
574 endif |
5801
|
575 endif |
|
576 if (status != 0) |
5928
|
577 rm_rf(desc.dir); |
6042
|
578 error("Couldn't copy files from 'src' to 'inst': %s", output); |
5801
|
579 endif |
|
580 endif |
|
581 endfunction |
|
582 |
5971
|
583 function pkg = extract_pkg (nm, pat) |
5955
|
584 fid = fopen (nm, "rt"); |
5971
|
585 pkg = ""; |
5955
|
586 if (fid >= 0) |
|
587 while (! feof(fid)) |
|
588 ln = fgetl (fid); |
|
589 if (ln > 0) |
6143
|
590 t = regexp(ln, pat, "tokens"); |
5955
|
591 if (!isempty(t)) |
5971
|
592 pkg = [pkg, "\n", t{1}{1}]; |
5955
|
593 endif |
|
594 endif |
|
595 endwhile |
5971
|
596 if (!isempty(pkg)) |
|
597 pkg = [pkg, "\n"]; |
5955
|
598 endif |
|
599 fclose (fid); |
|
600 endif |
|
601 endfunction |
|
602 |
5971
|
603 function create_pkgadddel (desc, packdir, nm) |
|
604 pkg = [desc.dir "/" nm]; |
|
605 fid = fopen(pkg, "wt"); |
5976
|
606 |
5955
|
607 if (fid >= 0) |
5971
|
608 ## Search all dot-m files for PKG commands |
5955
|
609 lst = dir ([packdir "inst/*.m"]); |
|
610 for i=1:length(lst) |
5976
|
611 nam = [packdir "inst/" lst(i).name]; |
|
612 fwrite (fid, extract_pkg (nam, ['^[#%][#%]* *' nm ': *(.*)$'])); |
5955
|
613 endfor |
|
614 |
5971
|
615 ## Search all C++ source files for PKG commands |
5955
|
616 lst = dir ([packdir "src/*.cc"]); |
|
617 for i=1:length(lst) |
5976
|
618 nam = [packdir "src/" lst(i).name]; |
|
619 fwrite (fid, extract_pkg (nam, ['^//* *' nm ': *(.*)$'])); |
|
620 fwrite (fid, extract_pkg (nam, ['^/\** *' nm ': *(.*) *\*/$'])); |
5955
|
621 endfor |
|
622 |
5971
|
623 ## Add developer included PKG commands |
|
624 if (exist([packdir nm],"file")) |
|
625 fid2 = fopen([packdir nm],"rt"); |
5955
|
626 if (fid2 >= 0) |
|
627 while (! feof(fid2)) |
|
628 ln = fgets (fid2); |
|
629 if (ln > 0) |
|
630 fwrite(fid, ln); |
|
631 endif |
|
632 endwhile |
5971
|
633 fclose(fid2); |
5955
|
634 endif |
|
635 endif |
|
636 fclose(fid); |
5971
|
637 |
|
638 ## If the file is empty remove it |
|
639 t = dir (pkg); |
|
640 if (t.bytes <= 0) |
|
641 unlink (pkg); |
|
642 endif |
5955
|
643 endif |
|
644 endfunction |
|
645 |
5971
|
646 function copy_files (desc, packdir, bindir) |
6020
|
647 ## Create the installation directory |
|
648 if (! exist (desc.dir, "dir")) |
|
649 [status, output] = mkdir (desc.dir); |
|
650 if (status != 1) |
6042
|
651 error("Couldn't create installation directory %s : %s", |
6020
|
652 desc.dir, output); |
|
653 endif |
|
654 endif |
|
655 |
5801
|
656 ## Copy the files from "inst" to installdir |
5971
|
657 if (! dirempty([packdir "inst"])) |
|
658 [status, output] = system(["cp -R " packdir "inst/* " desc.dir]); |
|
659 if (status != 0) |
|
660 rm_rf(desc.dir); |
6042
|
661 error("Couldn't copy files to the installation directory"); |
5971
|
662 endif |
5801
|
663 endif |
|
664 |
|
665 ## Create the "packinfo" directory |
|
666 packinfo = [desc.dir "/packinfo/"]; |
|
667 [status, msg] = mkdir (packinfo); |
|
668 if (status != 1) |
5928
|
669 rm_rf(desc.dir); |
6042
|
670 error("Couldn't create packinfo directory: %s", msg); |
5801
|
671 endif |
|
672 |
|
673 ## Copy DESCRIPTION |
5928
|
674 [status, output] = system(["cp " packdir "DESCRIPTION " packinfo]); |
5801
|
675 if (status != 0) |
5928
|
676 rm_rf(desc.dir); |
6042
|
677 error("Couldn't copy DESCRIPTION: %s", output); |
5801
|
678 endif |
|
679 |
5993
|
680 ## Copy COPYING |
|
681 [status, output] = system(["cp " packdir "COPYING " packinfo]); |
|
682 if (status != 0) |
|
683 rm_rf(desc.dir); |
6042
|
684 error("Couldn't copy COPYING: %s", output); |
5993
|
685 endif |
|
686 |
5801
|
687 ## Is there an INDEX file to copy or should we generate one? |
|
688 if (exist([packdir "INDEX"], "file")) |
5928
|
689 [status, output] = system(["cp " packdir "INDEX " packinfo]); |
5801
|
690 if (status != 0) |
5928
|
691 rm_rf(desc.dir); |
6042
|
692 error("Couldn't copy INDEX file: %s", output); |
5801
|
693 endif |
|
694 else |
|
695 try |
|
696 write_INDEX(desc, [packdir "inst"], [packinfo "INDEX"]); |
|
697 catch |
5928
|
698 rm_rf(desc.dir); |
5801
|
699 error(lasterr); |
|
700 end_try_catch |
|
701 endif |
|
702 |
|
703 ## Is there an 'on_uninstall.m' to install? |
|
704 if (exist([packdir "on_uninstall.m"], "file")) |
5928
|
705 [status, output] = system(["cp " packdir "on_uninstall.m " packinfo]); |
5801
|
706 if (status != 0) |
5928
|
707 rm_rf(desc.dir); |
6042
|
708 error("Couldn't copy on_uninstall.m: %s", output); |
5801
|
709 endif |
|
710 endif |
5971
|
711 |
|
712 ## Is there a doc/ directory that needs to be installed |
|
713 if (exist([packdir "doc"], "dir") && !dirempty([packdir "doc"])) |
|
714 [status, output] = system(["cp -pR " packdir "doc " desc.dir]); |
|
715 endif |
|
716 |
|
717 ## Is there a bin/ directory that needs to be installed |
|
718 if (exist([packdir "bin"], "dir") && !dirempty([packdir "bin"])) |
|
719 [status, output] = system(["cp -pR " packdir "bin " desc.dir]); |
|
720 endif |
5801
|
721 endfunction |
|
722 |
|
723 function finish_installation (desc, packdir) |
|
724 ## Is there a post-install to call? |
|
725 if (exist([packdir "post_install.m"], "file")) |
|
726 wd = pwd(); |
|
727 try |
|
728 cd(packdir); |
|
729 post_install(desc); |
|
730 cd(wd); |
|
731 catch |
|
732 cd(wd); |
5928
|
733 rm_rf(desc.dir); |
6042
|
734 error("The post_install function returned the following error: %s", lasterr); |
5801
|
735 end_try_catch |
|
736 endif |
|
737 endfunction |
|
738 |
|
739 function out = issuperuser() |
|
740 out = strcmp(getenv("USER"), "root"); |
|
741 endfunction |
|
742 |
|
743 ## This function makes sure the package contains the |
|
744 ## essential files. |
|
745 function verify_directory(dir) |
|
746 needed_files = {"COPYING", "DESCRIPTION"}; |
|
747 for f = needed_files |
|
748 if (!exist([dir "/" f{1}], "file")) |
|
749 error("Package is missing file: %s", f{1}); |
|
750 endif |
|
751 endfor |
|
752 endfunction |
|
753 |
|
754 ## This function parses the DESCRIPTION file |
|
755 function desc = get_description(filename) |
6042
|
756 [fid, msg] = fopen(filename, "r"); |
5801
|
757 if (fid == -1) |
6042
|
758 error("The DESCRIPTION file %s could not be read: %s", filename, msg); |
5801
|
759 endif |
|
760 |
|
761 desc = struct(); |
|
762 |
|
763 line = fgetl(fid); |
|
764 while (line != -1) |
|
765 ## Comments |
|
766 if (line(1) == "#") |
|
767 # Do nothing |
|
768 ## Continuation lines |
|
769 elseif (isspace(line(1))) |
|
770 if (exist("keyword", "var") && isfield(desc, keyword)) |
|
771 desc.(keyword) = [desc.(keyword) " " rstrip(line)]; |
|
772 endif |
|
773 ## Keyword/value pair |
|
774 else |
|
775 colon = find(line == ":"); |
|
776 if (length(colon) == 0) |
|
777 disp("Skipping line."); |
|
778 else |
|
779 colon = colon(1); |
|
780 keyword = tolower(strip(line(1:colon-1))); |
|
781 value = strip(line(colon+1:end)); |
|
782 if (length(value) == 0) |
|
783 fclose(fid); |
6042
|
784 error("The keyword %s have empty value", desc.keywords{end}); |
5801
|
785 endif |
|
786 desc.(keyword) = value; |
|
787 endif |
|
788 endif |
|
789 line = fgetl(fid); |
|
790 endwhile |
|
791 fclose(fid); |
|
792 |
|
793 ## Make sure all is okay |
|
794 needed_fields = {"name", "version", "date", "title", ... |
|
795 "author", "maintainer", "description"}; |
|
796 for f = needed_fields |
|
797 if (!isfield(desc, f{1})) |
6042
|
798 error("Description is missing needed field %s", f{1}); |
5801
|
799 endif |
|
800 endfor |
|
801 desc.version = fix_version(desc.version); |
|
802 if (isfield(desc, "depends")) |
|
803 desc.depends = fix_depends(desc.depends); |
|
804 else |
|
805 desc.depends = ""; |
|
806 endif |
|
807 desc.name = tolower(desc.name); |
|
808 endfunction |
|
809 |
|
810 ## Makes sure the version string v is a valid x.y.z version string |
|
811 ## Examples: "0.1" => "0.1.0", "monkey" => error(...) |
|
812 function out = fix_version(v) |
|
813 dots = find(v == "."); |
|
814 if (length(dots) == 1) |
|
815 major = str2num(v(1:dots-1)); |
|
816 minor = str2num(v(dots+1:end)); |
|
817 if (length(major) != 0 && length(minor) != 0) |
|
818 out = sprintf("%d.%d.0", major, minor); |
|
819 return |
|
820 endif |
|
821 elseif (length(dots) == 2) |
|
822 major = str2num(v(1:dots(1)-1)); |
|
823 minor = str2num(v(dots(1)+1:dots(2)-1)); |
|
824 rev = str2num(v(dots(2)+1:end)); |
|
825 if (length(major) != 0 && length(minor) != 0 && length(rev) != 0) |
|
826 out = sprintf("%d.%d.%d", major, minor, rev); |
|
827 return |
|
828 endif |
|
829 endif |
6042
|
830 error("Bad version string: %s", v); |
5801
|
831 endfunction |
|
832 |
|
833 ## Makes sure the depends field is of the right format. |
|
834 ## This function returns a cell of structures with the following fields: |
|
835 ## package, version, operator |
|
836 function deps_cell = fix_depends(depends) |
|
837 deps = split_by(tolower(depends), ","); |
|
838 deps_cell = cell(1, length(deps)); |
|
839 |
|
840 ## For each dependency |
|
841 for i = 1:length(deps) |
|
842 dep = deps{i}; |
|
843 lpar = find(dep == "("); |
|
844 rpar = find(dep == ")"); |
|
845 ## Does the dependency specify a version |
|
846 ## Example: package(>= version) |
|
847 if (length(lpar) == 1 && length(rpar) == 1) |
|
848 package = tolower(strip(dep(1:lpar-1))); |
|
849 sub = dep( lpar(1)+1:rpar(1)-1 ); |
|
850 parts = split_by(sub, " "); |
|
851 idx = []; |
|
852 for r = 1:size(parts,1) |
|
853 if (length(parts{r}) > 0) |
|
854 idx(end+1) = r; |
|
855 endif |
|
856 endfor |
|
857 |
|
858 if (length(idx) != 2) |
|
859 error(["There's something wrong with the DESCRIPTION file. " ... |
|
860 "The dependency %s has the wrong syntax.\n"], dep); |
|
861 endif |
|
862 operator = parts{idx(1)}; |
|
863 if (!any(strcmp(operator, {">=", "<=", "=="}))) ## XXX: I belive we also support ">" and "<" |
6042
|
864 error("Unsupported operator: %s", operator); |
5801
|
865 endif |
|
866 version = fix_version(parts{idx(2)}); |
|
867 |
|
868 ## If no version is specified for the dependency |
|
869 ## we say that the version should be greater than |
|
870 ## or equal to 0.0.0 |
|
871 else |
|
872 package = tolower(strip(dep)); |
|
873 operator = ">="; |
|
874 version = "0.0.0"; |
|
875 endif |
|
876 deps_cell{i} = struct("package", package, "operator", operator, "version", version); |
|
877 endfor |
|
878 endfunction |
|
879 |
|
880 ## Strips the text of spaces from the right |
|
881 ## Example: " hello world " => " hello world" (XXX: is this the same as deblank?) |
|
882 function text = rstrip(text) |
|
883 chars = find(!isspace(text)); |
|
884 if (length(chars) > 0) |
|
885 text = text(chars(1):end); # XXX: shouldn't it be text = text(1:chars(end)); |
|
886 else |
|
887 text = ""; |
|
888 endif |
|
889 endfunction |
|
890 |
|
891 ## Strips the text of spaces from the left and the right |
|
892 ## Example: " hello world " => "hello world" |
|
893 function text = strip(text) |
|
894 chars = find(!isspace(text)); |
|
895 if (length(chars) > 0) |
|
896 text = text(chars(1):chars(end)); |
|
897 else |
|
898 text = ""; |
|
899 endif |
|
900 endfunction |
|
901 |
|
902 ## Splits the text into a cell array of strings by sep |
|
903 ## Example: "A, B" => {"A", "B"} (with sep = ",") |
|
904 function out = split_by(text, sep) |
|
905 text_matrix = split(text, sep); |
|
906 num_words = size(text_matrix,1); |
|
907 out = cell(num_words, 1); |
|
908 for i = 1:num_words |
|
909 out{i} = strip(text_matrix(i, :)); |
|
910 endfor |
|
911 endfunction |
|
912 |
|
913 ## Creates an INDEX file for a package that doesn't provide one. |
|
914 ## 'desc' describes the package. |
|
915 ## 'dir' is the 'inst' direcotyr in temporary directory. |
|
916 ## 'INDEX' is the name (including path) of resulting INDEX file. |
|
917 function write_INDEX(desc, dir, INDEX) |
|
918 ## Get names of functions in dir |
|
919 [files, err, msg] = readdir(dir); |
|
920 if (err) |
6042
|
921 error("Couldn't read directory %s: %s", dir, msg); |
5801
|
922 endif |
|
923 |
|
924 functions = {}; |
|
925 for i = 1:length(files) |
|
926 file = files{i}; |
|
927 lf = length(file); |
|
928 if (lf > 2 && strcmp( file(end-1:end), ".m" )) |
|
929 functions{end+1} = file(1:end-2); |
|
930 elseif (lf > 4 && strcmp( file(end-3:end), ".oct" )) |
|
931 functions{end+1} = file(1:end-4); |
|
932 endif |
|
933 endfor |
|
934 |
|
935 ## Does desc have a categories field? |
|
936 if (!isfield(desc, "categories")) |
6042
|
937 error("The DESCRIPTION file must have a Categories field, when no INDEX file is given."); |
5801
|
938 endif |
|
939 categories = split_by(desc.categories, ","); |
|
940 if (length(categories) < 1) |
6042
|
941 error("The Category field is empty."); |
5801
|
942 endif |
|
943 |
|
944 ## Write INDEX |
|
945 fid = fopen(INDEX, "w"); |
|
946 if (fid == -1) |
6042
|
947 error("Couldn't open %s for writing.", INDEX); |
5801
|
948 endif |
|
949 fprintf(fid, "%s >> %s\n", desc.name, desc.title); |
|
950 fprintf(fid, "%s\n", categories{1}); |
|
951 fprintf(fid, " %s\n", functions{:}); |
|
952 fclose(fid); |
|
953 endfunction |
|
954 |
|
955 function bad_deps = get_unsatisfied_deps(desc, installed_packages) |
|
956 bad_deps = {}; |
|
957 |
|
958 ## For each dependency |
|
959 for i = 1:length(desc.depends) |
|
960 dep = desc.depends{i}; |
|
961 |
|
962 ## Is the current dependency Octave? |
|
963 if (strcmp(dep.package, "octave")) |
|
964 if (!compare_versions(OCTAVE_VERSION, dep.version, dep.operator)) |
|
965 bad_deps{end+1} = dep; |
|
966 endif |
|
967 ## Is the current dependency not Octave? |
|
968 else |
|
969 ok = false; |
|
970 for i = 1:length(installed_packages) |
|
971 cur_name = installed_packages{i}.name; |
|
972 cur_version = installed_packages{i}.version; |
|
973 if (strcmp(dep.package, cur_name) && compare_versions(cur_version, dep.version, dep.operator)) |
|
974 ok = true; |
|
975 break; |
|
976 endif |
|
977 endfor |
|
978 if (!ok) |
|
979 bad_deps{end+1} = dep; |
|
980 endif |
|
981 endif |
|
982 endfor |
|
983 endfunction |
|
984 |
|
985 function [out1, out2] = installed_packages() |
|
986 local_list = tilde_expand("~/.octave_packages"); |
6036
|
987 global_list = fullfile (OCTAVE_HOME (), "/share/octave/octave_packages"); |
5801
|
988 ## Get the list of installed packages |
|
989 try |
|
990 local_packages = load(local_list).local_packages; |
|
991 catch |
|
992 local_packages = {}; |
|
993 end_try_catch |
|
994 try |
|
995 global_packages = load(global_list).global_packages; |
|
996 catch |
|
997 global_packages = {}; |
|
998 end_try_catch |
|
999 installed_packages = {local_packages{:} global_packages{:}}; |
|
1000 |
|
1001 ## Should we return something? |
|
1002 if (nargout == 2) |
|
1003 out1 = local_packages; |
|
1004 out2 = global_packages; |
|
1005 return; |
|
1006 elseif (nargout == 1) |
|
1007 out1 = installed_packages; |
|
1008 return; |
|
1009 endif |
|
1010 |
|
1011 ## We shouldn't return something, so we'll print something |
|
1012 num_packages = length(installed_packages); |
|
1013 if (num_packages == 0) |
|
1014 printf("No packages installed.\n"); |
|
1015 return; |
|
1016 endif |
|
1017 |
5987
|
1018 ## Compute the maximal lengths of name, version, and dir |
5801
|
1019 h1 = "Package Name"; |
|
1020 h2 = "Version"; |
|
1021 h3 = "Installation directory"; |
5987
|
1022 max_name_length = length(h1); |
|
1023 max_version_length = length(h2); |
|
1024 names = cell(num_packages,1); |
|
1025 for i = 1:num_packages |
|
1026 max_name_length = max(max_name_length, |
|
1027 length(installed_packages{i}.name)); |
|
1028 max_version_length = max(max_version_length, |
|
1029 length(installed_packages{i}.version)); |
|
1030 names{i} = installed_packages{i}.name; |
|
1031 endfor |
|
1032 h1 = postpad (h1, max_name_length,' '); |
|
1033 h2 = postpad (h2, max_version_length, ' ');; |
|
1034 |
|
1035 ## Print a header |
5801
|
1036 header = sprintf("%s | %s | %s\n", h1, h2, h3); |
|
1037 printf(header); |
|
1038 tmp = sprintf(repmat("-", 1, length(header)-1)); |
|
1039 tmp(length(h1)+2) = "+"; tmp(length(h1)+length(h2)+5) = "+"; |
|
1040 printf("%s\n", tmp); |
|
1041 |
|
1042 ## Print the packages |
|
1043 format = sprintf("%%%ds | %%%ds | %%s\n", max_name_length, max_version_length); |
5987
|
1044 [dummy, idx] = sort(names); |
5801
|
1045 for i = 1:num_packages |
5987
|
1046 cur_name = installed_packages{idx(i)}.name; |
|
1047 cur_version = installed_packages{idx(i)}.version; |
|
1048 cur_dir = installed_packages{idx(i)}.dir; |
5801
|
1049 printf(format, cur_name, cur_version, cur_dir); |
|
1050 endfor |
|
1051 endfunction |
|
1052 |
|
1053 function load_packages(files, handle_deps) |
|
1054 installed_packages = installed_packages(); |
|
1055 num_packages = length(installed_packages); |
|
1056 |
6037
|
1057 ## Read package names and installdirs into a more convenient format |
|
1058 pnames = pdirs = cell(1, num_packages); |
|
1059 for i = 1:num_packages |
|
1060 pnames{i} = installed_packages{i}.name; |
|
1061 pdirs{i} = installed_packages{i}.dir; |
|
1062 pdeps{i} = installed_packages{i}.depends; |
|
1063 endfor |
|
1064 |
|
1065 ## load all |
5801
|
1066 if (length(files) == 1 && strcmp(files{1}, "all")) |
6037
|
1067 dirs = pdirs; |
|
1068 ## load package_name1 ... |
5801
|
1069 else |
6037
|
1070 dirs = {}; |
|
1071 for i = 1:length(files) |
|
1072 idx = strcmp(pnames, files{i}); |
|
1073 if (!any(idx)) |
6042
|
1074 error("Package %s is not installed", files{i}); |
6037
|
1075 endif |
|
1076 dirs{end+1} = pdirs{idx}; |
|
1077 if (handle_deps) |
|
1078 pdep = pdeps{idx}; |
|
1079 for j = 1:length(pdep) |
|
1080 depname = pdep{j}.package; |
|
1081 if (strcmp(depname, "octave")) continue; endif |
|
1082 idx = strcmp(pnames, depname); |
|
1083 if (!any(idx)) |
|
1084 error("Package %s could not be loaded since it depends on %s", ... |
|
1085 files{i}, depname); |
|
1086 endif |
|
1087 dirs{end+1} = pdirs{idx}; |
|
1088 endfor |
5971
|
1089 endif |
5801
|
1090 endfor |
6037
|
1091 dirs = unique(dirs); |
5801
|
1092 endif |
6037
|
1093 |
|
1094 ## Load the packages |
6025
|
1095 if (length(dirs) > 0) |
|
1096 addpath(dirs{:}); |
|
1097 endif |
5971
|
1098 |
|
1099 ## Add local binaries, if any, to the EXEC_PATH |
|
1100 for i = 1:length(dirs) |
|
1101 if (exist ([dirs{i} "/bin"],"dir")) |
|
1102 EXEC_PATH ([dirs{i} "/bin:" EXEC_PATH()]); |
|
1103 endif |
|
1104 endfor |
5801
|
1105 endfunction |
5928
|
1106 |
|
1107 function [status_out, msg_out] = rm_rf (dir) |
|
1108 crr = confirm_recursive_rmdir (); |
|
1109 unwind_protect |
|
1110 confirm_recursive_rmdir (false); |
|
1111 [status, msg] = rmdir (dir, "s"); |
|
1112 unwind_protect_cleanup |
|
1113 confirm_recursive_rmdir (crr); |
|
1114 end_unwind_protect |
|
1115 if (nargout > 0) |
|
1116 status_out = status; |
|
1117 endif |
|
1118 if (nargout > 1) |
|
1119 msg_out = msg; |
|
1120 endif |
|
1121 endfunction |
5971
|
1122 |
|
1123 function emp = dirempty (nm, ign) |
|
1124 if (nargin < 2) |
|
1125 ign = {".",".."}; |
|
1126 else |
|
1127 ign = [{".",".."},ign]; |
|
1128 endif |
|
1129 l = dir (nm); |
|
1130 for i=1:length(l) |
|
1131 found = false; |
|
1132 for j=1:length(ign) |
|
1133 if (strcmp(l(i).name,ign{j})) |
|
1134 found = true; |
|
1135 break; |
|
1136 endif |
|
1137 endfor |
|
1138 if (!found) |
|
1139 emp = false; |
|
1140 return |
|
1141 endif |
|
1142 endfor |
|
1143 emp = true; |
|
1144 return; |
|
1145 endfunction |