From 3b887320a4fb957b1ae109f04a64fef3b5561a2a Mon Sep 17 00:00:00 2001 From: Theo Chatzimichos Date: Nov 12 2017 22:05:17 +0000 Subject: Merge branch 'tampakrap_get_formulas' into 'production' improvements for bin/get_formulas.py See merge request infra/salt!89 --- diff --git a/bin/get_formulas.py b/bin/get_formulas.py index b8a9db1..53e048f 100755 --- a/bin/get_formulas.py +++ b/bin/get_formulas.py @@ -8,6 +8,22 @@ import sys import yaml +def check_open_pull_requests(): + from github import Github + + g = Github() + for formula, data in FORMULAS.items(): + open_pull_requests = data.get('pending', []) + if open_pull_requests: + namespace = data.get('original_namespace', 'saltstack-formulas') + prefix = data.get('prefix', '') + org = g.get_organization(namespace) + for pull_request in open_pull_requests: + pr = int(pull_request.split('/')[-1]) + state = org.get_repo('%s%s-formula' % (prefix, formula)).get_pull(pr).state + print('%s is %s' % (pull_request, state)) + + def git(cmd, cwd=None, additional_env=None): # pygit2 is not available for python3 in Leap, use plain git instead @@ -22,7 +38,7 @@ def git(cmd, cwd=None, additional_env=None): sys.exit(status) -def clone_or_pull(DEST, SYMLINK=False): +def clone_or_pull(DEST): def use_git_to_clone_or_pull_repo(): if not os.path.exists(DEST): os.mkdir(DEST) @@ -50,8 +66,11 @@ def clone_or_pull(DEST, SYMLINK=False): opensuse_fork_url = 'https://gitlab.infra.opensuse.org/saltstack-formulas/%s-formula' % formula FULL_PATH = '%s/%s-formula' % (DEST, formula) use_git_to_clone_or_pull_repo() - if SYMLINK: - os.symlink('%s/%s' % (FULL_PATH, formula), '/srv/salt/%s' % formula) + + +def create_symlinks(DEST): + for formula in FORMULAS.keys(): + os.symlink('%s/%s-formula/%s' % (DEST, formula, formula), '/srv/salt/%s' % formula) def enable_remote(REMOTE, DEST): @@ -61,47 +80,34 @@ def enable_remote(REMOTE, DEST): git(['checkout', '-qB', 'master', '%s/master' % REMOTE], cwd=FULL_PATH) -def check_open_pull_requests(): - from github import Github - - g = Github() - for formula, data in FORMULAS.items(): - open_pull_requests = data.get('pending', []) - if open_pull_requests: - namespace = data.get('original_namespace', 'saltstack-formulas') - prefix = data.get('prefix', '') - org = g.get_organization(namespace) - for pull_request in open_pull_requests: - pr = int(pull_request.split('/')[-1]) - state = org.get_repo('%s%s-formula' % (prefix, formula)).get_pull(pr).state - print('%s is %s' % (pull_request, state)) - - with open('FORMULAS.yaml', 'r') as f: FORMULAS = yaml.load(f) parser = argparse.ArgumentParser(description='Loads the formulas from FORMULAS.yaml and optionally clones them in a specified destination. Optionally it can also create a symlink from the cloned path to /srv/salt, useful for the CI worker. The internal gitlab fork will also be added as secondary remote.') +parser.add_argument('-p', '--pull-requests', action='store_true', help='Prints the status of the Pull Requests that are defined in FORMULAS.yaml under "pending".') +parser.add_argument('-d', '--destination', nargs=1, help='Destination absolute path of the cloned (or to-be-cloned) repositories of the formulas.') parser.add_argument('-c', '--clone', action='store_true', help='Clone the formulas to the destination specified with "--destination". The gitlab fork will also be added as remote. If the repository is already cloned, then both remotes will be pulled/fetched.') parser.add_argument('-s', '--symlink', action='store_true', help='Creates symlink from the specified destination to /srv/salt.') parser.add_argument('-r', '--remote', nargs=1, help='Enable the specified remote. Available remotes: origin, opensuse. Default: origin') -parser.add_argument('-p', '--pull-requests', action='store_true', help='Prints the status of the Pull Requests that are defined in FORMULAS.yaml under "pending".') -requiredArgs = parser.add_argument_group('required arguments') -requiredArgs.add_argument('-d', '--destination', nargs=1, required=True, help='Destination absolute path of the cloned (or to-be-cloned) repositories of the formulas.') args = parser.parse_args() -if not os.path.isabs(args.destination[0]): - parser.print_help() - sys.exit(1) +if args.pull_requests: + check_open_pull_requests() -if args.remote and args.remote[0] not in ['origin', 'opensuse']: - parser.print_help() - sys.exit(1) +# Every option below requires the --destination argument to be set +if args.clone or args.symlink or args.remote: + if not args.destination or not os.path.isabs(args.destination[0]): + parser.print_help() + sys.exit(1) -if args.clone: - clone_or_pull(args.destination[0], args.symlink) + if args.clone: + clone_or_pull(args.destination[0]) -if args.remote: - enable_remote(args.remote[0], args.destination[0]) + if args.symlink: + create_symlinks(args.destination[0]) -if args.pull_requests: - check_open_pull_requests() + if args.remote: + if args.remote[0] not in ['origin', 'opensuse']: + parser.print_help() + sys.exit(1) + enable_remote(args.remote[0], args.destination[0])