Skip to content

Commit f5e6d81

Browse files
committed
update python environment parsing
1 parent 988c0f3 commit f5e6d81

File tree

1 file changed

+24
-35
lines changed

1 file changed

+24
-35
lines changed

src/pb_utils.cc

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,8 @@ GenerateUUID()
374374
return boost::uuids::to_string(uuid);
375375
}
376376

377-
// Helper function to parse environment variables from activation script
377+
// Helper function to get environment variables for Python virtual environments
378+
// Uses pattern-based derivation instead of parsing activation scripts
378379
std::map<std::string, std::string>
379380
ParseActivationScript(const std::string& activate_path)
380381
{
@@ -395,45 +396,33 @@ ParseActivationScript(const std::string& activate_path)
395396
}
396397
#endif
397398

398-
// Parse activation script for environment changes
399-
std::ifstream activate_file(activate_path);
400-
if (!activate_file.is_open()) {
401-
return env_vars; // Return current environment if can't read activation
402-
// script
399+
// Extract virtual environment root from activation script path
400+
std::string venv_path = activate_path;
401+
size_t bin_activate_pos = venv_path.find("/bin/activate");
402+
if (bin_activate_pos != std::string::npos) {
403+
venv_path = venv_path.substr(0, bin_activate_pos);
403404
}
404405

405-
std::string line;
406-
while (std::getline(activate_file, line)) {
407-
// Look for export statements or direct assignments
408-
if (line.find("export ") == 0) {
409-
// Handle: export VAR=value
410-
line = line.substr(7); // Remove "export "
411-
}
412-
413-
size_t eq_pos = line.find('=');
414-
if (eq_pos != std::string::npos && line[0] != '#') {
415-
std::string key = line.substr(0, eq_pos);
416-
std::string value = line.substr(eq_pos + 1);
406+
// Set standard virtual environment variables
407+
env_vars["VIRTUAL_ENV"] = venv_path;
408+
env_vars["VIRTUAL_ENV_PROMPT"] = "(" + venv_path + ")";
417409

418-
// Remove quotes if present
419-
if (value.size() >= 2 && ((value[0] == '"' && value.back() == '"') ||
420-
(value[0] == '\'' && value.back() == '\''))) {
421-
value = value.substr(1, value.size() - 2);
422-
}
423-
424-
// Handle variable substitution for common cases
425-
if (value.find("$PATH") != std::string::npos) {
426-
size_t pos = value.find("$PATH");
427-
value.replace(pos, 5, env_vars["PATH"]);
428-
}
429-
if (value.find("$LD_LIBRARY_PATH") != std::string::npos) {
430-
size_t pos = value.find("$LD_LIBRARY_PATH");
431-
value.replace(pos, 16, env_vars["LD_LIBRARY_PATH"]);
432-
}
410+
// Update PATH to include the virtual environment's bin directory
411+
std::string new_path = venv_path + "/bin";
412+
if (env_vars.find("PATH") != env_vars.end()) {
413+
new_path += ":" + env_vars["PATH"];
414+
}
415+
env_vars["PATH"] = new_path;
433416

434-
env_vars[key] = value;
435-
}
417+
// Update LD_LIBRARY_PATH to include the virtual environment's lib directory
418+
std::string new_lib_path = venv_path + "/lib";
419+
if (env_vars.find("LD_LIBRARY_PATH") != env_vars.end()) {
420+
new_lib_path += ":" + env_vars["LD_LIBRARY_PATH"];
436421
}
422+
env_vars["LD_LIBRARY_PATH"] = new_lib_path;
423+
424+
// Remove PYTHONHOME if it exists
425+
env_vars.erase("PYTHONHOME");
437426

438427
return env_vars;
439428
}

0 commit comments

Comments
 (0)