forked from automaticanalysis/automaticanalysis
-
Notifications
You must be signed in to change notification settings - Fork 2
/
dependency_analysis.m
116 lines (96 loc) · 3.38 KB
/
dependency_analysis.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
clear; clc; % Clear the screen before proceeding
% We need to be inside the toolbox to work on it
cd(fileparts(which('aa_ver5')))
toolboxPath = pwd;
fldrDir = genpath(toolboxPath);
addpath(fldrDir); % To add the path to this toolbox!
ind = 0;
Dependencies = [];
Dependencies.name = [];
Dependencies.deps = [];
Dependencies.depsI = [];
Dependencies.depsT = [];
Dependencies.number = [];
% Then recurse inside each directory until you run out of paths
while ~isempty(strtok(fldrDir, ':'))
% Get each of the directories made by gendir
[fldrCurr fldrDir] = strtok(fldrDir, ':');
% Get all .m files in this folder
D = dir(fullfile(fldrCurr, '*.m'));
for d = 1:length(D)
if ~strcmp(D(d).name, [mfilename '.m'])
ind = ind + 1;
Dependencies(ind).name = fullfile(fldrCurr, D(d).name);
end
end
end
%% Find out MATLAB Toolbox dependecies
h = waitbar(0,'Cheked for MATLAB Toolbox dependecies');
for ind = 1:numel(Dependencies)
clear deps
try
deps = dependencies.toolboxDependencyAnalysis({Dependencies(ind).name});
catch
deps = 'failed';
end
deps(strcmp(deps,'MATLAB')) = [];
deps(strcmp(deps,'Statistical Parametric Mapping')) = [];
deps(strcmp(deps,'TFCE Toolbox')) = [];
Dependencies(ind).depsT = deps;
waitbar(ind/numel(Dependencies),h);
end
close(h);
%% Find out dependency!
h = waitbar(0,'Cheked for file dependecies');
for ind = 1:length(Dependencies)
fprintf('\nWorking %d/%d: %s', ind, length(Dependencies), Dependencies(ind).name)
clear deps
deps = depfun(Dependencies(ind).name, '-quiet');
pause(0.0001);
for e = length(deps):-1:1
% If toolbox path is not in the dependency, ignore it!
if isempty(strfind(deps{e}, toolboxPath))
deps(e) = [];
elseif strcmp(deps{e}, Dependencies(ind).name)
deps(e) = [];
end
end
Dependencies(ind).deps = deps;
waitbar(ind/numel(Dependencies),h);
end
close(h);
%% Find out inverse dependency!
h = waitbar(0,'Cheked for inverse dependecies');
for ind = 1:length(Dependencies)
Dependencies(ind).number = 0;
Dependencies(ind).depsI = {};
for d = 1:length(Dependencies)
if d ~= ind
for e = 1:length(Dependencies(d).deps)
if strcmp(Dependencies(ind).name, Dependencies(d).deps{e})
Dependencies(ind).depsI(end+1) = {Dependencies(d).name};
Dependencies(ind).number = Dependencies(ind).number + 1;
end
end
end
end
waitbar(ind/numel(Dependencies),h);
end
close(h);
unused_deps = {};
fprintf('\n')
%% Find out any functions that are not used by anything!
h = waitbar(0,'Cheked for orphan functions');
for ind = 1:length(Dependencies)
[path name] = fileparts(Dependencies(ind).name);
if Dependencies(ind).number == 0 && ... % The number of uses of this function must be 0 in total...
isempty(strfind(name, 'aamod')) && ... % ...and it must not be a module...
isempty(strfind(name, 'aa_user')) % ... nor a user script
unused_deps{end+1} = Dependencies(ind).name;
fprintf([Dependencies(ind).name '\n']);
end
waitbar(ind/numel(Dependencies),h);
end
close(h);
%% Now find how many dependencies each file has...
save('Dependencies.mat', 'Dependencies', 'unused_deps');