lollms-webui/update_script.py

84 lines
3.0 KiB
Python
Raw Normal View History

2023-07-21 13:29:08 +00:00
import os
import sys
import git
import subprocess
import argparse
2023-10-09 16:11:17 +00:00
from pathlib import Path
2023-11-08 14:39:08 +00:00
from ascii_colors import ASCIIColors, trace_exception
2023-07-21 13:29:08 +00:00
2023-07-23 23:25:05 +00:00
def run_git_pull():
2023-07-21 13:29:08 +00:00
try:
2023-10-09 16:11:17 +00:00
print("----------------> Updating the code <-----------------------")
2024-02-01 22:30:31 +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
origin.pull()
2023-10-16 06:55:02 +00:00
print("Updating submodules")
try:
2023-10-16 11:35:25 +00:00
repo.git.submodule('update', '--init')
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()
submodule_repo.git.checkout('main')
2023-10-16 11:37:05 +00:00
print(f"Checking out main from {submodule}")
2023-11-06 08:22:38 +00:00
submodule.repo.git.submodule('update', '--remote', submodule.name) # Force submodule update
print(f"Forcing update on {submodule}")
2023-11-07 07:58:23 +00:00
submodule_repo.git.checkout('main')
2023-10-16 11:37:05 +00:00
2023-10-16 07:52:43 +00:00
except Exception as ex:
print(f"Couldn't checkout module {submodule}")
2023-11-08 14:39:08 +00:00
execution_path = Path(os.getcwd())
# Clone the repository to the target path
ASCIIColors.info("Lollms_core found in the app space.\nPulling last lollms_core")
subprocess.run(["git", "-C", str(execution_path/"lollms_core"), "pull"])
2023-11-10 22:30:45 +00:00
subprocess.run(["git", "-C", str(execution_path/"utilities/safe_store"), "pull"])
2023-11-08 14:39:08 +00:00
2023-10-16 07:00:37 +00:00
except Exception as ex:
2023-10-16 06:55:02 +00:00
print("Couldn't update submodules")
2023-10-16 07:00:37 +00:00
print(ex)
2023-10-09 16:11:17 +00:00
return True
2023-07-21 13:29:08 +00:00
except git.GitCommandError as e:
print(f"Error during git pull: {e}")
2023-10-09 16:11:17 +00:00
return False
2023-07-21 13:29:08 +00:00
def install_requirements():
try:
2023-08-25 15:53:38 +00:00
subprocess.check_call(["pip", "install", "--upgrade", "-r", "requirements.txt"])
2023-10-20 15:02:56 +00:00
subprocess.check_call(["pip", "install", "--upgrade", "-e", "lollms_core"])
subprocess.check_call(["pip", "install", "--upgrade", "-e", "utilities/safe_store"])
2023-07-21 13:29:08 +00:00
except subprocess.CalledProcessError as e:
print(f"Error during pip install: {e}")
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
2023-07-23 23:25:05 +00:00
run_git_pull()
2023-07-21 13:29:08 +00:00
# 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"python {main_script} {' '.join(args)}")
os.remove(temp_file)
else:
print("Error: Temporary arguments file not found.")
sys.exit(1)
if __name__ == "__main__":
main()