2023-05-25 09:50:42 +00:00
|
|
|
import os
|
|
|
|
import subprocess
|
2023-05-26 08:59:39 +00:00
|
|
|
import yaml
|
2023-05-25 09:50:42 +00:00
|
|
|
|
|
|
|
class GitCloner:
|
|
|
|
def __init__(self, directory):
|
|
|
|
self.directory = directory
|
|
|
|
|
2023-05-25 09:55:35 +00:00
|
|
|
def clone_repo(self, repo_url, branch='main'):
|
2023-05-25 09:50:42 +00:00
|
|
|
repo_name = repo_url.split('/')[-1].split('.')[0]
|
|
|
|
repo_path = os.path.join(self.directory, repo_name)
|
|
|
|
|
|
|
|
if os.path.exists(repo_path):
|
|
|
|
print(f"Repository '{repo_name}' already exists. Skipping cloning.")
|
|
|
|
return
|
|
|
|
|
|
|
|
command = ['git', 'clone', '--branch', branch, repo_url, repo_path]
|
|
|
|
try:
|
|
|
|
subprocess.check_output(command)
|
2023-05-26 08:59:39 +00:00
|
|
|
print(f"Successfully cloned repository '{repo_name}' on branch '{branch}'.")
|
2023-05-25 09:50:42 +00:00
|
|
|
except subprocess.CalledProcessError as e:
|
2023-05-26 08:59:39 +00:00
|
|
|
print(f"Failed to clone repository '{repo_name}' on branch '{branch}': {e}")
|
|
|
|
|
2023-05-25 09:50:42 +00:00
|
|
|
|
|
|
|
# Usage example
|
|
|
|
cloner = GitCloner('./src')
|
2023-05-25 09:54:07 +00:00
|
|
|
|
2023-05-26 08:59:39 +00:00
|
|
|
# Load repository URLs from YAML file
|
|
|
|
with open('repos.yaml', 'r') as file:
|
|
|
|
repos_data = yaml.safe_load(file)
|
2023-05-26 08:56:14 +00:00
|
|
|
|
|
|
|
# Clone repositories
|
2023-05-26 08:59:39 +00:00
|
|
|
for repo_data in repos_data['repositories']:
|
|
|
|
repo_url = repo_data['url']
|
|
|
|
branch = repo_data.get('branch', 'main')
|
|
|
|
cloner.clone_repo(repo_url, branch)
|