From 25a8834b47590cc1ba41800596008994caab5abd Mon Sep 17 00:00:00 2001 From: Jan Kaluza Date: Feb 03 2018 18:12:32 +0000 Subject: Use our own parse_nvr in freshmaker-cli to not depend on kobo.rpmlib --- diff --git a/contrib/freshmaker-cli b/contrib/freshmaker-cli index 0dbb2cc..b90b1af 100755 --- a/contrib/freshmaker-cli +++ b/contrib/freshmaker-cli @@ -27,7 +27,6 @@ import argparse import sys import requests from tabulate import tabulate -from kobo.rpmlib import parse_nvr # Disable InsecureRequestWarning from requests.packages.urllib3.exceptions import InsecureRequestWarning @@ -59,6 +58,65 @@ EVENT_STATES = [ "skipped", ] +def split_nvr_epoch(nvre): + """Split nvre to N-V-R and E. + + @param nvre: E:N-V-R or N-V-R:E string + @type nvre: str + @return: (N-V-R, E) + @rtype: (str, str) + """ + + if ":" in nvre: + if nvre.count(":") != 1: + raise ValueError("Invalid NVRE: %s" % nvre) + + nvr, epoch = nvre.rsplit(":", 1) + if "-" in epoch: + if "-" not in nvr: + # switch nvr with epoch + nvr, epoch = epoch, nvr + else: + # it's probably N-E:V-R format, handle it after the split + nvr, epoch = nvre, "" + else: + nvr, epoch = nvre, "" + + return (nvr, epoch) + + +def parse_nvr(nvre): + """Split N-V-R into a dictionary. + + @param nvre: N-V-R:E, E:N-V-R or N-E:V-R string + @type nvre: str + @return: {name, version, release, epoch} + @rtype: dict + """ + + if "/" in nvre: + nvre = nvre.split("/")[-1] + + nvr, epoch = split_nvr_epoch(nvre) + + nvr_parts = nvr.rsplit("-", 2) + if len(nvr_parts) != 3: + raise ValueError("Invalid NVR: %s" % nvr) + + # parse E:V + if epoch == "" and ":" in nvr_parts[1]: + epoch, nvr_parts[1] = nvr_parts[1].split(":", 1) + + # check if epoch is empty or numeric + if epoch != "": + try: + int(epoch) + except ValueError: + raise ValueError("Invalid epoch '%s' in '%s'" % (epoch, nvr)) + + result = dict(zip(["name", "version", "release"], nvr_parts)) + result["epoch"] = epoch + return result def get_api_url(deployment, env): API_URLS = {