|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +from argparse import ArgumentParser |
| 4 | +from configparser import ConfigParser |
| 5 | +from os.path import dirname, expanduser, realpath |
| 6 | +from sys import exit |
| 7 | +import urllib.parse |
| 8 | +import urllib.request |
| 9 | +import xml.etree.ElementTree as ET |
| 10 | + |
| 11 | + |
| 12 | +def obs_get_package_link(args, package): |
| 13 | + url = "%s/source/%s/%s" % (args.apiurl, args.project, package) |
| 14 | + user = args.user |
| 15 | + password = args.password |
| 16 | + password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm() |
| 17 | + password_mgr.add_password(None, url, user, password) |
| 18 | + auth_handler = urllib.request.HTTPBasicAuthHandler(password_mgr) |
| 19 | + opener = urllib.request.build_opener(auth_handler) |
| 20 | + urllib.request.install_opener(opener) |
| 21 | + req = urllib.request.Request(url=url, method='GET') |
| 22 | + resource = urllib.request.urlopen(req) |
| 23 | + charset = resource.headers.get_content_charset() |
| 24 | + if charset is None: |
| 25 | + charset = 'utf-8' |
| 26 | + doc = ET.fromstring(resource.read().decode(charset)) |
| 27 | + return doc.findall("./linkinfo")[0] |
| 28 | + |
| 29 | + |
| 30 | +def parse_arguments(): |
| 31 | + """ Parse arguments from command line """ |
| 32 | + parser = ArgumentParser( |
| 33 | + description="Check if Uyuni versions are aligned at all packages before a release") |
| 34 | + parser.add_argument("-u", "--user", action="store", dest="user", |
| 35 | + help="OBS Username or read from ~/.oscrc") |
| 36 | + parser.add_argument("-P", "--password", action="store", dest="password", |
| 37 | + help="OBS Password or read from ~/.oscrc") |
| 38 | + parser.add_argument("-a", "--api-url", action="store", dest="apiurl", |
| 39 | + default="https://api.opensuse.org", |
| 40 | + help="OBS API URL (Default: https://api.opensuse.org") |
| 41 | + parser.add_argument("-p", "--project", action="store", dest="project", required=True, |
| 42 | + help="Project where the package to be checked is") |
| 43 | + parser.add_argument("-l", "--link", action="store", dest="link", required=True, |
| 44 | + help="The project that should be linked") |
| 45 | + parser.add_argument("-j", "--package", action="store", dest="package", required=True, |
| 46 | + help="The package to check") |
| 47 | + args = parser.parse_args() |
| 48 | + if not args.user or not args.password: |
| 49 | + try: |
| 50 | + creds_path = "%s/.oscrc" % expanduser('~') |
| 51 | + creds = ConfigParser() |
| 52 | + creds.read(creds_path) |
| 53 | + args.user = creds.get(args.apiurl, 'user') |
| 54 | + args.password = creds.get(args.apiurl, 'pass') |
| 55 | + except Exception: |
| 56 | + raise RuntimeError( |
| 57 | + 'Could not find credentials for %s at %s' % (args.apiurl, creds_path)) |
| 58 | + return args |
| 59 | + |
| 60 | + |
| 61 | +def print_info(msg): |
| 62 | + print("[\033[01m\033[34mINFO \033[0m] %s" % msg) |
| 63 | + |
| 64 | + |
| 65 | +def print_ok(msg): |
| 66 | + print("[\033[01m\033[32mOK \033[0m] %s" % msg) |
| 67 | + |
| 68 | + |
| 69 | +def print_error(msg): |
| 70 | + print("[\033[01m\033[31mERROR\033[0m] %s" % msg) |
| 71 | + |
| 72 | + |
| 73 | +args = parse_arguments() |
| 74 | +print_info("Checking %s/%s" % (args.project, args.package)) |
| 75 | +linkinfo = obs_get_package_link(args, args.package).attrib |
| 76 | +if linkinfo['project'] == args.link and linkinfo['package'] == args.package: |
| 77 | + print_ok("The package links to %s/%s" % |
| 78 | + (linkinfo['project'], linkinfo['package'])) |
| 79 | + exit(0) |
| 80 | +print_error("The package links to %s/%s" % |
| 81 | + (linkinfo['project'], linkinfo['package'])) |
| 82 | +exit(1) |
0 commit comments