Skip to content
Snippets Groups Projects
gtPythonCommand.m 1.63 KiB
function [status, msg, ver] = gtPythonCommand(args, use_fable)
% GTPYTHONCOMMAND  Call python witht the proper command
%
%     [status, msg, ver] = gtPythonCommand(args[, use_fable])
%     ------------------------------------------------------------------------
%     INPUT:
%       args      = <string>  Argumants passed to the python invocation
%
%     OPTIONAL INPUT:
%       use_fable = <logical> Source fable configuration file before {false}
%
%     OUTPUT:
%       status    = <int>     System command return code
%       msg       = <string>  Python command output
%
%     OPTIONAL OUTPUT:
%       ver       = <int>     Version of python
%
%     Version 001 08-02-2013 by YGuilhem

% Default paramater value
if ~exist('use_fable', 'var') || isempty(use_fable)
    use_fable = false;
end
pythonExes = {'python', 'python26'};

% Loop over each python executables
ok = false;
for ii=1:length(pythonExes)
    pythonExe = pythonExes{ii};
    [status, msg] = system([pythonExe ' -V']);
    if ~status
        version = str2num(strrep(strrep(msg, 'Python ', ''), '.', ' '));
        if version(1) >= 2 && version(2) >= 6
            ok = true;
            break;
        end
    end
end

if ~ok
    gtError('gtPythonCommand:old_python_version', ...
        'Cannot find python > 2.6 on this machine!');
end

% Add fable settings if needed
fableSet = [];
if use_fable
    fableConfig = '/sware/exp/fable/bin/fable.bash';
    fableSet = ['[ -f ' fableConfig ' ] && . ' fableConfig ' >& /dev/null ; '];
end

% Run command
[status, msg] = system([fableSet pythonExe ' ' args]);

% Add version output if asked
if nargout > 2
    ver = version;
end

end % end of function