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