From c647e0c780d3ed820a0b44be4a369b3075344349 Mon Sep 17 00:00:00 2001 From: "martin.cholewa" Date: Mon, 31 Mar 2025 12:52:26 +0200 Subject: [PATCH] inicial commit --- repos.txt | 3 ++ script.py | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 repos.txt create mode 100755 script.py diff --git a/repos.txt b/repos.txt new file mode 100644 index 0000000..6c265a1 --- /dev/null +++ b/repos.txt @@ -0,0 +1,3 @@ +/Users/xchose/git/jUNIPER_AUTOMATiON +/Users/xchose/git/juniper_automation_dev04 +/Users/xchose/git/juniper_automaton_templates diff --git a/script.py b/script.py new file mode 100755 index 0000000..4df2c3f --- /dev/null +++ b/script.py @@ -0,0 +1,117 @@ +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() +