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