provision: Source virtualenv for login shells conditionally.

This commit conditionally adds the source command for
virtualenv in the user's bash profile only when the
system is virtualized.

Checking WSL_DISTRO_NAME in the env vars
is the cheapest option in case of checking
whether the system runs as WSL VM since
it is an in memory operation.
Hence it is checked for first in `is_wsl_instace`.

Fixes #15029.
This commit is contained in:
apoorvapendse
2024-12-29 16:27:19 +05:30
committed by Tim Abbott
parent e65f3cf657
commit 5378464f87

View File

@@ -9,7 +9,9 @@
import argparse
import glob
import os
import pwd
import shutil
import subprocess
import sys
ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
@@ -91,6 +93,28 @@ def configure_rabbitmq_paths() -> list[str]:
return paths
def is_wsl_instance() -> bool:
if "WSL_DISTRO_NAME" in os.environ:
return True
with open("/proc/version") as file:
content = file.read().lower()
if "microsoft" in content:
return True
result = subprocess.run(["uname", "-r"], capture_output=True, text=True, check=True)
if "microsoft" in result.stdout.lower():
return True
return False
def is_vagrant_or_digitalocean_instance() -> bool:
user_id = os.getuid()
user_name = pwd.getpwuid(user_id).pw_name
return user_name in ["vagrant", "zulipdev"]
def setup_shell_profile(shell_profile: str) -> None:
shell_profile_path = os.path.expanduser(shell_profile)
@@ -106,7 +130,9 @@ def setup_shell_profile(shell_profile: str) -> None:
shell_profile_file.writelines(command + "\n")
source_activate_command = "source " + os.path.join(VENV_PATH, "bin", "activate")
write_command(source_activate_command)
# We want to activate the virtual environment for login shells only on virtualized systems.
if is_vagrant_or_digitalocean_instance() or is_wsl_instance():
write_command(source_activate_command)
if os.path.exists("/srv/zulip"):
write_command("cd /srv/zulip")