118 lines
4.0 KiB
Python
Executable File
118 lines
4.0 KiB
Python
Executable File
import os
|
|
import subprocess
|
|
|
|
# Color codes for terminal output
|
|
class Colors:
|
|
HEADER = '\033[95m'
|
|
BLUE = '\033[94m'
|
|
GREEN = '\033[92m'
|
|
YELLOW = '\033[93m'
|
|
RED = '\033[91m'
|
|
ENDC = '\033[0m'
|
|
BOLD = '\033[1m'
|
|
|
|
def print_colored(message, color, bold=False):
|
|
"""
|
|
Print a colored message to the terminal
|
|
"""
|
|
if bold:
|
|
print(f"{Colors.BOLD}{color}{message}{Colors.ENDC}")
|
|
else:
|
|
print(f"{color}{message}{Colors.ENDC}")
|
|
|
|
def git_checkout_and_pull(repo_path):
|
|
"""
|
|
Switch to the 'develop' branch and pull the latest changes for the given repository.
|
|
"""
|
|
try:
|
|
# Change to the repository directory
|
|
os.chdir(repo_path)
|
|
print_colored(f"\n=== Processing repository: {repo_path} ===", Colors.HEADER, bold=True)
|
|
|
|
# Check if the directory is a git repository
|
|
if not os.path.exists('.git'):
|
|
print_colored(f"The folder {repo_path} is not a git repository!", Colors.RED)
|
|
return False
|
|
|
|
# Get the current branch
|
|
current_branch = subprocess.check_output(['git', 'branch', '--show-current']).decode().strip()
|
|
print_colored(f"Current branch: {current_branch}", Colors.BLUE)
|
|
|
|
# Check if the 'develop' branch exists
|
|
branches = subprocess.check_output(['git', 'branch', '-a']).decode()
|
|
if 'develop' not in branches and 'origin/develop' not in branches:
|
|
print_colored("The 'develop' branch does not exist!", Colors.RED)
|
|
return False
|
|
|
|
# Switch to the 'develop' branch
|
|
subprocess.run(['git', 'checkout', 'develop'], check=True)
|
|
print_colored("Switched to the 'develop' branch", Colors.GREEN)
|
|
|
|
# Pull the latest changes
|
|
subprocess.run(['git', 'pull'], check=True)
|
|
print_colored("Successfully pulled the latest changes", Colors.GREEN)
|
|
|
|
return True
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
print_colored(f"Error while working with git: {str(e)}", Colors.RED)
|
|
return False
|
|
except Exception as e:
|
|
print_colored(f"An unexpected error occurred: {str(e)}", Colors.RED)
|
|
return False
|
|
|
|
def load_repositories(config_file):
|
|
"""
|
|
Load repository paths from a configuration file.
|
|
"""
|
|
try:
|
|
with open(config_file, 'r') as file:
|
|
# Read all lines, strip whitespace, and ignore empty lines
|
|
repositories = [line.strip() for line in file if line.strip()]
|
|
return repositories
|
|
except FileNotFoundError:
|
|
print_colored(f"Configuration file '{config_file}' not found!", Colors.RED)
|
|
return []
|
|
except Exception as e:
|
|
print_colored(f"An error occurred while reading the configuration file: {str(e)}", Colors.RED)
|
|
return []
|
|
|
|
def main():
|
|
# Path to the configuration file
|
|
config_file = 'repos.txt'
|
|
|
|
# Load repository paths from the configuration file
|
|
print_colored("Loading repositories from configuration file...", Colors.BLUE)
|
|
repositories = load_repositories(config_file)
|
|
|
|
if not repositories:
|
|
print_colored("No repositories found in the configuration file. Exiting.", Colors.RED)
|
|
return
|
|
|
|
print_colored(f"Found {len(repositories)} repositories", Colors.GREEN)
|
|
|
|
# Save the original working directory
|
|
original_dir = os.getcwd()
|
|
|
|
# Process each repository
|
|
for repo in repositories:
|
|
if os.path.exists(repo):
|
|
success = git_checkout_and_pull(repo)
|
|
if success:
|
|
print_colored(f"Repository {repo} was successfully updated", Colors.GREEN, bold=True)
|
|
else:
|
|
print_colored(f"Failed to update repository {repo}", Colors.RED, bold=True)
|
|
else:
|
|
print_colored(f"Repository {repo} does not exist!", Colors.RED)
|
|
|
|
# Return to the original directory
|
|
os.chdir(original_dir)
|
|
|
|
# Print summary
|
|
print_colored("\n=== Summary ===", Colors.HEADER, bold=True)
|
|
print_colored("Script execution completed!", Colors.GREEN)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|