forked from BUNPC/Homer3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetpaths.m
269 lines (225 loc) · 7.03 KB
/
setpaths.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
function setpaths(options)
%
% USAGE:
%
% setpaths
% setpaths(1)
% setpaths(0)
%
currdir = pwd;
try
warning('off','MATLAB:rmpath:DirNotFound');
appname = 'Homer3';
% Parse arguments
addremove = 1;
if ~exist('options','var')
options = '';
elseif isnumeric(options)
if options == 0
addremove = 0;
else
options = '';
end
end
% Add libraries on which Homer3 depends
addDependenciesSearchPaths()
% Create list of possible known similar apps that may conflic with current
% app
appNameExclList = {'Homer3','Homer2_UI','brainScape','ResolveCommonFunctions'};
appNameInclList = {'AtlasViewerGUI'};
exclSearchList = {'.git','.idea','Data','Docs','*_install','*.app','submodules'};
appThis = filesepStandard_startup(pwd);
appThisPaths = findDotMFolders(appThis, exclSearchList);
if addremove == 0
if ~isempty(which('deleteNamespace.m'))
deleteNamespace(appname);
end
removeSearchPaths(appThis);
return;
end
appExclList = {};
appInclList = {};
% Find all root folders of apps to exclude from search paths
for ii = 1:length(appNameExclList)
foo = which([appNameExclList{ii}, '.m'],'-all');
for jj = 1:length(foo)
p = filesepStandard_startup(fileparts(foo{jj}));
if pathscompare_startup(appThis, p)
continue
end
fprintf('Exclude paths for %s\n', p);
appExclList = [appExclList; p]; %#ok<AGROW>
end
end
% Find all root folders of apps to include in search paths
for ii = 1:length(appNameInclList)
foo = which([appNameInclList{ii}, '.m'],'-all');
for jj = 1:length(foo)
if jj > 1
p = filesepStandard_startup(fileparts(foo{jj}));
appExclList = [appExclList; p]; %#ok<AGROW>
fprintf('Exclude paths for %s\n', p);
else
p = filesepStandard_startup(fileparts(foo{jj}));
appInclList = [appInclList; p]; %#ok<AGROW>
fprintf('Include paths for %s\n', p);
end
end
end
% Remove all search paths for all other apps except for current one, to
% make sure that we use only search from the current app for download shared
% libraries (i.e, submodules).
for ii = 1:length(appExclList)
removeSearchPaths(appExclList{ii})
end
for ii = 1:length(appInclList)
removeSearchPaths(appInclList{ii})
end
addSearchPaths(appThisPaths);
if ~isempty(which('setNamespace.m'))
setNamespace(appname);
end
% Add back all search paths for all other apps except for current app
for ii = 1:length(appInclList)
% This app's path has already been added
if pathscompare_startup(appInclList{ii}, appThis)
continue;
end
foo = findDotMFolders(appInclList{ii}, exclSearchList);
addSearchPaths(foo);
end
if ~isempty(which('setpaths_proprietary.m'))
setpaths_proprietary(options);
end
warning('on','MATLAB:rmpath:DirNotFound');
catch ME
cd(currdir);
close all force;
fclose all;
rethrow(ME)
end
PrintSystemInfo([], appname)
cd(currdir);
% ---------------------------------------------------
function d = dependencies()
d = {
'DataTree';
'Utils';
};
% ---------------------------------------------------
function setpermissions(appPath)
if isunix() || ismac()
if ~isempty(strfind(appPath, '/bin')) %#ok<*STREMP>
fprintf(sprintf('chmod 755 %s/*\n', appPath));
files = dir([appPath, '/*']);
if ~isempty(files)
system(sprintf('chmod 755 %s/*', appPath));
end
end
end
% ----------------------------------------------------
function addSearchPaths(appPaths)
if ischar(appPaths)
p = genpath(appPaths);
if ispc
delimiter = ';';
else
delimiter = ':';
end
appPaths = str2cell(p, delimiter);
end
for kk = 1:length(appPaths)
if strfind(appPaths{kk}, '.git')
continue;
end
addpath(appPaths{kk}, '-end');
setpermissions(appPaths{kk});
end
fprintf('ADDED search paths for app %s\n', appPaths{1});
% ----------------------------------------------------
function removeSearchPaths(app)
p = path;
if ispc()
delimiter = ';';
elseif ismac() || isunix()
delimiter = ':';
end
[~,appname] = fileparts(fileparts(app));
r = version('-release');
msg = sprintf('Removing search paths for %s ...', appname);
h = waitbar(0, msg);
p = str2cell_startup(p, delimiter);
for kk = 1:length(p)
if mod(kk,100)==0
waitbar(kk/length(p), h);
end
if ~isempty(strfind(lower(p{kk}), 'matlab')) && ~isempty(strfind(p{kk}, r))
continue;
end
if ~isempty(strfind(filesepStandard_startup(p{kk}), app))
rmpath(p{kk});
end
end
close(h);
fprintf('REMOVED search paths for app %s\n', app);
% ----------------------------------------------------
function addDependenciesSearchPaths()
if exist([pwd, '/Utils/submodules'],'dir')
addpath([pwd, '/Utils/submodules'],'-end');
end
d = dependencies();
for ii = 1:length(d)
rootpath = findFolder(pwd, d{ii});
if ispathvalid_startup([rootpath, '/Shared'],'dir')
rootpath = [rootpath, '/Shared'];
end
if ~exist(rootpath,'dir')
fprintf('ERROR: Could not find required dependency %s\n', d{ii})
continue;
end
addSearchPaths(rootpath);
end
if exist([pwd, '/Utils/submodules'],'dir')
addpath([pwd, '/Utils/submodules'],'-end');
end
% -----------------------------------------------------------------------------
function [C,k] = str2cell(str, delimiters, options)
% Option tells weather to keep leading whitespaces.
% (Trailing whitespaces are always removed)
if ~exist('options','var')
options = '';
end
if ~strcmpi(options, 'keepblanks')
str = strtrim(str);
end
str = deblank(str);
if ~exist('delimiters','var') || isempty(delimiters)
delimiters{1} = sprintf('\n');
elseif ~iscell(delimiters)
foo{1} = delimiters;
delimiters = foo;
end
% Get indices of all the delimiters
k=[];
for kk=1:length(delimiters)
k = [k, find(str==delimiters{kk})];
end
j = find(~ismember(1:length(str),k));
% The following line seems to hurt performance a little bit. It was
% meant to preallocate to speed things up but it does not seem to do that.
% C = repmat({blanks(max(diff([k,length(str)])))}, length(k)+1, 1);
C = {};
ii=1; kk=1;
while ii<=length(j)
C{kk} = str(j(ii));
ii=ii+1;
jj=2;
while (ii<=length(j)) && ((j(ii)-j(ii-1))==1)
C{kk}(jj) = str(j(ii));
jj=jj+1;
ii=ii+1;
end
C{kk}(jj:end)='';
kk=kk+1;
end
C(kk:end) = [];