Update initialize.py

Update script to automate full cloning, update, building, sourcing process
This commit is contained in:
bjoernellens1 2023-05-26 11:05:04 +02:00 committed by GitHub
parent 298e8ad74f
commit 1cae0b1be7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,20 +6,49 @@ class GitCloner:
def __init__(self, directory): def __init__(self, directory):
self.directory = directory self.directory = directory
def clone_repo(self, repo_url, branch='main'): def clone_or_update_repo(self, repo_url, branch='main'):
repo_name = repo_url.split('/')[-1].split('.')[0] repo_name = repo_url.split('/')[-1].split('.')[0]
repo_path = os.path.join(self.directory, repo_name) repo_path = os.path.join(self.directory, repo_name)
if os.path.exists(repo_path): if os.path.exists(repo_path):
print(f"Repository '{repo_name}' already exists. Skipping cloning.") print(f"Repository '{repo_name}' already exists. Updating...")
return
command = ['git', 'clone', '--branch', branch, repo_url, repo_path]
try: try:
subprocess.check_output(command) # Change working directory to the repository path
os.chdir(repo_path)
# Pull the latest changes from the repository
subprocess.check_output(['git', 'pull'])
print(f"Successfully updated repository '{repo_name}'.")
except subprocess.CalledProcessError as e:
print(f"Failed to update repository '{repo_name}': {e}")
return
else:
try:
# Clone the repository
subprocess.check_output(['git', 'clone', '--branch', branch, repo_url, repo_path])
print(f"Successfully cloned repository '{repo_name}' on branch '{branch}'.") print(f"Successfully cloned repository '{repo_name}' on branch '{branch}'.")
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
print(f"Failed to clone repository '{repo_name}' on branch '{branch}': {e}") print(f"Failed to clone repository '{repo_name}' on branch '{branch}': {e}")
return
# Build the repository using colcon
try:
# Change working directory to the base directory
os.chdir(self.directory)
# Execute colcon build
subprocess.check_output(['colcon', 'build'])
print("Build completed successfully.")
# Execute "source install/setup.bash"
subprocess.check_call(['source', 'install/setup.bash'], shell=True)
print("Setup file sourced successfully.")
except subprocess.CalledProcessError as e:
print(f"Failed to build or source setup file: {e}")
# Usage example # Usage example
@ -29,8 +58,8 @@ cloner = GitCloner('./src')
with open('repos.yaml', 'r') as file: with open('repos.yaml', 'r') as file:
repos_data = yaml.safe_load(file) repos_data = yaml.safe_load(file)
# Clone repositories # Clone or update repositories and perform build and setup
for repo_data in repos_data['repositories']: for repo_data in repos_data['repositories']:
repo_url = repo_data['url'] repo_url = repo_data['url']
branch = repo_data.get('branch', 'main') branch = repo_data.get('branch', 'main')
cloner.clone_repo(repo_url, branch) cloner.clone_or_update_repo(repo_url, branch)