|
|
import os, subprocess |
|
|
|
|
|
def clone_repo_if_not_exists(git_url, git_dir): |
|
|
""" |
|
|
Clones a Git repository if it doesn't already exist in the git_dir folder. |
|
|
""" |
|
|
home_dir = os.path.expanduser("~") |
|
|
models_dir = os.path.join(home_dir, git_dir) |
|
|
|
|
|
|
|
|
if git_url.endswith(".git"): |
|
|
git_name = git_url.split('/')[-1][:-4] |
|
|
else: |
|
|
git_name = git_url.split('/')[-1] |
|
|
|
|
|
repo_path = os.path.join(models_dir, git_name) |
|
|
|
|
|
if not os.path.exists(models_dir): |
|
|
os.makedirs(models_dir) |
|
|
print(f"Created directory: {models_dir}") |
|
|
|
|
|
if not os.path.exists(repo_path): |
|
|
print(f"Repository '{git_name}' not found in '{models_dir}'. Cloning from {git_url}...") |
|
|
try: |
|
|
subprocess.run(["git", "clone", git_url, repo_path], check=True) |
|
|
print(f"Successfully cloned '{git_name}' to '{repo_path}'.") |
|
|
except subprocess.CalledProcessError as e: |
|
|
print(f"Error cloning repository: {e}") |
|
|
except FileNotFoundError: |
|
|
print("Error: 'git' command not found. Please ensure Git is installed and in your PATH.") |
|
|
else: |
|
|
print(f"Repository '{git_name}' already exists at '{repo_path}'. Skipping clone.") |
|
|
|
|
|
|