text-generation-webui/modules/github.py

39 lines
1.3 KiB
Python
Raw Normal View History

2023-06-11 22:11:06 +02:00
import subprocess
2023-09-26 04:48:30 +02:00
from pathlib import Path
new_extensions = set()
2023-06-11 22:11:06 +02:00
2023-06-11 22:11:06 +02:00
def clone_or_pull_repository(github_url):
2023-09-26 04:48:30 +02:00
global new_extensions
repository_folder = Path("extensions")
repo_name = github_url.rstrip("/").split("/")[-1].split(".")[0]
2023-06-11 22:11:06 +02:00
# Check if the repository folder exists
2023-09-26 04:48:30 +02:00
if not repository_folder.exists():
repository_folder.mkdir(parents=True)
2023-06-11 22:11:06 +02:00
2023-09-26 04:48:30 +02:00
repo_path = repository_folder / repo_name
2023-06-11 22:11:06 +02:00
# Check if the repository is already cloned
2023-09-26 04:48:30 +02:00
if repo_path.exists():
yield f"Updating {github_url}..."
2023-06-11 22:11:06 +02:00
# Perform a 'git pull' to update the repository
try:
pull_output = subprocess.check_output(["git", "-C", repo_path, "pull"], stderr=subprocess.STDOUT)
yield "Done."
2023-06-11 22:11:06 +02:00
return pull_output.decode()
except subprocess.CalledProcessError as e:
return str(e)
# Clone the repository
try:
yield f"Cloning {github_url}..."
2023-06-11 22:11:06 +02:00
clone_output = subprocess.check_output(["git", "clone", github_url, repo_path], stderr=subprocess.STDOUT)
2023-09-26 04:48:30 +02:00
new_extensions.add(repo_name)
yield f"The extension `{repo_name}` has been downloaded.\n\nPlease close the web UI completely and launch it again to be able to load it."
2023-06-11 22:11:06 +02:00
return clone_output.decode()
except subprocess.CalledProcessError as e:
return str(e)