-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate python setup processes and testing them
- Loading branch information
1 parent
898285d
commit 00aae1e
Showing
5 changed files
with
195 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/>. | ||
%} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/>. | ||
%} |