forked from plotly/plotly_matlab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotlyupdate.m
More file actions
365 lines (295 loc) · 13.3 KB
/
plotlyupdate.m
File metadata and controls
365 lines (295 loc) · 13.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
362
363
364
365
%-----AUTOMATIC PLOTLY MATLAB API UPDATING-----%
function plotlyupdate(varargin)
% plotlyupdate.m automatically updates the Plotly
% API MATLAB library if the current version is not
% up to date. The new version replaces all instances
% of the Plotly API MATLAB library in the users
% search path.
%successful update switch
success = true;
%check for verbose
verbose = any(strcmp(varargin,'verbose'));
%check for nocheck :)
nocheck = any(strcmp(varargin,'nocheck'));
%default output
exception.identifier = '';
exception.message = '';
%----check for necessary update----%
try
% local version number
pvLocal = plotly_version;
catch
exception.identifier = 'plotly:noVersion';
fprintf(['\n' exception.identifier '\n\nWe were unable to locate plotly_version.m. Please Add\n',...
'this script to your MATLAB search path and try again.\n\n']);
return
end
% remote Plotly API MATLAB Library url
remote = ['https://raw.githubusercontent.com/plotly/MATLAB-api/',...
'master/README.md'];
% remote Plotly API MATLAB Library
try
pvContent = urlread(remote);
catch
fprintf(['\nAn error occurred while trying to read the latest\n',...
'Plotly API MATLAB Library version number from:\n',...
'https://github.com/plotly/MATLAB-api.\n',...
'Please check your internet connection or\n',...
'contact chuck@plot.ly for further assistance.\n\n']);
return
end
% remote version number
[pvBounds(1), pvBounds(2)] = regexp(pvContent,'\d+.\d+.\d+', 'once');
pvRemote = pvContent(pvBounds(1):pvBounds(2));
%----check for local Plotly instances----%
try
plotlyScriptDirs = which('plotly.m','-all');
if isempty(plotlyScriptDirs);
error('plotly:missingScript',...
['\n\nWe were unable to locate plotly.m. Please Add this\n',...
'script to your MATLAB search path and try again.\n\n']);
end
catch exception
fprintf(['\n' exception.identifier exception.message]);
return
end
% plotly toolbox directory
plotlyToolboxDir = fullfile(matlabroot,'toolbox','plotly');
% find the location of all plotly/ directories
dircount = 1;
plotlyDirs = {};
for d = 1:length(plotlyScriptDirs)
%parse filepath string at the Plotly directory
plotlyLoc = strfind(fileparts(plotlyScriptDirs{d}),fullfile('MATLAB-api-master','plotly'));
plotlyToolboxLoc = strfind(fileparts(plotlyScriptDirs{d}),plotlyToolboxDir);
if ~isempty(plotlyLoc)
plotlyDirs{dircount} = fullfile(plotlyScriptDirs{d}(1:plotlyLoc-1),'MATLAB-api-master','plotly');
dircount = dircount + 1;
elseif ~isempty(plotlyToolboxLoc)
plotlyDirs{dircount} = plotlyToolboxDir;
dircount = dircount + 1;
end
end
if isempty(plotlyDirs)
error('It seems your plotly wrapper directory structure has changed. Update aborted.');
end
%----update if necessary----%
if strcmp(pvLocal,pvRemote)
fprintf(['\nYour Plotly API MATLAB Library v.' pvRemote ' is already up to date! \n\n'])
exception.identifier = 'plotly:alreadyUpdated';
return
else
if nocheck
fprintf('\n************************************************\n');
fprintf(['[UPDATING] Plotly v.' pvLocal ' ----> Plotly v.' pvRemote ' \n']);
fprintf('************************************************\n');
%----create temporary update folder----%
try
%temporary update folder location
plotlyUpdateDir = fullfile(pwd,['plotlyupdate_' pvRemote]);
if verbose
fprintf(['\nCreating temporary update directory: ' plotlyUpdateDir ' ... ']);
end
%----make plotlyUpdateDir----%
status = mkdir(plotlyUpdateDir);
if verbose
fprintf('Done! \n');
end
if (status == 0)
error('plotlyupdate:makeUpdateDir',...
['\n\nShoot! It looks like something went wrong while making',...
'the Plotly Update Directory. \n Please contact your system ',...
'admin. or chuck@plot.ly for more information. \n\n']);
end
catch exception
fprintf(['\n\n' exception.identifier exception.message]);
% update failed
success = false;
end
%----download plotly----%
if success
try
if verbose
fprintf(['Downloading the Plotly API Matlab Library v.' pvRemote ' ... ']);
end
newPlotlyUrl = 'https://github.com/plotly/MATLAB-api/archive/master.zip';
newPlotlyZip = fullfile(plotlyUpdateDir,['plotlyupdate_' pvRemote '.zip']);
%download from url
urlwrite(newPlotlyUrl,newPlotlyZip);
if verbose
fprintf('Done! \n');
end
catch
fprintf('\n\nAn error occured while downloading the newest version of Plotly\n\n');
% update failed
success = false;
end
end
%----unzip updated plotly----%
if success
try
if verbose
fprintf(['Unzipping the Plotly API Matlab Library v.' pvRemote ' ... ']);
end
unzip(newPlotlyZip,plotlyUpdateDir);
if verbose
fprintf('Done! \n');
end
catch exception
fprintf('\n\nAn error occured while unzipping the newest version of Plotly\n\n');
%update failed
success = false;
end
end
%----replace all instances of plotly----%
if success
try
if verbose
fprintf(['Updating the Plotly API Matlab Library v.' pvLocal ' ... ']);
end
% new Plotly directory
newPlotlyDir = fullfile(plotlyUpdateDir,'MATLAB-api-master','plotly');
% files in Plotly repo root
repoRoot = dir(fullfile(plotlyUpdateDir,'MATLAB-api-master'));
% files not to be included
repoExclude = {'.','..','.gitignore','plotly'};
% aux Plotly repo root files
d = 1;
for r = 1:length(repoRoot);
if(isempty(intersect(repoRoot(r).name,repoExclude)))
auxFiles{d} = fullfile(plotlyUpdateDir,'MATLAB-api-master',repoRoot(r).name);
d = d+1;
end
end
% remove old plotlyclean scripts
pcScripts = which('plotlycleanup.m','-all');
for d = 1:length(pcScripts)
% remove plotlycleanup path from searchpath
rmpath(fileparts(pcScripts{d}));
% delete plotlycleanup!
delete(pcScripts{d});
% add plotlycleanup path to searhpath
addpath(fileparts(pcScripts{d}));
end
% replace the old Plotly with the new Plotly
for d = 1:length(plotlyDirs)
% do not copy aux Plotly repo root files to toolbox dir. Plotly
if ~strcmp(plotlyDirs{d},plotlyToolboxDir)
% aux files destination
auxFileDest = fileparts(plotlyDirs{d});
% copy aux to appropriate destination
for r = 1:length(auxFiles)
copyfile(auxFiles{r},auxFileDest,'f');
end
end
% copy actual Plotly API Matlab Library
copyfile(newPlotlyDir,plotlyDirs{d},'f');
% add new scripts to path!
addpath(genpath(plotlyDirs{d}));
%rehash toolbox
rehash toolboxreset
end
if verbose
fprintf('Done! \n');
end
catch exception
fprintf(['\n\nAn error occured while updating to the newest version \n',...
'of Plotly v.' pvRemote '. Please check your write permissions\n',...
'for your outdated Plotly directories with your system admin.\n',...
'Contact chuck@plot.ly for more information.\n\n']);
% update failed
success = false;
end
end
%----clean up old Plotly wrapper scipts----%
if success
try
if verbose
fprintf('Cleaning up outdated Plotly API MATLAB library scripts ... ');
end
%run cleanup
removed = plotlycleanup;
if verbose
fprintf('Done! \n');
if ~isempty(removed)
fprintf('The following Plotly scripts were removed:\n');
% explicitly state the removed files
for r = 1:length(removed)
fprintf([removed{r} '\n']);
end
end
end
catch exception
fprintf([exception '\n\nAn error occured while cleaning up the outdated Plotly scripts. Please\n',...
'check your write permissions for your outdated Plotly directories with \n',...
'your system admin. Contact chuck@plot.ly for more information.\n\n']);
end
end
%----delete update directory-----%
if exist(plotlyUpdateDir,'dir')
try
if verbose
fprintf(['Removing temporary update directory: ' plotlyUpdateDir ' ... ']);
end
% delete temp update dir.
rmdir(plotlyUpdateDir,'s');
if verbose
fprintf('Done! \n');
end
catch exception
fprintf(['\n\n An error occured while attempting to remove',...
' the \n temporary update directory. Please remove manually.\n\n']);
end
end
%----successful update----%
if success
if exist(fullfile(matlabroot,'toolbox','plotly'),'dir')
fprintf('\n**************************************************\n');
fprintf(['[UPDATE SUCCESSFUL] visit: https://plot.ly/matlab \n',...
'or contact: chuck@plot.ly for further information.\n'...
'Please restart your MATLAB session so that the new\n',...
'Plotly API MATLAB Libary scripts can be recognized.\n']);
fprintf('**************************************************\n\n');
else
fprintf('\n**************************************************\n');
fprintf(['[UPDATE SUCCESSFUL] visit: https://plot.ly/matlab \n',...
'or contact: chuck@plot.ly for further information.\n']);
fprintf('**************************************************\n\n');
end
else
fprintf('\n***************************************************\n');
fprintf(['[UPDATE UNSUCCESSFUL] visit: https://plot.ly/matlab \n',...
'or contact: chuck@plot.ly for further information. \n']);
fprintf('***************************************************\n\n');
end
else %check
fprintf(['\nYou are about to update your Plotly API MATLAB Library v.' pvLocal ', which will\n',...
'overwrite modifications made to the Plotly scripts in the following directories:\n\n']);
% explicitly output directories
for d = 1:length(plotlyDirs)
fprintf([plotlyDirs{d} '\n']);
end
overwrite = input('\nProceed with update (y/n) ? : ','s');
if(strcmpi(overwrite,'y'))
if verbose
if nargout
plotlyupdate('verbose','nocheck');
else
plotlyupdate('verbose','nocheck');
end
else
if nargout
plotlyupdate('nocheck');
else
plotlyupdate('nocheck');
end
end
else
fprintf('\n***********************************************\n');
fprintf(['[UPDATE ABORTED] visit: https://plot.ly/matlab \n',...
'or contact: chuck@plot.ly for more information. \n']);
fprintf('***********************************************\n\n');
end
end
end
end