pw_env_setup: Detect incompatible virtualenvs

Detect and remove incompatible virtualenvs before doing virtualenv
setup.

Change-Id: I7322835514e4d911c9cdd33b469cb531a5feba82
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/48200
Pigweed-Auto-Submit: Rob Mohr <mohrr@google.com>
Commit-Queue: Auto-Submit <auto-submit@pigweed.google.com.iam.gserviceaccount.com>
Reviewed-by: Keir Mierle <keir@google.com>
diff --git a/pw_env_setup/py/pw_env_setup/virtualenv_setup/install.py b/pw_env_setup/py/pw_env_setup/virtualenv_setup/install.py
index 555f462..9843734 100644
--- a/pw_env_setup/py/pw_env_setup/virtualenv_setup/install.py
+++ b/pw_env_setup/py/pw_env_setup/virtualenv_setup/install.py
@@ -19,6 +19,7 @@
 import glob
 import os
 import re
+import shutil
 import subprocess
 import sys
 import tempfile
@@ -105,6 +106,22 @@
     return matches
 
 
+def _check_venv(python, version, venv_path, pyvenv_cfg):
+    # Check if the python location and version used for the existing virtualenv
+    # is the same as the python we're using. If it doesn't match, we need to
+    # delete the existing virtualenv and start again.
+    if os.path.exists(pyvenv_cfg):
+        pyvenv_values = {}
+        with open(pyvenv_cfg, 'r') as ins:
+            for line in ins:
+                key, value = line.split(' = ', 1)
+                pyvenv_values[key] = value
+        if os.path.dirname(python) != pyvenv_values.get('home'):
+            shutil.rmtree(venv_path)
+        elif pyvenv_values.get('version') not in version:
+            shutil.rmtree(venv_path)
+
+
 def install(
         project_root,
         venv_path,
@@ -138,6 +155,9 @@
                 os.unlink(os.path.join(venv_bin, entry))
 
     pyvenv_cfg = os.path.join(venv_path, 'pyvenv.cfg')
+
+    _check_venv(python, version, venv_path, pyvenv_cfg)
+
     if full_envsetup or not os.path.exists(pyvenv_cfg):
         # On Mac sometimes the CIPD Python has __PYVENV_LAUNCHER__ set to
         # point to the system Python, which causes CIPD Python to create