forked from ParisNeo/lollms-webui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_script.py
159 lines (142 loc) · 6.55 KB
/
update_script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import os
import sys
import git
import subprocess
import argparse
from pathlib import Path
from ascii_colors import ASCIIColors, trace_exception
def show_error_dialog(message):
try:
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
root.withdraw() # Hide the main window
messagebox.showerror("Error", message)
root.destroy()
except:
ASCIIColors.error(message)
def run_git_pull():
try:
ASCIIColors.info("----------------> Updating the code <-----------------------")
repo = git.Repo(Path(__file__).parent)
origin = repo.remotes.origin
# Fetch the latest changes
origin.fetch()
# Check if there are any changes to pull
if repo.head.commit == origin.refs.main.commit:
ASCIIColors.success("Already up-to-date.")
# Discard local changes and force update
try:
repo.git.reset('--hard', 'origin/main')
repo.git.clean('-fd')
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)
# show_error_dialog(error_message)
print("Updating submodules")
try:
repo.git.submodule('update', '--init', '--recursive', '--force')
except Exception as ex:
error_message = f"Couldn't update submodules: {str(ex)}"
ASCIIColors.error(error_message)
# show_error_dialog(error_message)
try:
# Checkout the main branch on each submodule
for submodule in repo.submodules:
try:
submodule_repo = submodule.module()
submodule_repo.git.fetch('origin')
submodule_repo.git.reset('--hard', 'origin/main')
submodule_repo.git.clean('-fd')
ASCIIColors.success(f"Updated submodule: {submodule}.")
except Exception as ex:
print(f"Couldn't update submodule {submodule}: {str(ex)}\nPlease report the error to ParisNeo either on Discord or on github.")
execution_path = Path(os.getcwd())
except Exception as ex:
error_message = f"Couldn't update submodules: {str(ex)}\nPlease report the error to ParisNeo either on Discord or on github."
ASCIIColors.error(error_message)
# show_error_dialog(error_message)
try:
# Update lollms_core
ASCIIColors.info("Updating lollms_core")
lollms_core_path = execution_path / "lollms_core"
if lollms_core_path.exists():
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)
ASCIIColors.success("Successfully updated lollms_core")
else:
ASCIIColors.warning("lollms_core directory not found")
except Exception as ex:
error_message = f"Couldn't update submodules: {str(ex)}"
ASCIIColors.error(error_message)
# show_error_dialog(error_message)
return True
except Exception as e:
error_message = f"Error during git operations: {str(e)}"
ASCIIColors.error(error_message)
# show_error_dialog(error_message)
return False
def get_valid_input():
while True:
update_prompt = "New code is updated. Do you want to update the requirements? (y/n): "
user_response = input(update_prompt).strip().lower()
if user_response in ['y', 'yes']:
return 'yes'
elif user_response in ['n', 'no']:
return 'no'
else:
print("Invalid input. Please respond with 'y' or 'n'.")
def install_requirements():
try:
# Get valid input from the user
user_choice = get_valid_input()
# Enhance the text based on the user's response
if user_choice == 'yes':
enhanced_text = "Great choice! Updating requirements ensures your project stays up-to-date with the latest changes."
print(enhanced_text)
subprocess.check_call([sys.executable, "-m", "pip", "install", "--upgrade", "-r", "requirements.txt"])
subprocess.check_call([sys.executable, "-m", "pip", "install", "--upgrade", "-e", "lollms_core"])
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)
except subprocess.CalledProcessError as e:
error_message = f"Error during pip install: {str(e)}"
ASCIIColors.error(error_message)
show_error_dialog(error_message)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--repo", type=str, default="https://github.com/ParisNeo/lollms-webui.git", help="Path to the Git repository")
args = parser.parse_args()
repo_path = args.repo
# Perform git pull to update the repository
if run_git_pull():
# Install the new requirements
install_requirements()
# 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)
else:
error_message = "Update process failed. Please check the console for more details."
ASCIIColors.error(error_message)
show_error_dialog(error_message)
sys.exit(1)
if __name__ == "__main__":
main()