-
Notifications
You must be signed in to change notification settings - Fork 86
/
InstallECGkit.m
447 lines (339 loc) · 17.3 KB
/
InstallECGkit.m
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
%% Installation script of ecg-kit
% Script to install this toolbox.
%
% ****************************************************************
% ** Check the pretty printed help documentation in help folder **
% ****************************************************************
%
% Example
%
% Installecg-kit()
%
% or
%
% Installecg-kit(false)
%
% to avoid checking online for updates.
%
% See also UnInstallecg-kit
%
% Author: Mariano Llamedo Soria
% <matlab:web('mailto:[email protected]','-browser') (email)>
% Version: 0.1 beta
% Birthdate: 01/09/2012
% Last update: 18/10/2014
% Copyright 2008-2015
%
function InstallECGkit(bIgnoreUpdate)
% MAC compatibility alert
% if( ismac() )
% disp_string_framed('*red', 'MAC is not fully supported at the moment');
% fprintf(1, 'If you think you can help, please contact us through the %s at the project %s.\n\n', ...
% '<a href = "matlab: web(''https://groups.google.com/forum/#!forum/ecg-kit-users'', ''-browser'' )">forum</a>', ...
% '<a href = "matlab: web(''http://marianux.github.io/ecg-kit/'', ''-browser'' )">ecg-kit web''s page</a>' );
% return
% end
backup_string = 'ecg-kit_backup';
if( nargin < 1 || ~islogical(bIgnoreUpdate) )
bIgnoreUpdate = false;
end
fprintf(1, 'Adding paths\n' );
%path related constants.
root_path = [fileparts(mfilename('fullpath')) filesep ];
%path related constants.
default_paths = { ...
[ root_path ]; ...
[ root_path 'help' filesep ]; ...
[ root_path 'common' filesep ]; ...
[ root_path 'common' filesep 'a2hbc' filesep ]; ...
[ root_path 'common' filesep 'a2hbc' filesep 'scripts' filesep ]; ...
[ root_path 'common' filesep 'export_fig' filesep ]; ...
[ root_path 'common' filesep 'wavedet' filesep ]; ...
[ root_path 'common' filesep 'plot2svg' filesep ]; ...
[ root_path 'common' filesep 'ppg' filesep ]; ...
[ root_path 'common' filesep 'prtools' filesep ]; ...
[ root_path 'common' filesep 'prtools_addins' filesep ]; ...
[ root_path 'common' filesep 'bin' filesep ]; ...
[ root_path 'common' filesep 'kur' filesep ]; ...
[ root_path 'common' filesep 'LIBRA' filesep ]; ...
};
cellfun(@(a)(addpath(a)), default_paths);
% get the correct command names in this architecture.
sys_cmds = sys_command_strings();
% ECGkit_URL_str = 'http://ecg-kit.readthedocs.org/en/master/';
ECGkit_URL_str = 'https://github.com/marianux/ecg-kit/releases';
% Version info.
fid = fopen([ root_path 'LatestVersion' ]);
if fid < 0
% version check not possible - force update message
this_release_str = 'v0.1.1 beta - 05/05/2015';
else
try
this_release_str = fgetl(fid);
fclose(fid);
catch MException
fclose(fid);
throw(MException);
end
end
% URL check for updates
% Check admin privileges
bAdminPrivs = HasAdminPrivs(false);
if( ~bAdminPrivs )
disp_string_framed('*red', 'No admin/root privileges');
cprintf('SystemCommands', 'To install tab-completion feature with the ecg-kit, it is suggested to run this script with administrator/root privileges.\nRun Matlab as administrator or ' );
cprintf('*red', 'answer "YES"' );
cprintf('SystemCommands', ' when the system asks for permission. Otherwise, tab completion will not be available.\n\n' );
end
% Check if the GUI is up and working
bUseDesktop = usejava('desktop');
bMatlab = isMatlab();
bOctave = isOctave();
if( bMatlab )
matlab_ver = ver('Matlab');
if( str2double(matlab_ver.Version) < 8 )
cprintf('Red', 'Check updates manually since the Matlab version is too old to do it automatically.\n' );
bIgnoreUpdate = true;
end
elseif( bOctave )
disp_string_framed('[1,0.5,0]', 'Octave is not fully supported');
cprintf('Red', 'Although you can find problems using the toolbox in Octave, I am trying to make it fully compatible. Please report any problem that you could find.\n\n' );
% Special functions not compatible with Matlab
% octave_files = { ...
% 'octave -ECGt.m' 'ECGtask.m' ...
% };
%
% for ii = 1:size(octave_files,1)
%
% octave_file_src = fullfile(root_path,'common', octave_files{ii,1});
% octave_file_dst = fullfile(root_path,'common', octave_files{ii,2});
%
% status = copyfile(octave_file_src, octave_file_dst, 'f');
%
% if( status == 0 )
%
% disp_string_framed('*Red', 'Installation Error' );
% fprintf(1, 'Failed copying "%s" -->> "%s".\n', octave_file_src, octave_file_dst );
% cellfun(@(a)(rmpath(a)), default_paths);
% return
%
% end
%
% end
end
if ~bIgnoreUpdate
fprintf(1, 'Checking for updates\n' );
URLstatus = 0;
if( bMatlab )
[strResponse, URLstatus] = urlread( ECGkit_URL_str,'TimeOut',5);
elseif( bOctave )
[ , URLstatus] = urlread( ECGkit_URL_str);
end
if( URLstatus == 1 )
aux_pattern_pre = '/marianux/ecg-kit/tree/';
aux_idx = strfind(strResponse, aux_pattern_pre);
if( ~isempty(aux_idx) )
aux_pattern_pos = '" ';
aux_idx2 = strfind(strResponse(aux_idx(1):end), aux_pattern_pos);
if( ~isempty(aux_idx2) )
online_latest_release_str = strResponse(aux_idx(1)+(length(aux_pattern_pre):(aux_idx2(1)-2)) );
if( islater(online_latest_release_str, this_release_str) )
disp_string_framed('*Red', sprintf('ecg-kit %s version available', online_latest_release_str) );
fprintf(1, 'Consider updating %s.\n', '<a href = "matlab: web(''http://marianux.github.io/ecg-kit/'', ''-browser'' )">here</a>' );
% dont abort installation, just warn.
% cellfun(@(a)(rmpath(a)), default_paths);
% return
end
end
end
end
end
fprintf(1, 'Compiling sources\n' );
%Check compilation of source MEX files
common_path = [ root_path 'common' filesep];
source_files = dir([ common_path '*.c'] );
lsource_files = length(source_files);
if(bOctave)
prev_folder = cd(common_path);
end
try
for ii = 1:lsource_files
[~, source_file_name] = fileparts( source_files(ii).name);
mex_file = dir([common_path source_file_name '.' mexext ]);
if( isempty(mex_file) || mex_file.datenum <= source_files(ii).datenum )
if( bMatlab )
eval(['mex -outdir ''' common_path ''' ''' [common_path source_files(ii).name] '''']);
elseif(bOctave)
eval(['mkoctfile --mex ''' [common_path source_files(ii).name] '''']);
end
end
end
catch MException
if( ismac() )
disp_string_framed(2, 'Mex files compilation failed' );
fprintf(2, '\nAs we don''t include binaries for MAC, you will have to ask for help in order tu debug this error, here is the cause:\n');
rethrow(MException);
else
if( bUseDesktop )
disp_string_framed('[1,0.5,0]', 'Mex files compilation failed' );
cprintf('[1,0.5,0]', '\nBut don''t worry, the kit includes several precompiled binaries for most architectures.\nTry the examples to check if yours is included. Down is the report error, use in case you want to ask for help in the forum:\n');
% report just in case
else
disp_string_framed(2, 'Mex files compilation failed' );
end
report = getReport(MException);
fprintf(2, '\n%s\n', report);
end
end
if(bOctave)
cd(prev_folder);
end
if( bUseDesktop || bOctave )
if( bMatlab )
% Tab completion of selected functions
% Modify the TC.xml and TC.xsd
fprintf(1, 'Installing tab-completion feature.\n' );
bTabInstallError = false;
fid = 0;
tcxml_filename = fullfile(matlabroot,'toolbox', 'local', 'TC.xml');
tcxsd_filename = fullfile(matlabroot,'toolbox', 'local', 'TC.xsd');
tmp_path = tempdir();
my_tcxml_filename = [tmp_path 'TC.xml' ];
my_tcxsd_filename = [tmp_path 'TC.xsd' ];
if( exist(tcxml_filename, 'file') && exist(tcxsd_filename, 'file'))
status = copyfile(tcxml_filename, my_tcxml_filename, 'f');
status = status & copyfile(tcxsd_filename, my_tcxsd_filename, 'f');
else
status = false;
end
if( status )
if( exist([tcxml_filename backup_string ], 'file') )
str_command = [];
else
str_command = [ sys_cmds.copy_command ' "' tcxml_filename '" "' tcxml_filename backup_string '"' sys_cmds.command_sep_str ];
end
if( ~exist([tcxsd_filename backup_string ], 'file') )
str_command = [ str_command sys_cmds.copy_command ' "' tcxsd_filename '" "' tcxsd_filename backup_string '"' sys_cmds.command_sep_str ];
end
try
last_binding = [];
bStartFound = false;
bFinishFound = false;
fid = fopen(my_tcxml_filename, 'r+');
if( fid > 0 )
while( ~feof(fid) && ~bFinishFound )
str_aux = fgetl(fid);
if( ~bStartFound && ~isempty(strfind( str_aux, '<!-- ecg-kit start -->')) )
bStartFound = true;
break
elseif( ~isempty(strfind( str_aux, '</TC>')) )
bFinishFound = true;
break
elseif( ~isempty(strfind( str_aux, '</binding>')) )
last_binding = ftell(fid);
end
end
if( ~bStartFound && bFinishFound && ~isempty(last_binding) )
fseek(fid, last_binding, 'bof');
our_fid = fopen( [ root_path 'common' filesep 'TC.xml_additions.xml' ] , 'r');
if( our_fid > 0 )
fwrite(fid, fread(our_fid, inf, '*uint8') );
fclose(our_fid);
end
end
fclose(fid);
else
bTabInstallError = true;
end
catch
if( fid > 0 )
fclose(fid);
end
bTabInstallError = true;
end
try
start_idx = [];
bStartFound = false;
fid = fopen(my_tcxsd_filename, 'r+');
if( fid > 0 )
while( ~feof(fid) )
last_binding = ftell(fid);
str_aux = fgetl(fid);
if( ~bStartFound && ~isempty(strfind( str_aux, '<!-- ecg-kit start')) )
bStartFound = true;
break
elseif( isempty(start_idx) && ~isempty(strfind( str_aux, '<xsd:pattern value="-[A-Za-z0-9]+" />')) )
fseek(fid, 0, 'bof');
str_aux_pre = rowvec(fread(fid, last_binding, '*char'));
str_aux = rowvec(fgetl(fid));
str_aux_post = rowvec(fread(fid, inf, '*char'));
fseek(fid, 0, 'bof');
fwrite(fid, [str_aux_pre sprintf('<!-- ecg-kit start\n') str_aux sprintf('\necg-kit end -->\n <xsd:pattern value="[-_A-Za-z0-9]+" />\n') str_aux_post], 'char' );
end
end
fclose(fid);
else
bTabInstallError = true;
end
catch
if( fid > 0 )
fclose(fid);
end
bTabInstallError = true;
end
if( bTabInstallError)
disp_string_framed('[1,0.5,0]', 'Could not setup tab completion');
cprintf('[1,0.5,0]', 'Some error happened during the tab completion installation, but don''t worry, this will not diminish the ecg-kit functionality.\nDo it manually or ask for help in the forum.\n' );
else
% apply changes
str_command = [ str_command ...
sys_cmds.copy_command ' "' my_tcxml_filename '" "' tcxml_filename '"' sys_cmds.command_sep_str ...
sys_cmds.copy_command ' "' my_tcxsd_filename '" "' tcxsd_filename '"'...
];
[status, ~] = system( str_command, '-runAsAdmin' );
if( status == 0 )
cprintf('*blue', 'Remember to restart Matlab for function''s "tab completion" changes to take effect.\n' );
else
disp_string_framed('[1,0.5,0]', 'Could not setup tab completion');
cprintf('[1,0.5,0]', 'Some error happened during the tab completion installation, but don''t worry, this will not diminish the ecg-kit functionality.\nDo it manually or ask for help in the forum.\n' );
end
end
end
end
bOk = savepath();
if( bOk == 0 )
fprintf(1, 'done !\n' );
disp_string_framed('*Blue', sprintf('ecg-kit for Matlab %s', this_release_str) );
str_aux = [ 'examples' filesep 'examples.m'];
if( bMatlab )
fprintf(1, '%s was correctly installed.\n\nYou can start reading the %s, or if you prefer, trying these %s.\nGo to the %s if you need help.\n', ...
'<a href = "matlab: web(''http://marianux.github.io/ecg-kit/'', ''-browser'' )">ecg-kit</a>', ...
'<a href = "matlab: web(''http://ecg-kit.readthedocs.org/en/master/'', ''-browser'' )">documentation</a>', ...
[ '<a href = "matlab: opentoline(' str_aux ',1)">examples</a>' ], ...
'<a href = "matlab: web(''https://groups.google.com/forum/#!forum/ecg-kit-users'', ''-browser'' )">forum</a>');
elseif( bOctave )
fprintf(1, [ 'ecg-kit was correctly installed.\n\nYou can start reading the documentation, or if you prefer, trying these examples.\nGo to the forum if you need help.\n\n' ...
'web-page: http://marianux.github.io/ecg-kit/\n', ...
'documentation: http://ecg-kit.readthedocs.org/en/master/\n', ...
'examples: .\' filesep 'examples\' filesep 'examples.m\n', ...
'forum: https://groups.google.com/forum/#!forum/ecg-kit-users\n' ]);
end
else
if( ispc() )
home_path = [getenv('HOMEDRIVE') getenv('HOMEPATH') ];
elseif( isunix() )
home_path = getenv('HOME');
elseif( ismac() )
home_path = getenv('HOME');
end
if( home_path(end) ~= filesep )
home_path = [home_path filesep];
end
bOk = savepath([home_path 'pathdef.m'] );
if( bOk ~= 0 )
disp_string_framed(2, 'Path Not Saved');
fprintf(2, 'Save path manually to install the ecg-kit permanently for future sessions.\n');
end
end
end
end