bobo.yang 7149ca0190 Modify DevChat installation command in install.py
- Changed the DevChat installation command to include the '--force' flag to ensure the installation proceeds even if DevChat is already installed.
2023-07-06 07:42:44 +08:00

82 lines
2.7 KiB
Python

import os
import subprocess
import sys
# replace python3 with sys.executable, we will do everything in the same envrionment
pythonCommand = sys.executable
print('Python command:', pythonCommand)
tools_dir = os.path.dirname(os.path.realpath(__file__))
def ensure_pip_installed():
print("install pip ...")
try:
subprocess.run([pythonCommand, "-m", "pip", "--version"], check=True)
return True
except Exception as e:
pass
try:
subprocess.run([pythonCommand, tools_dir + "/get-pip.py", "--force-reinstall"], check=True)
return True
except Exception as e:
print(e)
return False
def check_pipx_installed():
try:
subprocess.run([pythonCommand, "-m", "pipx", "--version"], check=True)
return True
except Exception as e:
return False
def install_pipx():
print("Installing pipx...")
try:
subprocess.run([pythonCommand, "-m", "pip", "install", "--user", "pipx", "--force-reinstall"], check=True)
print("pipx installed successfully.")
except subprocess.CalledProcessError as e:
print("Error installing pipx:", e, file=sys.stderr)
sys.exit(1)
def add_pipx_to_path():
print("Adding pipx to PATH...")
subprocess.run([pythonCommand, "-m", "pipx", "ensurepath"], check=True)
result = subprocess.run([pythonCommand, "-m", "pipx", "environment"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
pipx_path_line = [line for line in result.stdout.splitlines() if "PIPX_BIN_DIR" in line]
if pipx_path_line:
pipx_path = pipx_path_line[0].split('=')[-1].strip()
os.environ["PATH"] += os.pathsep + pipx_path
print("pipx path added to environment variables.")
else:
print("Error: Could not find pipx path in environment output.", file=sys.stderr)
sys.exit(1)
def install_devchat():
print("Installing devchat...")
try:
subprocess.run([pythonCommand, "-m", "pipx", "install", "devchat", "--force"], check=True)
print("devchat installed successfully.")
except subprocess.CalledProcessError as e:
print("Error installing devchat:", e, file=sys.stderr)
sys.exit(1)
def upgrade_devchat():
print("Upgrading devchat...")
try:
subprocess.run([pythonCommand, "-m", "pipx", "upgrade", "devchat"], check=True)
print("devchat upgraded successfully.")
except subprocess.CalledProcessError as e:
print("Error upgrading devchat:", e, file=sys.stderr)
sys.exit(1)
def main():
ensure_pip_installed()
if not check_pipx_installed():
install_pipx()
add_pipx_to_path()
install_devchat()
upgrade_devchat()
if __name__ == "__main__":
main()