2023-06-11 16:11:06 -04:00
|
|
|
import subprocess
|
2023-09-25 22:48:30 -04:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
new_extensions = set()
|
2023-06-11 16:11:06 -04:00
|
|
|
|
2023-06-16 18:00:37 -04:00
|
|
|
|
2023-06-11 16:11:06 -04:00
|
|
|
def clone_or_pull_repository(github_url):
|
2023-09-25 22:48:30 -04:00
|
|
|
global new_extensions
|
|
|
|
|
|
|
|
repository_folder = Path("extensions")
|
|
|
|
repo_name = github_url.rstrip("/").split("/")[-1].split(".")[0]
|
2023-06-11 16:11:06 -04:00
|
|
|
|
|
|
|
# Check if the repository folder exists
|
2023-09-25 22:48:30 -04:00
|
|
|
if not repository_folder.exists():
|
|
|
|
repository_folder.mkdir(parents=True)
|
2023-06-11 16:11:06 -04:00
|
|
|
|
2023-09-25 22:48:30 -04:00
|
|
|
repo_path = repository_folder / repo_name
|
2023-06-11 16:11:06 -04:00
|
|
|
|
|
|
|
# Check if the repository is already cloned
|
2023-09-25 22:48:30 -04:00
|
|
|
if repo_path.exists():
|
2023-07-03 23:03:30 -04:00
|
|
|
yield f"Updating {github_url}..."
|
2023-06-11 16:11:06 -04:00
|
|
|
# Perform a 'git pull' to update the repository
|
|
|
|
try:
|
|
|
|
pull_output = subprocess.check_output(["git", "-C", repo_path, "pull"], stderr=subprocess.STDOUT)
|
2023-07-03 23:03:30 -04:00
|
|
|
yield "Done."
|
2023-06-11 16:11:06 -04:00
|
|
|
return pull_output.decode()
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
return str(e)
|
|
|
|
|
|
|
|
# Clone the repository
|
|
|
|
try:
|
2023-07-03 23:03:30 -04:00
|
|
|
yield f"Cloning {github_url}..."
|
2023-06-11 16:11:06 -04:00
|
|
|
clone_output = subprocess.check_output(["git", "clone", github_url, repo_path], stderr=subprocess.STDOUT)
|
2023-09-25 22:48:30 -04:00
|
|
|
new_extensions.add(repo_name)
|
2024-06-22 20:40:25 -04:00
|
|
|
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 16:11:06 -04:00
|
|
|
return clone_output.decode()
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
return str(e)
|