Spaces:
Running
on
Zero
Running
on
Zero
File size: 1,785 Bytes
4f97a9e 656efd6 4f97a9e d2efa73 |
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 35 36 37 38 39 40 41 42 43 44 45 46 |
import os, subprocess, shutil
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.")
def move_folder(source_path, destination_path):
"""
Moves a folder from source_path to destination_path.
If a folder already exists at destination_path, it will be replaced.
"""
home_dir = os.path.expanduser("~")
source_path = os.path.join(home_dir, source_path)
destination_path = os.path.join(home_dir, destination_path)
if shutil.os.path.exists(destination_path):
shutil.rmtree(destination_path) # Remove existing destination folder
shutil.copytree(source_path, destination_path) |