pw_env_setup: Don't clear venv directory

Bug: 287
Change-Id: Iaa28ae0e3f2c853b15ea9189eb2c562a69fb1104
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/23501
Pigweed-Auto-Submit: Rob Mohr <mohrr@google.com>
Commit-Queue: Auto-Submit <auto-submit@pigweed.google.com.iam.gserviceaccount.com>
Reviewed-by: Wyatt Hepler <hepler@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 c864836..774962a 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
@@ -127,6 +127,16 @@
         print('=' * 60, file=sys.stderr)
         return False
 
+    # The bin/ directory is called Scripts/ on Windows. Don't ask.
+    venv_bin = os.path.join(venv_path, 'Scripts' if os.name == 'nt' else 'bin')
+
+    # Delete activation scripts. Typically they're created read-only and venv
+    # will complain when trying to write over them fails.
+    if os.path.isdir(venv_bin):
+        for entry in os.listdir(venv_bin):
+            if entry.lower().startswith('activate'):
+                os.unlink(os.path.join(venv_bin, entry))
+
     pyvenv_cfg = os.path.join(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
@@ -137,11 +147,9 @@
         if '__PYVENV_LAUNCHER__' in envcopy:
             del envcopy['__PYVENV_LAUNCHER__']
 
-        cmd = (python, '-m', 'venv', '--clear', venv_path)
+        cmd = (python, '-m', 'venv', '--upgrade', venv_path)
         _check_call(cmd, env=envcopy)
 
-    # The bin/ directory is called Scripts/ on Windows. Don't ask.
-    venv_bin = os.path.join(venv_path, 'Scripts' if os.name == 'nt' else 'bin')
     venv_python = os.path.join(venv_bin, 'python')
 
     pw_root = os.environ.get('PW_ROOT')