|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Script to replace go module dependencies for Cinder: |
| 3 | +# Supports 4 different formats: |
| 4 | +# - By PR numbers: |
| 5 | +# ./setdeps.py lib-common=88 openstack-operator=38 |
| 6 | +# - By local directory where the local dir is already with the right code: |
| 7 | +# ./setdeps.py lib-common=../lib-common \ |
| 8 | +# openstack-operator=../openstack-operator |
| 9 | +# - By upstream repository |
| 10 | +# ./setdeps.py \ |
| 11 | +# lib-common=https://github.com/fmount/lib-common/@extra_volumes \ |
| 12 | +# openstack-operator=https://github.com/fmount/openstack-operator/@extra_volumes |
| 13 | +# - By short upstream repository: |
| 14 | +# ./setdeps.py lib-common=fmount/lib-common@extra_volumes \ |
| 15 | +# openstack-operator=fmount/openstack-operator@extra_volumes |
| 16 | +import json |
| 17 | +import os |
| 18 | +import re |
| 19 | +import subprocess |
| 20 | +import sys |
| 21 | +import urllib.request |
| 22 | + |
| 23 | +GH_API_URL = 'https://api.github.com/repos/openstack-k8s-operators' |
| 24 | + |
| 25 | + |
| 26 | +def get_modules(repo, requires): |
| 27 | + """Return a list of tuples of (repository, module) for a specific repo.""" |
| 28 | + result = [] |
| 29 | + for require in requires: |
| 30 | + try: |
| 31 | + index = require.index('/' + repo) |
| 32 | + |
| 33 | + module_index = index + 1 + len(repo) |
| 34 | + url = require[:module_index] |
| 35 | + module = require[module_index:] |
| 36 | + if module[0] == '/': |
| 37 | + result.append((url, module)) |
| 38 | + except ValueError: |
| 39 | + continue |
| 40 | + return result |
| 41 | + |
| 42 | + |
| 43 | +def get_requires(): |
| 44 | + """Get all requires modules from go.mod""" |
| 45 | + # Get location of the go.mod file |
| 46 | + result = subprocess.run('go env GOMOD', shell=True, check=True, |
| 47 | + capture_output=True) |
| 48 | + go_mod_path = result.stdout.strip() |
| 49 | + re_res = re.search(r"^require \(\n(.*?)\n\)", |
| 50 | + open(go_mod_path).read(), |
| 51 | + re.MULTILINE | re.DOTALL) |
| 52 | + if not re_res: |
| 53 | + raise Exception('Error parsing go.mod') |
| 54 | + lines = re_res.group(1).split('\n') |
| 55 | + return [line.strip().split()[0] for line in lines if line] |
| 56 | + |
| 57 | + |
| 58 | +def main(args): |
| 59 | + source_is = None |
| 60 | + |
| 61 | + requires = get_requires() |
| 62 | + for arg in args: |
| 63 | + repo = arg[0] |
| 64 | + go_module_url = f'github.com/openstack-k8s-operators/{repo}' |
| 65 | + |
| 66 | + source_version = '' |
| 67 | + |
| 68 | + try: |
| 69 | + pr = int(arg[1]) |
| 70 | + source_is = 'PR' |
| 71 | + except ValueError: |
| 72 | + src_path = arg[1] |
| 73 | + if os.path.exists(src_path): |
| 74 | + source_is = 'LOCAL' |
| 75 | + else: |
| 76 | + source_is = 'REPO' |
| 77 | + # Build url if we where just provided a partial repo |
| 78 | + # eg: fpantano/lib-common@extravol |
| 79 | + if not src_path.startswith('http'): |
| 80 | + src_path = 'github.com/' + src_path |
| 81 | + |
| 82 | + if source_is == 'PR': |
| 83 | + api_url = f'{GH_API_URL}/{repo}/pulls/{pr}' |
| 84 | + |
| 85 | + contents_str = urllib.request.urlopen(api_url).read() |
| 86 | + contents = json.loads(contents_str) |
| 87 | + source_version = '@' + contents['head']['ref'] |
| 88 | + |
| 89 | + src_path = 'github.com/' + contents['head']['repo']['full_name'] |
| 90 | + print(f'Source for {repo} PR#{pr} is {src_path}{source_version}') |
| 91 | + |
| 92 | + elif source_is == 'LOCAL': |
| 93 | + src_path = os.path.abspath(src_path) |
| 94 | + print(f'Source repo for {repo} is {src_path}') |
| 95 | + elif source_is == 'REPO': # is a repo |
| 96 | + if '@' in src_path: |
| 97 | + src_path, source_version = src_path.split('@') |
| 98 | + source_version = '@' + source_version |
| 99 | + print(f'Source repo for {repo} is {src_path}') |
| 100 | + |
| 101 | + if source_version: |
| 102 | + print(f'Checking the go mod version for branch {source_version}') |
| 103 | + sha = contents['head']['sha'][:12] |
| 104 | + result = subprocess.run(f'go list -m -json {src_path}@{sha}', |
| 105 | + shell=True, check=True, |
| 106 | + capture_output=True) |
| 107 | + source_version = '@' + json.loads(result.stdout)['Version'] |
| 108 | + |
| 109 | + modules = get_modules(repo, requires) |
| 110 | + for go_module_url, module in modules: |
| 111 | + cmd = (f'go work edit -replace {go_module_url}{module}=' |
| 112 | + f'{src_path}{module}{source_version}') |
| 113 | + print(cmd) |
| 114 | + os.system(cmd) |
| 115 | + print() |
| 116 | + |
| 117 | + |
| 118 | +if __name__ == '__main__': |
| 119 | + if len(sys.argv) < 2: |
| 120 | + print('Error, missing arguments') |
| 121 | + exit(1) |
| 122 | + |
| 123 | + args = [] |
| 124 | + for arg in sys.argv[1:]: |
| 125 | + args.append(arg.split('=')) |
| 126 | + |
| 127 | + main(args) |
0 commit comments