Mercurial > jwe > octave
annotate scripts/miscellaneous/what.m @ 28127:9413b3d56e00 stable
what.m: Report mex files in directory (bug #57877)
* what.m: Change comparison of extension looking for mex file from "mexext" to
"['.' mexext]" because extension includes the period.
author | Rik <rik@octave.org> |
---|---|
date | Fri, 21 Feb 2020 14:07:14 -0800 |
parents | bd51beb6205e |
children | 28de41192f3c 0a5b15007766 |
rev | line source |
---|---|
27928
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
2 ## |
27924
1891570abac8
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
27923
diff
changeset
|
3 ## Copyright (C) 2007-2020 The Octave Project Developers |
27923
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
27910
diff
changeset
|
4 ## |
27928
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
7050 | 7 ## |
8 ## This file is part of Octave. | |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
7050 | 11 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
12 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## (at your option) any later version. |
7050 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
18 ## GNU General Public License for more details. |
7050 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
21 ## along with Octave; see the file COPYING. If not, see | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
22 ## <https://www.gnu.org/licenses/>. |
27928
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
24 ######################################################################## |
7050 | 25 |
26 ## -*- texinfo -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20164
diff
changeset
|
27 ## @deftypefn {} {} what |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20164
diff
changeset
|
28 ## @deftypefnx {} {} what @var{dir} |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20164
diff
changeset
|
29 ## @deftypefnx {} {w =} what (@var{dir}) |
19183 | 30 ## List the Octave specific files in directory @var{dir}. |
31 ## | |
32 ## If @var{dir} is not specified then the current directory is used. | |
33 ## | |
34 ## If a return argument is requested, the files found are returned in the | |
35 ## structure @var{w}. The structure contains the following fields: | |
36 ## | |
37 ## @table @asis | |
38 ## @item path | |
39 ## Full path to directory @var{dir} | |
19405
cbce5d1bcaf9
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19183
diff
changeset
|
40 ## |
19183 | 41 ## @item m |
42 ## Cell array of m-files | |
19405
cbce5d1bcaf9
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19183
diff
changeset
|
43 ## |
19183 | 44 ## @item mat |
45 ## Cell array of mat files | |
19405
cbce5d1bcaf9
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19183
diff
changeset
|
46 ## |
19183 | 47 ## @item mex |
48 ## Cell array of mex files | |
19405
cbce5d1bcaf9
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19183
diff
changeset
|
49 ## |
19183 | 50 ## @item oct |
51 ## Cell array of oct files | |
19405
cbce5d1bcaf9
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19183
diff
changeset
|
52 ## |
19183 | 53 ## @item mdl |
54 ## Cell array of mdl files | |
19405
cbce5d1bcaf9
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19183
diff
changeset
|
55 ## |
19183 | 56 ## @item slx |
57 ## Cell array of slx files | |
19405
cbce5d1bcaf9
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19183
diff
changeset
|
58 ## |
19183 | 59 ## @item p |
60 ## Cell array of p-files | |
19405
cbce5d1bcaf9
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19183
diff
changeset
|
61 ## |
19183 | 62 ## @item classes |
63 ## Cell array of class directories (@file{@@@var{classname}/}) | |
19405
cbce5d1bcaf9
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19183
diff
changeset
|
64 ## |
19183 | 65 ## @item packages |
66 ## Cell array of package directories (@file{+@var{pkgname}/}) | |
67 ## @end table | |
68 ## | |
27910 | 69 ## Compatibility Note: Octave does not support mdl, slx, and p files. |
70 ## @code{what} will always return an empty list for these categories. | |
19183 | 71 ## @seealso{which, ls, exist} |
7050 | 72 ## @end deftypefn |
73 | |
19183 | 74 function retval = what (dir) |
75 | |
76 if (nargin > 1) | |
77 print_usage (); | |
78 endif | |
7050 | 79 |
80 if (nargin == 0) | |
27909
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
81 dir = { pwd() }; |
7050 | 82 else |
19183 | 83 dtmp = canonicalize_file_name (dir); |
18060
427412d40f1a
what.m: Workaround file separator issues in code (bug #40726).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
84 if (isempty (dtmp)) |
27909
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
85 dtmp = {}; |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
86 else |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
87 dtmp = {dtmp}; |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
88 endif |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
89 ## Search for directory name in path |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
90 if (dir(end) == '/' || dir(end) == '\') |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
91 dir(end) = []; |
7050 | 92 endif |
27909
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
93 dtmp = unique ([dtmp; dir_in_loadpath(dir, "all")]); |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
94 |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
95 if (isempty (dtmp) && nargout == 0) |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
96 printf ("%s not found\n", dir); |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
97 return; |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
98 endif |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
99 |
19183 | 100 dir = dtmp; |
7050 | 101 endif |
102 | |
27909
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
103 ## Lookup info for each directory |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
104 for i = 1 : numel (dir) |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
105 w(i) = __what__ (dir{i}); |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
106 endfor |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
107 |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
108 ## If none was found, return an empty struct |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
109 if (numel (dir) == 0) |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
110 w = __what__ (""); |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
111 w = resize (w, [0, 1]); # Matlab compatibility, return 0x1 empty array |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
112 end |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
113 |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
114 if (nargout == 0) |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
115 for i = 1 : numel (w) |
27910 | 116 __print_fnames__ ("M-files in directory", w(i).path, w(i).m); |
117 __print_fnames__ ("\nMAT-files in directory", w(i).path, w(i).mat); | |
118 __print_fnames__ ("\nMEX-files in directory", w(i).path, w(i).mex); | |
119 __print_fnames__ ("\nOCT-files in directory", w(i).path, w(i).oct); | |
120 __print_fnames__ ("\nClasses in directory", w(i).path, w(i).classes); | |
121 __print_fnames__ ("\nPackages in directory", w(i).path, w(i).packages); | |
27909
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
122 endfor |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
123 else |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
124 retval = w; |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
125 endif |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
126 |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
127 endfunction |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
128 |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
129 |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
130 ## what() functionality for a single directory |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
131 function w = __what__ (dir) |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
132 |
19183 | 133 files = readdir (dir); |
134 w.path = dir; | |
7050 | 135 w.m = cell (0, 1); |
19183 | 136 w.mat = cell (0, 1); |
7050 | 137 w.mex = cell (0, 1); |
138 w.oct = cell (0, 1); | |
139 w.mdl = cell (0, 1); | |
19183 | 140 w.slx = cell (0, 1); |
7050 | 141 w.p = cell (0, 1); |
142 w.classes = cell (0, 1); | |
19183 | 143 w.packages = cell (0, 1); |
7050 | 144 |
27910 | 145 for i = 1 : numel (files) |
27907
32f4a15c77c7
what.m: Detect and report on packages (bug #57530)
Rik <rik@octave.org>
parents:
26379
diff
changeset
|
146 nm = files{i}; |
32f4a15c77c7
what.m: Detect and report on packages (bug #57530)
Rik <rik@octave.org>
parents:
26379
diff
changeset
|
147 |
32f4a15c77c7
what.m: Detect and report on packages (bug #57530)
Rik <rik@octave.org>
parents:
26379
diff
changeset
|
148 if (strcmp (nm, ".") || strcmp (nm, "..")) |
32f4a15c77c7
what.m: Detect and report on packages (bug #57530)
Rik <rik@octave.org>
parents:
26379
diff
changeset
|
149 continue; # Ignore . and .. |
7050 | 150 endif |
27907
32f4a15c77c7
what.m: Detect and report on packages (bug #57530)
Rik <rik@octave.org>
parents:
26379
diff
changeset
|
151 |
32f4a15c77c7
what.m: Detect and report on packages (bug #57530)
Rik <rik@octave.org>
parents:
26379
diff
changeset
|
152 ## mdl, slx, and p are ignored (no if test) since they are not implemented |
32f4a15c77c7
what.m: Detect and report on packages (bug #57530)
Rik <rik@octave.org>
parents:
26379
diff
changeset
|
153 [~, f, e] = fileparts (nm); |
32f4a15c77c7
what.m: Detect and report on packages (bug #57530)
Rik <rik@octave.org>
parents:
26379
diff
changeset
|
154 if (strcmp (e, ".m")) |
27908
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27907
diff
changeset
|
155 if (isfile (fullfile (dir, nm))) |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27907
diff
changeset
|
156 w.m{end+1} = nm; |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27907
diff
changeset
|
157 endif |
27907
32f4a15c77c7
what.m: Detect and report on packages (bug #57530)
Rik <rik@octave.org>
parents:
26379
diff
changeset
|
158 elseif (strcmp (e, ".mat")) |
27908
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27907
diff
changeset
|
159 if (isfile (fullfile (dir, nm))) |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27907
diff
changeset
|
160 w.mat{end+1} = nm; |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27907
diff
changeset
|
161 endif |
27907
32f4a15c77c7
what.m: Detect and report on packages (bug #57530)
Rik <rik@octave.org>
parents:
26379
diff
changeset
|
162 elseif (strcmp (e, ".oct")) |
27908
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27907
diff
changeset
|
163 if (isfile (fullfile (dir, nm))) |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27907
diff
changeset
|
164 w.oct{end+1} = nm; |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27907
diff
changeset
|
165 endif |
28127
9413b3d56e00
what.m: Report mex files in directory (bug #57877)
Rik <rik@octave.org>
parents:
27928
diff
changeset
|
166 elseif (strcmp (e, ['.' mexext])) |
27908
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27907
diff
changeset
|
167 if (isfile (fullfile (dir, nm))) |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27907
diff
changeset
|
168 w.mex{end+1} = nm; |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27907
diff
changeset
|
169 endif |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27907
diff
changeset
|
170 elseif (nm(1) == "@") |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27907
diff
changeset
|
171 if (isfolder (fullfile (dir, nm))) |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27907
diff
changeset
|
172 w.classes{end+1} = nm; |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27907
diff
changeset
|
173 endif |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27907
diff
changeset
|
174 elseif (nm(1) == "+") |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27907
diff
changeset
|
175 if (isfolder (fullfile (dir, nm))) |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27907
diff
changeset
|
176 w.packages{end+1} = nm; |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27907
diff
changeset
|
177 endif |
27907
32f4a15c77c7
what.m: Detect and report on packages (bug #57530)
Rik <rik@octave.org>
parents:
26379
diff
changeset
|
178 endif |
32f4a15c77c7
what.m: Detect and report on packages (bug #57530)
Rik <rik@octave.org>
parents:
26379
diff
changeset
|
179 |
7050 | 180 endfor |
181 | |
182 endfunction | |
183 | |
27909
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
184 |
121d33ab44bc
what.m: Find all instances in loadpath for Matlab compatibility (bug #40904).
Rik <rik@octave.org>
parents:
27908
diff
changeset
|
185 ## Pretty print filenames to terminal |
27910 | 186 function __print_fnames__ (msg, p, f) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
187 |
27910 | 188 if (! isempty (f)) |
189 printf ("%s %s:\n\n", msg, p); | |
190 printf ("%s", list_in_columns (f, 0, " ")); | |
7050 | 191 endif |
19183 | 192 |
7050 | 193 endfunction |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
16724
diff
changeset
|
194 |
19183 | 195 |
196 %!test | |
197 %! w = what (); | |
198 %! assert (w.path, pwd); | |
199 %! assert (fieldnames (w), {"path"; "m"; "mat"; "mex"; "oct"; "mdl"; "slx"; | |
200 %! "p"; "classes"; "packages"}); | |
201 | |
27910 | 202 ## FIXME: Should have additional tests. Possibly create a temporary directory |
203 ## within TMPDIR, create files and folders, and call what() on that dir. | |
19183 | 204 %!error what (1, 2) |