File: check_matlab_path.m

package info (click to toggle)
dynare 4.5.7-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 49,408 kB
  • sloc: cpp: 84,998; ansic: 29,058; pascal: 13,843; sh: 4,833; objc: 4,236; yacc: 3,622; makefile: 2,278; lex: 1,541; python: 236; lisp: 69; xml: 8
file content (146 lines) | stat: -rw-r--r-- 5,754 bytes parent folder | download
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
function check_matlab_path(change_path_flag)

% Copyright (C) 2015-2017 Dynare Team
%
% This file is part of Dynare.
%
% Dynare 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 3 of the License, or
% (at your option) any later version.
%
% Dynare 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 Dynare.  If not, see <http://www.gnu.org/licenses/>.

if ~nargin || isempty(change_path_flag)
    change_path_flag = true;
end

% Get path to dynare/matlab folder.
DYNARE_PATH = strrep(which('dynare'),'dynare.m','');

if isempty(DYNARE_PATH)
    % Nothing to do here (this case should not happen)
    disp('dynare.m is not in the Matlab''s path.')
    return
else
    % Removes trailing slash.
    DYNARE_PATH = DYNARE_PATH(1:end-1);
end

% Get matlab path
MATLAB_PATH = path();
if isoctave
    % Octave always has '.' at the top of the path, so remove '.:'
    MATLAB_PATH = MATLAB_PATH(3:end);
end

% Position of DYNARE_PATH in MATLAB_PATH
idDYNARE = strfind(MATLAB_PATH,DYNARE_PATH);

if isempty(idDYNARE)
    disp('dynare.m is not in the Matlab''s path.')
    return
else
    if isequal(length(idDYNARE),1)
        if isequal(idDYNARE, 1)
            % Dynare is on top of matlab's path! Nothing to do here...
            return
        else
            str0 = sprintf('Dynare is not on top of matlab''s path!');
            % Check that this will not create a problem
            MATLAB_PATH_ = path2cell(MATLAB_PATH);
            DYNARE_ROUTINES = getallroutinenames(DYNARE_PATH, getalldirectories(DYNARE_PATH));
            MATLAB_ROUTINES = {};
            for i=1:position(idDYNARE, MATLAB_PATH)
                TMP_MATLAB_ROUTINES = getallroutinenames(MATLAB_PATH_{i});
                MATLAB_ROUTINES = { MATLAB_ROUTINES{:}  TMP_MATLAB_ROUTINES{:} };
            end
            COMMON_ROUTINES = intersect(MATLAB_ROUTINES, DYNARE_ROUTINES);
            if ~isempty(COMMON_ROUTINES)
                warning off backtrace
                skipline()
                if length(COMMON_ROUTINES)==1
                    warning(sprintf('%s This can cause problems because the Dynare version of %s will be overriden.', str0, COMMON_ROUTINES{1}));
                else
                    str1 = repmat('%s, ', 1, length(COMMON_ROUTINES)-1);
                    str2 = 'and %s ';
                    str3 = sprintf(['%s This can cause problems because the Dynare versions of ' str1, str2, 'will be overriden.'], str0, COMMON_ROUTINES{:});
                    warning(str3);
                end
                if change_path_flag
                    skipline()
                    msg = sprintf('I put %s on top of your matlab''s path. Note that this is a', DYNARE_PATH);
                    msg = sprintf(' %s a temporary change (ie will not affect future matlab''s session).', msg);
                    msg = sprintf(' %s If the ordering was intentional, ie if you really want to override the routines distributed with Dynare,', msg);
                    msg = sprintf(' %s you can change this behaviour using option nopathchange (see the reference manual).', msg);
                    warning(msg);
                    skipline()
                    rmpath(DYNARE_PATH)
                    addpath(DYNARE_PATH)
                end
                warning on backtrace
            end
        end
    else
        % Check that the user did not put all the subfolders in the path.
        % => If DYNARE_PATH/qz is in the path while mjdgges dll is available
        % it most likely means that user wrongly put all subfolders in the
        % matlab's path!
        mexpath = add_path_to_mex_files([DYNARE_PATH filesep], false);
        MATLAB_PATH = path2cell(MATLAB_PATH);
        for i=1:length(mexpath)
            if exist([mexpath{i} filesep 'mjdgges.' mexext],'file') && ismember([DYNARE_PATH filesep 'qz'],MATLAB_PATH)
                msg = sprintf(['You  put all the dynare/matlab subfolders in matlab''s path! Only ' ...
                               'the dynare/matlab folder (without subfolders)\nshould be in the ' ...
                               'path, Dynare will automatically add any required subfolders in the ' ...
                               'path.']);
                error(msg)
            end
        end
    end
end

function q = path2cell(p)
% Converts the output of path() to a cell
s = strfind(p,pathsep);
n = length(s)+1;
q = cell(n,1);
q(1) = {p(1:s(1)-1)};
q(n) = {p(s(end)+1:end)};
for i=2:n-1
    q(i) = {p(s(i-1)+1:s(i)-1)};
end

function flist = getallroutinenames(p, excludedsubfolders)
if nargin<2
    excludedsubfolders = {};
end
flist={};
%get m-files in this directory
dd = dir([p,filesep '*.m']);
temp=struct2cell(dd);
flist=[flist temp(1,:)];
%deal with subdirectories
dlist=getalldirectories(p,excludedsubfolders); %first call with excluded directories
for ii=1:length(dlist)
    flist=[flist getallroutinenames([ p filesep dlist{ii}])]; %recursive calls without subfolders
end


function dlist = getalldirectories(p,excluded_directories)
if nargin<2
    excluded_directories = {};
end
dd = dir(p);
dir_result=struct2cell(dd);
directory_indicator=cell2mat(dir_result(4,:));
dlist = dir_result(1,directory_indicator==1 & ~strcmp('.',dir_result(1,:)) & ~strcmp('..',dir_result(1,:)) & ~ismember(dir_result(1,:),excluded_directories));

function n = position(i, currentpath)
n = length(strfind(currentpath(1:i), pathsep));