Skip to content

Commit 36682dd

Browse files
authored
Fix SOFAPYTHON_PLUGINS_PATH env var with Windows (and add a warning error) (#349)
* Fix handling env var sp3pluginpath with windows * more info in the message * use sofapy3_plugins_path if both are set * fix compil
1 parent bfe1d91 commit 36682dd

1 file changed

Lines changed: 33 additions & 13 deletions

File tree

Plugin/src/SofaPython3/PythonEnvironment.cpp

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -238,20 +238,34 @@ void PythonEnvironment::Init()
238238
}
239239
}
240240

241-
/// Add the directories listed in the SOFAPYTHON_PLUGINS_PATH environnement
242-
/// variable (colon-separated) to sys.path
243-
char * pathVar = getenv("SOFAPYTHON_PLUGINS_PATH");
244-
if (pathVar != nullptr)
241+
/// Add the directories listed in the SOFAPYTHON3_PLUGINS_PATH environnement
242+
/// variable to sys.path
243+
const std::string envVarName = "SOFAPYTHON3_PLUGINS_PATH";
244+
const std::string deprecatedEnvVarName = "SOFAPYTHON_PLUGINS_PATH";
245+
std::string usedEnvVarName = envVarName;
246+
247+
char* deprecatedPathVar = getenv(deprecatedEnvVarName.c_str());
248+
char* pathVar = getenv(envVarName.c_str());
249+
250+
// case where only the deprecated env var is set
251+
if (pathVar != nullptr && deprecatedPathVar == nullptr)
245252
{
246-
std::istringstream ss(pathVar);
247-
std::string path;
248-
while(std::getline(ss, path, ':'))
249-
{
250-
if (FileSystem::exists(path))
251-
addPythonModulePathsFromDirectory(path);
252-
else
253-
msg_warning("SofaPython3") << "no such directory: '" + path + "'";
254-
}
253+
msg_deprecated("SofaPython3") << deprecatedEnvVarName << " environment variable is deprecated, use " << envVarName << " instead.";
254+
usedEnvVarName = "SOFAPYTHON_PLUGINS_PATH";
255+
}
256+
// case where both env vars are set
257+
else if (pathVar != nullptr && deprecatedPathVar != nullptr)
258+
{
259+
msg_deprecated("SofaPython3") << deprecatedEnvVarName << " and " << envVarName << " environment variables are both set.";
260+
msg_deprecated("SofaPython3") << deprecatedEnvVarName << " is deprecated, and only " << envVarName << " will be used.";
261+
}
262+
263+
sofa::helper::system::FileRepository pluginPathsRepository(envVarName.c_str());
264+
const auto& pluginPaths = pluginPathsRepository.getPaths();
265+
for (auto pluginPath : pluginPaths)
266+
{
267+
std::string cleanPath = FileSystem::cleanPath(pluginPath);
268+
addPythonModulePath(cleanPath);
255269
}
256270

257271
// Add sites-packages wrt the plugin
@@ -332,6 +346,12 @@ void PythonEnvironment::Release()
332346

333347
void PythonEnvironment::addPythonModulePath(const std::string& path)
334348
{
349+
if (!(FileSystem::exists(path) && FileSystem::isDirectory(path)))
350+
{
351+
msg_warning("SofaPython3") << "Could not add '" + path + "'" << " to sys.path (it does not exist or is not a directory)";
352+
return;
353+
}
354+
335355
PythonEnvironmentData* data = getStaticData() ;
336356
if ( data->addedPath.find(path)==data->addedPath.end())
337357
{

0 commit comments

Comments
 (0)