comparison scripts/pkg/pkg.m @ 7497:bb7cc90cdc5e

added describe command to pkg
author carlo@guglielmo.local
date Tue, 19 Feb 2008 19:23:58 -0500
parents bf3fb3477d2a
children a939fb03a137
comparison
equal deleted inserted replaced
7496:e27f8afa99e5 7497:bb7cc90cdc5e
99 ## [@var{user_packages}, @var{system_packages}] = pkg list 99 ## [@var{user_packages}, @var{system_packages}] = pkg list
100 ## @end example 100 ## @end example
101 ## @noindent 101 ## @noindent
102 ## splits the list of installed packages into those who are installed by 102 ## splits the list of installed packages into those who are installed by
103 ## the current user, and those installed by the system administrator. 103 ## the current user, and those installed by the system administrator.
104 ## @item describe
105 ## Show a short description of the named installed packages, with the option
106 ## '-verbose' also list functions provided by the package, e.g.:
107 ## @example
108 ## pkg describe -verbose all
109 ## @end example
110 ## @noindent
111 ## will describe all installed packages and the functions they provide.
112 ## If one output is requested a cell of structure containing the
113 ## description and list of functions of each package is returned as
114 ## output rather than printed on screen:
115 ## @example
116 ## desc = pkg ("describe", "secs1d", "image")
117 ## @end example
118 ## @noindent
119 ## If any of the requested packages is not installed, pkg returns an
120 ## error, unless a second output is requested:
121 ## @example
122 ## [ desc, flag] = pkg ("describe", "secs1d", "image")
123 ## @end example
124 ## @noindent
125 ## @var{flag} will take one of the values "Not installed", "Loaded" or
126 ## "Not loaded" for each of the named packages.
104 ## @item prefix 127 ## @item prefix
105 ## Set the installation prefix directory. For example, 128 ## Set the installation prefix directory. For example,
106 ## @example 129 ## @example
107 ## pkg prefix ~/my_octave_packages 130 ## pkg prefix ~/my_octave_packages
108 ## @end example 131 ## @end example
196 endif 219 endif
197 prefix = tilde_expand (prefix); 220 prefix = tilde_expand (prefix);
198 archprefix = tilde_expand (archprefix); 221 archprefix = tilde_expand (archprefix);
199 endif 222 endif
200 223
224 available_actions = {"list", "install", "uninstall", "load", "unload", "prefix", ...
225 "local_list", "global_list", "rebuild", "build","describe"};
201 ## Handle input 226 ## Handle input
202 if (length (varargin) == 0 || ! iscellstr (varargin)) 227 if (length (varargin) == 0 || ! iscellstr (varargin))
203 print_usage (); 228 print_usage ();
204 endif 229 endif
205 files = {}; 230 files = {};
225 case "-global" 250 case "-global"
226 global_install = true; 251 global_install = true;
227 if (! user_prefix) 252 if (! user_prefix)
228 prefix = fullfile (OCTAVE_HOME (), "share", "octave", "packages"); 253 prefix = fullfile (OCTAVE_HOME (), "share", "octave", "packages");
229 endif 254 endif
230 case {"list", "install", "uninstall", "load", "unload", "prefix", ... 255 case available_actions
231 "local_list", "global_list", "rebuild", "build"}
232 if (strcmp (action, "none")) 256 if (strcmp (action, "none"))
233 action = varargin{i}; 257 action = varargin{i};
234 else 258 else
235 error ("more than one action specified"); 259 error ("more than one action specified");
236 endif 260 endif
363 if (length (files) < 2) 387 if (length (files) < 2)
364 error ("you must specify at least the build directory and one filename\nwhen calling 'pkg build'"); 388 error ("you must specify at least the build directory and one filename\nwhen calling 'pkg build'");
365 endif 389 endif
366 build (files, deps, auto, verbose); 390 build (files, deps, auto, verbose);
367 391
392 case "describe"
393 if (length (files) == 0)
394 error ("you must specify at least one package or 'all' when calling 'pkg describe'");
395 endif
396 ## XXX FIXME: the name of the output variables is inconsistent
397 ## with their content
398 switch (nargout)
399 case 0
400 describe (files, verbose, local_list, global_list);
401 case 1
402 pkg_desc_list = describe (files, verbose, local_list, ...
403 global_list);
404 local_packages = pkg_desc_list;
405 case 2
406 [pkg_desc_list, flag] = describe (files, verbose, local_list, ...
407 global_list);
408 local_packages = pkg_desc_list;
409 global_packages = flag;
410 otherwise
411 error ("you can request at most two outputs when calling 'pkg describe'");
412 endswitch
413
368 otherwise 414 otherwise
369 error ("you must specify a valid action for 'pkg'. See 'help pkg' for details"); 415 error ("you must specify a valid action for 'pkg'. See 'help pkg' for details");
370 endswitch 416 endswitch
371 endfunction 417 endfunction
372 418
898 endif 944 endif
899 endif 945 endif
900 946
901 endfunction 947 endfunction
902 948
949 function [pkg_desc_list, flag] = describe (pkgnames, verbose,
950 local_list, global_list)
951
952 ## Get the list of installed packages
953 installed_pkgs_lst = installed_packages(local_list, global_list);
954 num_packages = length (installed_pkgs_lst);
955
956
957 describe_all = false;
958 if (any (strcmp ('all', pkgnames)))
959 describe_all = true;
960 flag{1:num_packages} = "Not Loaded";
961 num_pkgnames = num_packages;
962 else
963 num_pkgnames = length (pkgnames);
964 flag{1:num_pkgnames} = "Not installed";
965 end
966
967 for i = 1:num_packages
968 curr_name= installed_pkgs_lst{i}.name;
969 if (describe_all)
970 name_pos = i;
971 else
972 name_pos = find(strcmp (curr_name, pkgnames));
973 end
974 if (!isempty(name_pos))
975 if (installed_pkgs_lst{i}.loaded)
976 flag{name_pos} = "Loaded";
977 else
978 flag{name_pos} = "Not loaded";
979 endif
980
981 pkg_desc_list{name_pos}.name = ...
982 installed_pkgs_lst{i}.name;
983 pkg_desc_list{name_pos}.description = ...
984 installed_pkgs_lst{i}.description;
985 pkg_desc_list{name_pos}.provides = ...
986 parse_pkg_idx(installed_pkgs_lst{i}.dir);
987
988 endif
989 endfor
990
991 non_inst = find(strcmp(flag,"Not installed"));
992 if (!isempty(non_inst) && (nargout<2))
993 non_inst_str = sprintf(" %s ",pkgnames{non_inst});
994 error("some packages are not installed: %s",non_inst_str);
995 endif
996
997 if (nargout == 0)
998 for i=1:num_pkgnames
999 print_package_description (pkg_desc_list{i}.name,
1000 pkg_desc_list{i}.provides,
1001 pkg_desc_list{i}.description,
1002 flag{i}, verbose);
1003 endfor
1004 endif
1005
1006 endfunction
1007
903 ########################################################## 1008 ##########################################################
904 ## A U X I L I A R Y F U N C T I O N S ## 1009 ## A U X I L I A R Y F U N C T I O N S ##
905 ########################################################## 1010 ##########################################################
1011
1012 ## This function reads an INDEX file
1013 function [pkg_idx_struct] = parse_pkg_idx(packdir)
1014
1015 fINDEX = fullfile (packdir, "packinfo", "INDEX");
1016
1017 if (!exist(fINDEX, "file") )
1018 error("could not find any INDEX file in directory %s, \
1019 \ntry 'pkg rebuild all' to generate missing INDEX files", packdir);
1020 endif
1021
1022
1023 [fid, msg] = fopen(fINDEX,"r");
1024 if (fid == -1)
1025 error("the INDEX file %s could not be read: %s",
1026 fINDEX, msg);
1027 endif
1028
1029 cat_num = 1;
1030 pkg_idx_struct{1}.category = "Uncategorized";
1031 pkg_idx_struct{1}.functions = {};
1032
1033
1034 line = fgetl(fid);
1035 while ((isempty(strfind(line,">>"))) && (! feof (fid)))
1036 line = fgetl(fid);
1037 endwhile
1038
1039 while ((! feof (fid) ) || (line != -1))
1040 if (!any(!isspace(line)) || (line(1) == "#") || any(line=="="))
1041 ## Comments, blank lines or comments about unimplemented
1042 ## functions: do nothing
1043 ## XXX: probably comments and pointers to external functions
1044 ## could be treated better when printing to screen?
1045 elseif (!isempty(strfind(line,">>")))
1046 ## Skip package name and description as they are in
1047 ## DESCRIPTION already
1048 elseif (!isspace(line(1)))
1049 ## Category
1050 if ( !isempty(pkg_idx_struct{cat_num}.functions))
1051 pkg_idx_struct{++cat_num}.functions = {};
1052 endif
1053 pkg_idx_struct{cat_num}.category = deblank(line);
1054 else
1055 ## Function names
1056 while (any(!isspace(line)))
1057 [fun_name, line] = strtok(line);
1058 pkg_idx_struct{cat_num}.functions{end+1} = deblank(fun_name);
1059 endwhile
1060 endif
1061 line = fgetl(fid);
1062 endwhile
1063 fclose (fid);
1064 endfunction
1065
1066 function print_package_description (pkg_name, pkg_idx_struct,
1067 pkg_desc, status, verbose)
1068
1069 printf("---\nPackage name:\n\t%s\n", pkg_name);
1070 printf("Short description:\n\t%s\n", pkg_desc);
1071 printf("Status:\n\t%s\n", status);
1072 if (verbose)
1073 printf("---\nProvides:\n");
1074 for i=1:length(pkg_idx_struct)
1075 if (!isempty(pkg_idx_struct{i}.functions))
1076 printf("%s\n", pkg_idx_struct{i}.category);
1077 for j=1:length(pkg_idx_struct{i}.functions)
1078 printf("\t%s\n", pkg_idx_struct{i}.functions{j});
1079 endfor
1080 endif
1081 endfor
1082 endif
1083
1084 endfunction
1085
906 1086
907 function pth = absolute_pathname (pth) 1087 function pth = absolute_pathname (pth)
908 [status, msg, msgid] = fileattrib(pth); 1088 [status, msg, msgid] = fileattrib(pth);
909 if (status != 1) 1089 if (status != 1)
910 error ("could not find the file or path %s", pth); 1090 error ("could not find the file or path %s", pth);
1578 endfor 1758 endfor
1579 endfunction 1759 endfunction
1580 1760
1581 ## Creates an INDEX file for a package that doesn't provide one. 1761 ## Creates an INDEX file for a package that doesn't provide one.
1582 ## 'desc' describes the package. 1762 ## 'desc' describes the package.
1583 ## 'dir' is the 'inst' direcotyr in temporary directory. 1763 ## 'dir' is the 'inst' direcotry in temporary directory.
1584 ## 'INDEX' is the name (including path) of resulting INDEX file. 1764 ## 'INDEX' is the name (including path) of resulting INDEX file.
1585 function write_INDEX (desc, dir, INDEX, global_install) 1765 function write_INDEX (desc, dir, INDEX, global_install)
1586 ## Get names of functions in dir 1766 ## Get names of functions in dir
1587 [files, err, msg] = readdir (dir); 1767 [files, err, msg] = readdir (dir);
1588 if (err) 1768 if (err)
2102 dep = true; 2282 dep = true;
2103 break; 2283 break;
2104 endif 2284 endif
2105 endfor 2285 endfor
2106 endfunction 2286 endfunction
2287
2288