lollms-webui/update_script.py

222 lines
7.4 KiB
Python
Raw Normal View History

2024-12-19 12:48:57 +00:00
import argparse
2023-07-21 13:29:08 +00:00
import os
import subprocess
2024-12-19 12:48:57 +00:00
import sys
2023-10-09 16:11:17 +00:00
from pathlib import Path
2024-08-29 06:40:46 +00:00
2024-12-19 12:48:57 +00:00
import git
2024-12-15 01:17:42 +00:00
import pipmaster as pm
2024-12-19 12:48:57 +00:00
from ascii_colors import ASCIIColors, trace_exception
2024-12-15 01:17:42 +00:00
if not pm.is_installed("PyQt5"):
pm.install("PyQt5")
2024-12-19 12:48:57 +00:00
2024-12-15 01:17:42 +00:00
import sys
2024-12-19 12:48:57 +00:00
2024-12-15 01:17:42 +00:00
from PyQt5.QtWidgets import QApplication, QMessageBox
2024-12-19 12:48:57 +00:00
2024-08-29 06:40:46 +00:00
def show_error_dialog(message):
2024-10-08 13:51:23 +00:00
try:
2024-12-15 01:17:42 +00:00
app = QApplication(sys.argv)
msg = QMessageBox()
msg.setIcon(QMessageBox.Critical)
msg.setText(message)
msg.setWindowTitle("Error")
msg.exec_()
2024-10-08 13:51:23 +00:00
except:
ASCIIColors.error(message)
2024-12-15 01:17:42 +00:00
2024-12-19 12:48:57 +00:00
2023-07-23 23:25:05 +00:00
def run_git_pull():
2023-07-21 13:29:08 +00:00
try:
2024-08-29 06:40:46 +00:00
ASCIIColors.info("----------------> Updating the code <-----------------------")
2024-12-19 12:48:57 +00:00
2023-10-09 16:11:17 +00:00
repo = git.Repo(Path(__file__).parent)
2023-07-21 13:29:08 +00:00
origin = repo.remotes.origin
2024-12-19 12:48:57 +00:00
2024-08-29 06:40:46 +00:00
# Fetch the latest changes
origin.fetch()
2024-12-19 12:48:57 +00:00
2024-08-29 06:40:46 +00:00
# Check if there are any changes to pull
if repo.head.commit == origin.refs.main.commit:
ASCIIColors.success("Already up-to-date.")
2024-12-19 12:48:57 +00:00
2024-08-29 06:40:46 +00:00
# Discard local changes and force update
try:
2024-12-19 12:48:57 +00:00
repo.git.reset("--hard", "origin/main")
repo.git.clean("-fd")
2024-08-29 06:40:46 +00:00
origin.pull()
ASCIIColors.success("Successfully updated the code.")
except git.GitCommandError as e:
error_message = f"Failed to update the code: {str(e)}"
ASCIIColors.error(error_message)
2024-09-03 07:39:03 +00:00
# show_error_dialog(error_message)
2024-08-29 06:40:46 +00:00
2023-10-16 06:55:02 +00:00
print("Updating submodules")
try:
2024-12-19 12:48:57 +00:00
repo.git.submodule("update", "--init", "--recursive", "--force")
2024-09-03 07:36:04 +00:00
except Exception as ex:
error_message = f"Couldn't update submodules: {str(ex)}"
ASCIIColors.error(error_message)
2024-09-03 07:39:03 +00:00
# show_error_dialog(error_message)
2024-09-03 07:36:04 +00:00
try:
2023-10-16 06:59:01 +00:00
# Checkout the main branch on each submodule
for submodule in repo.submodules:
2023-10-16 07:52:43 +00:00
try:
submodule_repo = submodule.module()
2024-12-19 12:48:57 +00:00
submodule_repo.git.fetch("origin")
submodule_repo.git.reset("--hard", "origin/main")
submodule_repo.git.clean("-fd")
2024-09-18 11:46:50 +00:00
ASCIIColors.success(f"Updated submodule: {submodule}.")
2023-10-16 07:52:43 +00:00
except Exception as ex:
2024-12-19 12:48:57 +00:00
print(
f"Couldn't update submodule {submodule}: {str(ex)}\nPlease report the error to ParisNeo either on Discord or on github."
)
2023-11-08 14:39:08 +00:00
execution_path = Path(os.getcwd())
2024-09-03 07:36:04 +00:00
except Exception as ex:
2024-09-18 18:34:54 +00:00
error_message = f"Couldn't update submodules: {str(ex)}\nPlease report the error to ParisNeo either on Discord or on github."
2024-09-03 07:36:04 +00:00
ASCIIColors.error(error_message)
2024-09-03 07:39:03 +00:00
# show_error_dialog(error_message)
2024-09-03 07:36:04 +00:00
try:
2024-08-29 06:40:46 +00:00
# Update lollms_core
ASCIIColors.info("Updating lollms_core")
lollms_core_path = execution_path / "lollms_core"
if lollms_core_path.exists():
2024-12-19 12:48:57 +00:00
subprocess.run(
["git", "-C", str(lollms_core_path), "fetch", "origin"], check=True
)
subprocess.run(
[
"git",
"-C",
str(lollms_core_path),
"reset",
"--hard",
"origin/main",
],
check=True,
)
subprocess.run(
["git", "-C", str(lollms_core_path), "clean", "-fd"], check=True
)
2024-08-29 06:40:46 +00:00
ASCIIColors.success("Successfully updated lollms_core")
else:
ASCIIColors.warning("lollms_core directory not found")
2024-12-19 12:48:57 +00:00
2023-10-16 07:00:37 +00:00
except Exception as ex:
2024-08-29 06:40:46 +00:00
error_message = f"Couldn't update submodules: {str(ex)}"
ASCIIColors.error(error_message)
2024-09-03 07:39:03 +00:00
# show_error_dialog(error_message)
2024-08-29 06:40:46 +00:00
2023-10-09 16:11:17 +00:00
return True
2024-08-29 06:40:46 +00:00
except Exception as e:
error_message = f"Error during git operations: {str(e)}"
ASCIIColors.error(error_message)
2024-09-03 07:39:03 +00:00
# show_error_dialog(error_message)
2023-10-09 16:11:17 +00:00
return False
2024-12-19 12:48:57 +00:00
2024-09-03 07:15:23 +00:00
def get_valid_input():
while True:
2024-12-19 12:48:57 +00:00
update_prompt = (
"New code is updated. Do you want to update the requirements? (y/n): "
)
2024-09-03 07:15:23 +00:00
user_response = input(update_prompt).strip().lower()
2024-12-19 12:48:57 +00:00
if user_response in ["y", "yes"]:
return "yes"
elif user_response in ["n", "no"]:
return "no"
2024-09-03 07:15:23 +00:00
else:
print("Invalid input. Please respond with 'y' or 'n'.")
2024-12-19 12:48:57 +00:00
2023-07-21 13:29:08 +00:00
def install_requirements():
try:
2024-09-03 07:15:23 +00:00
# Get valid input from the user
user_choice = get_valid_input()
# Enhance the text based on the user's response
2024-12-19 12:48:57 +00:00
if user_choice == "yes":
2024-09-03 07:15:23 +00:00
enhanced_text = "Great choice! Updating requirements ensures your project stays up-to-date with the latest changes."
print(enhanced_text)
2024-12-19 12:48:57 +00:00
subprocess.check_call(
[
sys.executable,
"-m",
"pip",
"install",
"--upgrade",
"-r",
"requirements.txt",
]
)
subprocess.check_call(
[
sys.executable,
"-m",
"pip",
"install",
"--upgrade",
"-e",
"lollms_core",
]
)
2024-09-03 07:15:23 +00:00
ASCIIColors.success("Successfully installed requirements")
else: # user_choice == 'no'
enhanced_text = "Understood. Skipping the requirements update. Make sure to update them later if needed."
print(enhanced_text)
2023-07-21 13:29:08 +00:00
except subprocess.CalledProcessError as e:
2024-08-29 06:40:46 +00:00
error_message = f"Error during pip install: {str(e)}"
ASCIIColors.error(error_message)
show_error_dialog(error_message)
2023-07-21 13:29:08 +00:00
2024-12-19 12:48:57 +00:00
2023-07-21 13:29:08 +00:00
def main():
parser = argparse.ArgumentParser()
2024-12-19 12:48:57 +00:00
parser.add_argument(
"--repo",
type=str,
default="https://github.com/ParisNeo/lollms-webui.git",
help="Path to the Git repository",
)
2023-07-21 13:29:08 +00:00
args = parser.parse_args()
repo_path = args.repo
# Perform git pull to update the repository
2024-08-29 06:40:46 +00:00
if run_git_pull():
# Install the new requirements
install_requirements()
2023-07-21 13:29:08 +00:00
2024-08-29 06:40:46 +00:00
# Reload the main script with the original arguments
temp_file = "temp_args.txt"
if os.path.exists(temp_file):
with open(temp_file, "r") as file:
args = file.read().split()
main_script = "app.py" # Replace with the actual name of your main script
os.system(f"{sys.executable} {main_script} {' '.join(args)}")
try:
os.remove(temp_file)
except Exception as e:
error_message = f"Couldn't remove temp file. Try to remove it manually.\nThe file is located here: {temp_file}\nError: {str(e)}"
ASCIIColors.warning(error_message)
else:
error_message = "Error: Temporary arguments file not found."
ASCIIColors.error(error_message)
show_error_dialog(error_message)
sys.exit(1)
2023-07-21 13:29:08 +00:00
else:
2024-12-19 12:48:57 +00:00
error_message = (
"Update process failed. Please check the console for more details."
)
2024-08-29 06:40:46 +00:00
ASCIIColors.error(error_message)
show_error_dialog(error_message)
2023-07-21 13:29:08 +00:00
sys.exit(1)
2024-12-19 12:48:57 +00:00
2023-07-21 13:29:08 +00:00
if __name__ == "__main__":
main()