#!/usr/bin/python """ This script is meant to be run in pkgs01.stg or pkgs02 (ie the dist-git servers) and will be adding the specified new git branch to the specified git repositories. It takes two arguments: gitbranch - a branch that needed to be created. inputfile - file with list of packages in rpm/package_name format Example usage: $ sudo -u pagure python branch_packages.py f42 """ import argparse import os import subprocess import requests _base_path = '/srv/git/repositories' def _get_arguments(): """ Set and retrieve the command line arguments. """ parser = argparse.ArgumentParser(description=__doc__) parser.add_argument( 'gitbranch', help='The new git branch to create') parser.add_argument( 'inputfile', help='The input file listing the repositories to update') return parser.parse_args() def does_package_has_branch(package, branch): endpoint = f"https://src.fedoraproject.org/api/0/{package}/git/branches" response = requests.get(endpoint) if response.status_code != 200: raise Exception(f"Error getting branch {package}: {response.status_code}") try: branches = response.json()["branches"] except Exception as e: raise Exception(f"Error getting branches of {package}: {e}") if branch in branches: return True return False def does_package_retired(package): endpoint = f"https://src.fedoraproject.org/api/0/{package}/tree" response = requests.get(endpoint) if response.status_code != 200: raise Exception(f"Error getting package {package}: {response.status_code}") try: content = response.json()["content"] content_count = len(content) if content_count == 1: file = content[0]["name"] if file == "dead.package": return True return False except Exception as e: print(f"Error getting package {package} data: {e}") def create_git_branch(path, gitbranch): """ Create the specified git branch in the specified git repository. """ if not os.path.isdir(path): print(' ERROR: %s does not appear to be a directory' % path) return None cmd = ['git', 'branch', gitbranch] return subprocess.check_output( cmd, stderr=subprocess.STDOUT, shell=False, cwd=path) def main(): args = _get_arguments() if not os.path.isfile(args.inputfile): print('%s does not appear to be a file' % args.inputfile) return 1 with open(args.inputfile) as stream: data = [row.strip() for row in stream if '+' in row] if not data: print('%s appears to be empty' % args.inputfile) return 1 branch = args.gitbranch for entry in sorted(data): if entry.startswith('rpm/'): entry = 'rpms/%s' % entry[4:] else: continue print("Processing %s" % entry) if not does_package_has_branch(entry, branch): if not does_package_retired(entry): path = os.path.join(_base_path, '%s.git' % entry) print('Processing %s' % path) try: create_git_branch(path, args.gitbranch, args.branch_from) except subprocess.CalledProcessError as err: print( ' ERROR: %s failed to branch, return code: %s\n %s' % ( entry, err.returncode, err.output)) return 0 if __name__ == "__main__": main()