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