mpociot / chatgpt-vscode

A VSCode extension that allows you to use ChatGPT
4.99k stars 365 forks source link

Wrote a script to automate the install of the plugin and the authentication #20

Open wanfuse123 opened 1 year ago

wanfuse123 commented 1 year ago

I wrote a script to install the plugin.

Unfortunately it has a permissions issue:

Anyway here is the script (at the risk of getting yelled at or being called a novice).

If anyone can add it to this project, and perhaps figure out what's wrong with the permissions issue.

I think it might be helpful. If not, just let me know:

!/usr/bin/env python3

import subprocess import os import random import webbrowser import subprocess import venv import logging

logging.basicConfig(level=logging.DEBUG)

class SetupChatGPT:

Added init method

 def __init__(self):
     pass

 # Added check_python_venv_pip method

 def check_python_venv_pip(self):
     # Check if python is installed
     try:
         subprocess.run(["python", "--version"], check=True)

     except subprocess.CalledProcessError:
         # python is not installed, so install it
         subprocess.run(["apt-get", "install", "python"], check=True)

         # Check if virtual environments are installed
         if venv is not None:
              # virtual environments are installed
              pass
         else:
              # virtual environments are not installed, so install them
              subprocess.run(
                  ["apt-get", "install", "python3-venv"], check=True)

              # Check if pip is installed
         try:
              subprocess.run(["pip", "--version"], check=True)
         except subprocess.CalledProcessError:
              # pip is not installed, so install it
              subprocess.run(["apt-get", "install", "pip"], check=True)

 # Added create_venv method

 def create_venv(self):
     # Create a virtual environment with a random name
     venv_name = f"setup-chatGPT-{random.randint(1, 1000000)}"

     # Use try/except block for error handling
     try:
         # Create the virtual environment
         os.system(f"python -m venv {venv_name}")

         # Check if the requirements.txt file exists
         if os.path.exists("requirements.txt"):
             # If it exists, install the required packages in the virtual
             # environment
             os.system(
                 f"source {venv_name}/bin/activate && pip install -r requirements.txt")
         else:
             # If it does not exist, print a warning message
             print(
                 "Warning: requirements.txt file not found. Skipping package installation.")

     except:
         print("An error occurred while creating the virtual environment.")
         # Handle the error here (e.g. log it, raise an exception, etc.)
         raise
     else:
         # If no error occurred, activate the virtual environment
         subprocess.run(["source", f"{venv_name}/bin/activate"], check=True)
         print(
             f"Successfully created and activated virtual environment: {venv_name}")
         return venv_name

  def start_venv(self, venv_name):
      # This function starts the virtual environment with the given name.

      # Check if the virtual environment is already activated
      if "VIRTUAL_ENV" in os.environ:
           # If it is already activated, prompt the user to restart it
           response = input(
           "Virtual environment is already activated. Do you want to restart it? (y/n) "
           )
           if response == "y":
                # Restart the virtual environment
                os.system("deactivate")
                os.system(f"source {venv_name}/bin/activate")
           elif response != "y":
                # If the user does not want to restart it, do nothing
                pass
           else:
                # If the virtual environment is not already activated, activate it
                os.system(f"source {venv_name}/bin/activate")

                # Check if the chatgpt package is installed in the virtual environment
      try:
           subprocess.run(["python", "-c", "import chatgpt"], check=True)

      except subprocess.CalledProcessError:
          # If the chatgpt package is not installed, install it
          os.system("pip install chatgpt")

          # Print a success message
          print("Successfully started virtual environment and installed chatgpt package.")

  def check_vscode(self):
     """
     This function checks to see if Visual Studio Code is installed.
     If it is not installed, it suggests a URL where someone can download it.

     In the above code, the check_vscode function uses the try/except block to
     check if the code command is available. If the code command is not available,
     it means that Visual Studio Code is not installed, and the function suggests
     a URL where the user can download it. Then, the function exits the program.
     This way, if the install_chatgpt_plugin function calls the check_vscode
     function and Visual Studio Code is not installed, the program will exit,
     and the user will not be able to install the ChatGPT VSCode plugin.

     """
     # Use try/except block to check if the "code" command is available
     try:
         # Run the "code" command to see if Visual Studio Code is installed
         os.system("code")
     except:
         # If the "code" command is not available, Visual Studio Code is not installed
         print(
             "Visual Studio Code is not installed. You can download it from: https://code.visualstudio.com/"
         )
         # Exit the program
         sys.exit()
     else:
         print("Visual Studio Code is installed.")

  def install_chatgpt_plugin(self):
     """
     In the above code, the install_chatgpt_plugin function calls the check_vscode function
     to check if Visual Studio Code is installed. If it is not installed, the check_vscode
     function suggests a URL where the user can download it. Then, the
     install_chatgpt_plugin function installs the ChatGPT VSCode plugin from the Visual Studio repository.
     """
     # Check if Visual Studio Code is installed
     self.check_vscode()

     # Use try/except block for error handling
     try:
         # Install the ChatGPT VSCode plugin from the Visual Studio repository
         os.system("code --install-extension Harshal-Mehta.chat-gpt")
     except:
         print(
             "An error occurred while installing the ChatGPT VSCode plugin."
         )
         # Handle the error here (e.g. log it, raise an exception, etc.)
         raise
     else:
         print("Successfully installed the ChatGPT VSCode plugin.")

  def start_login(self):
     """
     This function opens the ChatGPT website and starts the login process.
     """
     # Use try/except block for error handling
     try:
         # Open the ChatGPT website in the default web browser
         webbrowser.open("https://www.chatgpt.com/login")
     except:
         print("An error occurred while opening the ChatGPT website.")
         # Handle the error here (e.g. log it, raise an exception, etc.)
         raise
     else:
         print("Successfully opened the ChatGPT website.")

  if __name__ == "__main__":
       setup_chatgpt = SetupChatGPT()
       setup_chatgpt.check_python_venv_pip()

       # Call the create_venv() method and store the return value in a variable named venv_name
       venv_name = setup_chatgpt.create_venv()

       # Pass the venv_name variable as an argument to the start_venv() method
       setup_chatgpt.start_venv(venv_name)
       setup_chatgpt.check_vscode()
       setup_chatgpt.install_chatgpt_plugin()
       setup_chatgpt.start_login()
       setup_chatgpt.install_chatgpt()