Skip to content

Commit

Permalink
#546: Remove hunting for Python venv in working directory and parents
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilMiller authored and mattw-nws committed Aug 8, 2023
1 parent ef8e201 commit 3d53cf0
Showing 1 changed file with 2 additions and 42 deletions.
44 changes: 2 additions & 42 deletions include/utilities/python/InterpreterUtil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,35 +225,6 @@ namespace utils {

protected:

/**
* Search for and return a recognized virtual env directory.
*
* Both ``.venv`` and ``venv`` will be recognized. Search locations are the current working directory, one
* level up (parent directory), and two levels up, with the first find being return.
*
* A Python ``None`` object is returned if no existing directory is found in the search locations.
*
* @return A Python Path object for a found venv dir, or a Python ``None`` object.
*/
py::object searchForVenvDir() {
py::object current_dir = Path.attr("cwd")();
std::vector<py::object> parent_options = {
current_dir,
current_dir.attr("parent"),
current_dir.attr("parent").attr("parent")
};
std::vector<std::string> dir_name_options = {".venv", "venv"};
for (py::object &parent_option : parent_options) {
for (const std::string &dir_name_option : dir_name_options) {
py::object venv_dir_candidate = parent_option.attr("joinpath")(dir_name_option);
if (py::bool_(venv_dir_candidate.attr("is_dir")())) {
return venv_dir_candidate;
}
}
}
return py::none();
}

/**
* Find any virtual environment site packages directory, starting from options under the current directory.
*
Expand All @@ -262,7 +233,7 @@ namespace utils {
py::list getVenvPackagesDirOptions() {
// Look for a local virtual environment directory also, if there is one
const char* env_var_venv = std::getenv("VIRTUAL_ENV");
py::object venv_dir = env_var_venv != nullptr ? Path(env_var_venv): searchForVenvDir();
py::object venv_dir = env_var_venv != nullptr ? Path(env_var_venv): py::none();

if (!venv_dir.is_none() && py::bool_(venv_dir.attr("is_dir")())) {
// Resolve the full path
Expand Down Expand Up @@ -303,18 +274,7 @@ namespace utils {
//std::string r(env_var_venv);
return std::string(env_var_venv);
}
py::object venv_dir;
venv_dir = searchForVenvDir();
if(venv_dir.is_none()){
return std::string("None");
}
if(py::bool_(venv_dir.attr("is_dir")())) {
// Probably better to not resolve symlinks so you can see where it was found...
//venv_dir = venv_dir.attr("resolve")();
return py::str(venv_dir);
}
assert("Unexpected value of venv_dir in InterpreterUtil::getDiscoveredVenvPath()!");
return ""; // silence warning
return std::string("None");
}

protected:
Expand Down

0 comments on commit 3d53cf0

Please sign in to comment.