File size: 1,248 Bytes
bb2d84c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)

    # Extract repository name from the Git URL
    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.")