-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathnSTAT_Install.m
More file actions
361 lines (320 loc) · 11.3 KB
/
nSTAT_Install.m
File metadata and controls
361 lines (320 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
function opts = nSTAT_Install(varargin)
% nSTAT_Install Configure nSTAT MATLAB runtime paths and help integration.
%
% Usage:
% nSTAT_Install
% nSTAT_Install('RebuildDocSearch',true,'CleanUserPathPrefs',false)
% nSTAT_Install('DownloadExampleData','prompt')
% opts = nSTAT_Install(...)
%
% Name-value options:
% RebuildDocSearch (default true) Rebuild help search DB in helpfiles/.
% CleanUserPathPrefs (default false) Remove stale user MATLAB path entries.
% DownloadExampleData (default 'prompt') Prompt, download, or skip the
% external figshare paper-example data package. Accepts
% true/'always', false/'never', or 'prompt'.
%
% This installer excludes non-runtime trees (python, cache folders, hidden
% folders) from the MATLAB path to avoid shadowing.
%
% nSTAT v1 Copyright (C) 2012 Masschusetts Institute of Technology
% Cajigas, I, Malik, WQ, Brown, EN
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License as published
% by the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
% See the GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software Foundation,
% Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
opts = parseInstallOptions(varargin{:});
fileLocation = mfilename('fullpath');
rootDir = fileparts(fileLocation);
helpDir = fullfile(rootDir, 'helpfiles');
display('Configuring nSTAT MATLAB runtime search path');
removeExistingRootPaths(rootDir);
maybeInstallExampleData(rootDir, opts.DownloadExampleData);
runtimePaths = collectRuntimePaths(rootDir);
if isempty(runtimePaths)
warning('nSTAT:EmptyRuntimePath', ...
'No runtime MATLAB paths were discovered under %s', rootDir);
else
addpath(runtimePaths{:}, '-begin');
end
if opts.RebuildDocSearch
if isfolder(helpDir)
display('Building nSTAT help search database');
builddocsearchdb(helpDir);
else
warning('nSTAT:MissingHelpFolder', ...
'Help folder was not found at: %s', helpDir);
end
end
if opts.CleanUserPathPrefs
cleanupFcn = 'cleanup_user_path_prefs';
cleanupFile = fullfile(rootDir, 'tools', 'matlab', [cleanupFcn '.m']);
if exist(cleanupFcn, 'file') == 2
feval(cleanupFcn, rootDir);
elseif isfile(cleanupFile)
addpath(fileparts(cleanupFile), '-begin');
if exist(cleanupFcn, 'file') == 2
feval(cleanupFcn, rootDir);
else
warning('nSTAT:CleanupFunctionUnavailable', ...
'Could not invoke %s after running %s', cleanupFcn, cleanupFile);
end
else
warning('nSTAT:MissingCleanupScript', ...
'Cleanup script not found: %s', cleanupFile);
end
end
display('Refreshing MATLAB toolbox cache');
rehash toolboxcache;
display('Saving path');
savepath;
end
function opts = parseInstallOptions(varargin)
parser = inputParser;
parser.FunctionName = 'nSTAT_Install';
addParameter(parser, 'RebuildDocSearch', true, @(x)islogical(x) || isnumeric(x));
addParameter(parser, 'CleanUserPathPrefs', false, @(x)islogical(x) || isnumeric(x));
addParameter(parser, 'DownloadExampleData', 'prompt', ...
@(x)islogical(x) || isnumeric(x) || ischar(x) || (isstring(x) && isscalar(x)));
parse(parser, varargin{:});
opts.RebuildDocSearch = logical(parser.Results.RebuildDocSearch);
opts.CleanUserPathPrefs = logical(parser.Results.CleanUserPathPrefs);
opts.DownloadExampleData = normalizeDownloadMode(parser.Results.DownloadExampleData);
end
function mode = normalizeDownloadMode(rawMode)
if islogical(rawMode) || (isnumeric(rawMode) && isscalar(rawMode))
if logical(rawMode)
mode = 'always';
else
mode = 'never';
end
return;
end
mode = lower(char(string(rawMode)));
switch mode
case {'always', 'prompt', 'never'}
return;
otherwise
error('nSTAT:InvalidDownloadMode', ...
'DownloadExampleData must be true/false or one of: always, prompt, never.');
end
end
function removeExistingRootPaths(rootDir)
pathEntries = strsplit(path, pathsep);
isRepoPath = startsWith(pathEntries, rootDir);
toRemove = unique(pathEntries(isRepoPath));
toRemove = toRemove(cellfun(@(p)~isempty(p) && isfolder(p), toRemove));
if ~isempty(toRemove)
rmpath(toRemove{:});
end
end
function runtimePaths = collectRuntimePaths(rootDir)
rawPath = strsplit(genpath(rootDir), pathsep);
runtimePaths = {};
for iDir = 1:numel(rawPath)
dirPath = rawPath{iDir};
if isempty(dirPath) || ~isfolder(dirPath)
continue;
end
relPath = strrep(dirPath, [rootDir filesep], '');
if strcmp(dirPath, rootDir)
runtimePaths{end+1} = dirPath; %#ok<AGROW>
continue;
end
if shouldExcludePath(relPath)
continue;
end
hasMatlabFiles = ~isempty(dir(fullfile(dirPath, '*.m')));
hasDataFiles = ~isempty(dir(fullfile(dirPath, '*.mat')));
hasClassFolders = ~isempty(dir(fullfile(dirPath, '@*')));
hasPackageFolders = ~isempty(dir(fullfile(dirPath, '+*')));
if hasMatlabFiles || hasDataFiles || hasClassFolders || hasPackageFolders
runtimePaths{end+1} = dirPath; %#ok<AGROW>
end
end
runtimePaths = unique(runtimePaths, 'stable');
end
function tf = shouldExcludePath(relPath)
if isempty(relPath)
tf = false;
return;
end
segments = strsplit(relPath, filesep);
segments = segments(~cellfun(@isempty, segments));
if isempty(segments)
tf = false;
return;
end
excludedExact = { ...
'.git', '.github', 'python', 'slprj', 'porting', ...
'__pycache__', '.pytest_cache', '.mypy_cache', '.vscode', '.idea'};
tf = false;
for iSeg = 1:numel(segments)
seg = segments{iSeg};
if startsWith(seg, '.')
tf = true;
return;
end
if any(strcmpi(seg, excludedExact))
tf = true;
return;
end
end
end
function maybeInstallExampleData(rootDir, mode)
info = nSTAT_ExampleDataInfo(rootDir);
if info.isInstalled
display('nSTAT example data already present');
return;
end
shouldDownload = false;
switch mode
case 'always'
shouldDownload = true;
case 'never'
warning('nSTAT:ExampleDataMissing', ...
['nSTAT example data was not found under %s. ', ...
'Run nSTAT_Install(''DownloadExampleData'',true) to install it.'], ...
info.dataDir);
case 'prompt'
shouldDownload = shouldPromptForExampleData(info);
otherwise
error('nSTAT:InvalidDownloadModeInternal', ...
'Unsupported download mode: %s', mode);
end
if shouldDownload
downloadExampleData(rootDir, info);
end
end
function tf = shouldPromptForExampleData(info)
message = sprintf([ ...
'nSTAT example data was not found.\n\n', ...
'Download the paper-example dataset from figshare and install it into:\n%s\n\n', ...
'Dataset DOI: %s'], info.dataDir, info.figshareDoi);
if usejava('desktop') && feature('ShowFigureWindows')
choice = questdlg(message, 'Install nSTAT Example Data', ...
'Download', 'Skip', 'Download');
tf = strcmp(choice, 'Download');
if ~tf
warning('nSTAT:ExampleDataSkipped', ...
['nSTAT example data is still missing. ', ...
'Run nSTAT_Install(''DownloadExampleData'',true) to install it later.']);
end
return;
end
warning('nSTAT:ExampleDataPromptUnavailable', ...
['nSTAT example data is missing, but interactive prompting is unavailable. ', ...
'Run nSTAT_Install(''DownloadExampleData'',true) to install it.']);
tf = false;
end
function downloadExampleData(rootDir, info)
display('Querying figshare metadata for nSTAT example data');
article = webread(info.figshareApiUrl, weboptions('Timeout', 60));
fileEntry = selectExampleDataFile(article);
archivePath = fullfile(tempdir, ['nstat_example_data_' char(java.util.UUID.randomUUID) '.zip']);
cleanupObj = onCleanup(@()deleteTempFile(archivePath)); %#ok<NASGU>
display(sprintf('Downloading nSTAT example data archive (%.1f MB)', fileEntry.size / 1e6));
downloadFile(fileEntry.download_url, archivePath);
if isfield(fileEntry, 'supplied_md5') && ~isempty(fileEntry.supplied_md5)
localMd5 = computeFileMd5(archivePath);
if ~strcmpi(localMd5, fileEntry.supplied_md5)
error('nSTAT:ExampleDataChecksumMismatch', ...
'Downloaded example data MD5 mismatch. Expected %s, got %s.', ...
fileEntry.supplied_md5, localMd5);
end
end
if exist(info.dataDir, 'dir') ~= 7
mkdir(info.dataDir);
end
display('Extracting nSTAT example data archive');
unzip(archivePath, rootDir);
installedInfo = nSTAT_ExampleDataInfo(rootDir);
if ~installedInfo.isInstalled
error('nSTAT:ExampleDataInstallIncomplete', ...
['Example data download completed, but the required files were not found ', ...
'under %s after extraction.'], installedInfo.dataDir);
end
display(sprintf('Installed nSTAT example data into %s', installedInfo.dataDir));
end
function fileEntry = selectExampleDataFile(article)
if ~isfield(article, 'files') || isempty(article.files)
error('nSTAT:ExampleDataNoFiles', ...
'The figshare dataset metadata did not contain any downloadable files.');
end
fileEntry = article.files(1);
for iFile = 1:numel(article.files)
candidate = article.files(iFile);
if endsWith(lower(candidate.name), '.zip')
fileEntry = candidate;
return;
end
end
end
function md5Hex = computeFileMd5(filePath)
if ismac
[status, output] = system(sprintf('md5 -q %s', shellQuote(filePath)));
if status == 0
md5Hex = strtrim(lower(output));
return;
end
elseif isunix
[status, output] = system(sprintf('md5sum %s', shellQuote(filePath)));
if status == 0
tokens = regexp(strtrim(output), '^(?<hash>[0-9a-fA-F]+)\s+', 'names', 'once');
if ~isempty(tokens)
md5Hex = lower(tokens.hash);
return;
end
end
elseif ispc
[status, output] = system(sprintf('certutil -hashfile "%s" MD5', filePath));
if status == 0
tokens = regexp(output, '([0-9A-Fa-f]{32})', 'tokens', 'once');
if ~isempty(tokens)
md5Hex = lower(tokens{1});
return;
end
end
end
import java.io.FileInputStream
import java.security.DigestInputStream
import java.security.MessageDigest
digest = MessageDigest.getInstance('MD5');
stream = DigestInputStream(FileInputStream(filePath), digest);
cleanupObj = onCleanup(@()stream.close()); %#ok<NASGU>
while stream.read() ~= -1
end
hashBytes = typecast(int8(digest.digest()), 'uint8');
md5Hex = lower(reshape(dec2hex(hashBytes)', 1, []));
end
function deleteTempFile(pathStr)
if exist(pathStr, 'file') == 2
delete(pathStr);
end
end
function downloadFile(url, destinationPath)
if isunix || ismac
[curlStatus, ~] = system('command -v curl');
if curlStatus == 0
command = sprintf('curl -L --fail --silent --show-error -o %s %s', ...
shellQuote(destinationPath), shellQuote(url));
status = system(command);
if status == 0
return;
end
end
end
websave(destinationPath, url, weboptions('Timeout', 900));
end
function quoted = shellQuote(pathStr)
quoted = ['''' strrep(pathStr, '''', '''"''"''') ''''];
end