Skip to content

Commit

Permalink
Separate python setup processes and testing them
Browse files Browse the repository at this point in the history
  • Loading branch information
augustjohansson committed Jan 4, 2024
1 parent 898285d commit 00aae1e
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 62 deletions.
26 changes: 26 additions & 0 deletions Tests/TestExamples/TestPython.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
classdef TestPython < matlab.unittest.TestCase

properties(TestParameter)

module = {'checkLint', ...
'validateJsonScript'};

end

methods(Test)

function testSetupPythonExecutable(test)
setupPythonExecutable();
end

function testSetupPythonPath(test)
setupPythonPath();
end

function testLoadModule(test, module)
loadModule(module);
end

end

end
27 changes: 25 additions & 2 deletions Tests/runGitHubTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,33 @@

run('../startupBattMo.m')

%% Run tests

%% Run python setup tests
mrstVerbose 'on';
doTestPython = true;
stopOnError = false;

if doTestPython
suite = testsuite('TestPython');
runner = testrunner('textoutput');

if stopOnError
import matlab.unittest.plugins.StopOnFailuresPlugin
runner.addPlugin(StopOnFailuresPlugin)
end

results = runner.run(suite);

% Display results
t = table(results);
disp(t)

assertSuccess(results);
end

return

%% Run json file testing
mrstVerbose 'on';
doTestJsonFiles = true;
stopOnError = false;

Expand Down
77 changes: 17 additions & 60 deletions Utilities/JsonUtils/loadModule.m
Original file line number Diff line number Diff line change
@@ -1,71 +1,28 @@
function loadModule(modulename)
function loadModule(modulename, varargin)

% Setup python environment
if mrstPlatform('matlab')
penv = pyenv;

if penv.Version == ""

% Try to setup python
try

pyenv('Version', '/usr/bin/python3.10');

catch e

warning('Cannot load module %s because no python installation is found. Try setting it manually, for example as pyenv(''Version'', ''/usr/bin/python3.10'');. You may have to install the correct libpython package (eg. libpython3.10) separately. To find the path to the executable you may use ''which python3''.\n', modulename);
e.message
e.ExceptionObject

end

% Try to add directory to the python path
dirname = fullfile(battmoDir(), 'Utilities', 'JsonUtils');

if count(py.sys.path, dirname) == 0
dispif(mrstVerbose, 'Directory %s not found in py path, trying to insert it', dirname);
% Top level script for loading Python modules and (optionally) setup
% custom Python executable and custom Python paths).

insert(py.sys.path, int32(0), dirname);
if count(py.sys.path, dirname) == 0
insert(py.sys.path, int64(0), dirname);
end
if count(py.sys.path, dirname) == 0
py.sys.path.insert(int32(0), dirname);
end
if count(py.sys.path, dirname) == 0
py.sys.path.insert(int64(0), dirname);
end
opt = struct('setupPython', true, ...
'dir', fullfile(battmoDir(), 'Utilities', 'JsonUtils'));
[opt, extra] = merge_options(opt, varargin{:});

assert(count(py.sys.path, dirname))
end

% catch e
if mrstPlatform('matlab')

% warning('Could not add directory to Python path. This may be due to an incompability between the MATLAB and Python versions, or that the correct libpython package is installed (eg. libpython3.10). See also https://se.mathworks.com/support/requirements/python-compatibility.html.');
% e.message
% e.ExceptionObject
if opt.setupPython
setupPythonExecutable(extra{:});
setupPythonPath(opt.dir);
end

% end
try
py.importlib.import_module(modulename);
catch e
disp(e);
error('Failed to load module %s', modulename);
end

else
warning('Calling python is not supported on this platform (%s). Only MATLAB is supported.', mrstPlatform('platform'));
end

dispif(mrstVerbose, [char(py.sys.path), '\n']);

% Try to load module
try
py.importlib.import_module(modulename);
% mod = py.importlib.import_module(modulename);
% py.importlib.reload(mod);

catch e

warning('Failed to load module %s', modulename);
e.message
e.ExceptionObject

warning('Calling Python is not supported on this platform (%s). Only MATLAB is supported.', mrstPlatform('platform'));
end

end
Expand Down
63 changes: 63 additions & 0 deletions Utilities/JsonUtils/setupPythonExecutable.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
function ok = setupPythonExecutable(varargin)

% Low level tool for setting the Python executable.

opt = struct('executable', '');
opt = merge_options(opt, varargin{:});

if isempty(opt.executable)

% FIXME Base selection on MATLAB version and operating system.
% https://se.mathworks.com/support/requirements/python-compatibility.html

executables = {'/usr/bin/python3.10', '/usr/bin/python3.9'};

for k = 1:numel(executables)
ok = setExec(executables{k});
if ok
return;
end
end
else
ok = setExec(opt.executable);
end

assert(ok, 'Unable to set Python executable');

end

function ok = setExec(exec)

dispif(mrstVerbose, 'Trying to set Python executable to %s...\n', exec);

% Put in try/catch to allow for errors
try
pyenv('Version', exec);
dispif(mrstVerbose, 'Setting Python executable to %s\n', pyenv().Executable);
ok = true;
catch
dispif(mrstVerbose, 'Unable to set Python executable to %s\n', exec);
ok = false;
end

end

%{
Copyright 2021-2023 SINTEF Industry, Sustainable Energy Technology
and SINTEF Digital, Mathematics & Cybernetics.
This file is part of The Battery Modeling Toolbox BattMo
BattMo 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.
BattMo 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 BattMo. If not, see <http://www.gnu.org/licenses/>.
%}
64 changes: 64 additions & 0 deletions Utilities/JsonUtils/setupPythonPath.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
function setupPythonPath(dirs)

% Low level tool for adding directories to the Python path. The
% directories should be full paths.

if nargin == 0
dirs = fullfile(battmoDir(), 'Utilities', 'JsonUtils');
end

if ~iscell(dirs)
dirs = {dirs};
end

for k = 1:numel(dirs)

dirname = dirs{k};

if count(py.sys.path, dirname) == 0

dispif(mrstVerbose, 'Directory %s is not found in the Python path, trying to adding it...\n', dirname);

insert(py.sys.path, int32(0), dirname);

if count(py.sys.path, dirname) == 0
insert(py.sys.path, int64(0), dirname);
end

if count(py.sys.path, dirname) == 0
py.sys.path.insert(int32(0), dirname);
end

if count(py.sys.path, dirname) == 0
py.sys.path.insert(int64(0), dirname);
end

end

assert(count(py.sys.path, dirname), 'Unable to add directory %s to the Python path');

end

end



%{
Copyright 2021-2023 SINTEF Industry, Sustainable Energy Technology
and SINTEF Digital, Mathematics & Cybernetics.
This file is part of The Battery Modeling Toolbox BattMo
BattMo 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.
BattMo 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 BattMo. If not, see <http://www.gnu.org/licenses/>.
%}

0 comments on commit 00aae1e

Please sign in to comment.